diff --git a/src/CodegenTests/Codegen/CodeFormatterTests.cs b/src/CodegenTests/Codegen/CodeFormatterTests.cs index 0129f60..c28f7e8 100644 --- a/src/CodegenTests/Codegen/CodeFormatterTests.cs +++ b/src/CodegenTests/Codegen/CodeFormatterTests.cs @@ -21,6 +21,29 @@ public void write_string() .ShouldBe("\"Hello!\""); } + [Fact] + public void write_string_array() + { + CodeFormatter.Write(new string[]{"Hello!", "Bad", "Good"}) + .ShouldBe("new string[]{\"Hello!\", \"Bad\", \"Good\"}"); + } + + [Fact] + public void write_int_array() + { + CodeFormatter.Write(new int[]{1, 2, 4}) + .ShouldBe("new int[]{1, 2, 4}"); + + CodeFormatter.Write(new int[]{1, 2}) + .ShouldBe("new int[]{1, 2}"); + + CodeFormatter.Write(new int[]{1}) + .ShouldBe("new int[]{1}"); + + CodeFormatter.Write(new int[]{}) + .ShouldBe("new int[]{}"); + } + [Fact] public void write_enum() { diff --git a/src/JasperFx/CodeGeneration/CodeFormatter.cs b/src/JasperFx/CodeGeneration/CodeFormatter.cs index c4295fc..2b7b464 100644 --- a/src/JasperFx/CodeGeneration/CodeFormatter.cs +++ b/src/JasperFx/CodeGeneration/CodeFormatter.cs @@ -1,4 +1,5 @@ using JasperFx.CodeGeneration.Model; +using JasperFx.Core; using JasperFx.Core.Reflection; namespace JasperFx.CodeGeneration; @@ -8,7 +9,7 @@ public static class CodeFormatter public static string Write(object? value) { // TODO -- add Guid, int, double, long, bool - + if (value == null) { return "null"; @@ -29,6 +30,42 @@ public static string Write(object? value) return value.GetType().FullNameInCode() + "." + value; } + if (value.GetType() == typeof(string[])) + { + var array = (string[])value; + return $"new string[]{{{array.Select(Write).Join(", ")}}}"; + } + + if (value.GetType().IsArray) + { + var code = $"new {value.GetType().GetElementType().FullNameInCode()}[]{{"; + + var enumerable = (Array)value; + switch (enumerable.Length) + { + case 0: + + break; + case 1: + code += Write(enumerable.GetValue(0)); + break; + + default: + for (int i = 0; i < enumerable.Length - 1; i++) + { + code += Write(enumerable.GetValue(i)); + code += ", "; + } + + code += Write(enumerable.GetValue(enumerable.Length - 1)); + break; + } + + code += "}"; + + return code; + } + if (value is Type t) { return $"typeof({t.FullNameInCode()})";