22// Licensed under the MIT License.
33
44using System ;
5+ using System . ClientModel . Primitives ;
56using System . Collections . Generic ;
7+ using System . IO ;
68using System . Linq ;
9+ using System . Text ;
10+ using System . Text . Json ;
711using System . Threading . Tasks ;
812using Azure ;
913using Azure . Core ;
@@ -30,13 +34,16 @@ internal class ArrayResponseCollectionResultDefinition : TypeProvider
3034 private readonly ClientProvider _restClient ;
3135 private readonly InputServiceMethod _serviceMethod ;
3236 private readonly CSharpType _itemType ;
33- private readonly CSharpType _listType ;
3437 private readonly bool _isAsync ;
35- private readonly string _scopeName ;
3638 private readonly IReadOnlyList < ParameterProvider > _constructorParameters ;
3739 private readonly string _methodName ;
3840 private readonly string _enclosingTypeName ;
3941
42+ private readonly FieldProvider _clientField ;
43+ private readonly IReadOnlyList < FieldProvider > _parameterFields ;
44+ private readonly FieldProvider _contextField ;
45+ private readonly FieldProvider _diagnosticScopeField ;
46+
4047 private static readonly ParameterProvider ContinuationTokenParameter =
4148 new ( "continuationToken" , $ "A continuation token indicating where to resume paging.", new CSharpType ( typeof ( string ) ) ) ;
4249 private static readonly ParameterProvider PageSizeHintParameter =
@@ -46,26 +53,43 @@ public ArrayResponseCollectionResultDefinition(
4653 ClientProvider restClient ,
4754 InputServiceMethod serviceMethod ,
4855 CSharpType itemType ,
49- CSharpType listType ,
5056 bool isAsync ,
51- string scopeName ,
5257 IReadOnlyList < ParameterProvider > constructorParameters ,
5358 string methodName ,
5459 string enclosingTypeName )
5560 {
5661 _restClient = restClient ;
5762 _serviceMethod = serviceMethod ;
5863 _itemType = itemType ;
59- _listType = listType ;
6064 _isAsync = isAsync ;
61- _scopeName = scopeName ;
6265 _constructorParameters = constructorParameters ;
6366 _methodName = methodName ;
6467 _enclosingTypeName = enclosingTypeName ;
68+
69+ _clientField = new FieldProvider (
70+ FieldModifiers . Private | FieldModifiers . ReadOnly ,
71+ _restClient . Type ,
72+ "_client" ,
73+ this ) ;
74+ _parameterFields = _constructorParameters . Select ( p => new FieldProvider (
75+ FieldModifiers . Private | FieldModifiers . ReadOnly ,
76+ p . Type ,
77+ ToFieldName ( p . Name ) ,
78+ this ) ) . ToArray ( ) ;
79+ _contextField = new FieldProvider (
80+ FieldModifiers . Private | FieldModifiers . ReadOnly ,
81+ typeof ( RequestContext ) ,
82+ "_context" ,
83+ this ) ;
84+ _diagnosticScopeField = new FieldProvider (
85+ FieldModifiers . Private | FieldModifiers . ReadOnly ,
86+ typeof ( string ) ,
87+ "_diagnosticScope" ,
88+ this ) ;
6589 }
6690
6791 protected override string BuildRelativeFilePath ( ) =>
68- System . IO . Path . Combine ( "src" , "Generated" , "CollectionResults" , $ "{ Name } .cs") ;
92+ Path . Combine ( "src" , "Generated" , "CollectionResults" , $ "{ Name } .cs") ;
6993
7094 protected override string BuildName ( )
7195 {
@@ -89,33 +113,7 @@ protected override CSharpType[] BuildImplements()
89113
90114 protected override FieldProvider [ ] BuildFields ( )
91115 {
92- var fields = new List < FieldProvider >
93- {
94- new FieldProvider (
95- FieldModifiers . Private | FieldModifiers . ReadOnly ,
96- _restClient . Type ,
97- "_client" ,
98- this )
99- } ;
100-
101- // Add fields for constructor parameters
102- foreach ( var param in _constructorParameters )
103- {
104- fields . Add ( new FieldProvider (
105- FieldModifiers . Private | FieldModifiers . ReadOnly ,
106- param . Type ,
107- ToFieldName ( param . Name ) ,
108- this ) ) ;
109- }
110-
111- // Add _context field
112- fields . Add ( new FieldProvider (
113- FieldModifiers . Private | FieldModifiers . ReadOnly ,
114- typeof ( RequestContext ) ,
115- "_context" ,
116- this ) ) ;
117-
118- return [ .. fields ] ;
116+ return [ _clientField , .. _parameterFields , _contextField , _diagnosticScopeField ] ;
119117 }
120118
121119 protected override ConstructorProvider [ ] BuildConstructors ( )
@@ -126,8 +124,10 @@ protected override ConstructorProvider[] BuildConstructors()
126124 } ;
127125 parameters . AddRange ( _constructorParameters ) ;
128126 parameters . Add ( new ParameterProvider ( "context" , $ "The request options, which can override default behaviors of the client pipeline on a per-call basis.", typeof ( RequestContext ) ) ) ;
127+ parameters . Add ( new ParameterProvider ( "diagnosticScope" , $ "The diagnostic scope name.", typeof ( string ) ) ) ;
129128
130- var contextParam = parameters . Last ( ) ;
129+ var contextParam = parameters [ ^ 2 ] ;
130+ var scopeParam = parameters [ ^ 1 ] ;
131131
132132 var signature = new ConstructorSignature (
133133 Type ,
@@ -137,15 +137,16 @@ protected override ConstructorProvider[] BuildConstructors()
137137
138138 var bodyStatements = new List < MethodBodyStatement >
139139 {
140- This . Property ( "_client" ) . Assign ( parameters [ 0 ] ) . Terminate ( )
140+ _clientField . Assign ( parameters [ 0 ] ) . Terminate ( )
141141 } ;
142142
143- foreach ( var param in _constructorParameters )
143+ for ( int i = 0 ; i < _constructorParameters . Count ; i ++ )
144144 {
145- bodyStatements . Add ( This . Property ( ToFieldName ( param . Name ) ) . Assign ( param ) . Terminate ( ) ) ;
145+ bodyStatements . Add ( _parameterFields [ i ] . Assign ( _constructorParameters [ i ] ) . Terminate ( ) ) ;
146146 }
147147
148- bodyStatements . Add ( This . Property ( "_context" ) . Assign ( contextParam ) . Terminate ( ) ) ;
148+ bodyStatements . Add ( _contextField . Assign ( contextParam ) . Terminate ( ) ) ;
149+ bodyStatements . Add ( _diagnosticScopeField . Assign ( scopeParam ) . Terminate ( ) ) ;
149150
150151 return [ new ConstructorProvider ( signature , bodyStatements . ToArray ( ) , this ) ] ;
151152 }
@@ -225,30 +226,30 @@ private MethodBodyStatement[] BuildGetNextResponseMethodBody()
225226 var requestArgs = new List < ValueExpression > ( ) ;
226227
227228 // Add arguments from fields
228- foreach ( var param in _constructorParameters )
229+ foreach ( var field in _parameterFields )
229230 {
230- requestArgs . Add ( This . Property ( ToFieldName ( param . Name ) ) ) ;
231+ requestArgs . Add ( field ) ;
231232 }
232233
233234 // Add context parameter
234- requestArgs . Add ( This . Property ( "_context" ) ) ;
235+ requestArgs . Add ( _contextField ) ;
235236
236237 var bodyStatements = new List < MethodBodyStatement >
237238 {
238239 Declare ( "message" , typeof ( HttpMessage ) ,
239- This . Property ( "_client" ) . Invoke ( createRequestMethodName , requestArgs ) ,
240+ _clientField . Invoke ( createRequestMethodName , requestArgs ) ,
240241 out var messageVariable ) ,
241242 UsingDeclare ( "scope" , typeof ( DiagnosticScope ) ,
242- This . Property ( "_client" ) . Property ( " ClientDiagnostics") . Invoke ( "CreateScope" , [ Literal ( _scopeName ) ] ) ,
243+ _clientField . Property ( "ClientDiagnostics" ) . Invoke ( "CreateScope" , [ _diagnosticScopeField ] ) ,
243244 out var scopeVariable ) ,
244245 scopeVariable . Invoke ( "Start" ) . Terminate ( )
245246 } ;
246247
247248 var tryStatements = new List < MethodBodyStatement >
248249 {
249- Return ( This . Property ( "_client" ) . Property ( "Pipeline" ) . Invoke (
250+ Return ( _clientField . Property ( "Pipeline" ) . Invoke (
250251 _isAsync ? "ProcessMessageAsync" : "ProcessMessage" ,
251- [ messageVariable , This . Property ( "_context" ) ] ,
252+ [ messageVariable , _contextField ] ,
252253 _isAsync ) )
253254 } ;
254255
@@ -280,8 +281,8 @@ private MethodProvider BuildParseArrayFromResponseMethod()
280281 var bodyStatements = new List < MethodBodyStatement >
281282 {
282283 // using var document = JsonDocument.Parse(response.Content, ModelSerializationExtensions.JsonDocumentOptions);
283- UsingDeclare ( "document" , typeof ( System . Text . Json . JsonDocument ) ,
284- Static ( typeof ( System . Text . Json . JsonDocument ) ) . Invoke ( "Parse" ,
284+ UsingDeclare ( "document" , typeof ( JsonDocument ) ,
285+ Static ( typeof ( JsonDocument ) ) . Invoke ( "Parse" ,
285286 new ValueExpression [ ]
286287 {
287288 responseParam . Property ( "Content" ) ,
@@ -290,7 +291,7 @@ private MethodProvider BuildParseArrayFromResponseMethod()
290291 out var documentVariable ) ,
291292
292293 // var array = document.RootElement;
293- Declare ( "array" , typeof ( System . Text . Json . JsonElement ) ,
294+ Declare ( "array" , typeof ( JsonElement ) ,
294295 documentVariable . Property ( "RootElement" ) ,
295296 out var arrayVariable ) ,
296297
@@ -300,8 +301,8 @@ private MethodProvider BuildParseArrayFromResponseMethod()
300301 out var resultVariable ) ,
301302
302303 // foreach (var element in array.EnumerateArray())
303- new Microsoft . TypeSpec . Generator . Statements . ForEachStatement (
304- typeof ( System . Text . Json . JsonElement ) ,
304+ new ForEachStatement (
305+ typeof ( JsonElement ) ,
305306 "element" ,
306307 arrayVariable . Invoke ( "EnumerateArray" ) ,
307308 isAsync : false ,
@@ -312,11 +313,11 @@ private MethodProvider BuildParseArrayFromResponseMethod()
312313 resultVariable . Invoke ( "Add" ,
313314 new ValueExpression [ ]
314315 {
315- Static ( new CSharpType ( typeof ( System . ClientModel . Primitives . ModelReaderWriter ) ) ) . Invoke ( "Read" ,
316+ Static ( new CSharpType ( typeof ( ModelReaderWriter ) ) ) . Invoke ( "Read" ,
316317 new ValueExpression [ ]
317318 {
318319 New . Instance ( typeof ( BinaryData ) ,
319- Static ( typeof ( System . Text . Encoding ) ) . Property ( "UTF8" ) . Invoke ( "GetBytes" ,
320+ Static ( typeof ( Encoding ) ) . Property ( "UTF8" ) . Invoke ( "GetBytes" ,
320321 elementVariable . Invoke ( "GetRawText" ) ) ) ,
321322 Static < ModelSerializationExtensionsDefinition > ( ) . Property ( "WireOptions" ) ,
322323 Static ( ManagementClientGenerator . Instance . OutputLibrary . ModelReaderWriterContextType ) . Property ( "Default" )
0 commit comments