Skip to content

Commit

Permalink
Added CodeFormatter support for string arrays and really and primitiv…
Browse files Browse the repository at this point in the history
…e array type. Bumps to 3.7.2
  • Loading branch information
jeremydmiller committed Dec 26, 2024
1 parent af7e9e0 commit 1630c43
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<VersionPrefix>3.7.1</VersionPrefix>
<VersionPrefix>3.7.2</VersionPrefix>
<LangVersion>10.0</LangVersion>
<Authors>Jeremy D. Miller;Babu Annamalai;Oskar Dudycz;Joona-Pekka Kokko</Authors>
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
Expand Down
23 changes: 23 additions & 0 deletions src/CodegenTests/Codegen/CodeFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
39 changes: 39 additions & 0 deletions src/JasperFx.CodeGeneration/CodeFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections;
using System.Linq;
using JasperFx.CodeGeneration.Model;
using JasperFx.Core;
using JasperFx.Core.Reflection;

namespace JasperFx.CodeGeneration;
Expand Down Expand Up @@ -30,6 +33,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()}[]{{";

Check warning on line 44 in src/JasperFx.CodeGeneration/CodeFormatter.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'type' in 'string TypeNameExtensions.FullNameInCode(Type type)'.

Check warning on line 44 in src/JasperFx.CodeGeneration/CodeFormatter.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'type' in 'string TypeNameExtensions.FullNameInCode(Type type)'.

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()})";
Expand Down

0 comments on commit 1630c43

Please sign in to comment.