@@ -41,13 +41,12 @@ private record OAuth2Fields(FieldProvider AuthField, FieldProvider Authorization
41
41
42
42
private FieldProvider ? _apiVersionField ;
43
43
private readonly Lazy < IReadOnlyList < ParameterProvider > > _subClientInternalConstructorParams ;
44
- private IReadOnlyList < Lazy < ClientProvider > > ? _subClients ;
44
+ private readonly Lazy < IReadOnlyList < ClientProvider > > _subClients ;
45
45
private RestClientProvider ? _restClient ;
46
- private readonly InputParameter [ ] _allClientParameters ;
47
- private Lazy < List < FieldProvider > > _additionalClientFields ;
46
+ private readonly IReadOnlyList < InputParameter > _allClientParameters ;
47
+ private Lazy < IReadOnlyList < FieldProvider > > _additionalClientFields ;
48
48
49
49
private Lazy < ParameterProvider ? > ClientOptionsParameter { get ; }
50
- private IReadOnlyList < Lazy < ClientProvider > > SubClients => _subClients ??= GetSubClients ( ) ;
51
50
52
51
// for mocking
53
52
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
@@ -141,6 +140,7 @@ public ClientProvider(InputClient inputClient)
141
140
_allClientParameters = _inputClient . Parameters . Concat ( _inputClient . Operations . SelectMany ( op => op . Parameters ) . Where ( p => p . Kind == InputOperationParameterKind . Client ) ) . DistinctBy ( p => p . Name ) . ToArray ( ) ;
142
141
_subClientInternalConstructorParams = new ( GetSubClientInternalConstructorParameters ) ;
143
142
_clientParameters = new ( GetClientParameters ) ;
143
+ _subClients = new ( GetSubClients ) ;
144
144
}
145
145
146
146
private IReadOnlyList < ParameterProvider > GetSubClientInternalConstructorParameters ( )
@@ -225,18 +225,18 @@ protected override FieldProvider[] BuildFields()
225
225
fields . AddRange ( _additionalClientFields . Value ) ;
226
226
227
227
// add sub-client caching fields
228
- foreach ( var subClient in SubClients )
228
+ foreach ( var subClient in _subClients . Value )
229
229
{
230
- if ( subClient . Value . Methods . Count != 0 && subClient . Value . _clientCachingField != null )
230
+ if ( subClient . Methods . Count != 0 && subClient . _clientCachingField != null )
231
231
{
232
- fields . Add ( subClient . Value . _clientCachingField ) ;
232
+ fields . Add ( subClient . _clientCachingField ) ;
233
233
}
234
234
}
235
235
236
236
return [ .. fields ] ;
237
237
}
238
238
239
- private List < FieldProvider > BuildAdditionalClientFields ( )
239
+ private IReadOnlyList < FieldProvider > BuildAdditionalClientFields ( )
240
240
{
241
241
var fields = new List < FieldProvider > ( ) ;
242
242
// Add optional client parameters as fields
@@ -452,7 +452,8 @@ [.. primaryCtorOrderedParams.Select(p => p.InitializationValue ?? p)
452
452
453
453
protected override MethodProvider [ ] BuildMethods ( )
454
454
{
455
- var subClientCount = SubClients . Count ;
455
+ var subClients = _subClients . Value ;
456
+ var subClientCount = subClients . Count ;
456
457
List < MethodProvider > methods = new List < MethodProvider > ( ( _inputClient . Operations . Count * 4 ) + subClientCount ) ;
457
458
458
459
// Build methods for all the operations
@@ -474,19 +475,18 @@ protected override MethodProvider[] BuildMethods()
474
475
var parentClientFields = Fields . ToDictionary ( f => f . Name . ToVariableName ( ) ) ;
475
476
476
477
// Build factory accessor methods for the sub-clients
477
- foreach ( var subClient in SubClients )
478
+ foreach ( var subClient in subClients )
478
479
{
479
- var subClientInstance = subClient . Value ;
480
- if ( subClientInstance . _clientCachingField is null || subClientInstance . Methods . Count == 0 )
480
+ if ( subClient . _clientCachingField is null || subClient . Methods . Count == 0 )
481
481
{
482
482
continue ;
483
483
}
484
484
485
- var cachedClientFieldVar = new VariableExpression ( subClientInstance . Type , subClientInstance . _clientCachingField . Declaration , IsRef : true ) ;
485
+ var cachedClientFieldVar = new VariableExpression ( subClient . Type , subClient . _clientCachingField . Declaration , IsRef : true ) ;
486
486
List < ValueExpression > subClientConstructorArgs = new ( 3 ) ;
487
487
488
488
// Populate constructor arguments
489
- foreach ( var param in subClientInstance . _subClientInternalConstructorParams . Value )
489
+ foreach ( var param in subClient . _subClientInternalConstructorParams . Value )
490
490
{
491
491
if ( parentClientProperties . TryGetValue ( param . Name , out var parentProperty ) )
492
492
{
@@ -501,23 +501,23 @@ protected override MethodProvider[] BuildMethods()
501
501
// Create the interlocked compare exchange expression for the body
502
502
var interlockedCompareExchange = Static ( typeof ( Interlocked ) ) . Invoke (
503
503
nameof ( Interlocked . CompareExchange ) ,
504
- [ cachedClientFieldVar , New . Instance ( subClientInstance . Type , subClientConstructorArgs ) , Null ] ) ;
505
- var factoryMethodName = subClient . Value . Name . EndsWith ( ClientSuffix , StringComparison . OrdinalIgnoreCase )
506
- ? $ "Get{ subClient . Value . Name } "
507
- : $ "Get{ subClient . Value . Name } { ClientSuffix } ";
504
+ [ cachedClientFieldVar , New . Instance ( subClient . Type , subClientConstructorArgs ) , Null ] ) ;
505
+ var factoryMethodName = subClient . Name . EndsWith ( ClientSuffix , StringComparison . OrdinalIgnoreCase )
506
+ ? $ "Get{ subClient . Name } "
507
+ : $ "Get{ subClient . Name } { ClientSuffix } ";
508
508
509
509
var factoryMethod = new MethodProvider (
510
510
new (
511
511
factoryMethodName ,
512
- $ "Initializes a new instance of { subClientInstance . Type . Name } ",
512
+ $ "Initializes a new instance of { subClient . Type . Name } ",
513
513
MethodSignatureModifiers . Public | MethodSignatureModifiers . Virtual ,
514
- subClientInstance . Type ,
514
+ subClient . Type ,
515
515
null ,
516
516
[ ] ) ,
517
517
// return Volatile.Read(ref _cachedClient) ?? Interlocked.CompareExchange(ref _cachedClient, new Client(_pipeline, _keyCredential, _endpoint), null) ?? _cachedClient;
518
518
Return (
519
519
Static ( typeof ( Volatile ) ) . Invoke ( nameof ( Volatile . Read ) , cachedClientFieldVar )
520
- . NullCoalesce ( interlockedCompareExchange . NullCoalesce ( subClientInstance . _clientCachingField ) ) ) ,
520
+ . NullCoalesce ( interlockedCompareExchange . NullCoalesce ( subClient . _clientCachingField ) ) ) ,
521
521
this ) ;
522
522
methods . Add ( factoryMethod ) ;
523
523
}
@@ -545,18 +545,17 @@ private ParameterProvider BuildClientEndpointParameter()
545
545
} ;
546
546
}
547
547
548
- // TODO: Update method to be more efficient
549
- private IReadOnlyList < Lazy < ClientProvider > > GetSubClients ( )
548
+ private IReadOnlyList < ClientProvider > GetSubClients ( )
550
549
{
551
550
var inputClients = ClientModelPlugin . Instance . InputLibrary . InputNamespace . Clients ;
552
- var subClients = new List < Lazy < ClientProvider > > ( inputClients . Count ) ;
551
+ var subClients = new List < ClientProvider > ( inputClients . Count ) ;
553
552
554
553
foreach ( var client in inputClients )
555
554
{
556
555
// add direct child clients
557
556
if ( client . Parent != null && client . Parent == _inputClient . Key )
558
557
{
559
- subClients . Add ( new ( ( ) => ClientModelPlugin . Instance . TypeFactory . CreateClient ( client ) ) ) ;
558
+ subClients . Add ( ClientModelPlugin . Instance . TypeFactory . CreateClient ( client ) ) ;
560
559
}
561
560
}
562
561
0 commit comments