Skip to content

Commit fdbd540

Browse files
committed
Refactor bool to avoid a closure
1 parent 3753120 commit fdbd540

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

tracer/src/Datadog.Trace/Configuration/ConfigurationSources/Telemetry/ConfigurationBuilder.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,21 +252,39 @@ public T GetAs<T>(Func<DefaultResult<T>> getDefaultValue, Func<T, bool>? validat
252252
// ****************
253253
// Bool accessors
254254
// ****************
255-
public bool? AsBool() => AsBool(getDefaultValue: null, validator: null);
255+
public bool? AsBool() => AsBool(defaultValue: null, validator: null, converter: null);
256256

257257
public bool AsBool(bool defaultValue) => AsBool(() => defaultValue, validator: null).Value;
258258

259-
public bool? AsBool(Func<bool, bool> validator) => AsBool(null, validator);
259+
public bool? AsBool(Func<bool, bool> validator) => AsBool(defaultValue: null, validator, converter: null);
260260

261261
public bool AsBool(bool defaultValue, Func<bool, bool>? validator)
262-
=> AsBool(() => defaultValue, validator).Value;
262+
=> AsBool(defaultValue, validator, converter: null).Value;
263263

264264
[return: NotNullIfNotNull(nameof(getDefaultValue))] // This doesn't work with nullables, but it still expresses intent
265-
public bool? AsBool(Func<DefaultResult<bool>>? getDefaultValue, Func<bool, bool>? validator)
265+
public bool? AsBool(Func<bool>? getDefaultValue, Func<bool, bool>? validator)
266266
=> AsBool(getDefaultValue, validator, converter: null);
267267

268+
[return: NotNullIfNotNull(nameof(defaultValue))]
269+
public bool? AsBool(bool? defaultValue, Func<bool, bool>? validator, Func<string, ParsingResult<bool>>? converter)
270+
{
271+
var result = GetBoolResult(validator, converter: null);
272+
if (result is { Result: { } ddResult, IsValid: true })
273+
{
274+
return ddResult;
275+
}
276+
277+
if (defaultValue is not { } value)
278+
{
279+
return null;
280+
}
281+
282+
Telemetry.Record(Key, value, ConfigurationOrigins.Default);
283+
return value;
284+
}
285+
268286
[return: NotNullIfNotNull(nameof(getDefaultValue))] // This doesn't work with nullables, but it still expresses intent
269-
public bool? AsBool(Func<DefaultResult<bool>>? getDefaultValue, Func<bool, bool>? validator, Func<string, ParsingResult<bool>>? converter)
287+
public bool? AsBool(Func<bool>? getDefaultValue, Func<bool, bool>? validator, Func<string, ParsingResult<bool>>? converter)
270288
{
271289
var result = GetBoolResult(validator, converter);
272290
return TryHandleResult(Telemetry, Key, result, recordValue: true, getDefaultValue, out var value) ? value : null;

0 commit comments

Comments
 (0)