-
Notifications
You must be signed in to change notification settings - Fork 161
/
ChangeTrackingListProvider.cs
300 lines (268 loc) · 13.6 KB
/
ChangeTrackingListProvider.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using AutoRest.CSharp.Common.Input;
using AutoRest.CSharp.Common.Output.Expressions.KnownValueExpressions;
using AutoRest.CSharp.Common.Output.Expressions.Statements;
using AutoRest.CSharp.Common.Output.Expressions.ValueExpressions;
using AutoRest.CSharp.Common.Output.Models;
using AutoRest.CSharp.Generation.Types;
using AutoRest.CSharp.Generation.Writers;
using AutoRest.CSharp.Input.Source;
using AutoRest.CSharp.Output.Models.Shared;
using static AutoRest.CSharp.Common.Output.Models.Snippets;
namespace AutoRest.CSharp.Output.Models.Types
{
internal class ChangeTrackingListProvider : ExpressionTypeProvider
{
private static readonly Lazy<ChangeTrackingListProvider> _instance = new(() => new ChangeTrackingListProvider());
private class ChangeTrackingListTemplate<T> { }
private readonly MethodSignature _ensureListSignature;
private readonly MethodSignature _getEnumeratorSignature;
private readonly CSharpType _t;
private readonly FieldDeclaration _innerListField;
private readonly CSharpType _tArray;
private readonly Parameter _tParam;
private readonly Parameter _indexParam = new Parameter("index", null, typeof(int), null, ValidationType.None, null);
private VariableReference _innerList;
private readonly CSharpType _iListOfT;
private readonly CSharpType _iReadOnlyListOfT;
private BoolExpression IsUndefined { get; } = new BoolExpression(new MemberExpression(This, "IsUndefined"));
private InvokeInstanceMethodExpression EnsureList { get; init; }
public static ChangeTrackingListProvider Instance => _instance.Value;
private ChangeTrackingListProvider() : base(Configuration.HelperNamespace, null)
{
_t = typeof(ChangeTrackingListTemplate<>).GetGenericArguments()[0];
_iListOfT = new CSharpType(typeof(IList<>), _t);
_iReadOnlyListOfT = new CSharpType(typeof(IReadOnlyList<>), _t);
_ensureListSignature = new MethodSignature("EnsureList", null, null, MethodSignatureModifiers.Public, _iListOfT, null, Array.Empty<Parameter>());
_getEnumeratorSignature = new MethodSignature("GetEnumerator", null, null, MethodSignatureModifiers.Public, new CSharpType(typeof(IEnumerator<>), _t), null, Array.Empty<Parameter>());
_innerListField = new FieldDeclaration(FieldModifiers.Private, _iListOfT, "_innerList");
_innerList = new VariableReference(_iListOfT, _innerListField.Declaration);
_tArray = typeof(ChangeTrackingListTemplate<>).GetGenericArguments()[0].MakeArrayType();
_tParam = new Parameter("item", null, _t, null, ValidationType.None, null);
DeclarationModifiers = TypeSignatureModifiers.Internal;
EnsureList = This.Invoke(_ensureListSignature);
}
protected override string DefaultName => "ChangeTrackingList";
protected override IEnumerable<Method> BuildConstructors()
{
yield return new Method(new ConstructorSignature(Type, null, null, MethodSignatureModifiers.Public, Array.Empty<Parameter>()), EmptyStatement);
var iListParam = new Parameter("innerList", null, _iListOfT, null, ValidationType.None, null);
var iListSignature = new ConstructorSignature(Type, null, null, MethodSignatureModifiers.Public, new Parameter[] { iListParam });
var iListVariable = new ParameterReference(iListParam);
var iListBody = new MethodBodyStatement[]
{
new IfStatement(NotEqual(iListVariable, Null))
{
new AssignValueStatement(_innerList, iListVariable)
}
};
yield return new Method(iListSignature, iListBody);
var iReadOnlyListParam = new Parameter("innerList", null, _iReadOnlyListOfT, null, ValidationType.None, null);
var iReadOnlyListSignature = new ConstructorSignature(Type, null, null, MethodSignatureModifiers.Public, new Parameter[] { iReadOnlyListParam });
var iReadOnlyListVariable = new ParameterReference(iReadOnlyListParam);
var iReadOnlyListBody = new MethodBodyStatement[]
{
new IfStatement(NotEqual(iReadOnlyListVariable, Null))
{
new AssignValueStatement(_innerList, Linq.ToList(iReadOnlyListVariable))
}
};
yield return new Method(iReadOnlyListSignature, iReadOnlyListBody);
}
protected override IEnumerable<CSharpType> BuildTypeArguments()
{
yield return _t;
}
protected override IEnumerable<CSharpType> BuildImplements()
{
yield return _iListOfT;
yield return _iReadOnlyListOfT;
}
protected override IEnumerable<FieldDeclaration> BuildFields()
{
yield return _innerListField;
}
protected override PropertyDeclaration[] BuildProperties() =>
new[]
{
new PropertyDeclaration(null, MethodSignatureModifiers.Public, typeof(bool), "IsUndefined", new ExpressionPropertyBody(Equal(_innerList, Null))),
BuildCount(),
BuildIsReadOnly(),
BuildIndexer()
};
private PropertyDeclaration BuildIsReadOnly()
{
return new PropertyDeclaration(null, MethodSignatureModifiers.Public, typeof(bool), "IsReadOnly",
new ExpressionPropertyBody(new TernaryConditionalOperator(
IsUndefined,
False,
new MemberExpression(EnsureList, "IsReadOnly"))));
}
private PropertyDeclaration BuildCount()
{
return new PropertyDeclaration(null, MethodSignatureModifiers.Public, typeof(int), "Count",
new ExpressionPropertyBody(new TernaryConditionalOperator(
IsUndefined,
Literal(0),
new MemberExpression(EnsureList, "Count"))));
}
private PropertyDeclaration BuildIndexer()
{
var indexParam = new Parameter("index", null, typeof(int), null, ValidationType.None, null);
return new IndexerDeclaration(null, MethodSignatureModifiers.Public, _t, "this", indexParam, new MethodPropertyBody(
new MethodBodyStatement[]
{
new IfStatement(IsUndefined)
{
Throw(New.Instance(typeof(ArgumentOutOfRangeException), Nameof(new ParameterReference(_indexParam))))
},
Return(new ArrayElementExpression(EnsureList, new ParameterReference(_indexParam))),
},
new MethodBodyStatement[]
{
new IfStatement(IsUndefined)
{
Throw(New.Instance(typeof(ArgumentOutOfRangeException), Nameof(new ParameterReference(_indexParam))))
},
new AssignValueStatement(
new ArrayElementExpression(EnsureList, new ParameterReference(_indexParam)),
new KeywordExpression("value", null))
}));
}
protected override IEnumerable<Method> BuildMethods()
{
yield return BuildReset();
yield return BuildGetEnumeratorOfT();
yield return BuildGetEnumerator();
yield return BuildAdd();
yield return BuildClear();
yield return BuildContains();
yield return BuildCopyTo();
yield return BuildRemove();
yield return BuildIndexOf();
yield return BuildInsert();
yield return BuildRemoveAt();
yield return BuildEnsureList();
}
private Method BuildRemoveAt()
{
var indexVariable = new ParameterReference(_indexParam);
return new Method(new MethodSignature("RemoveAt", null, null, MethodSignatureModifiers.Public, null, null, new Parameter[] { _indexParam }), new MethodBodyStatement[]
{
new IfStatement(IsUndefined)
{
Throw(New.Instance(typeof(ArgumentOutOfRangeException), Nameof(indexVariable)))
},
new InvokeInstanceMethodStatement(EnsureList, "RemoveAt", new ValueExpression[] { indexVariable }, false)
});
}
private Method BuildInsert()
{
return new Method(new MethodSignature("Insert", null, null, MethodSignatureModifiers.Public, null, null, new Parameter[] { _indexParam, _tParam }), new MethodBodyStatement[]
{
new InvokeInstanceMethodStatement(EnsureList, "Insert", new ValueExpression[] { new ParameterReference(_indexParam), new ParameterReference(_tParam) }, false)
});
}
private Method BuildIndexOf()
{
var signature = new MethodSignature("IndexOf", null, null, MethodSignatureModifiers.Public, typeof(int), null, new Parameter[] { _tParam });
return new Method(signature, new MethodBodyStatement[]
{
new IfStatement(IsUndefined)
{
Return(Literal(-1))
},
Return(EnsureList.Invoke(signature))
});
}
private Method BuildRemove()
{
var signature = new MethodSignature("Remove", null, null, MethodSignatureModifiers.Public, typeof(bool), null, new Parameter[] { _tParam });
return new Method(signature, new MethodBodyStatement[]
{
new IfStatement(IsUndefined)
{
Return(False)
},
Return(EnsureList.Invoke(signature))
});
}
private Method BuildCopyTo()
{
var arrayParam = new Parameter("array", null, _tArray, null, ValidationType.None, null);
var arrayIndexParam = new Parameter("arrayIndex", null, typeof(int), null, ValidationType.None, null);
return new Method(new MethodSignature("CopyTo", null, null, MethodSignatureModifiers.Public, null, null, new Parameter[] { arrayParam, arrayIndexParam }), new MethodBodyStatement[]
{
new IfStatement(IsUndefined)
{
Return()
},
new InvokeInstanceMethodStatement(EnsureList, "CopyTo", new ValueExpression[] { new ParameterReference(arrayParam), new ParameterReference(arrayIndexParam) }, false)
});
}
private Method BuildContains()
{
var signature = new MethodSignature("Contains", null, null, MethodSignatureModifiers.Public, typeof(bool), null, new Parameter[] { _tParam });
return new Method(signature, new MethodBodyStatement[]
{
new IfStatement(IsUndefined)
{
Return(False)
},
Return(EnsureList.Invoke(signature))
});
}
private Method BuildClear()
{
return new Method(new MethodSignature("Clear", null, null, MethodSignatureModifiers.Public, null, null, Array.Empty<Parameter>()), new MethodBodyStatement[]
{
new InvokeInstanceMethodStatement(EnsureList, "Clear")
});
}
private Method BuildAdd()
{
var genericParameter = new Parameter("item", null, _t, null, ValidationType.None, null);
return new Method(new MethodSignature("Add", null, null, MethodSignatureModifiers.Public, null, null, new Parameter[] { genericParameter }), new MethodBodyStatement[]
{
new InvokeInstanceMethodStatement(EnsureList, "Add", new ParameterReference(genericParameter))
});
}
private Method BuildGetEnumerator()
{
return new Method(new MethodSignature("GetEnumerator", null, null, MethodSignatureModifiers.None, typeof(IEnumerator), null, Array.Empty<Parameter>(), ExplicitInterface: typeof(IEnumerable)), new MethodBodyStatement[]
{
Return(This.Invoke(_getEnumeratorSignature))
});
}
private Method BuildGetEnumeratorOfT()
{
return new Method(_getEnumeratorSignature, new MethodBodyStatement[]
{
new IfStatement(IsUndefined)
{
new DeclareLocalFunctionStatement(new CodeWriterDeclaration("enumerateEmpty"), Array.Empty<Parameter>(), new CSharpType(typeof(IEnumerator<>), _t), new KeywordStatement("yield", new KeywordExpression("break", null))),
Return(new InvokeStaticMethodExpression(null, "enumerateEmpty", Array.Empty<ValueExpression>()))
},
Return(EnsureList.Invoke(_getEnumeratorSignature))
});
}
private Method BuildReset()
{
return new Method(new MethodSignature("Reset", null, null, MethodSignatureModifiers.Public, null, null, Array.Empty<Parameter>()), new MethodBodyStatement[]
{
Assign(_innerList, Null)
});
}
private Method BuildEnsureList()
{
return new Method(_ensureListSignature, new MethodBodyStatement[]
{
Return(new BinaryOperatorExpression("??=", _innerList, New.Instance(new CSharpType(typeof(List<>), _t))))
});
}
}
}