namespace JetBrains.TeamCity.ServiceMessages.Write
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
///
/// Helper class to create complex service message. Use object initilizer to simplify code, i.e.
///
/// new ServiceMessage("buildProblem") { { "identity", identity}, {"description", message}}
///
///
public class ServiceMessage : IServiceMessage, IEnumerable>
{
private readonly Dictionary _arguments = new Dictionary();
///
/// Copy constructor
///
/// service message to copy from
public ServiceMessage(IServiceMessage message)
: this(message.Name)
{
if (message == null) throw new ArgumentNullException(nameof(message));
AddRange(message.Keys.ToDictionary(x => x, message.GetValue));
}
///
/// Simple constructor
///
/// service message name
public ServiceMessage([NotNull] string name)
{
if (name == null) throw new ArgumentNullException(nameof(name));
Name = name;
}
public IEnumerator> GetEnumerator()
{
return _arguments.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public string Name { get; }
public string DefaultValue => null;
public IEnumerable Keys => _arguments.Keys;
public string GetValue(string key)
{
string s;
return _arguments.TryGetValue(key, out s) ? s : null;
}
public void Add(string key, string value)
{
if (key == null) throw new ArgumentNullException(nameof(key));
_arguments[key] = value;
}
public void AddRange(IEnumerable> values)
{
foreach (var e in values)
{
_arguments[e.Key] = e.Value;
}
}
}
}