-
Notifications
You must be signed in to change notification settings - Fork 166
/
InheritanceChooser.cs
235 lines (209 loc) · 9.34 KB
/
InheritanceChooser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using AutoRest.CSharp.AutoRest.Plugins;
using AutoRest.CSharp.Generation.Types;
using AutoRest.CSharp.Input;
using AutoRest.CSharp.Mgmt.AutoRest;
using AutoRest.CSharp.Mgmt.Generation;
using AutoRest.CSharp.Mgmt.Output;
using AutoRest.CSharp.Output.Models.Types;
using Azure.ResourceManager.Core;
namespace AutoRest.CSharp.Mgmt.Decorator
{
internal static class InheritanceChooser
{
public static IList<System.Type> ReferenceClassCollection = GetOrderedList();
private static IList<System.Type> GetReferenceClassCollection()
{
var assembly = Assembly.GetAssembly(typeof(ArmClient));
if (assembly == null)
{
return new List<System.Type>();
}
return assembly.GetTypes().Where(t => t.GetCustomAttributes(false).Where(a => a.GetType() == typeof(ReferenceTypeAttribute)).Count() > 0).ToList();
}
private static List<System.Type> GetOrderedList()
{
var referenceClasses = ConvertGenericType(GetReferenceClassCollection());
var trees = GetTrees(referenceClasses);
var output = new List<System.Type>();
foreach (var root in trees)
{
var treeNodes = new List<System.Type>();
Queue<Node> queue = new Queue<Node>();
queue.Enqueue(root);
while (queue.Count != 0)
{
Node tempNode = queue.Dequeue();
treeNodes.Add(tempNode.type);
List<Node> tempChilren = tempNode.children;
if (tempChilren != null)
{
int childNum = tempChilren.Count;
while (childNum > 0)
{
queue.Enqueue(tempChilren[childNum - 1]);
childNum--;
}
}
}
treeNodes.Reverse();
output.AddRange(treeNodes);
}
return output;
}
private static IList<System.Type> ConvertGenericType(IList<System.Type> referenceClassCollection)
{
for (int i = 0; i < referenceClassCollection.Count; i++)
{
if (referenceClassCollection[i].IsGenericType)
{
var attributeObj = referenceClassCollection[i].GetCustomAttributes().First() as ReferenceTypeAttribute;
referenceClassCollection[i] = referenceClassCollection[i].MakeGenericType(attributeObj!.GenericType);
}
}
return referenceClassCollection;
}
private static List<Node> GetTrees(IList<System.Type> referenceClassCollection)
{
List<Node> trees = new List<Node>();
var added = new Dictionary<System.Type, Node>();
foreach (System.Type reference in referenceClassCollection)
{
if (!added.ContainsKey(reference))
{
Node node = new Node(reference);
if (reference.BaseType != null && added.ContainsKey(reference.BaseType))
{
added[reference.BaseType].children.Add(node);
}
else
{
trees.Add(node);
}
added.Add(reference, node);
}
}
return trees;
}
public static CSharpType? GetExactMatch(OperationGroup? operationGroup, MgmtObjectType childType, ObjectTypeProperty[] properties)
{
foreach (System.Type parentType in ReferenceClassCollection)
{
if (IsEqual(parentType, properties))
{
return GetCSharpType(operationGroup, childType, parentType);
}
}
return null;
}
public static CSharpType? GetSupersetMatch(OperationGroup? operationGroup, MgmtObjectType originalType, ObjectTypeProperty[] properties)
{
foreach (System.Type parentType in ReferenceClassCollection)
{
if (IsSuperset(parentType, properties))
{
return GetCSharpType(operationGroup, originalType, parentType);
}
}
return null;
}
private static CSharpType GetCSharpType(OperationGroup? operationGroup, MgmtObjectType originalType, Type parentType)
{
var newParentType = operationGroup == null || !parentType.IsGenericType
? parentType
: parentType.GetGenericTypeDefinition().MakeGenericType(operationGroup.GetResourceIdentifierType(originalType, originalType.Context.Configuration.MgmtConfiguration, true));
return CSharpType.FromSystemType(originalType.Context, newParentType);
}
private static bool IsEqual(System.Type parentType, ObjectTypeProperty[] properties)
{
var childProperties = properties.ToList();
List<PropertyInfo> parentProperties = parentType.GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
if (parentProperties.Count != childProperties.Count)
return false;
Dictionary<string, PropertyInfo> parentDict = new Dictionary<string, PropertyInfo>();
foreach (var parentProperty in parentProperties)
{
parentDict.Add(parentProperty.Name, parentProperty);
}
foreach (var childProperty in childProperties)
{
if (!DoesPropertyExistInParent(childProperty, parentDict))
return false;
}
return true;
}
private static bool DoesPropertyExistInParent(ObjectTypeProperty childProperty, Dictionary<string, PropertyInfo> parentDict)
{
PropertyInfo? parentProperty;
CSharpType childPropertyType = childProperty.Declaration.Type;
if (!parentDict.TryGetValue(childProperty.Declaration.Name, out parentProperty))
return false;
if (parentProperty.PropertyType.IsGenericType)
{
if (!childPropertyType.Equals(new CSharpType(parentProperty.PropertyType)))
return false;
}
else if (parentProperty.PropertyType.FullName == $"{childPropertyType.Namespace}.{childPropertyType.Name}" ||
IsAssignable(parentProperty.PropertyType, childPropertyType))
{
if (childProperty.IsReadOnly != (parentProperty.GetSetMethod() == null))
return false;
}
else if (!(parentProperty.PropertyType.IsGenericParameter && IsAssignable(parentProperty.PropertyType.BaseType!, childPropertyType)))
{
return false;
}
return true;
}
/// <summary>
/// Tells if <paramref name="childPropertyType" /> can be assigned to <paramref name="parentPropertyType" />
/// by checking if there's an implicit type convertor in <paramref name="parentPropertyType" />.
/// Todo: should we check childPropertyType as well since an implicit can be defined in either classes?
/// </summary>
/// <param name="parentPropertyType">The type to be assigned to.</param>
/// <param name="childPropertyType">The type to assign.</param>
/// <returns></returns>
private static bool IsAssignable(System.Type parentPropertyType, CSharpType childPropertyType)
{
return parentPropertyType.GetMethods().Where(m => m.Name == "op_Implicit" &&
m.ReturnType == parentPropertyType &&
m.GetParameters().First().ParameterType.FullName == $"{childPropertyType.Namespace}.{childPropertyType.Name}").Count() > 0;
}
private static bool IsSuperset(System.Type parentType, ObjectTypeProperty[] properties)
{
var childProperties = properties.ToList();
List<PropertyInfo> parentProperties = parentType.GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
if (parentProperties.Count >= childProperties.Count)
return false;
Dictionary<string, PropertyInfo> parentDict = new Dictionary<string, PropertyInfo>();
int matchCount = 0;
foreach (var parentProperty in parentProperties)
{
parentDict.Add(parentProperty.Name, parentProperty);
}
foreach (var childProperty in childProperties)
{
if (parentProperties.Count == matchCount)
break;
if (DoesPropertyExistInParent(childProperty, parentDict))
matchCount++;
}
return parentProperties.Count == matchCount;
}
private class Node
{
public System.Type type;
public List<Node> children;
public Node(System.Type type)
{
this.type = type;
this.children = new List<Node>();
}
}
}
}