Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 64-bit numerical data types #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ConfigGenerator.Tests/TestFiles/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
"DuplicateEntryOnlyShowsOnce": false,
"IntValue": 1,
"DoubleValue": 2.2,
"LongValue": 9223372036854775807,
"ULongValue": 18446744073709551615,
"ArrayWithDoubles": [1.1, 2.245, 4.67],
"ArrayWithStrings": [ "Key1", "Value 1" ],
"ArrayWithInts": [ 1, 2 ],
"ArrayWithBools": [ true, false, true ]
"ArrayWithBools": [ true, false, true ],
"ArrayWithLongs": [ 9223372036854775807, 1234567890123456789 ],
"ArrayWithULongs": [ 18446744073709551615, 1234567890123456789 ]
}
7 changes: 5 additions & 2 deletions ConfigGenerator.Tests/TestFiles/expected.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

using System;
using System;
using System.Collections.Generic;

namespace ApplicationConfig
Expand All @@ -11,10 +10,14 @@ public class MyAppConfig
public bool DuplicateEntryOnlyShowsOnce { get; set; }
public int IntValue { get; set; }
public double DoubleValue { get; set; }
public long LongValue { get; set; }
public ulong ULongValue { get; set; }
public IEnumerable<double> ArrayWithDoubles { get; set; }
public IEnumerable<string> ArrayWithStrings { get; set; }
public IEnumerable<int> ArrayWithInts { get; set; }
public IEnumerable<bool> ArrayWithBools { get; set; }
public IEnumerable<long> ArrayWithLongs { get; set; }
public IEnumerable<ulong> ArrayWithULongs { get; set; }
public bool ShowDeveloperWarnings { get; set; }
public Logging Logging { get; set; }
}
Expand Down
13 changes: 11 additions & 2 deletions src/GenerateConfigClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,20 @@ private static string GetPropertyTypeNameBasedOnValue(JsonElement value)
{
return value.ValueKind switch
{
JsonValueKind.Number => value.TryGetInt32(out _) ? "int" : "double",
JsonValueKind.Number => DetermineNumericType(value),
JsonValueKind.True or JsonValueKind.False => "bool",
_ => "string",
};
}

private static string DetermineNumericType(JsonElement value)
{
if (value.TryGetInt32(out _)) return "int";
if (value.TryGetInt64(out _)) return "long";
if (value.TryGetUInt64(out _)) return "ulong";

return "double";
}

private static string NormalizePropertyName(string originalName)
{
Expand Down Expand Up @@ -218,4 +227,4 @@ private static void MergeDictionaries(Dictionary<string, object> dictionary1, Di
}
}
}
}
}