Skip to content

Commit aaabb1a

Browse files
committed
feat(meta): add a meta builder class
1 parent 3d51f1c commit aaabb1a

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
3+
namespace JsonApiDotNetCore.Builders
4+
{
5+
public interface IMetaBuilder
6+
{
7+
void Add(string key, object value);
8+
void Add(Dictionary<string,object> values);
9+
Dictionary<string, object> Build();
10+
}
11+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace JsonApiDotNetCore.Builders
5+
{
6+
public class MetaBuilder : IMetaBuilder
7+
{
8+
private Dictionary<string, object> _meta = new Dictionary<string, object>();
9+
10+
public void Add(string key, object value)
11+
{
12+
_meta[key] = value;
13+
}
14+
15+
/// <summary>
16+
/// Joins the new dictionary with the current one. In the event of a key collision,
17+
/// the new value will override the old.
18+
/// </summary>
19+
public void Add(Dictionary<string,object> values)
20+
{
21+
_meta = values.Keys.Union(_meta.Keys)
22+
.ToDictionary(key => key,
23+
key => values.ContainsKey(key) ? values[key] : _meta[key]);
24+
}
25+
26+
public Dictionary<string, object> Build()
27+
{
28+
return _meta;
29+
}
30+
}
31+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using Xunit;
2+
using JsonApiDotNetCore.Builders;
3+
using System.Collections.Generic;
4+
5+
namespace JsonApiDotNetCoreExampleTests.Unit.Builders
6+
{
7+
public class MetaBuilderTests
8+
{
9+
[Fact]
10+
public void Can_Add_Key_Value()
11+
{
12+
// arrange
13+
var builder = new MetaBuilder();
14+
var key = "test";
15+
var value = "testValue";
16+
17+
// act
18+
builder.Add(key, value);
19+
var result = builder.Build();
20+
21+
// assert
22+
Assert.NotEmpty(result);
23+
Assert.Equal(value, result[key]);
24+
}
25+
26+
[Fact]
27+
public void Can_Add_Multiple_Values()
28+
{
29+
// arrange
30+
var builder = new MetaBuilder();
31+
var input = new Dictionary<string, object> {
32+
{ "key1", "value1" },
33+
{ "key2", "value2" }
34+
};
35+
36+
// act
37+
builder.Add(input);
38+
var result = builder.Build();
39+
40+
// assert
41+
Assert.NotEmpty(result);
42+
foreach (var entry in input)
43+
Assert.Equal(input[entry.Key], result[entry.Key]);
44+
}
45+
46+
[Fact]
47+
public void When_Adding_Duplicate_Values_Keep_Newest()
48+
{
49+
// arrange
50+
var builder = new MetaBuilder();
51+
52+
var key = "key";
53+
var oldValue = "oldValue";
54+
var newValue = "newValue";
55+
56+
builder.Add(key, oldValue);
57+
58+
var input = new Dictionary<string, object> {
59+
{ key, newValue },
60+
{ "key2", "value2" }
61+
};
62+
63+
// act
64+
builder.Add(input);
65+
var result = builder.Build();
66+
67+
// assert
68+
Assert.NotEmpty(result);
69+
Assert.Equal(input.Count, result.Count);
70+
Assert.Equal(input[key], result[key]);
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)