Skip to content

Commit cb5f88d

Browse files
Allow mutation of InvokeMethodExpression and TryCatchFinallyStatement (#7229)
Contributes to Azure/azure-sdk-for-net#49632
1 parent 1ae0a5e commit cb5f88d

File tree

5 files changed

+184
-19
lines changed

5 files changed

+184
-19
lines changed

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Expressions/InvokeMethodExpression.cs

Lines changed: 100 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,71 @@ private InvokeMethodExpression(
2121
bool addConfigureAwaitFalse = true,
2222
CSharpType? extensionType = null)
2323
{
24-
InstanceReference = instanceReference;
25-
MethodName = methodName;
26-
MethodSignature = methodSignature;
27-
Arguments = arguments;
28-
TypeArguments = typeArguments;
29-
CallAsAsync = callAsAsync;
30-
AddConfigureAwaitFalse = addConfigureAwaitFalse;
31-
ExtensionType = extensionType;
24+
_instanceReference = instanceReference;
25+
_methodName = methodName;
26+
_methodSignature = methodSignature;
27+
_arguments = arguments;
28+
_typeArguments = typeArguments;
29+
_callAsAsync = callAsAsync;
30+
_addConfigureAwaitFalse = addConfigureAwaitFalse;
31+
_extensionType = extensionType;
3232
}
3333

34-
public CSharpType? ExtensionType { get; init; }
34+
public CSharpType? ExtensionType
35+
{
36+
get => _extensionType;
37+
init => _extensionType = value;
38+
}
39+
private CSharpType? _extensionType;
3540

36-
public bool AddConfigureAwaitFalse { get; init; }
41+
public bool AddConfigureAwaitFalse
42+
{
43+
get => _addConfigureAwaitFalse;
44+
init => _addConfigureAwaitFalse = value;
45+
}
46+
private bool _addConfigureAwaitFalse;
3747

38-
public bool CallAsAsync { get; init; }
48+
public bool CallAsAsync
49+
{
50+
get => _callAsAsync;
51+
init => _callAsAsync = value;
52+
}
53+
private bool _callAsAsync;
3954

40-
public IReadOnlyList<CSharpType>? TypeArguments { get; init; }
55+
public IReadOnlyList<CSharpType>? TypeArguments
56+
{
57+
get => _typeArguments;
58+
init => _typeArguments = value;
59+
}
60+
private IReadOnlyList<CSharpType>? _typeArguments;
4161

42-
public IReadOnlyList<ValueExpression> Arguments { get; init; }
62+
public IReadOnlyList<ValueExpression> Arguments
63+
{
64+
get => _arguments;
65+
init => _arguments = value;
66+
}
67+
private IReadOnlyList<ValueExpression> _arguments;
4368

44-
public MethodSignatureBase? MethodSignature { get; init; }
69+
public MethodSignatureBase? MethodSignature
70+
{
71+
get => _methodSignature;
72+
init => _methodSignature = value;
73+
}
74+
private MethodSignatureBase? _methodSignature;
4575

46-
public string? MethodName { get; init; }
76+
public string? MethodName
77+
{
78+
get => _methodName;
79+
init => _methodName = value;
80+
}
81+
private string? _methodName;
4782

48-
public ValueExpression? InstanceReference { get; init; }
83+
public ValueExpression? InstanceReference
84+
{
85+
get => _instanceReference;
86+
init => _instanceReference = value;
87+
}
88+
private ValueExpression? _instanceReference;
4989

5090
public InvokeMethodExpression(ValueExpression? instanceReference, string methodName, IReadOnlyList<ValueExpression> arguments) : this(instanceReference, methodName, null, arguments, null, false) { }
5191

@@ -71,5 +111,49 @@ internal override void Write(CodeWriter writer)
71111

72112
private MethodBodyStatement? _terminated;
73113
public MethodBodyStatement Terminate() => _terminated ??= new ExpressionStatement(this);
114+
115+
public void Update(
116+
ValueExpression? instanceReference = null,
117+
string? methodName = null,
118+
MethodSignatureBase? methodSignature = null,
119+
IReadOnlyList<ValueExpression>? arguments = null,
120+
IReadOnlyList<CSharpType>? typeArguments = null,
121+
bool? callAsAsync = null,
122+
bool? addConfigureAwaitFalse = null,
123+
CSharpType? extensionType = null)
124+
{
125+
if (instanceReference != null)
126+
{
127+
_instanceReference = instanceReference;
128+
}
129+
if (methodName != null)
130+
{
131+
_methodName = methodName;
132+
}
133+
if (methodSignature != null)
134+
{
135+
_methodSignature = methodSignature;
136+
}
137+
if (arguments != null)
138+
{
139+
_arguments = arguments;
140+
}
141+
if (typeArguments != null)
142+
{
143+
_typeArguments = typeArguments;
144+
}
145+
if (callAsAsync != null)
146+
{
147+
_callAsAsync = callAsAsync.Value;
148+
}
149+
if (addConfigureAwaitFalse != null)
150+
{
151+
_addConfigureAwaitFalse = addConfigureAwaitFalse.Value;
152+
}
153+
if (extensionType != null)
154+
{
155+
_extensionType = extensionType;
156+
}
157+
}
74158
}
75159
}

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Statements/TryCatchFinallyStatement.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace Microsoft.TypeSpec.Generator.Statements
99
{
1010
public sealed class TryCatchFinallyStatement : MethodBodyStatement
1111
{
12-
public TryStatement Try { get; }
13-
public IReadOnlyList<CatchStatement> Catches { get; }
14-
public FinallyStatement? Finally { get; }
12+
public TryStatement Try { get; private set; }
13+
public IReadOnlyList<CatchStatement> Catches { get; private set; }
14+
public FinallyStatement? Finally { get; private set; }
1515

1616
public TryCatchFinallyStatement(TryStatement @try, IReadOnlyList<CatchStatement> catches, FinallyStatement? @finally)
1717
{
@@ -39,5 +39,24 @@ internal override void Write(CodeWriter writer)
3939

4040
Finally?.Write(writer);
4141
}
42+
43+
public void Update(
44+
TryStatement? @try = null,
45+
IReadOnlyList<CatchStatement>? catches = null,
46+
FinallyStatement? @finally = null)
47+
{
48+
if (@try != null)
49+
{
50+
Try = @try;
51+
}
52+
if (catches != null)
53+
{
54+
Catches = catches;
55+
}
56+
if (@finally != null)
57+
{
58+
Finally = @finally;
59+
}
60+
}
4261
}
4362
}

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Expressions/ExpressionsTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,44 @@ public void TestInvokeInstanceMethodExpression()
5858
Assert.AreEqual(expectedResult, test);
5959
}
6060

