-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PropertyReferenceType attribute in ResourceIdentity (#22255)
* Add PropertyReferenceTypeAttribute attribute in ResourceIdentity * Feedback
- Loading branch information
1 parent
8fe07a7
commit ddf31a6
Showing
5 changed files
with
112 additions
and
13 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...esourcemanager/Azure.ResourceManager.Core/src/Resources/PropertyReferenceTypeAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Azure.ResourceManager.Core | ||
{ | ||
/// <summary> | ||
/// An attribute class indicating a reference type for code generation. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class)] | ||
public class PropertyReferenceTypeAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Instatiate a new reference type attribute. | ||
/// </summary> | ||
/// <param name="skipTypes"> An array of types to skip for this reference type. </param> | ||
public PropertyReferenceTypeAttribute(Type[] skipTypes) | ||
{ | ||
SkipTypes = skipTypes; | ||
} | ||
|
||
/// <summary> | ||
/// Instatiate a new reference type attribute. | ||
/// </summary> | ||
public PropertyReferenceTypeAttribute() : this(null) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Get an array of types to skip for this reference type. | ||
/// </summary> | ||
public Type[] SkipTypes { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
sdk/resourcemanager/Azure.ResourceManager.Core/tests/Unit/PropertyReferenceTypeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.ResourceManager.Core.Tests | ||
{ | ||
public class PropertyReferenceTypeTests | ||
{ | ||
private static readonly Type PropertyReferenceTypeAttribute = typeof(PropertyReferenceTypeAttribute); | ||
private static readonly Type SerializationConstructor = typeof(SerializationConstructorAttribute); | ||
private static readonly Type InitializationConstructor = typeof(InitializationConstructorAttribute); | ||
private static readonly IEnumerable<Type> AssemblyTypes = typeof(ArmClient).Assembly.GetTypes(); | ||
|
||
[Test] | ||
public void ValidatePropertyReferenceTypeAttribute() | ||
{ | ||
var type = typeof(PropertyReferenceTypeAttribute); | ||
var fieldInfo = type.GetProperties(BindingFlags.Instance | BindingFlags.Public).FirstOrDefault(p => p.Name == "SkipTypes"); | ||
Assert.NotNull(fieldInfo, $"Field 'SkipTypes' is not found"); | ||
Assert.AreEqual(fieldInfo.PropertyType, typeof(Type[])); | ||
Assert.True(fieldInfo.CanRead); | ||
Assert.False(fieldInfo.CanWrite); | ||
} | ||
|
||
[Test] | ||
public void ValidateSerializationConstructor() | ||
{ | ||
foreach (var refType in AssemblyTypes.Where(t => HasAttribute(t.GetCustomAttributes<Attribute>(false), PropertyReferenceTypeAttribute))) | ||
{ | ||
var serializationCtor = refType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) | ||
.Where(c => HasAttribute(c.GetCustomAttributes<Attribute>(false), SerializationConstructor)).FirstOrDefault(); | ||
Assert.IsNotNull(serializationCtor); | ||
Assert.IsTrue(serializationCtor.IsPublic, $"Serialization ctor for {refType.Name} should not be public"); | ||
} | ||
} | ||
|
||
[Test] | ||
public void ValidateInitializationConstructor() | ||
{ | ||
foreach (var refType in AssemblyTypes.Where(t => HasAttribute(t.GetCustomAttributes<Attribute>(false), PropertyReferenceTypeAttribute))) | ||
{ | ||
var initializationCtor = refType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) | ||
.Where(c => HasAttribute(c.GetCustomAttributes<Attribute>(false), InitializationConstructor)).FirstOrDefault(); | ||
Assert.IsNotNull(initializationCtor); | ||
Assert.IsTrue(refType.IsAbstract == initializationCtor.IsFamily, $"If {refType.Name} is abstract then its initialization ctor should be protected"); | ||
Assert.IsTrue(refType.IsAbstract != initializationCtor.IsPublic, $"If {refType.Name} is abstract then its initialization ctor should be public"); | ||
Assert.IsFalse(initializationCtor.IsAssembly, $"Initialization ctor for {refType.Name} should not be internal"); | ||
} | ||
} | ||
|
||
public bool HasAttribute(IEnumerable<Attribute> list, Type attributeType) | ||
{ | ||
return list.FirstOrDefault(a => a.GetType() == attributeType) is not null; | ||
} | ||
} | ||
} |