From 9f691502023c3282da8c2e2d47267d6d2ce4917d Mon Sep 17 00:00:00 2001 From: adam Date: Wed, 30 Oct 2024 16:46:57 -0400 Subject: [PATCH] we TDD now? --- .vscode/launch.json | 4 +- .vscode/tasks.json | 6 +- Deployment/ConfigurationBootstrapper.cs | 82 +++++++++++++++++++++---- deployment.tests/ConfigTests.cs | 2 + 4 files changed, 76 insertions(+), 18 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index ce279f1..3f0d5fa 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,9 +10,9 @@ "request": "launch", "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/deploy-test/bin/Debug/net8.0/deploy-test.dll", + "program": "${workspaceFolder}/deployment.tests/bin/Debug/net8.0/deployment.tests.dll", "args": [], - "cwd": "${workspaceFolder}/deploy-test", + "cwd": "${workspaceFolder}/deployment.tests", // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console "console": "internalConsole", "stopAtEntry": false diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 05a2991..7bbd3e2 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -7,7 +7,7 @@ "type": "process", "args": [ "build", - "${workspaceFolder}/deploy-test/deploy-test.csproj", + "${workspaceFolder}/deployment.tests/deployment.tests.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary;ForceNoAlign" ], @@ -19,7 +19,7 @@ "type": "process", "args": [ "publish", - "${workspaceFolder}/deploy-test/deploy-test.csproj", + "${workspaceFolder}/deployment.tests/deployment.tests.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary;ForceNoAlign" ], @@ -33,7 +33,7 @@ "watch", "run", "--project", - "${workspaceFolder}/deploy-test/deploy-test.csproj" + "${workspaceFolder}/deployment.tests/deployment.tests.csproj" ], "problemMatcher": "$msCompile" } diff --git a/Deployment/ConfigurationBootstrapper.cs b/Deployment/ConfigurationBootstrapper.cs index a7ec107..c41c710 100644 --- a/Deployment/ConfigurationBootstrapper.cs +++ b/Deployment/ConfigurationBootstrapper.cs @@ -39,43 +39,99 @@ namespace greyn.Deployment } } - //TODO: make private but get tests to cooperate - public static void populateExpando(T config, ref ExpandoObject fromFile) + //TODO: make private but get tests to cooperate... I think that's a c# limitation + internal static void populateExpando(T config, ref ExpandoObject fromFile) { if (config == null) return; + Console.WriteLine("config is not null"); + Console.WriteLine($"fromfile null: {fromFile == null}"); if (fromFile == null) { var expandoPopulated = new ExpandoObject(); var dictionaryFromExpandoPopulated = (IDictionary)expandoPopulated; - foreach (var property in config.GetType().GetProperties()) - dictionaryFromExpandoPopulated.Add(property.Name, property.GetValue(config)); + foreach (var memberInfo in config.GetType().GetMembers()) + { + switch (memberInfo.MemberType) + { + case MemberTypes.Field: + dictionaryFromExpandoPopulated.Add(memberInfo.Name, ((FieldInfo)memberInfo).GetValue(config)); + break; + case MemberTypes.Property: + dictionaryFromExpandoPopulated.Add(memberInfo.Name, ((PropertyInfo)memberInfo).GetValue(config)); + break; + default: + break; + } + } expandoPopulated = (ExpandoObject)dictionaryFromExpandoPopulated; fromFile = expandoPopulated; return; } var dictionaryFromExpandoFromFile = (IDictionary)fromFile; - foreach (var property in config.GetType().GetProperties()) + foreach (var memberInfo in config.GetType().GetMembers(BindingFlags.DeclaredOnly)) { - if (dictionaryFromExpandoFromFile.TryGetValue(property.Name, out object? value)) + Console.WriteLine($"checking property: {memberInfo.Name}"); + + if (dictionaryFromExpandoFromFile.TryGetValue(memberInfo.Name, out object? valueFromDict)) { - if (value != null) + Console.WriteLine($"dictionary has it - {valueFromDict}"); + if (valueFromDict != null) { - if (property.PropertyType.GetMembers() == null) + Console.WriteLine($"value from configuration value is not null"); + + switch (memberInfo.MemberType) { - var childProperty = (ExpandoObject)property.GetValue(fromFile); - populateExpando(property.GetValue(config), ref childProperty); - property.SetValue(fromFile, childProperty); + case MemberTypes.Field: + var asField = (FieldInfo)memberInfo; + + if ((asField.FieldType.GetMembers(BindingFlags.DeclaredOnly)?.Count() ?? 0) > 0) + { + Console.WriteLine($"{asField.Name} (as field) has {asField.FieldType.GetMembers(BindingFlags.DeclaredOnly) .Count()} members."); + var childMember = (ExpandoObject)dictionaryFromExpandoFromFile[asField.Name]; // (ExpandoObject)asField.GetValue(fromFile); + populateExpando(asField.GetValue(config), ref childMember); + asField.SetValue(fromFile, childMember); + } + break; + case MemberTypes.Property: + var asProperty = (PropertyInfo)memberInfo; + if ((asProperty.PropertyType.GetMembers(BindingFlags.DeclaredOnly)?.Count() ?? 0) > 0) + { + Console.WriteLine($"{asProperty.Name} (as property) has {asProperty.PropertyType.GetMembers(BindingFlags.DeclaredOnly) .Count()} members."); + var childMember = (ExpandoObject)dictionaryFromExpandoFromFile[asProperty.Name]; // (ExpandoObject)asProperty.GetValue(fromFile); + populateExpando(asProperty.GetValue(config), ref childMember); + asProperty.SetValue(fromFile, childMember); + } + break; + default: + Console.WriteLine($"membertype unhanlded. {memberInfo.MemberType}"); + break; } + } } else { - fromFile.TryAdd(property.Name, property.GetValue(config)); + Console.WriteLine($"dictionary didn't have it. so let's add to fromFile"); + switch (memberInfo.MemberType) + { + case MemberTypes.Field: + var addTry = fromFile.TryAdd(memberInfo.Name, ((FieldInfo)memberInfo).GetValue(config)); + Console.WriteLine($"ostensibly, succeeded? {addTry}"); + break; + case MemberTypes.Property: + var addTry2 = fromFile.TryAdd(memberInfo.Name, ((PropertyInfo)memberInfo).GetValue(config)); + Console.WriteLine($"ostensibly, succeeded? {addTry2}"); + break; + default: + break; + } + } + Console.WriteLine($"{memberInfo.Name} handled"); } } - public static void readFromExpando(ref T config, ExpandoObject readFromFile) + internal static void readFromExpando(ref T config, ExpandoObject readFromFile) { //TODO: read from Expando } diff --git a/deployment.tests/ConfigTests.cs b/deployment.tests/ConfigTests.cs index 6231004..1c8b536 100644 --- a/deployment.tests/ConfigTests.cs +++ b/deployment.tests/ConfigTests.cs @@ -140,6 +140,7 @@ public class ConfigTests } }"); greyn.Deployment.ConfigurationBootstrapper.populateExpando(new AConfigurationType(), ref actualConfig); + Console.WriteLine("survived populate expando"); var casted = (IDictionary)actualConfig; Console.WriteLine(casted["aValueTypeButNotAField"]); Assert.Pass(); @@ -350,6 +351,7 @@ public class ConfigTests var actualConfig = parse("{}"); greyn.Deployment.ConfigurationBootstrapper.populateExpando(new AConfigurationType(), ref actualConfig); var casted = (IDictionary)actualConfig; + Console.WriteLine(JsonConvert.SerializeObject(casted, Formatting.Indented)); var subtypeCasted = (IDictionary)casted["subtyped"]; Console.WriteLine(subtypeCasted["aValueTypeButNotAField"]); Assert.Pass();