61+
[Test]
62+
public void TestUpdateInvokeInstanceMethodExpression()
63+
{
64+
// declare the instance method
65+
var mockTypeProvider = new Mock<TypeProvider>();
66+
var returnInstanceMethod = Return(new InvokeMethodExpression(null, "Bar", [Bool(true), Bool(false), Bool(false)]));
67+
var fooMethod = new MethodProvider(
68+
new MethodSignature(
69+
Name: "Foo",
70+
Modifiers: MethodSignatureModifiers.Public,
71+
ReturnType: typeof(bool),
72+
Parameters: [],
73+
Description: null, ReturnDescription: null),
74+
new MethodBodyStatement[] { returnInstanceMethod },
75+
mockTypeProvider.Object);
76+
77+
var returnExpression = returnInstanceMethod as ExpressionStatement;
78+
Assert.IsNotNull(returnExpression);
79+
80+
var keyWordExpression = returnExpression!.Expression as KeywordExpression;
81+
Assert.IsNotNull(keyWordExpression);
82+
83+
var invokeMethodExpression = keyWordExpression!.Expression as InvokeMethodExpression;
84+
Assert.IsNotNull(invokeMethodExpression);
85+
86+
invokeMethodExpression!.Update(
87+
methodName: "Baz",
88+
arguments: [Bool(true)]);
89+
90+
// Verify the expected behavior
91+
using var writer = new CodeWriter();
92+
writer.WriteMethod(fooMethod);
93+
94+
var expectedResult = Helpers.GetExpectedFromFile();
95+
var test = writer.ToString(false);
96+
Assert.AreEqual(expectedResult, test);
97+
}
98+
6199
[Test]
62100
public void TestNewInstanceExpression()
63101
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public bool Foo()
2+
{
3+
return Baz(true);
4+
}

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Statements/StatementTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,26 @@ public void TryCatchFinallyStatementWithMultipleCatches()
457457
Assert.AreEqual(expectedResult, test);
458458
}
459459

460+
[Test]
461+
public void TryCatchFinallyUpdate()
462+
{
463+
var tryCatchFinally = new TryCatchFinallyStatement(new TryStatement(), new CatchStatement(null), new FinallyStatement());
464+
465+
var tryStatement = new TryStatement();
466+
var catchStatement = new CatchStatement(null);
467+
var finallyStatement = new FinallyStatement();
468+
469+
tryCatchFinally.Update(
470+
@try: tryStatement,
471+
catches: [catchStatement],
472+
@finally: finallyStatement);
473+
474+
Assert.AreEqual(tryStatement, tryCatchFinally.Try);
475+
Assert.AreEqual(1, tryCatchFinally.Catches.Count);
476+
Assert.AreEqual(catchStatement, tryCatchFinally.Catches[0]);
477+
Assert.AreEqual(finallyStatement, tryCatchFinally.Finally);
478+
}
479+
460480
[Test]
461481
public void TestIfElsePreprocessorStatement()
462482
{

0 commit comments

Comments
 (0)