Skip to content

Commit

Permalink
Hide DynamicDataProperty (Azure#35970)
Browse files Browse the repository at this point in the history
* Move DynamicData into Azure.Core.Dynamic namespace

* Make DynamicDataProperty internal

* fix bad merge

* Export API

* tidy
  • Loading branch information
annelo-msft authored May 3, 2023
1 parent 7971fd9 commit 9d94f05
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 44 deletions.
8 changes: 0 additions & 8 deletions sdk/core/Azure.Core/api/Azure.Core.net461.cs
Original file line number Diff line number Diff line change
Expand Up @@ -817,14 +817,6 @@ public enum DynamicDataNameMapping
PascalCaseGetters = 1,
PascalCaseGettersCamelCaseSetters = 2,
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public readonly partial struct DynamicDataProperty
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public string Name { get { throw null; } }
public Azure.Core.Dynamic.DynamicData Value { get { throw null; } }
}
}
namespace Azure.Core.Extensions
{
Expand Down
8 changes: 0 additions & 8 deletions sdk/core/Azure.Core/api/Azure.Core.net5.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -817,14 +817,6 @@ public enum DynamicDataNameMapping
PascalCaseGetters = 1,
PascalCaseGettersCamelCaseSetters = 2,
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public readonly partial struct DynamicDataProperty
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public string Name { get { throw null; } }
public Azure.Core.Dynamic.DynamicData Value { get { throw null; } }
}
}
namespace Azure.Core.Extensions
{
Expand Down
8 changes: 0 additions & 8 deletions sdk/core/Azure.Core/api/Azure.Core.net6.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -817,14 +817,6 @@ public enum DynamicDataNameMapping
PascalCaseGetters = 1,
PascalCaseGettersCamelCaseSetters = 2,
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public readonly partial struct DynamicDataProperty
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public string Name { get { throw null; } }
public Azure.Core.Dynamic.DynamicData Value { get { throw null; } }
}
}
namespace Azure.Core.Extensions
{
Expand Down
8 changes: 0 additions & 8 deletions sdk/core/Azure.Core/api/Azure.Core.netcoreapp2.1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -817,14 +817,6 @@ public enum DynamicDataNameMapping
PascalCaseGetters = 1,
PascalCaseGettersCamelCaseSetters = 2,
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public readonly partial struct DynamicDataProperty
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public string Name { get { throw null; } }
public Azure.Core.Dynamic.DynamicData Value { get { throw null; } }
}
}
namespace Azure.Core.Extensions
{
Expand Down
8 changes: 0 additions & 8 deletions sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -817,14 +817,6 @@ public enum DynamicDataNameMapping
PascalCaseGetters = 1,
PascalCaseGettersCamelCaseSetters = 2,
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public readonly partial struct DynamicDataProperty
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public string Name { get { throw null; } }
public Azure.Core.Dynamic.DynamicData Value { get { throw null; } }
}
}
namespace Azure.Core.Extensions
{
Expand Down
32 changes: 31 additions & 1 deletion sdk/core/Azure.Core/src/DynamicData/DynamicDataProperty.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Dynamic;
using System.Linq.Expressions;

namespace Azure.Core.Dynamic
{
/// <summary>
/// Represents a single property on a dynamic JSON object.
/// </summary>
public readonly struct DynamicDataProperty
internal readonly struct DynamicDataProperty : IDynamicMetaObjectProvider
{
internal DynamicDataProperty(string name, DynamicData value)
{
Expand All @@ -23,5 +27,31 @@ internal DynamicDataProperty(string name, DynamicData value)
/// Gets the value of this property.
/// </summary>
public DynamicData Value { get; }

/// <inheritdoc />
DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter) => new MetaObject(parameter, this);

private class MetaObject : DynamicMetaObject
{
private static readonly string[] _memberNames = new string[] { "Name", "Value" };

internal MetaObject(Expression parameter, IDynamicMetaObjectProvider value) : base(parameter, BindingRestrictions.Empty, value)
{
}

public override IEnumerable<string> GetDynamicMemberNames()
{
return _memberNames;
}

public override DynamicMetaObject BindGetMember(GetMemberBinder binder)
{
UnaryExpression this_ = Expression.Convert(Expression, LimitType);
MemberExpression property = Expression.Property(this_, binder.Name);

BindingRestrictions restrictions = BindingRestrictions.GetTypeRestriction(Expression, LimitType);
return new DynamicMetaObject(property, restrictions);
}
}
}
}
19 changes: 16 additions & 3 deletions sdk/core/Azure.Core/tests/public/JsonDataObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,25 @@ public void CanEnumerateObject()
var expectedValues = new[] { 1, 2 };

int i = 0;
foreach (var pair in data)
foreach (dynamic property in data)
{
Assert.AreEqual(expectedNames[i], pair.Name);
Assert.AreEqual(expectedValues[i], (int)pair.Value);
Assert.AreEqual(expectedNames[i], property.Name);
Assert.AreEqual(expectedValues[i], (int)property.Value);
i++;
}

Assert.AreEqual(2, i);
}

[Test]
public void UnsupportedPropertyAccessThrow()
{
dynamic data = JsonDataTestHelpers.CreateFromJson("""{ "first": 1, "second": 2 }""");

foreach (dynamic property in data)
{
Assert.Throws<ArgumentException>(() => { var value = property.InvalidName; });
}
}

[Test]
Expand Down

0 comments on commit 9d94f05

Please sign in to comment.