@@ -32,20 +32,20 @@ namespace Azure.Generator.Management.Providers
32
32
internal class ResourceClientProvider : TypeProvider
33
33
{
34
34
private IReadOnlyCollection < InputOperation > _resourceOperations ;
35
- private ClientProvider _clientProvider ;
36
35
private readonly IReadOnlyList < string > _contextualParameters ;
37
36
private bool _isSingleton ;
38
37
39
38
private FieldProvider _dataField ;
40
- private FieldProvider _clientDiagonosticsField ;
41
- private FieldProvider _restClientField ;
42
39
private FieldProvider _resourcetypeField ;
40
+ protected ClientProvider _clientProvider ;
41
+ protected FieldProvider _clientDiagonosticsField ;
42
+ protected FieldProvider _restClientField ;
43
43
44
44
public ResourceClientProvider ( InputClient inputClient )
45
45
{
46
46
var resourceMetadata = inputClient . Decorators . Single ( d => d . Name . Equals ( KnownDecorators . ResourceMetadata ) ) ;
47
47
var codeModelId = resourceMetadata . Arguments ? [ KnownDecorators . ResourceModel ] . ToObjectFromJson < string > ( ) ! ;
48
- _isSingleton = _isSingleton = resourceMetadata . Arguments ? . TryGetValue ( "isSingleton" , out var isSingleton ) == true ? isSingleton . ToObjectFromJson < string > ( ) == "true" : false ;
48
+ _isSingleton = resourceMetadata . Arguments ? . TryGetValue ( "isSingleton" , out var isSingleton ) == true ? isSingleton . ToObjectFromJson < string > ( ) == "true" : false ;
49
49
var resourceType = resourceMetadata . Arguments ? [ KnownDecorators . ResourceType ] . ToObjectFromJson < string > ( ) ! ;
50
50
_resourcetypeField = new FieldProvider ( FieldModifiers . Public | FieldModifiers . Static | FieldModifiers . ReadOnly , typeof ( ResourceType ) , "ResourceType" , this , description : $ "Gets the resource type for the operations.", initializationValue : Literal ( resourceType ) ) ;
51
51
var resourceModel = ManagementClientGenerator . Instance . InputLibrary . GetModelByCrossLanguageDefinitionId ( codeModelId ) ! ;
@@ -144,7 +144,7 @@ private ConstructorProvider BuildPrimaryConstructor()
144
144
return new ConstructorProvider ( signature , bodyStatements , this ) ;
145
145
}
146
146
147
- private ConstructorProvider BuildInitializationConstructor ( )
147
+ protected ConstructorProvider BuildInitializationConstructor ( )
148
148
{
149
149
var idParameter = new ParameterProvider ( "id" , $ "The identifier of the resource that is the target of operations.", typeof ( ResourceIdentifier ) ) ;
150
150
var parameters = new List < ParameterProvider >
@@ -164,7 +164,7 @@ private ConstructorProvider BuildInitializationConstructor()
164
164
165
165
var bodyStatements = new MethodBodyStatement [ ]
166
166
{
167
- _clientDiagonosticsField . Assign ( New . Instance ( typeof ( ClientDiagnostics ) , Literal ( Type . Namespace ) , _resourcetypeField . Property ( nameof ( ResourceType . Namespace ) ) , This . Property ( "Diagnostics" ) ) ) . Terminate ( ) ,
167
+ _clientDiagonosticsField . Assign ( New . Instance ( typeof ( ClientDiagnostics ) , Literal ( Type . Namespace ) , ResourceTypeExpression . Property ( nameof ( ResourceType . Namespace ) ) , This . Property ( "Diagnostics" ) ) ) . Terminate ( ) ,
168
168
TryGetApiVersion ( out var apiVersion ) . Terminate ( ) ,
169
169
_restClientField . Assign ( New . Instance ( _clientProvider . Type , This . Property ( "Pipeline" ) , This . Property ( "Endpoint" ) , apiVersion ) ) . Terminate ( ) ,
170
170
Static ( Type ) . Invoke ( ValidateResourceIdMethodName , idParameter ) . Terminate ( )
@@ -174,7 +174,7 @@ private ConstructorProvider BuildInitializationConstructor()
174
174
}
175
175
176
176
private const string ValidateResourceIdMethodName = "ValidateResourceId" ;
177
- private MethodProvider BuildValidateResourceIdMethod ( )
177
+ protected MethodProvider BuildValidateResourceIdMethod ( )
178
178
{
179
179
var idParameter = new ParameterProvider ( "id" , $ "" , typeof ( ResourceIdentifier ) ) ;
180
180
var signature = new MethodSignature (
@@ -187,13 +187,17 @@ private MethodProvider BuildValidateResourceIdMethod()
187
187
idParameter
188
188
] ,
189
189
[ new AttributeStatement ( typeof ( ConditionalAttribute ) , Literal ( "DEBUG" ) ) ] ) ;
190
- var bodyStatements = new IfStatement ( idParameter . NotEqual ( _resourcetypeField ) )
190
+ var bodyStatements = new IfStatement ( idParameter . NotEqual ( ExpectedResourceTypeForValidation ) )
191
191
{
192
- Throw ( New . ArgumentException ( idParameter , StringSnippets . Format ( Literal ( "Invalid resource type {0} expected {1}" ) , idParameter . Property ( nameof ( ResourceIdentifier . ResourceType ) ) , _resourcetypeField ) , false ) )
192
+ Throw ( New . ArgumentException ( idParameter , StringSnippets . Format ( Literal ( "Invalid resource type {0} expected {1}" ) , idParameter . Property ( nameof ( ResourceIdentifier . ResourceType ) ) , ResourceTypeExpression ) , false ) )
193
193
} ;
194
194
return new MethodProvider ( signature , bodyStatements , this ) ;
195
195
}
196
196
197
+ protected virtual ValueExpression ResourceTypeExpression => _resourcetypeField ;
198
+
199
+ protected virtual ValueExpression ExpectedResourceTypeForValidation => _resourcetypeField ;
200
+
197
201
protected override CSharpType [ ] BuildImplements ( ) => [ typeof ( ArmResource ) ] ;
198
202
199
203
protected override MethodProvider [ ] BuildMethods ( )
@@ -246,7 +250,7 @@ private MethodProvider BuildOperationMethod(InputOperation operation, MethodProv
246
250
return new MethodProvider ( signature , bodyStatements , this ) ;
247
251
}
248
252
249
- private IReadOnlyList < ParameterProvider > GetOperationMethodParameters ( MethodProvider convenienceMethod , bool isLongRunning )
253
+ protected IReadOnlyList < ParameterProvider > GetOperationMethodParameters ( MethodProvider convenienceMethod , bool isLongRunning )
250
254
{
251
255
var result = new List < ParameterProvider > ( ) ;
252
256
if ( isLongRunning )
@@ -263,7 +267,7 @@ private IReadOnlyList<ParameterProvider> GetOperationMethodParameters(MethodProv
263
267
return result ;
264
268
}
265
269
266
- private CSharpType GetOperationMethodReturnType ( bool isAsync , bool isLongRunningOperation , IReadOnlyList < InputOperationResponse > operationResponses , out bool isGeneric )
270
+ protected CSharpType GetOperationMethodReturnType ( bool isAsync , bool isLongRunningOperation , IReadOnlyList < InputOperationResponse > operationResponses , out bool isGeneric )
267
271
{
268
272
isGeneric = false ;
269
273
if ( isLongRunningOperation )
@@ -372,7 +376,7 @@ private ValueExpression[] PopulateArguments(IReadOnlyList<ParameterProvider> par
372
376
}
373
377
374
378
// TODO: get clean name of operation Name
375
- private MethodProvider GetCorrespondingConvenienceMethod ( InputOperation operation , bool isAsync )
379
+ protected MethodProvider GetCorrespondingConvenienceMethod ( InputOperation operation , bool isAsync )
376
380
=> _clientProvider . CanonicalView . Methods . Single ( m => m . Signature . Name . Equals ( isAsync ? $ "{ operation . Name } Async" : operation . Name , StringComparison . OrdinalIgnoreCase ) && m . Signature . Parameters . Any ( p => p . Type . Equals ( typeof ( CancellationToken ) ) ) ) ;
377
381
378
382
private MethodProvider GetCorrespondingRequestMethod ( InputOperation operation )
@@ -382,7 +386,7 @@ public ScopedApi<bool> TryGetApiVersion(out ScopedApi<string> apiVersion)
382
386
{
383
387
var apiVersionDeclaration = new VariableExpression ( typeof ( string ) , $ "{ SpecName . ToLower ( ) } ApiVersion") ;
384
388
apiVersion = apiVersionDeclaration . As < string > ( ) ;
385
- var invocation = new InvokeMethodExpression ( This , "TryGetApiVersion" , [ _resourcetypeField , new DeclarationExpression ( apiVersionDeclaration , true ) ] ) ;
389
+ var invocation = new InvokeMethodExpression ( This , "TryGetApiVersion" , [ ResourceTypeExpression , new DeclarationExpression ( apiVersionDeclaration , true ) ] ) ;
386
390
return invocation . As < bool > ( ) ;
387
391
}
388
392
}
0 commit comments