Skip to content

Commit

Permalink
reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaspimentel committed Jun 18, 2024
1 parent 42e857e commit 966e4c5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,7 @@ bool ITelemeteredConfigurationSource.IsPresent(string key)

try
{
var valueAsString = token switch
{
null => null,
_ => token.Type switch
{
JTokenType.Null or JTokenType.None or JTokenType.Undefined => null,
JTokenType.String => token.Value<string>(),
_ => token.ToString(Formatting.None) // serialize back into json
}
};
var valueAsString = JTokenToString<T>(token);

if (valueAsString is not null)
{
Expand Down Expand Up @@ -381,6 +372,20 @@ bool ITelemeteredConfigurationSource.IsPresent(string key)
return null;
}

internal static string? JTokenToString(JToken? token)
{
return token switch
{
null => null,
_ => token.Type switch
{
JTokenType.Null or JTokenType.None or JTokenType.Undefined => null, // handle null-like values
JTokenType.String => token.Value<string>(), // return the underlying string value
_ => token.ToString(Formatting.None) // serialize back into json
}
};
}

/// <inheritdoc />
ConfigurationResult<IDictionary<string, string>>? ITelemeteredConfigurationSource.GetDictionary(string key, IConfigurationTelemetry telemetry, Func<IDictionary<string, string>, bool>? validator)
=> GetDictionary(key, telemetry, validator, allowOptionalMappings: false, separator: ':');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,25 +209,11 @@ static string FlattenJsonArray(JToken json)
return string.Empty;
}

static string JTokenToString(JToken token)
{
return token switch
{
null => null,
_ => token.Type switch
{
JTokenType.Null or JTokenType.None or JTokenType.Undefined => null,
JTokenType.String => token.Value<string>(),
_ => token.ToString(Formatting.None) // serialize back into json
}
};
}

// json["runtime_metrics_enabled"]?.Value<bool>().Should().Be(expectedConfig.RuntimeMetricsEnabled);
// json["debug"]?.Value<bool>().Should().Be(expectedConfig.DebugLogsEnabled);
json["log_injection_enabled"]?.Value<bool>().Should().Be(expectedConfig.LogInjectionEnabled);
json["sample_rate"]?.Value<double?>().Should().Be(expectedConfig.TraceSampleRate);
JTokenToString(json["remote_sampling_rules"]).Should().Be(expectedConfig.TraceSamplingRules);
JsonConfigurationSource.JTokenToString(json["remote_sampling_rules"]).Should().Be(expectedConfig.TraceSamplingRules);
// json["span_sampling_rules"]?.Value<string>().Should().Be(expectedConfig.SpanSamplingRules);
// json["data_streams_enabled"]?.Value<bool>().Should().Be(expectedConfig.DataStreamsEnabled);
FlattenJsonArray(json["header_tags"]).Should().Be(expectedConfig.TraceHeaderTags ?? string.Empty);
Expand Down

0 comments on commit 966e4c5

Please sign in to comment.