@@ -25,72 +25,13 @@ internal readonly struct ConfigurationBuilder(IConfigurationSource source, IConf
25
25
26
26
public HasKeys WithKeys ( string key , string fallbackKey1 , string fallbackKey2 , string fallbackKey3 ) => new ( _source , _telemetry , key , fallbackKey1 , fallbackKey2 , fallbackKey3 ) ;
27
27
28
- private static bool TryHandleResult < T > (
29
- IConfigurationTelemetry telemetry ,
30
- string key ,
31
- ConfigurationResult < T > result ,
32
- bool recordValue ,
33
- Func < T > ? getDefaultValue ,
34
- [ NotNullIfNotNull ( nameof ( getDefaultValue ) ) ] out T ? value )
35
- where T : notnull
36
- {
37
- if ( result is { Result : { } ddResult , IsValid : true } )
38
- {
39
- value = ddResult ;
40
- return true ;
41
- }
42
-
43
- // don't have a default value, so caller (which knows what the <T> is needs
44
- // to return the default value. Necessary because we can't create a generic
45
- // method that works "correctly" for both value types and reference types.
46
- if ( getDefaultValue is null )
47
- {
48
- value = default ;
49
- return false ;
50
- }
51
-
52
- var defaultValue = getDefaultValue ( ) ;
53
- RecordTelemetry ( telemetry , key , recordValue , defaultValue ) ;
54
-
55
- value = defaultValue ;
56
- return true ;
57
- }
58
-
59
- private static bool TryHandleResult < T > (
60
- IConfigurationTelemetry telemetry ,
61
- string key ,
62
- ConfigurationResult < T > result ,
63
- bool recordValue ,
64
- Func < DefaultResult < T > > ? getDefaultValue ,
65
- [ NotNullIfNotNull ( nameof ( getDefaultValue ) ) ] out T ? value )
66
- where T : notnull
67
- {
68
- if ( result is { Result : { } ddResult , IsValid : true } )
69
- {
70
- value = ddResult ;
71
- return true ;
72
- }
73
-
74
- // don't have a default value, so caller (which knows what the <T> is needs
75
- // to return the default value. Necessary because we can't create a generic
76
- // method that works "correctly" for both value types and reference types.
77
- if ( getDefaultValue is null )
78
- {
79
- value = default ;
80
- return false ;
81
- }
82
-
83
- var defaultValue = getDefaultValue ( ) ;
84
- RecordTelemetry ( telemetry , key , recordValue , defaultValue ) ;
85
-
86
- value = defaultValue . Result ;
87
- return true ;
88
- }
89
-
90
28
private static void RecordTelemetry < T > ( IConfigurationTelemetry telemetry , string key , bool recordValue , T defaultValue )
91
29
{
92
30
switch ( defaultValue )
93
31
{
32
+ case DefaultResult < T > defaultResult :
33
+ telemetry . Record ( key , defaultResult . TelemetryValue , recordValue : true , ConfigurationOrigins . Default ) ;
34
+ break ;
94
35
case int intVal :
95
36
telemetry . Record ( key , intVal , ConfigurationOrigins . Default ) ;
96
37
break ;
@@ -112,25 +53,6 @@ private static void RecordTelemetry<T>(IConfigurationTelemetry telemetry, string
112
53
}
113
54
}
114
55
115
- private static void RecordTelemetry < T > ( IConfigurationTelemetry telemetry , string key , bool recordValue , DefaultResult < T > defaultValue )
116
- {
117
- switch ( defaultValue . Result )
118
- {
119
- case int intVal :
120
- telemetry . Record ( key , intVal , ConfigurationOrigins . Default ) ;
121
- break ;
122
- case double doubleVal :
123
- telemetry . Record ( key , doubleVal , ConfigurationOrigins . Default ) ;
124
- break ;
125
- case bool boolVal :
126
- telemetry . Record ( key , boolVal , ConfigurationOrigins . Default ) ;
127
- break ;
128
- default :
129
- telemetry . Record ( key , defaultValue . TelemetryValue , recordValue , ConfigurationOrigins . Default ) ;
130
- break ;
131
- }
132
- }
133
-
134
56
internal readonly struct HasKeys
135
57
{
136
58
public HasKeys ( IConfigurationSource source , IConfigurationTelemetry telemetry , string key , string ? fallbackKey1 = null , string ? fallbackKey2 = null , string ? fallbackKey3 = null )
@@ -217,7 +139,19 @@ public string AsString(string defaultValue, Func<string, bool>? validator)
217
139
private string ? AsString ( Func < string > ? getDefaultValue , Func < string , bool > ? validator , Func < string , ParsingResult < string > > ? converter , bool recordValue )
218
140
{
219
141
var result = GetStringResult ( validator , converter , recordValue ) ;
220
- return TryHandleResult ( Telemetry , Key , result , recordValue , getDefaultValue , out var value ) ? value : null ;
142
+ if ( result is { Result : { } ddResult , IsValid : true } )
143
+ {
144
+ return ddResult ;
145
+ }
146
+
147
+ if ( getDefaultValue is null )
148
+ {
149
+ return null ;
150
+ }
151
+
152
+ var defaultValue = getDefaultValue ( ) ;
153
+ RecordTelemetry ( Telemetry , Key , recordValue , defaultValue ) ;
154
+ return defaultValue ;
221
155
}
222
156
223
157
// ****************
@@ -241,9 +175,14 @@ public T GetAs<T>(Func<DefaultResult<T>> getDefaultValue, Func<T, bool>? validat
241
175
where T : notnull
242
176
{
243
177
var result = GetAs ( validator , converter ) ;
244
- return TryHandleResult ( Telemetry , Key , result , recordValue : true , getDefaultValue , out var value )
245
- ? value
246
- : default ! ; // TryHandleResult always returns true as getDefaultValue != null
178
+ if ( result is { Result : { } ddResult , IsValid : true } )
179
+ {
180
+ return ddResult ;
181
+ }
182
+
183
+ var defaultValue = getDefaultValue ( ) ;
184
+ RecordTelemetry ( Telemetry , Key , true , defaultValue . TelemetryValue ) ;
185
+ return defaultValue . Result ;
247
186
}
248
187
249
188
public T ? GetAsClass < T > ( Func < T , bool > ? validator , Func < string , ParsingResult < T > > converter )
@@ -298,7 +237,19 @@ public bool AsBool(bool defaultValue, Func<bool, bool>? validator)
298
237
public bool ? AsBool ( Func < bool > ? getDefaultValue , Func < bool , bool > ? validator , Func < string , ParsingResult < bool > > ? converter )
299
238
{
300
239
var result = GetBoolResult ( validator , converter ) ;
301
- return TryHandleResult ( Telemetry , Key , result , recordValue : true , getDefaultValue , out var value ) ? value : null ;
240
+ if ( result is { Result : { } ddResult , IsValid : true } )
241
+ {
242
+ return ddResult ;
243
+ }
244
+
245
+ if ( getDefaultValue is null )
246
+ {
247
+ return null ;
248
+ }
249
+
250
+ var defaultValue = getDefaultValue ( ) ;
251
+ RecordTelemetry ( Telemetry , Key , true , defaultValue ) ;
252
+ return defaultValue ;
302
253
}
303
254
304
255
// ****************
@@ -698,7 +649,7 @@ public T OverrideWith(in ClassConfigurationResultWithKey<T> otelConfig, IConfigu
698
649
}
699
650
700
651
var defaultValue = getDefaultValue ( ) ;
701
- RecordTelemetry ( Telemetry , Key , RecordValue , defaultValue ) ;
652
+ RecordTelemetry ( Telemetry , Key , RecordValue , defaultValue . TelemetryValue ) ;
702
653
return defaultValue . Result ;
703
654
}
704
655
0 commit comments