Skip to content

Commit c719d00

Browse files
authored
Add suffix support for JSON as well
1 parent 12bf375 commit c719d00

File tree

1 file changed

+52
-12
lines changed

1 file changed

+52
-12
lines changed

HashifyNETCLI/Program.cs

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -530,13 +530,33 @@ static void PrintProfiles(Type algorithm)
530530
return retval;
531531
}
532532

533-
static IReadOnlyList<(Type AlgorithmType, IHashConfigBase Config)>? ParseJSONConfig(string jsonPath)
533+
static IReadOnlyList<JsonConfig>? ParseJSONConfig(string jsonPath)
534534
{
535535
try
536536
{
537-
List<(Type AlgorithmType, IHashConfigBase Config)> retval = new List<(Type AlgorithmType, IHashConfigBase Config)>();
537+
List<JsonConfigBuilder> retval = new List<JsonConfigBuilder>();
538538

539-
JsonDocument doc = JsonDocument.Parse(File.ReadAllText(jsonPath));
539+
JsonConfigBuilder GetOrAdd(Type type)
540+
{
541+
if (retval.Exists(t => t.Type == type))
542+
{
543+
return retval.Find(t => t.Type == type)!;
544+
}
545+
else
546+
{
547+
var builder = new JsonConfigBuilder(type);
548+
retval.Add(builder);
549+
return builder;
550+
}
551+
}
552+
553+
JsonDocumentOptions options = new JsonDocumentOptions()
554+
{
555+
AllowTrailingCommas = true,
556+
CommentHandling = JsonCommentHandling.Skip,
557+
};
558+
559+
JsonDocument doc = JsonDocument.Parse(File.ReadAllText(jsonPath), options);
540560
JsonElement root = doc.RootElement;
541561
var objectEnumerator = root.EnumerateObject();
542562
foreach (var property in objectEnumerator)
@@ -604,8 +624,12 @@ static void PrintProfiles(Type algorithm)
604624
continue;
605625
}
606626

607-
retval.Add((hashFunctionType.Function, configBase));
608-
}
627+
if (!GetOrAdd(hashFunctionType.Function).TryAddProfile(hashFunctionType.Name, configBase))
628+
{
629+
Logger.Warning($"Config profile '{profile.Name}' for '{n}' already exists, skipping.");
630+
continue;
631+
}
632+
}
609633
else if (bodyFirstProp.Value.Name.Equals("config", StringComparison.OrdinalIgnoreCase))
610634
{
611635
if (bodyFirstProp.Value.Value.ValueKind != JsonValueKind.Object)
@@ -668,11 +692,15 @@ static void PrintProfiles(Type algorithm)
668692
continue;
669693
}
670694

671-
retval.Add((hashFunctionType.Function, config));
695+
if (!GetOrAdd(hashFunctionType.Function).TryAddProfile(hashFunctionType.Name, config))
696+
{
697+
Logger.Warning($"Config for '{n}' already exists, skipping.");
698+
continue;
699+
}
672700
}
673701
}
674702

675-
return retval;
703+
return retval.ConvertAll(t => t.Build());
676704
}
677705
catch (JsonException ex)
678706
{
@@ -904,7 +932,7 @@ public static int Main(string[] args)
904932
}
905933

906934
string configJson = cl.GetValueString("-cf", null!);
907-
IReadOnlyList<(Type AlgorithmType, IHashConfigBase Config)>? _jsonConfigs = null;
935+
IReadOnlyList<JsonConfig>? _jsonConfigs = null;
908936
if (configJson != null)
909937
{
910938
if (!IsValidString(configJson) || !File.Exists(configJson))
@@ -1028,15 +1056,27 @@ public static int Main(string[] args)
10281056
}
10291057
else
10301058
{
1031-
(Type AlgorithmType, IHashConfigBase Config)? jsonConfig = null;
1059+
JsonConfigProfile? jsonConfigProfile = null;
10321060
if (_jsonConfigs != null && _jsonConfigs.Count > 0)
10331061
{
1034-
jsonConfig = _jsonConfigs.Where(t => t.AlgorithmType == fvar.Function).FirstOrDefault();
1062+
jsonConfigProfile = _jsonConfigs.Where(t => t.Type == fvar.Function).FirstOrDefault().Profiles?.Where(t => t.AsVar() == fvar).FirstOrDefault();
1063+
1064+
// Try again without the var name, in case there is a globalized config for this function type.
1065+
if (jsonConfigProfile.HasValue && !jsonConfigProfile.Value.IsValid)
1066+
{
1067+
FunctionVar fvar2 = new FunctionVar(null, fvar.Function);
1068+
jsonConfigProfile = _jsonConfigs.Where(t => t.Type == fvar.Function).FirstOrDefault().Profiles?.Where(t => t.AsVar() == fvar2).FirstOrDefault();
1069+
}
1070+
1071+
if (jsonConfigProfile.HasValue && jsonConfigProfile.Value.Config == null && jsonConfigProfile.Value.Owner == null && jsonConfigProfile.Value.Name == null)
1072+
{
1073+
jsonConfigProfile = null;
1074+
}
10351075
}
10361076

1037-
if (jsonConfig.HasValue)
1077+
if (jsonConfigProfile.HasValue)
10381078
{
1039-
function = HashFactory.Create(fvar.Function, jsonConfig.Value.Config);
1079+
function = HashFactory.Create(fvar.Function, jsonConfigProfile.Value.Config);
10401080
}
10411081
else
10421082
{

0 commit comments

Comments
 (0)