Skip to content

Commit d4f5c97

Browse files
refactor(client): add JsonDictionary identity methods
1 parent 18e3cf0 commit d4f5c97

5 files changed

Lines changed: 113 additions & 32 deletions

File tree

src/Orb.Tests/Core/JsonDictionaryTest.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,76 @@ public void ComplexWorkflow_SetFreezeAndGet()
331331

332332
Assert.Throws<InvalidOperationException>(() => dict.Set("new", "value"));
333333
}
334+
335+
[Fact]
336+
public void ToString_ContainsJsonValues()
337+
{
338+
var dict = new JsonDictionary();
339+
dict.Set("name", "Alice");
340+
dict.Set("age", 30);
341+
342+
var json = dict.ToString();
343+
344+
Assert.Contains("\"name\"", json);
345+
Assert.Contains("\"Alice\"", json);
346+
Assert.Contains("\"age\"", json);
347+
Assert.Contains("30", json);
348+
}
349+
350+
[Fact]
351+
public void Equals_ReturnsTrueForSameContent()
352+
{
353+
var dict1 = new JsonDictionary();
354+
dict1.Set("name", "Alice");
355+
dict1.Set("age", 30);
356+
357+
var dict2 = new JsonDictionary();
358+
dict2.Set("name", "Alice");
359+
dict2.Set("age", 30);
360+
361+
Assert.True(dict1.Equals(dict2));
362+
}
363+
364+
[Fact]
365+
public void Equals_ReturnsFalseForDifferentContent()
366+
{
367+
var dict1 = new JsonDictionary();
368+
dict1.Set("name", "Alice");
369+
370+
var dict2 = new JsonDictionary();
371+
dict2.Set("name", "Bob");
372+
373+
Assert.False(dict1.Equals(dict2));
374+
}
375+
376+
[Fact]
377+
public void Equals_ReturnsFalseForDifferentCounts()
378+
{
379+
var dict1 = new JsonDictionary();
380+
dict1.Set("name", "Alice");
381+
dict1.Set("age", 30);
382+
383+
var dict2 = new JsonDictionary();
384+
dict2.Set("name", "Alice");
385+
386+
Assert.False(dict1.Equals(dict2));
387+
}
388+
389+
[Fact]
390+
public void Equals_ReturnsFalseForNull()
391+
{
392+
var dict = new JsonDictionary();
393+
dict.Set("name", "Alice");
394+
395+
Assert.False(dict.Equals(null));
396+
}
397+
398+
[Fact]
399+
public void Equals_ReturnsFalseForDifferentType()
400+
{
401+
var dict = new JsonDictionary();
402+
dict.Set("name", "Alice");
403+
404+
Assert.False(dict.Equals("not a dictionary"));
405+
}
334406
}

src/Orb/Core/JsonDictionary.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public JsonDictionary()
4444
public JsonDictionary(IReadOnlyDictionary<string, JsonElement> dictionary)
4545
{
4646
_rawData = Enumerable.ToDictionary(dictionary, (e) => e.Key, (e) => e.Value);
47-
;
4847
_deserializedData = [];
4948
}
5049

@@ -57,7 +56,6 @@ public JsonDictionary(FrozenDictionary<string, JsonElement> dictionary)
5756
public JsonDictionary(JsonDictionary dictionary)
5857
{
5958
_rawData = Enumerable.ToDictionary(dictionary._rawData, (e) => e.Key, (e) => e.Value);
60-
;
6159
_deserializedData = new(dictionary._deserializedData);
6260
}
6361

@@ -186,4 +184,35 @@ public T GetNotNullStruct<T>(string key)
186184
_deserializedData[key] = deserialized;
187185
return deserialized;
188186
}
187+
188+
public override string ToString() =>
189+
JsonSerializer.Serialize(this._rawData, ModelBase.ToStringSerializerOptions);
190+
191+
public override bool Equals(object? obj)
192+
{
193+
if (obj is not JsonDictionary other || _rawData.Count != other._rawData.Count)
194+
{
195+
return false;
196+
}
197+
198+
foreach (var item in _rawData)
199+
{
200+
if (!other._rawData.TryGetValue(item.Key, out var otherValue))
201+
{
202+
return false;
203+
}
204+
205+
if (!JsonElement.DeepEquals(item.Value, otherValue))
206+
{
207+
return false;
208+
}
209+
}
210+
211+
return true;
212+
}
213+
214+
public override int GetHashCode()
215+
{
216+
return 0;
217+
}
189218
}

src/Orb/Core/JsonModel.cs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ public abstract record class JsonModel : ModelBase
1212
{
1313
private protected JsonDictionary _rawData = new();
1414

15-
protected JsonModel(JsonModel jsonModel)
16-
: base(jsonModel)
17-
{
18-
this._rawData = new(jsonModel._rawData);
19-
}
20-
2115
/// <summary>
2216
/// The backing JSON properties of the instance.
2317
/// </summary>
@@ -26,36 +20,25 @@ public IReadOnlyDictionary<string, JsonElement> RawData
2620
get { return this._rawData.Freeze(); }
2721
}
2822

29-
public sealed override string ToString() =>
30-
JsonSerializer.Serialize(this.RawData, ModelBase.ToStringSerializerOptions);
23+
protected JsonModel(JsonModel jsonModel)
24+
: base(jsonModel)
25+
{
26+
this._rawData = new(jsonModel._rawData);
27+
}
28+
29+
public sealed override string ToString() => this._rawData.ToString();
3130

3231
public virtual bool Equals(JsonModel? other)
3332
{
34-
if (other == null || this.RawData.Count != other.RawData.Count)
33+
if (other == null)
3534
{
3635
return false;
3736
}
3837

39-
foreach (var item in this.RawData)
40-
{
41-
if (!other.RawData.TryGetValue(item.Key, out var otherValue))
42-
{
43-
return false;
44-
}
45-
46-
if (!JsonElement.DeepEquals(item.Value, otherValue))
47-
{
48-
return false;
49-
}
50-
}
51-
52-
return true;
38+
return this._rawData.Equals(other._rawData);
5339
}
5440

55-
public override int GetHashCode()
56-
{
57-
return 0;
58-
}
41+
public override int GetHashCode() => this._rawData.GetHashCode();
5942
}
6043

6144
/// <summary>

src/Orb/Core/MultipartJsonDictionary.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public MultipartJsonDictionary()
4444
public MultipartJsonDictionary(IReadOnlyDictionary<string, MultipartJsonElement> dictionary)
4545
{
4646
_rawData = Enumerable.ToDictionary(dictionary, (e) => e.Key, (e) => e.Value);
47-
;
4847
_deserializedData = [];
4948
}
5049

@@ -57,7 +56,6 @@ public MultipartJsonDictionary(FrozenDictionary<string, MultipartJsonElement> di
5756
public MultipartJsonDictionary(MultipartJsonDictionary dictionary)
5857
{
5958
_rawData = Enumerable.ToDictionary(dictionary._rawData, (e) => e.Key, (e) => e.Value);
60-
;
6159
_deserializedData = new(dictionary._deserializedData);
6260
}
6361

src/Orb/Core/MultipartJsonElement.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ public static MultipartJsonElement SerializeToElement<T>(
122122
(e) => e.Key,
123123
(e) => e.Value
124124
);
125-
;
126125
return JsonSerializer.Deserialize<T>(element.Json, MultipartSerializerOptions(options));
127126
}
128127
finally

0 commit comments

Comments
 (0)