From d62cb5f27822e55ae12badbc68a8f26bbaa59ddd Mon Sep 17 00:00:00 2001 From: giawa Date: Tue, 18 Jun 2019 14:39:39 -0700 Subject: [PATCH] Process the variants into a dictionary and then mark them as used as they are applied. Loop over all variants at the end and display an error if the variant was unused. --- src/CodeGenerator/Program.cs | 61 +++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/src/CodeGenerator/Program.cs b/src/CodeGenerator/Program.cs index f3e1441..5c87592 100644 --- a/src/CodeGenerator/Program.cs +++ b/src/CodeGenerator/Program.cs @@ -140,6 +140,17 @@ namespace CodeGenerator } } + Dictionary variants = new Dictionary(); + foreach (var jt in variantsJson.Children()) + { + JProperty jp = (JProperty)jt; + ParameterVariant[] methodVariants = jp.Values().Select(jv => + { + return new ParameterVariant(jv["name"].ToString(), jv["type"].ToString(), jv["variants"].Select(s => s.ToString()).ToArray()); + }).ToArray(); + variants.Add(jp.Name, new MethodVariant(jp.Name, methodVariants)); + } + EnumDefinition[] enums = typesJson["enums"].Select(jt => { JProperty jp = (JProperty)jt; @@ -206,7 +217,8 @@ namespace CodeGenerator List parameters = new List(); // find any variants that can be applied to the parameters of this method based on the method name - JToken variants = variantsJson?.Children().Where(variant => ((JProperty)variant).Name == cimguiname).FirstOrDefault() ?? null; + MethodVariant methodVariants = null; + variants.TryGetValue(jp.Name, out methodVariants); foreach (JToken p in val["argsT"]) { @@ -214,11 +226,10 @@ namespace CodeGenerator string pName = p["name"].ToString(); // if there are possible variants for this method then try to match them based on the parameter name and expected type - var matchingVariant = variants?.Values().Where(v => v["name"].ToString() == pName && v["type"].ToString() == pType).FirstOrDefault() ?? null; - // if there was a match to this name and type then apply those variants to this parameter - string[] pVariants = matchingVariant?["variants"].Values().Select(variant => variant.ToString()).ToArray() ?? null; + ParameterVariant matchingVariant = methodVariants?.Parameters.Where(pv => pv.Name == pName && pv.OriginalType == pType).FirstOrDefault() ?? null; + if (matchingVariant != null) matchingVariant.Used = true; - parameters.Add(new TypeReference(pName, pType, enums, pVariants)); + parameters.Add(new TypeReference(pName, pType, enums, matchingVariant?.VariantTypes)); } Dictionary defaultValues = new Dictionary(); @@ -564,6 +575,14 @@ namespace CodeGenerator writer.PopBlock(); writer.PopBlock(); } + + foreach (var method in variants) + { + foreach (var variant in method.Value.Parameters) + { + if (!variant.Used) Console.WriteLine($"Error: Variants targetting parameter {variant.Name} with type {variant.OriginalType} could not be applied to method {method.Key}."); + } + } } private static bool IsStringFieldName(string name) @@ -982,6 +1001,38 @@ namespace CodeGenerator } } + class MethodVariant + { + public string Name { get; } + + public ParameterVariant[] Parameters { get; } + + public MethodVariant(string name, ParameterVariant[] parameters) + { + Name = name; + Parameters = parameters; + } + } + + class ParameterVariant + { + public string Name { get; } + + public string OriginalType { get; } + + public string[] VariantTypes { get; } + + public bool Used { get; set; } + + public ParameterVariant(string name, string originalType, string[] variantTypes) + { + Name = name; + OriginalType = originalType; + VariantTypes = variantTypes; + Used = false; + } + } + class EnumDefinition { private readonly Dictionary _sanitizedNames;