@@ -493,23 +493,9 @@ private ConfigurationResult<T> GetAs<T>(Func<T, bool>? validator, Func<string, P
493493 /// <returns>The raw <see cref="ConfigurationResult{T}"/></returns>
494494 private ConfigurationResult < T > GetResult < T > ( Func < IConfigurationSource , string , IConfigurationTelemetry , Func < T , bool > ? , bool , ConfigurationResult < T > > selector , Func < T , bool > ? validator , bool recordValue )
495495 {
496- var result = selector ( Source , Key , Telemetry , validator , recordValue ) ;
497- if ( result . ShouldFallBack && FallbackKey1 is not null )
498- {
499- result = selector ( Source , FallbackKey1 , Telemetry , validator , recordValue ) ;
500- }
501-
502- if ( result . ShouldFallBack && FallbackKey2 is not null )
503- {
504- result = selector ( Source , FallbackKey2 , Telemetry , validator , recordValue ) ;
505- }
506-
507- if ( result . ShouldFallBack && FallbackKey3 is not null )
508- {
509- result = selector ( Source , FallbackKey3 , Telemetry , validator , recordValue ) ;
510- }
511-
512- return result ;
496+ var source = Source ;
497+ var telemetry = Telemetry ;
498+ return GetResultWithFallback ( key => selector ( source , key , telemetry , validator , recordValue ) ) ;
513499 }
514500
515501 /// <summary>
@@ -523,62 +509,50 @@ private ConfigurationResult<T> GetResult<T>(Func<IConfigurationSource, string, I
523509 /// <returns>The raw <see cref="ConfigurationResult{T}"/></returns>
524510 private ConfigurationResult < T > GetResult < T > ( Func < IConfigurationSource , string , IConfigurationTelemetry , Func < T , bool > ? , Func < string , ParsingResult < T > > , bool , ConfigurationResult < T > > selector , Func < T , bool > ? validator , Func < string , ParsingResult < T > > converter , bool recordValue )
525511 {
526- var result = selector ( Source , Key , Telemetry , validator , converter , recordValue ) ;
527- if ( result . ShouldFallBack && FallbackKey1 is not null )
528- {
529- result = selector ( Source , FallbackKey1 , Telemetry , validator , converter , recordValue ) ;
530- }
531-
532- if ( result . ShouldFallBack && FallbackKey2 is not null )
533- {
534- result = selector ( Source , FallbackKey2 , Telemetry , validator , converter , recordValue ) ;
535- }
536-
537- if ( result . ShouldFallBack && FallbackKey3 is not null )
538- {
539- result = selector ( Source , FallbackKey3 , Telemetry , validator , converter , recordValue ) ;
540- }
541-
542- return result ;
512+ var source = Source ;
513+ var telemetry = Telemetry ;
514+ return GetResultWithFallback ( key => selector ( source , key , telemetry , validator , converter , recordValue ) ) ;
543515 }
544516
545517 private ConfigurationResult < IDictionary < string , string > > GetDictionaryResult ( bool allowOptionalMappings , char separator )
546518 {
547- var result = Source . GetDictionary ( Key , Telemetry , validator : null , allowOptionalMappings , separator ) ;
548- if ( result . ShouldFallBack && FallbackKey1 is not null )
549- {
550- result = Source . GetDictionary ( FallbackKey1 , Telemetry , validator : null , allowOptionalMappings , separator ) ;
551- }
552-
553- if ( result . ShouldFallBack && FallbackKey2 is not null )
554- {
555- result = Source . GetDictionary ( FallbackKey2 , Telemetry , validator : null , allowOptionalMappings , separator ) ;
556- }
557-
558- if ( result . ShouldFallBack && FallbackKey3 is not null )
559- {
560- result = Source . GetDictionary ( FallbackKey3 , Telemetry , validator : null , allowOptionalMappings , separator ) ;
561- }
562-
563- return result ;
519+ var source = Source ;
520+ var telemetry = Telemetry ;
521+ return GetResultWithFallback ( key => source . GetDictionary ( key , telemetry , validator : null , allowOptionalMappings , separator ) ) ;
564522 }
565523
566524 private ConfigurationResult < IDictionary < string , string > > GetDictionaryResult ( Func < string , IDictionary < string , string > > parser )
567525 {
568- var result = Source . GetDictionary ( Key , Telemetry , validator : null , parser ) ;
569- if ( result . ShouldFallBack && FallbackKey1 is not null )
570- {
571- result = Source . GetDictionary ( FallbackKey1 , Telemetry , validator : null , parser ) ;
572- }
526+ var source = Source ;
527+ var telemetry = Telemetry ;
528+ return GetResultWithFallback ( key => source . GetDictionary ( key , telemetry , validator : null , parser ) ) ;
529+ }
573530
574- if ( result . ShouldFallBack && FallbackKey2 is not null )
531+ /// <summary>
532+ /// Common method that handles key resolution and alias fallback logic
533+ /// </summary>
534+ /// <param name="selector">The method to call for each key</param>
535+ /// <typeparam name="T">The type being retrieved</typeparam>
536+ /// <returns>The raw <see cref="ConfigurationResult{T}"/></returns>
537+ private ConfigurationResult < T > GetResultWithFallback < T > ( Func < string , ConfigurationResult < T > > selector )
538+ {
539+ var hasAllKeys = _allKeys is not null ;
540+ var canonicalKey = hasAllKeys ? _allKeys ! [ 0 ] : Key ;
541+ var result = selector ( canonicalKey ) ;
542+ if ( ! result . ShouldFallBack )
575543 {
576- result = Source . GetDictionary ( FallbackKey2 , Telemetry , validator : null , parser ) ;
544+ return result ;
577545 }
578546
579- if ( result . ShouldFallBack && FallbackKey3 is not null )
547+ string [ ] aliases = ! hasAllKeys ? ConfigKeyAliasesSwitcher . GetAliases ( Key ) : [ _allKeys ! [ 1 ] , _allKeys [ 2 ] ] ;
548+
549+ foreach ( var alias in aliases )
580550 {
581- result = Source . GetDictionary ( FallbackKey3 , Telemetry , validator : null , parser ) ;
551+ result = selector ( alias ) ;
552+ if ( ! result . ShouldFallBack )
553+ {
554+ break ;
555+ }
582556 }
583557
584558 return result ;
@@ -606,10 +580,9 @@ public static StructConfigurationResultWithKey<bool> Create(IConfigurationTeleme
606580 public static StructConfigurationResultWithKey < int > Create ( IConfigurationTelemetry telemetry , string key , ConfigurationResult < int > configurationResult )
607581 => new ( telemetry , key , configurationResult ) ;
608582
609- public static StructConfigurationResultWithKey < double > Create ( IConfigurationTelemetry telemetry , string key , ConfigurationResult < double > configurationResult )
610- => new ( telemetry , key , configurationResult ) ;
583+ public static StructConfigurationResultWithKey < double > Create ( IConfigurationTelemetry telemetry , string key , ConfigurationResult < double > configurationResult ) => new ( telemetry , key , configurationResult ) ;
611584
612- [ return : NotNullIfNotNull ( nameof ( defaultValue ) ) ]
585+ [ return : NotNullIfNotNull ( nameof ( defaultValue ) ) ]
613586 public T ? WithDefault ( T ? defaultValue )
614587 {
615588 if ( ConfigurationResult is { Result : { } ddResult , IsValid : true } )
@@ -623,7 +596,7 @@ public static StructConfigurationResultWithKey<double> Create(IConfigurationTele
623596
624597 public T WithDefault ( T defaultValue )
625598 {
626- if ( ConfigurationResult is { Result : { } ddResult , IsValid : true } )
599+ if ( ConfigurationResult is { Result : var ddResult , IsValid : true } )
627600 {
628601 return ddResult ;
629602 }
0 commit comments