Skip to content

Commit b7efe81

Browse files
authored
Pass property name mapping to array, object enumerators (Azure#35210)
* Pass property name mapping to array enumerator * Use proper casing for object enumerator as well Resolves some other feedback
1 parent fae55e0 commit b7efe81

File tree

5 files changed

+145
-10
lines changed

5 files changed

+145
-10
lines changed

sdk/core/Azure.Core.Experimental/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bugs Fixed
1010

11+
- Use specified `DynamicJsonNameMapping` in array and object enumerators.
12+
1113
### Other Changes
1214

1315
## 0.1.0-preview.25 (2023-03-02)

sdk/core/Azure.Core.Experimental/src/DynamicJson.ArrayEnumerator.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@ public partial class DynamicJson
1717
public struct ArrayEnumerator : IEnumerable<DynamicJson>, IEnumerator<DynamicJson>
1818
{
1919
private MutableJsonElement.ArrayEnumerator _enumerator;
20+
private readonly DynamicJsonOptions _options;
2021

21-
internal ArrayEnumerator(MutableJsonElement.ArrayEnumerator enumerator)
22+
internal ArrayEnumerator(MutableJsonElement.ArrayEnumerator enumerator, DynamicJsonOptions options)
2223
{
2324
_enumerator = enumerator;
25+
_options = options;
2426
}
2527

2628
/// <summary> Returns an enumerator that iterates through a collection.</summary>
2729
/// <returns> An <see cref="ArrayEnumerator"/> value that can be used to iterate through the array.</returns>
28-
public ArrayEnumerator GetEnumerator() => new(_enumerator.GetEnumerator());
30+
public ArrayEnumerator GetEnumerator() => new(_enumerator.GetEnumerator(), _options);
2931

3032
/// <inheritdoc />
31-
public DynamicJson Current => new(_enumerator.Current);
33+
public DynamicJson Current => new(_enumerator.Current, _options);
3234

3335
/// <inheritdoc />
3436
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

sdk/core/Azure.Core.Experimental/src/DynamicJson.ObjectEnumerator.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ public partial class DynamicJson
1717
public struct ObjectEnumerator : IEnumerable<DynamicJsonProperty>, IEnumerator<DynamicJsonProperty>
1818
{
1919
private MutableJsonElement.ObjectEnumerator _enumerator;
20+
private readonly DynamicJsonOptions _options;
2021

21-
internal ObjectEnumerator(MutableJsonElement.ObjectEnumerator enumerator)
22+
internal ObjectEnumerator(MutableJsonElement.ObjectEnumerator enumerator, DynamicJsonOptions options)
2223
{
2324
_enumerator = enumerator;
25+
_options = options;
2426
}
2527

2628
/// <summary>
@@ -36,10 +38,10 @@ internal ObjectEnumerator(MutableJsonElement.ObjectEnumerator enumerator)
3638
/// property they will all individually be returned (each in the order
3739
/// they appear in the content).
3840
/// </remarks>
39-
public ObjectEnumerator GetEnumerator() => new(_enumerator.GetEnumerator());
41+
public ObjectEnumerator GetEnumerator() => new(_enumerator.GetEnumerator(), _options);
4042

4143
/// <inheritdoc />
42-
public DynamicJsonProperty Current => new(_enumerator.Current.Name, new(_enumerator.Current.Value));
44+
public DynamicJsonProperty Current => new(_enumerator.Current.Name, new(_enumerator.Current.Value, _options));
4345

4446
/// <inheritdoc />
4547
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

sdk/core/Azure.Core.Experimental/src/DynamicJson.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ private IEnumerable GetEnumerable()
100100
{
101101
return _element.ValueKind switch
102102
{
103-
JsonValueKind.Array => new ArrayEnumerator(_element.EnumerateArray()),
104-
JsonValueKind.Object => new ObjectEnumerator(_element.EnumerateObject()),
103+
JsonValueKind.Array => new ArrayEnumerator(_element.EnumerateArray(), _options),
104+
JsonValueKind.Object => new ObjectEnumerator(_element.EnumerateObject(), _options),
105105
_ => throw new InvalidOperationException($"Unable to enumerate JSON element."),
106106
};
107107
}

sdk/core/Azure.Core.Experimental/tests/DynamicJsonTests.cs

Lines changed: 131 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Collections;
56
using Azure.Core.Dynamic;
67
using NUnit.Framework;
78

@@ -710,10 +711,138 @@ public void CanGetPascalCaseNestedProperties()
710711
Assert.IsTrue(dynamicJson.Root.Child[0].Item.Leaf);
711712
}
712713

714+
[Test]
715+
public void CanEnumerateArrayCamelGetters()
716+
{
717+
dynamic jsonData = GetDynamicJson("""
718+
{
719+
"array": [
720+
{
721+
"foo": "a"
722+
},
723+
{
724+
"foo": "b"
725+
}
726+
]
727+
}
728+
""");
729+
730+
IEnumerable ary = (IEnumerable)jsonData.array;
731+
IEnumerator e = ary.GetEnumerator();
732+
733+
Assert.IsTrue(e.MoveNext());
734+
dynamic item = e.Current;
735+
Assert.AreEqual("a", (string)item.foo);
736+
737+
Assert.IsTrue(e.MoveNext());
738+
item = e.Current;
739+
Assert.AreEqual("b", (string)item.foo);
740+
741+
Assert.IsFalse(e.MoveNext());
742+
}
743+
744+
[Test]
745+
public void CanEnumerateArrayPascalGetters()
746+
{
747+
dynamic jsonData = GetDynamicJson("""
748+
{
749+
"array": [
750+
{
751+
"foo": "a"
752+
},
753+
{
754+
"foo": "b"
755+
}
756+
]
757+
}
758+
""", DynamicJsonNameMapping.PascalCaseGetters);
759+
760+
IEnumerable ary = (IEnumerable)jsonData.Array;
761+
IEnumerator e = ary.GetEnumerator();
762+
763+
Assert.IsTrue(e.MoveNext());
764+
dynamic item = e.Current;
765+
Assert.AreEqual("a", (string)item.Foo);
766+
767+
Assert.IsTrue(e.MoveNext());
768+
item = e.Current;
769+
Assert.AreEqual("b", (string)item.Foo);
770+
771+
Assert.IsFalse(e.MoveNext());
772+
}
773+
774+
[Test]
775+
public void CanEnumeratePropertiesCamelGetters()
776+
{
777+
dynamic jsonData = GetDynamicJson("""
778+
{
779+
"a": {
780+
"description": "description of a",
781+
"index": 1
782+
},
783+
"b": {
784+
"description": "description of b",
785+
"index": 2
786+
}
787+
}
788+
""");
789+
790+
IEnumerable ary = (IEnumerable)jsonData;
791+
IEnumerator e = ary.GetEnumerator();
792+
793+
Assert.IsTrue(e.MoveNext());
794+
dynamic item = e.Current;
795+
Assert.AreEqual("a", (string)item.Name);
796+
Assert.AreEqual("description of a", (string)item.Value.description);
797+
Assert.AreEqual(1, (int)item.Value.index);
798+
799+
Assert.IsTrue(e.MoveNext());
800+
item = e.Current;
801+
Assert.AreEqual("b", (string)item.Name);
802+
Assert.AreEqual("description of b", (string)item.Value.description);
803+
Assert.AreEqual(2, (int)item.Value.index);
804+
805+
Assert.IsFalse(e.MoveNext());
806+
}
807+
808+
[Test]
809+
public void CanEnumeratePropertiesPascalGetters()
810+
{
811+
dynamic jsonData = GetDynamicJson("""
812+
{
813+
"a": {
814+
"description": "description of a",
815+
"index": 1
816+
},
817+
"b": {
818+
"description": "description of b",
819+
"index": 2
820+
}
821+
}
822+
""", DynamicJsonNameMapping.PascalCaseGetters);
823+
824+
IEnumerable ary = (IEnumerable)jsonData;
825+
IEnumerator e = ary.GetEnumerator();
826+
827+
Assert.IsTrue(e.MoveNext());
828+
dynamic item = e.Current;
829+
Assert.AreEqual("a", (string)item.Name);
830+
Assert.AreEqual("description of a", (string)item.Value.Description);
831+
Assert.AreEqual(1, (int)item.Value.Index);
832+
833+
Assert.IsTrue(e.MoveNext());
834+
item = e.Current;
835+
Assert.AreEqual("b", (string)item.Name);
836+
Assert.AreEqual("description of b", (string)item.Value.Description);
837+
Assert.AreEqual(2, (int)item.Value.Index);
838+
839+
Assert.IsFalse(e.MoveNext());
840+
}
841+
713842
#region Helpers
714-
internal static dynamic GetDynamicJson(string json)
843+
internal static dynamic GetDynamicJson(string json, DynamicJsonNameMapping propertyNameCasing = default)
715844
{
716-
return new BinaryData(json).ToDynamic();
845+
return new BinaryData(json).ToDynamic(propertyNameCasing);
717846
}
718847

719848
internal class CustomType

0 commit comments

Comments
 (0)