Skip to content

Commit

Permalink
fix resolution of boolean properties
Browse files Browse the repository at this point in the history
  • Loading branch information
riina committed Jan 21, 2024
1 parent f26a12a commit 14afb39
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Art.Common/ArtifactToolOptionExtensions.cs
Expand Up @@ -10,7 +10,7 @@ namespace Art.Common;
/// </summary>
public static class ArtifactToolOptionExtensions
{
private static readonly HashSet<string> s_yesLower = new() { "y", "yes", "" };
private static readonly HashSet<string> s_yesLower = new() { "y", "yes", "1", "true" };

#region Base

Expand Down
38 changes: 32 additions & 6 deletions src/Art.Tesler/Common.cs
Expand Up @@ -96,15 +96,41 @@ internal static void AddProps(this Dictionary<string, JsonElement> dictionary, I
{
foreach (string prop in props)
{
if (s_propRe.Match(prop) is not { Success: true } match) throw new ArgumentException($@"Invalid property entry ""{prop}""");
if (s_propRe.Match(prop) is not { Success: true } match)
{
throw new ArgumentException($@"Invalid property entry ""{prop}""");
}
string k = match.Groups[1].Value;
string val = match.Groups[2].Value;
JsonElement v;
if (val.StartsWith('{') || val.StartsWith('[')) v = JsonSerializer.Deserialize(val, SourceGenerationContext.s_context.JsonElement);
else if (long.TryParse(val, out long valLong)) v = JsonSerializer.SerializeToElement(valLong, SourceGenerationContext.s_context.Int64);
else if (ulong.TryParse(val, out ulong valULong)) v = JsonSerializer.SerializeToElement(valULong, SourceGenerationContext.s_context.UInt64);
else if (double.TryParse(val, out double valDouble)) v = JsonSerializer.SerializeToElement(valDouble, SourceGenerationContext.s_context.Double);
else v = JsonSerializer.SerializeToElement(val, SourceGenerationContext.s_context.String);
if (val.StartsWith('{') || val.StartsWith('['))
{
v = JsonSerializer.Deserialize(val, SourceGenerationContext.s_context.JsonElement);
}
else if (long.TryParse(val, out long valLong))
{
v = JsonSerializer.SerializeToElement(valLong, SourceGenerationContext.s_context.Int64);
}
else if (ulong.TryParse(val, out ulong valULong))
{
v = JsonSerializer.SerializeToElement(valULong, SourceGenerationContext.s_context.UInt64);
}
else if (double.TryParse(val, out double valDouble))
{
v = JsonSerializer.SerializeToElement(valDouble, SourceGenerationContext.s_context.Double);
}
else if (string.Equals(val, "true", StringComparison.InvariantCulture))
{
v = JsonSerializer.SerializeToElement(true, SourceGenerationContext.s_context.Boolean);
}
else if (string.Equals(val, "false", StringComparison.InvariantCulture))
{
v = JsonSerializer.SerializeToElement(false, SourceGenerationContext.s_context.Boolean);
}
else
{
v = JsonSerializer.SerializeToElement(val, SourceGenerationContext.s_context.String);
}
dictionary.AddPropWithWarning(k, v, console);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Art.Tesler/SourceGenerationContext.cs
Expand Up @@ -4,6 +4,7 @@
namespace Art.Tesler;

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(bool))]
[JsonSerializable(typeof(ulong))]
[JsonSerializable(typeof(long))]
[JsonSerializable(typeof(double))]
Expand Down

0 comments on commit 14afb39

Please sign in to comment.