in src/AutoRest.CSharp/Common/Output/Builders/BicepSerializationMethodsBuilder.cs [247:366]
private static IEnumerable<MethodBodyStatement> SerializeProperty(
StringBuilderExpression stringBuilder,
BicepPropertySerialization property,
int spaces,
PropertyOverrideVariables propertyOverrideVariables)
{
var indent = new string(' ', spaces);
bool isFlattened = false;
bool wasFlattened = false;
string? wirePath = null;
string? childPropertyName = null;
// is the property that we are trying to serialize a flattened property? If so, we need to use the name of the childmost property for overrides.
ValueExpression overridePropertyName = Nameof(property.Value);
string? previouslyFlattenedProperty = null;
if (property.Property!.FlattenedProperty != null)
{
overridePropertyName = Literal(property.Property!.FlattenedProperty.Declaration.Name);
isFlattened = true;
}
else if (WasPreviouslyFlattened(propertyOverrideVariables.serialization, property.Property, out previouslyFlattenedProperty, out wirePath))
{
wasFlattened = true;
childPropertyName = ((SerializableObjectType)property.Property.ValueType.Implementation).Properties
.Single(
// find the property of the child object that corresponds to the next piece of the wirepath
p => p.GetWirePath() == string.Join(".", wirePath!.Split('.').Skip(1))).Declaration.Name;
}
yield return Assign(
propertyOverrideVariables.HasPropertyOverride,
new BoolExpression(
And(
new BoolExpression(propertyOverrideVariables.HasObjectOverride),
new BoolExpression(propertyOverrideVariables.PropertyOverrides.Invoke("TryGetValue", overridePropertyName,
new KeywordExpression("out", propertyOverrideVariables.PropertyOverride))))));
var formattedPropertyName = $"{indent}{property.SerializedName}: ";
var propertyDictionary = new VariableReference(typeof(Dictionary<string, string>), "propertyDictionary");
// we write the properties if there is a value or an override for that property
wirePath = isFlattened ? property.Property.FlattenedProperty!.GetWirePath() : wirePath;
yield return new IfElseStatement(
new IfStatement(new BoolExpression(propertyOverrideVariables.HasPropertyOverride))
{
stringBuilder.Append(formattedPropertyName),
new[]
{
isFlattened
? WriteFlattenedPropertiesWithOverrides(wirePath!, stringBuilder, propertyOverrideVariables.PropertyOverride, spaces)
: stringBuilder.AppendLine(propertyOverrideVariables.PropertyOverride)
}
},
ConstructElseStatement());
MethodBodyStatement ConstructElseStatement()
{
if (wasFlattened)
{
return new IfElseStatement(
new IfStatement(
new BoolExpression(
And(
new BoolExpression(propertyOverrideVariables.HasObjectOverride),
new BoolExpression(propertyOverrideVariables.PropertyOverrides.Invoke("TryGetValue",
Literal(previouslyFlattenedProperty),
new KeywordExpression("out", propertyOverrideVariables.PropertyOverride))))))
{
stringBuilder.Append(formattedPropertyName),
new IfElseStatement(
Equal(property.Value, Null),
WriteFlattenedPropertiesWithOverrides(wirePath!, stringBuilder,
propertyOverrideVariables.PropertyOverride, spaces),
new[]
{
Declare(propertyDictionary, New.Instance(typeof(Dictionary<string, string>))),
propertyDictionary.Invoke(
"Add",
Literal(childPropertyName),
propertyOverrideVariables.PropertyOverride).ToStatement(),
propertyOverrideVariables.BicepOptions
.Property(nameof(BicepModelReaderWriterOptions.PropertyOverrides)).Invoke(
"Add",
property.Value,
propertyDictionary).ToStatement(),
InvokeAppendChildObject(stringBuilder, property, spaces, formattedPropertyName)
})
},
WrapInIsDefined(property,
new[]
{
WrapInIsNotEmpty(property,
new[]
{
stringBuilder.Append(formattedPropertyName),
InvokeAppendChildObject(stringBuilder, property, spaces,
formattedPropertyName)
})
})
);
}
return WrapInIsDefined(property,
new[]
{
WrapInIsNotEmpty(property,
new[]
{
stringBuilder.Append(formattedPropertyName),
InvokeAppendChildObject(stringBuilder, property, spaces,
formattedPropertyName)
})
});
}
yield return EmptyLine;
}