Skip to content

Commit b219608

Browse files
live1206Copilot
andauthored
Tweak public cosntructor to initialize collection-type properties (Azure#52535)
* Tweak public cosntructor to initialize collection-type properties * Update eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/Visitors/FlattenPropertyVisitor.cs Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 8bd6848 commit b219608

File tree

3 files changed

+18
-55
lines changed

3 files changed

+18
-55
lines changed

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/Utilities/PropertyHelpers.cs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -107,30 +107,6 @@ public static MethodBodyStatement BuildGetter(bool? includeGetterNullCheck, Prop
107107
}
108108
}
109109

110-
public static MethodBodyStatement BuildGetterForCollectionProperty(IEnumerable<PropertyProvider> collectionTypeProperties, bool? includeGetterNullCheck, string initializationMethodName, PropertyProvider internalProperty, TypeProvider innerModel, PropertyProvider singleProperty)
111-
{
112-
var checkNullExpression = This.Property(internalProperty.Name).Is(Null);
113-
if (includeGetterNullCheck == true)
114-
{
115-
return new List<MethodBodyStatement> {
116-
This.Invoke(initializationMethodName).Terminate(),
117-
Return(new MemberExpression(internalProperty, singleProperty.Name))
118-
};
119-
}
120-
else if (includeGetterNullCheck == false)
121-
{
122-
return Return(new TernaryConditionalExpression(checkNullExpression, Default, new MemberExpression(internalProperty, singleProperty.Name)));
123-
}
124-
else
125-
{
126-
if (innerModel.Type.IsNullable)
127-
{
128-
return Return(new MemberExpression(internalProperty.AsVariableExpression.NullConditional(), singleProperty.Name));
129-
}
130-
return Return(new MemberExpression(internalProperty, singleProperty.Name));
131-
}
132-
}
133-
134110
public static MethodBodyStatement BuildSetterForPropertyFlatten(ModelProvider innerModel, PropertyProvider internalProperty, PropertyProvider innerProperty)
135111
{
136112
var isNullableValueType = innerProperty.Type.IsValueType && innerProperty.Type.IsNullable;
@@ -146,17 +122,6 @@ public static MethodBodyStatement BuildSetterForPropertyFlatten(ModelProvider in
146122
return setter;
147123
}
148124

149-
public static MethodBodyStatement BuildSetterForCollectionProperty(IEnumerable<PropertyProvider> collectionTypeProperties, string initializationMethodName, PropertyProvider internalProperty, PropertyProvider innerProperty)
150-
{
151-
var isNullableValueType = innerProperty.Type.IsValueType && innerProperty.Type.IsNullable;
152-
var setter = new List<MethodBodyStatement>();
153-
var internalPropertyExpression = This.Property(internalProperty.Name);
154-
155-
setter.Add(This.Invoke(initializationMethodName).Terminate());
156-
setter.Add(internalPropertyExpression.Property(innerProperty.Name).Assign(isNullableValueType ? Value.Property(nameof(Nullable<int>.Value)) : Value).Terminate());
157-
return setter;
158-
}
159-
160125
public static Dictionary<ValueExpression, ValueExpression> PopulateCollectionProperties(IEnumerable<PropertyProvider> collectionTypeProperties)
161126
{
162127
var result = new Dictionary<ValueExpression, ValueExpression>();

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/Visitors/FlattenPropertyVisitor.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,21 @@ internal class FlattenPropertyVisitor : ScmLibraryVisitor
4040
{
4141
var innerCollectionProperties = collectionProperties.Select(x => x.InnerProperty);
4242
var initializationMethod = BuildInitializationMethod(innerCollectionProperties, internalProperty, model);
43-
model.Update(methods: [.. model.Methods, initializationMethod]);
43+
var publicConstructor = model.Constructors.Single(m => m.Signature.Modifiers.HasFlag(MethodSignatureModifiers.Public));
44+
var invokeInitialization = This.Invoke(initializationMethod.Signature.Name).Terminate();
45+
4446
// If the property is a collection type, we need to ensure that it is initialized
45-
foreach (var (flattenedProperty, innerProperty) in collectionProperties)
46-
flattenedProperty.Update(body: new MethodPropertyBody(
47-
PropertyHelpers.BuildGetterForCollectionProperty(innerCollectionProperties, true, initializationMethod.Signature.Name, internalProperty, ManagementClientGenerator.Instance.TypeFactory.CSharpTypeMap[internalProperty.Type]!, innerProperty),
48-
PropertyHelpers.BuildSetterForCollectionProperty(innerCollectionProperties, initializationMethod.Signature.Name, internalProperty, innerProperty)));
47+
if (publicConstructor.BodyStatements is null)
48+
{
49+
publicConstructor.Update(bodyStatements: new List<MethodBodyStatement> { invokeInitialization });
50+
}
51+
else
52+
{
53+
var body = publicConstructor.BodyStatements.ToList();
54+
body.Add(invokeInitialization);
55+
publicConstructor.Update(bodyStatements: body);
56+
}
57+
model.Update(methods: [.. model.Methods, initializationMethod]);
4958
}
5059
}
5160

@@ -276,7 +285,7 @@ private void FlattenProperties(ModelProvider model)
276285
var (_, includeGetterNullCheck, _) = PropertyHelpers.GetFlags(property, innerProperty);
277286
var flattenPropertyName = innerProperty.Name; // TODO: handle name conflicts
278287
var flattenPropertyBody = new MethodPropertyBody(
279-
PropertyHelpers.BuildGetter(includeGetterNullCheck, property, modelProvider, innerProperty),
288+
PropertyHelpers.BuildGetter(includeGetterNullCheck == true && !innerProperty.Type.IsCollection, property, modelProvider, innerProperty),
280289
!innerProperty.Body.HasSetter ? null : PropertyHelpers.BuildSetterForPropertyFlatten(modelProvider, property, innerProperty)
281290
);
282291

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/BarSettingsResourceData.cs

Lines changed: 3 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)