Skip to content

Commit

Permalink
fix implicit KeyValues operator
Browse files Browse the repository at this point in the history
  • Loading branch information
lvermeulen committed Feb 5, 2017
1 parent 4581235 commit 1bfcb8b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Nanophone.RegistryHost.InMemoryRegistry/KeyValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Nanophone.RegistryHost.InMemoryRegistry
{
public class KeyValues : IHaveKeyValues
{
private readonly Base64Codec _codec = new Base64Codec();
private static readonly Base64Codec s_codec = new Base64Codec();
private readonly Dictionary<string, string> _dictionary = new Dictionary<string, string>();

public KeyValues WithKeyValue(string key, string value)
Expand All @@ -22,7 +22,7 @@ public KeyValues WithKeyValue(string key, string value)

public Task KeyValuePutAsync(string key, string value)
{
_dictionary.Add(key, _codec.Encode(value));
_dictionary.Add(key, s_codec.Encode(value));

return Task.FromResult(0);
}
Expand All @@ -32,7 +32,7 @@ public Task<string> KeyValueGetAsync(string key)
string value;
if (_dictionary.TryGetValue(key, out value))
{
return Task.FromResult(_codec.Decode(value));
return Task.FromResult(s_codec.Decode(value));
}

return Task.FromResult(default(string));
Expand Down Expand Up @@ -71,7 +71,9 @@ public Task<string[]> KeyValuesGetKeysAsync(string prefix)

public static implicit operator List<KeyValuePair<string, string>>(KeyValues keyValues)
{
return keyValues._dictionary.ToList();
return keyValues._dictionary
.Select(x => new KeyValuePair<string, string>(x.Key, s_codec.Decode(x.Value)))
.ToList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,22 @@ public async Task KeyValueDeleteTreeAsync()
await _host.KeyValueDeleteTreeAsync("1");
Assert.Equal(2, _keyValues.Count);
}

[Fact]
public void ConvertKeyValuesToKeyValuePairList()
{
var keyValues = new KeyValues()
.WithKeyValue("1", "One")
.WithKeyValue("1.1", "One.1")
.WithKeyValue("2", 2.0.ToString(CultureInfo.InvariantCulture))
.WithKeyValue("3", 3M.ToString(CultureInfo.InvariantCulture));

var list = (List<KeyValuePair<string, string>>)keyValues;
Assert.NotNull(list);
Assert.True(list.Any(x => x.Key == "1" && x.Value == "One"));
Assert.True(list.Any(x => x.Key == "1.1" && x.Value == "One.1"));
Assert.True(list.Any(x => x.Key == "2" && x.Value == 2.0.ToString(CultureInfo.InvariantCulture)));
Assert.True(list.Any(x => x.Key == "3" && x.Value == 3M.ToString(CultureInfo.InvariantCulture)));
}
}
}

0 comments on commit 1bfcb8b

Please sign in to comment.