in src/AutoRest.CSharp/Common/Output/Models/Types/HelperTypeProviders/BicepSerializationTypeProvider.cs [72:175]
private List<MethodBodyStatement> WriteAppendChildObject()
{
var statements = new List<MethodBodyStatement>();
VariableReference indent = new VariableReference(typeof(string), "indent");
statements.Add(Declare(indent, New.Instance(typeof(string), Literal(' '), Spaces)));
VariableReference data = new VariableReference(typeof(BinaryData), "data");
var emptyObjectLength = new VariableReference(typeof(int), "emptyObjectLength");
var length = new VariableReference(typeof(int), "length");
var stringBuilder = new StringBuilderExpression(new ParameterReference(StringBuilderParameter));
statements.Add(Declare(
emptyObjectLength,
new BinaryOperatorExpression("+",
new BinaryOperatorExpression("+",
new BinaryOperatorExpression("+",
// 2 chars for open and close brace
new ConstantExpression(new Constant(2, typeof(int))),
Spaces),
// 2 new lines
EnvironmentExpression.NewLine().Property(nameof(string.Length))),
EnvironmentExpression.NewLine().Property(nameof(string.Length)))));
statements.Add(Declare(length, stringBuilder.Property(nameof(StringBuilder.Length))));
var inMultilineString = new VariableReference(typeof(bool), "inMultilineString");
statements.Add(Declare(inMultilineString, BoolExpression.False));
statements.Add(EmptyLine);
statements.Add(Declare(
data,
new InvokeStaticMethodExpression(typeof(ModelReaderWriter), nameof(ModelReaderWriter.Write),
new[]
{
new ParameterReference(ChildObject),
new ParameterReference(KnownParameters.Serializations.Options)
})));
VariableReference lines = new VariableReference(typeof(string[]), "lines");
statements.Add(Declare(
lines,
new InvokeInstanceMethodExpression(
new InvokeInstanceMethodExpression(data, nameof(ToString), Array.Empty<ValueExpression>(), null,
false),
nameof(string.Split),
new ValueExpression[]
{
new InvokeInstanceMethodExpression(
EnvironmentExpression.NewLine(), nameof(string.ToCharArray),
Array.Empty<ValueExpression>(), null, false),
new TypeReference(typeof(StringSplitOptions)).Property(nameof(StringSplitOptions.RemoveEmptyEntries))
},
null,
false)
));
var line = new VariableReference(typeof(string), "line");
var i = new VariableReference(typeof(int), "i");
statements.Add(new ForStatement(new AssignmentExpression(i, Int(0)), LessThan(i, lines.Property("Length")), new UnaryOperatorExpression("++", i, true))
{
Declare(line, new IndexerExpression(lines, i)),
// if this is a multiline string, we do not apply the indentation, except for the first line containing only the ''' which is handled
// in the subsequent if statement
new IfStatement(new BoolExpression(inMultilineString))
{
new IfStatement(new BoolExpression(line.Invoke(nameof(string.Contains), Literal("'''"))))
{
Assign(new BoolExpression(inMultilineString), BoolExpression.False)
},
stringBuilder.AppendLine(line),
Continue
},
new IfStatement(new BoolExpression(line.Invoke(nameof(string.Contains), Literal("'''"))))
{
Assign(new BoolExpression(inMultilineString), BoolExpression.True),
stringBuilder.AppendLine(new FormattableStringExpression("{0}{1}",indent, line)),
Continue
},
new IfElseStatement(
And(Equal(i, Int(0)),
Not(new BoolExpression(IndentFirstLine))),
stringBuilder.AppendLine(new FormattableStringExpression("{0}", line)),
stringBuilder.AppendLine(new FormattableStringExpression("{0}{1}", indent, line)))
});
statements.Add(new IfStatement(
new BoolExpression(
Equal(
stringBuilder.Property(nameof(StringBuilder.Length)),
new BinaryOperatorExpression("+", length, emptyObjectLength))))
{
Assign(
stringBuilder.Property(nameof(StringBuilder.Length)),
new BinaryOperatorExpression(
"-",
new BinaryOperatorExpression(
"-",
stringBuilder.Property(nameof(StringBuilder.Length)),
emptyObjectLength),
Literal(new StringExpression(FormattedPropertyName)).Property(nameof(string.Length))))
});
return statements;
}