Skip to content

Commit

Permalink
Simplify if logic and nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerBarreto committed Nov 21, 2024
1 parent 5da2f35 commit 540c833
Showing 1 changed file with 19 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -726,40 +726,32 @@ private static (Func<KernelFunction, Kernel, KernelArguments, CancellationToken,

object? Process(object? value)
{
if (!type.IsAssignableFrom(value?.GetType()))
if (type.IsAssignableFrom(value?.GetType()))
{
if (parameter?.ParameterType is null)
{
return value;
}
return value;
}

if (value is not JsonElement or JsonDocument or JsonNode)
if (converter is not null && value is not JsonElement or JsonDocument or JsonNode)
{
try
{
if (converter is not null)
{
try
{
return converter(value, kernel.Culture);
}
catch (Exception e) when (!e.IsCriticalException())
{
throw new ArgumentOutOfRangeException(name, value, e.Message);
}
}
return converter(value, kernel.Culture);
}

if (value is JsonElement element && element.ValueKind == JsonValueKind.String)
catch (Exception e) when (!e.IsCriticalException())
{
if (jsonStringParsers.TryGetValue(type, out var jsonStringParser))
{
return jsonStringParser(element.GetString()!);
}
throw new ArgumentOutOfRangeException(name, value, e.Message);
}
}

if (value is not null && TryToDeserializeValue(value, type, jsonSerializerOptions, out var deserializedValue))
{
return deserializedValue;
}
if (value is JsonElement element && element.ValueKind == JsonValueKind.String
&& jsonStringParsers.TryGetValue(type, out var jsonStringParser))
{
return jsonStringParser(element.GetString()!);
}

if (value is not null && TryToDeserializeValue(value, type, jsonSerializerOptions, out var deserializedValue))
{
return deserializedValue;
}

return value;
Expand Down

0 comments on commit 540c833

Please sign in to comment.