-
Notifications
You must be signed in to change notification settings - Fork 727
/
AzResourceTypeFactory.cs
264 lines (235 loc) · 14.5 KB
/
AzResourceTypeFactory.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Concurrent;
using Bicep.Core.Resources;
using Bicep.Core.Semantics;
using Bicep.Core.TypeSystem.Types;
namespace Bicep.Core.TypeSystem.Providers.Az
{
public class AzResourceTypeFactory
{
private readonly ConcurrentDictionary<(Azure.Bicep.Types.Concrete.TypeBase definedType, bool isResourceBodyType, bool isResourceBodyTopLevelPropertyType), TypeSymbol> typeCache;
public AzResourceTypeFactory()
{
typeCache = new();
}
public ResourceTypeComponents GetResourceType(Azure.Bicep.Types.Concrete.ResourceType resourceType, IEnumerable<FunctionOverload> resourceFunctions)
{
var resourceTypeReference = ResourceTypeReference.Parse(resourceType.Name);
var bodyType = GetTypeSymbol(resourceType.Body.Type, isResourceBodyType: true, isResourceBodyTopLevelPropertyType: false);
var assertsProperty = new TypeProperty(LanguageConstants.ResourceAssertPropertyName, AzResourceTypeProvider.ResourceAsserts);
// DeclaredResourceMetadata.TypeReference.FormatType()
if (bodyType is ObjectType objectType)
{
var properties = objectType.Properties.SetItem(LanguageConstants.ResourceAssertPropertyName, assertsProperty);
if (resourceFunctions.Any())
{
bodyType = new ObjectType(bodyType.Name, bodyType.ValidationFlags, properties.Values, objectType.AdditionalPropertiesType, objectType.AdditionalPropertiesFlags, resourceFunctions);
}
else
{
bodyType = new ObjectType(bodyType.Name, bodyType.ValidationFlags, properties.Values, objectType.AdditionalPropertiesType, objectType.AdditionalPropertiesFlags);
}
}
return new ResourceTypeComponents(resourceTypeReference, ToResourceScope(resourceType.ScopeType), ToResourceScope(resourceType.ReadOnlyScopes), ToResourceFlags(resourceType.Flags), bodyType);
}
public IEnumerable<FunctionOverload> GetResourceFunctionOverloads(Azure.Bicep.Types.Concrete.ResourceFunctionType resourceFunctionType)
{
yield return new FunctionOverloadBuilder(resourceFunctionType.Name)
.WithReturnType(GetTypeSymbol(resourceFunctionType.Output.Type, false, false))
.WithFlags(FunctionFlags.RequiresInlining)
.Build();
if (resourceFunctionType.Input is not null)
{
yield return new FunctionOverloadBuilder(resourceFunctionType.Name)
.WithRequiredParameter("apiVersion", TypeFactory.CreateStringLiteralType(resourceFunctionType.ApiVersion), "The api version")
.WithRequiredParameter("params", GetTypeSymbol(resourceFunctionType.Input.Type, false, false), $"{resourceFunctionType.Name} parameters")
.WithReturnType(GetTypeSymbol(resourceFunctionType.Output.Type, false, false))
.WithFlags(FunctionFlags.RequiresInlining)
.Build();
}
}
private TypeSymbol GetTypeSymbol(Azure.Bicep.Types.Concrete.TypeBase serializedType, bool isResourceBodyType, bool isResourceBodyTopLevelPropertyType)
// The cache key should always include *all* arguments passed to this function
=> typeCache.GetOrAdd((serializedType, isResourceBodyType, isResourceBodyTopLevelPropertyType),
t => ToTypeSymbol(t.definedType, t.isResourceBodyType, t.isResourceBodyTopLevelPropertyType));
private ITypeReference GetTypeReference(Azure.Bicep.Types.Concrete.ITypeReference input, bool isResourceBodyType, bool isResourceBodyTopLevelPropertyType)
=> new DeferredTypeReference(() => GetTypeSymbol(input.Type, isResourceBodyType, isResourceBodyTopLevelPropertyType));
private TypeProperty GetTypeProperty(string name, Azure.Bicep.Types.Concrete.ObjectTypeProperty input, bool isResourceBodyTopLevelPropertyType)
{
return new TypeProperty(name, GetTypeReference(input.Type, isResourceBodyType: false, isResourceBodyTopLevelPropertyType), GetTypePropertyFlags(input), input.Description);
}
private static TypePropertyFlags GetTypePropertyFlags(Azure.Bicep.Types.Concrete.ObjectTypeProperty input)
{
var flags = TypePropertyFlags.None;
if (input.Flags.HasFlag(Azure.Bicep.Types.Concrete.ObjectTypePropertyFlags.Required))
{
flags |= TypePropertyFlags.Required;
}
if (input.Flags.HasFlag(Azure.Bicep.Types.Concrete.ObjectTypePropertyFlags.ReadOnly))
{
flags |= TypePropertyFlags.ReadOnly;
}
if (input.Flags.HasFlag(Azure.Bicep.Types.Concrete.ObjectTypePropertyFlags.WriteOnly))
{
flags |= TypePropertyFlags.WriteOnly;
}
if (input.Flags.HasFlag(Azure.Bicep.Types.Concrete.ObjectTypePropertyFlags.DeployTimeConstant))
{
flags |= TypePropertyFlags.DeployTimeConstant;
}
if (!input.Flags.HasFlag(Azure.Bicep.Types.Concrete.ObjectTypePropertyFlags.Required) && !input.Flags.HasFlag(Azure.Bicep.Types.Concrete.ObjectTypePropertyFlags.ReadOnly))
{
// for non-required and non-readonly resource properties, we allow null assignment
flags |= TypePropertyFlags.AllowImplicitNull;
}
return flags;
}
private TypeSymbol ToTypeSymbol(Azure.Bicep.Types.Concrete.TypeBase typeBase, bool isResourceBodyType, bool isResourceBodyTopLevelPropertyType)
{
switch (typeBase)
{
case Azure.Bicep.Types.Concrete.AnyType:
return LanguageConstants.Any;
case Azure.Bicep.Types.Concrete.NullType:
return LanguageConstants.Null;
case Azure.Bicep.Types.Concrete.BooleanType:
return TypeFactory.CreateBooleanType(GetValidationFlags(isResourceBodyType, isResourceBodyTopLevelPropertyType));
case Azure.Bicep.Types.Concrete.IntegerType @int:
return TypeFactory.CreateIntegerType(@int.MinValue,
@int.MaxValue,
GetValidationFlags(isResourceBodyType, isResourceBodyTopLevelPropertyType));
case Azure.Bicep.Types.Concrete.StringType @string:
return TypeFactory.CreateStringType(@string.MinLength,
@string.MaxLength,
GetValidationFlags(isResourceBodyType, isResourceBodyTopLevelPropertyType));
case Azure.Bicep.Types.Concrete.BuiltInType builtInType:
return builtInType.Kind switch
{
#pragma warning disable 618
Azure.Bicep.Types.Concrete.BuiltInTypeKind.Any => LanguageConstants.Any,
Azure.Bicep.Types.Concrete.BuiltInTypeKind.Null => LanguageConstants.Null,
Azure.Bicep.Types.Concrete.BuiltInTypeKind.Bool => LanguageConstants.Bool,
Azure.Bicep.Types.Concrete.BuiltInTypeKind.Int => LanguageConstants.Int,
Azure.Bicep.Types.Concrete.BuiltInTypeKind.String => LanguageConstants.String,
Azure.Bicep.Types.Concrete.BuiltInTypeKind.Object => LanguageConstants.Object,
Azure.Bicep.Types.Concrete.BuiltInTypeKind.Array => LanguageConstants.Array,
Azure.Bicep.Types.Concrete.BuiltInTypeKind.ResourceRef => LanguageConstants.ResourceRef,
#pragma warning restore 618
_ => throw new ArgumentException(),
};
case Azure.Bicep.Types.Concrete.ObjectType objectType:
{
var additionalProperties = objectType.AdditionalProperties != null
? GetTypeReference(
input: objectType.AdditionalProperties,
isResourceBodyType: false,
isResourceBodyTopLevelPropertyType: isResourceBodyType)
: null;
var properties = objectType.Properties.Select(kvp => GetTypeProperty(
name: kvp.Key,
input: kvp.Value,
isResourceBodyTopLevelPropertyType: isResourceBodyType));
return new ObjectType(objectType.Name,
GetValidationFlags(isResourceBodyType, isResourceBodyTopLevelPropertyType),
properties,
additionalProperties,
TypePropertyFlags.None);
}
case Azure.Bicep.Types.Concrete.ArrayType arrayType:
{
return new TypedArrayType(GetTypeReference(arrayType.ItemType, false, false),
GetValidationFlags(isResourceBodyType: false, isResourceBodyTopLevelPropertyType: isResourceBodyType));
}
case Azure.Bicep.Types.Concrete.UnionType unionType:
{
return TypeHelper.CreateTypeUnion(
unionType.Elements.Select(x => GetTypeReference(x, isResourceBodyType, isResourceBodyTopLevelPropertyType)));
}
case Azure.Bicep.Types.Concrete.StringLiteralType stringLiteralType:
return TypeFactory.CreateStringLiteralType(stringLiteralType.Value,
GetValidationFlags(isResourceBodyType, isResourceBodyTopLevelPropertyType));
case Azure.Bicep.Types.Concrete.DiscriminatedObjectType discriminatedObjectType:
{
var elementReferences = discriminatedObjectType.Elements.Select(kvp => new DeferredTypeReference(() => ToCombinedType(
discriminatedObjectType.BaseProperties,
kvp.Key,
kvp.Value,
isResourceBodyType,
isResourceBodyTopLevelPropertyType)));
return new DiscriminatedObjectType(discriminatedObjectType.Name,
GetValidationFlags(isResourceBodyType, isResourceBodyTopLevelPropertyType),
discriminatedObjectType.Discriminator,
elementReferences);
}
default:
throw new ArgumentException();
}
}
private ObjectType ToCombinedType(
IEnumerable<KeyValuePair<string, Azure.Bicep.Types.Concrete.ObjectTypeProperty>> baseProperties,
string name,
Azure.Bicep.Types.Concrete.ITypeReference extendedType,
bool isResourceBodyType,
bool isResourceBodyTopLevelPropertyType)
{
if (extendedType.Type is not Azure.Bicep.Types.Concrete.ObjectType objectType)
{
throw new ArgumentException();
}
var additionalProperties = objectType.AdditionalProperties != null
? GetTypeReference(objectType.AdditionalProperties, isResourceBodyType: false, isResourceBodyTopLevelPropertyType: isResourceBodyType)
: null;
var extendedProperties = objectType.Properties.ToDictionary(kvp => kvp.Key, kvp => kvp.Value, StringComparer.OrdinalIgnoreCase);
foreach (var property in baseProperties.Where(x => !extendedProperties.ContainsKey(x.Key)))
{
extendedProperties[property.Key] = property.Value;
}
return new ObjectType(name,
GetValidationFlags(isResourceBodyType, isResourceBodyTopLevelPropertyType),
extendedProperties.Select(kvp => GetTypeProperty(kvp.Key, kvp.Value, isResourceBodyType)),
additionalProperties,
TypePropertyFlags.None);
}
private static TypeSymbolValidationFlags GetValidationFlags(bool isResourceBodyType, bool isResourceBodyTopLevelPropertyType)
{
var flags = TypeSymbolValidationFlags.Default;
if (!isResourceBodyType)
{
flags |= TypeSymbolValidationFlags.WarnOnPropertyTypeMismatch;
}
// strict validation on top-level resource properties, as 'custom' top-level properties are not supported by the platform
if (!isResourceBodyType && !isResourceBodyTopLevelPropertyType)
{
// in all other places, we should allow some wiggle room so that we don't block compilation if there are any swagger inaccuracies
flags |= TypeSymbolValidationFlags.WarnOnTypeMismatch;
}
return flags;
}
private static ResourceFlags ToResourceFlags(Azure.Bicep.Types.Concrete.ResourceFlags input)
{
var output = ResourceFlags.None;
if (input.HasFlag(Azure.Bicep.Types.Concrete.ResourceFlags.ReadOnly))
{
output |= ResourceFlags.ReadOnly;
}
return output;
}
private static ResourceScope ToResourceScope(Azure.Bicep.Types.Concrete.ScopeType input)
{
if (input == Azure.Bicep.Types.Concrete.ScopeType.Unknown)
{
return ResourceScope.Tenant | ResourceScope.ManagementGroup | ResourceScope.Subscription | ResourceScope.ResourceGroup | ResourceScope.Resource;
}
var output = ResourceScope.None;
output |= input.HasFlag(Azure.Bicep.Types.Concrete.ScopeType.Extension) ? ResourceScope.Resource : ResourceScope.None;
output |= input.HasFlag(Azure.Bicep.Types.Concrete.ScopeType.Tenant) ? ResourceScope.Tenant : ResourceScope.None;
output |= input.HasFlag(Azure.Bicep.Types.Concrete.ScopeType.ManagementGroup) ? ResourceScope.ManagementGroup : ResourceScope.None;
output |= input.HasFlag(Azure.Bicep.Types.Concrete.ScopeType.Subscription) ? ResourceScope.Subscription : ResourceScope.None;
output |= input.HasFlag(Azure.Bicep.Types.Concrete.ScopeType.ResourceGroup) ? ResourceScope.ResourceGroup : ResourceScope.None;
return output;
}
private static ResourceScope ToResourceScope(Azure.Bicep.Types.Concrete.ScopeType? input)
=> input.HasValue ? ToResourceScope(input.Value) : ResourceScope.None;
}
}