@@ -32,20 +32,20 @@ namespace Azure.Generator.Management.Providers
3232 internal class ResourceClientProvider : TypeProvider
3333 {
3434 private IReadOnlyCollection < InputOperation > _resourceOperations ;
35- private ClientProvider _clientProvider ;
3635 private readonly IReadOnlyList < string > _contextualParameters ;
3736 private bool _isSingleton ;
3837
3938 private FieldProvider _dataField ;
40- private FieldProvider _clientDiagonosticsField ;
41- private FieldProvider _restClientField ;
4239 private FieldProvider _resourcetypeField ;
40+ protected ClientProvider _clientProvider ;
41+ protected FieldProvider _clientDiagonosticsField ;
42+ protected FieldProvider _restClientField ;
4343
4444 public ResourceClientProvider ( InputClient inputClient )
4545 {
4646 var resourceMetadata = inputClient . Decorators . Single ( d => d . Name . Equals ( KnownDecorators . ResourceMetadata ) ) ;
4747 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 ;
4949 var resourceType = resourceMetadata . Arguments ? [ KnownDecorators . ResourceType ] . ToObjectFromJson < string > ( ) ! ;
5050 _resourcetypeField = new FieldProvider ( FieldModifiers . Public | FieldModifiers . Static | FieldModifiers . ReadOnly , typeof ( ResourceType ) , "ResourceType" , this , description : $ "Gets the resource type for the operations.", initializationValue : Literal ( resourceType ) ) ;
5151 var resourceModel = ManagementClientGenerator . Instance . InputLibrary . GetModelByCrossLanguageDefinitionId ( codeModelId ) ! ;
@@ -144,7 +144,7 @@ private ConstructorProvider BuildPrimaryConstructor()
144144 return new ConstructorProvider ( signature , bodyStatements , this ) ;
145145 }
146146
147- private ConstructorProvider BuildInitializationConstructor ( )
147+ protected ConstructorProvider BuildInitializationConstructor ( )
148148 {
149149 var idParameter = new ParameterProvider ( "id" , $ "The identifier of the resource that is the target of operations.", typeof ( ResourceIdentifier ) ) ;
150150 var parameters = new List < ParameterProvider >
@@ -164,7 +164,7 @@ private ConstructorProvider BuildInitializationConstructor()
164164
165165 var bodyStatements = new MethodBodyStatement [ ]
166166 {
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 ( ) ,
168168 TryGetApiVersion ( out var apiVersion ) . Terminate ( ) ,
169169 _restClientField . Assign ( New . Instance ( _clientProvider . Type , This . Property ( "Pipeline" ) , This . Property ( "Endpoint" ) , apiVersion ) ) . Terminate ( ) ,
170170 Static ( Type ) . Invoke ( ValidateResourceIdMethodName , idParameter ) . Terminate ( )
@@ -174,7 +174,7 @@ private ConstructorProvider BuildInitializationConstructor()
174174 }
175175
176176 private const string ValidateResourceIdMethodName = "ValidateResourceId" ;
177- private MethodProvider BuildValidateResourceIdMethod ( )
177+ protected MethodProvider BuildValidateResourceIdMethod ( )
178178 {
179179 var idParameter = new ParameterProvider ( "id" , $ "" , typeof ( ResourceIdentifier ) ) ;
180180 var signature = new MethodSignature (
@@ -187,13 +187,17 @@ private MethodProvider BuildValidateResourceIdMethod()
187187 idParameter
188188 ] ,
189189 [ new AttributeStatement ( typeof ( ConditionalAttribute ) , Literal ( "DEBUG" ) ) ] ) ;
190- var bodyStatements = new IfStatement ( idParameter . NotEqual ( _resourcetypeField ) )
190+ var bodyStatements = new IfStatement ( idParameter . NotEqual ( ExpectedResourceTypeForValidation ) )
191191 {
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 ) )
193193 } ;
194194 return new MethodProvider ( signature , bodyStatements , this ) ;
195195 }
196196
197+ protected virtual ValueExpression ResourceTypeExpression => _resourcetypeField ;
198+
199+ protected virtual ValueExpression ExpectedResourceTypeForValidation => _resourcetypeField ;
200+
197201 protected override CSharpType [ ] BuildImplements ( ) => [ typeof ( ArmResource ) ] ;
198202
199203 protected override MethodProvider [ ] BuildMethods ( )
@@ -246,7 +250,7 @@ private MethodProvider BuildOperationMethod(InputOperation operation, MethodProv
246250 return new MethodProvider ( signature , bodyStatements , this ) ;
247251 }
248252
249- private IReadOnlyList < ParameterProvider > GetOperationMethodParameters ( MethodProvider convenienceMethod , bool isLongRunning )
253+ protected IReadOnlyList < ParameterProvider > GetOperationMethodParameters ( MethodProvider convenienceMethod , bool isLongRunning )
250254 {
251255 var result = new List < ParameterProvider > ( ) ;
252256 if ( isLongRunning )
@@ -263,7 +267,7 @@ private IReadOnlyList<ParameterProvider> GetOperationMethodParameters(MethodProv
263267 return result ;
264268 }
265269
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 )
267271 {
268272 isGeneric = false ;
269273 if ( isLongRunningOperation )
@@ -372,7 +376,7 @@ private ValueExpression[] PopulateArguments(IReadOnlyList<ParameterProvider> par
372376 }
373377
374378 // TODO: get clean name of operation Name
375- private MethodProvider GetCorrespondingConvenienceMethod ( InputOperation operation , bool isAsync )
379+ protected MethodProvider GetCorrespondingConvenienceMethod ( InputOperation operation , bool isAsync )
376380 => _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 ) ) ) ) ;
377381
378382 private MethodProvider GetCorrespondingRequestMethod ( InputOperation operation )
@@ -382,7 +386,7 @@ public ScopedApi<bool> TryGetApiVersion(out ScopedApi<string> apiVersion)
382386 {
383387 var apiVersionDeclaration = new VariableExpression ( typeof ( string ) , $ "{ SpecName . ToLower ( ) } ApiVersion") ;
384388 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 ) ] ) ;
386390 return invocation . As < bool > ( ) ;
387391 }
388392 }
0 commit comments