Skip to content

Commit 03b052b

Browse files
authored
Add ToString methods (#437)
* Specify runner versions Latest macos runner had missing dependencies for testing, macos-12 still has these. * Add ToString methods * Allow upstream branch to trigger CI * Specify runner versions * Opt-in for method generation * Opt-in for method generation
1 parent 53da419 commit 03b052b

File tree

9 files changed

+159
-4
lines changed

9 files changed

+159
-4
lines changed

.github/workflows/dotnet.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: .NET
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [ main, upstream ]
66
pull_request:
7-
branches: [ main ]
7+
branches: [ main, upstream ]
88
workflow_dispatch:
99

1010
jobs:

src/FlatSharp.Compiler/CompilerOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public IList<FlatBufferDeserializationOption> Deserializers
5959
}
6060
}
6161

62+
[Option("generate-methods", Hidden = false, Default = false, HelpText = "Enable generation of methods.")]
63+
public bool GenerateMethods { get; set; }
64+
6265
[Option("class-definitions-only", Hidden = false, HelpText = "Emits only class and data definitions. No serializers.")]
6366
public bool ClassDefinitionsOnly { get; set; }
6467

src/FlatSharp.Compiler/FlatSharp.Compiler.targets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@
154154
<CompilerCommand>$(CompilerCommand) --normalize-field-names $(FlatSharpNameNormalization)</CompilerCommand>
155155
</PropertyGroup>
156156

157+
<PropertyGroup Condition=" '$(FlatSharpGenerateMethods)' == 'true' ">
158+
<CompilerCommand>$(CompilerCommand) --generate-methods</CompilerCommand>
159+
</PropertyGroup>
160+
157161
<PropertyGroup Condition=" '$(FlatSharpClassDefinitionsOnly)' == 'true' ">
158162
<CompilerCommand>$(CompilerCommand) --class-definitions-only</CompilerCommand>
159163
</PropertyGroup>

src/FlatSharp.Compiler/SchemaModel/BaseReferenceTypeSchemaModel.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ protected sealed override void OnWriteCode(CodeWriter writer, CompileContext con
8585
writer.AppendLine("this.OnInitialized(context);");
8686
}
8787

88-
foreach (var property in this.properties.OrderBy(x => x.Key))
88+
var orderedProperties = this.properties.OrderBy(x => x.Key);
89+
foreach (var property in orderedProperties)
8990
{
9091
int index = property.Key;
9192
PropertyFieldModel model = property.Value;
@@ -99,6 +100,14 @@ protected sealed override void OnWriteCode(CodeWriter writer, CompileContext con
99100
sv.WriteCode(writer, context);
100101
}
101102

103+
if (context.Options.GenerateMethods)
104+
{
105+
// This matches C# records
106+
string fieldStrings = string.Join(", ", orderedProperties.Select(p => p.Value.FieldName).Select(n => $"{n} = {{this.{n}}}"));
107+
string fieldStringsWithSpace = this.properties.Count == 0 ? " " : $" {fieldStrings} ";
108+
writer.AppendLine($"public override string ToString() => $\"{this.Name} {{{{{fieldStringsWithSpace}}}}}\";");
109+
}
110+
102111
this.EmitExtraData(writer, context);
103112
}
104113
}

src/FlatSharp.Compiler/SchemaModel/ValueStructSchemaModel.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ protected override void OnWriteCode(CodeWriter writer, CompileContext context)
149149
writer.AppendLine();
150150
}
151151

152+
if (context.Options.GenerateMethods)
153+
{
154+
// This matches C# records
155+
string fieldStrings = string.Join(", ", this.fields.Select(x => $"{x.Name} = {{this.{x.Name}}}"));
156+
string fieldStringsWithSpace = this.fields.Count == 0 ? " " : $" {fieldStrings} ";
157+
writer.AppendLine($"public override string ToString() => $\"{this.Name} {{{{{fieldStringsWithSpace}}}}}\";");
158+
}
159+
152160
foreach (var sv in this.structVectors)
153161
{
154162
writer.AppendSummaryComment($"Gets the number of items in the {sv.Name} vector.");

src/FlatSharp.Compiler/SchemaModel/ValueUnionSchemaModel.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ protected override void OnWriteCode(CodeWriter writer, CompileContext context)
152152
writer.AppendLine();
153153
writer.AppendLine("public byte Discriminator { get; }");
154154

155+
if (!generateUnsafeItems && context.Options.GenerateMethods)
156+
{
157+
string item = this.union.Values.Count == 0 ? " " : $" this.value ";
158+
writer.AppendLine($"public override string ToString() => $\"{this.Name} {{{{ {{{item}}} }}}}\";");
159+
}
160+
155161
int index = 1;
156162
foreach (var item in innerTypes)
157163
{

src/Tests/FlatSharpEndToEndTests/FlatSharpEndToEndTests.csproj

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,36 @@
5656

5757
<ItemGroup>
5858
<FlatSharpSchema Include="**\*.fbs" />
59+
<FlatSharpSchema Remove="ToStringMethods/ToStringMethods.fbs" />
5960
</ItemGroup>
60-
</Project>
6161

62+
<Target Name="FlatSharpFbsCompileToString" BeforeTargets="ResolveAssemblyReferences">
63+
<PropertyGroup>
64+
<CompilerVersion>net8.0</CompilerVersion>
65+
</PropertyGroup>
66+
67+
<PropertyGroup>
68+
<FlatSharpOutput>$(IntermediateOutputPath)</FlatSharpOutput>
69+
<FlatSharpOutput Condition=" '$(FlatSharpMutationTestingMode)' == 'true' ">$(MSBuildProjectDirectory)/</FlatSharpOutput>
70+
<FlatSharpOutput>$(FlatSharpOutput)ToStringMethods</FlatSharpOutput>
71+
</PropertyGroup>
72+
73+
<MakeDir Directories="$(FlatSharpOutput)" Condition="!Exists('$(FlatSharpOutput)')" />
74+
75+
<!-- find compiler and set base command -->
76+
<PropertyGroup>
77+
<CompilerPath>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)\..\tools\$(CompilerVersion)\FlatSharp.Compiler.dll'))</CompilerPath>
78+
<CompilerPath Condition=" '$(FlatSharpCompilerPath)' != '' ">$(FlatSharpCompilerPath)</CompilerPath>
79+
<CompilerCommand>dotnet &quot;$(CompilerPath)&quot; --input &quot;ToStringMethods/ToStringMethods.fbs&quot; --output &quot;$(FlatSharpOutput)&quot; --generate-methods</CompilerCommand>
80+
</PropertyGroup>
81+
82+
<Message Text="$(CompilerCommand)" Importance="high" />
83+
<Exec Command="$(CompilerCommand)" CustomErrorRegularExpression=".*" />
84+
85+
<ItemGroup>
86+
<GeneratedFbsToString Include="$([MSBuild]::EnsureTrailingSlash('$(FlatSharpOutput)'))FlatSharp*.cs" />
87+
<Compile Include="@(GeneratedFbsToString)" />
88+
<FileWrites Include="@(GeneratedFbsToString)" />
89+
</ItemGroup>
90+
</Target>
91+
</Project>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using FlatSharp.Internal;
2+
3+
namespace FlatSharpEndToEndTests.ToStringMethods;
4+
5+
[TestClass]
6+
public class ToStringTests
7+
{
8+
[TestMethod]
9+
public void Table_ToString()
10+
{
11+
Assert.AreEqual("MyTable { FieldA = hello, FieldB = 123 }", new MyTable { FieldA = "hello", FieldB = 123}.ToString());
12+
}
13+
14+
[TestMethod]
15+
public void EmptyTable_ToString()
16+
{
17+
Assert.AreEqual("MyEmptyTable { }", new MyEmptyTable().ToString());
18+
}
19+
20+
[TestMethod]
21+
public void Struct_ToString()
22+
{
23+
Assert.AreEqual("MyStruct { FieldA = 456, FieldB = 123 }", new MyStruct { FieldA = 456, FieldB = 123}.ToString());
24+
}
25+
26+
[TestMethod]
27+
public void ValueStruct_ToString()
28+
{
29+
Assert.AreEqual("MyValueStruct { FieldX = 1, FieldY = 2 }", new MyValueStruct { FieldX = 1f, FieldY = 2f}.ToString());
30+
}
31+
32+
[TestMethod]
33+
public void UnionStructs_ToString()
34+
{
35+
Assert.AreEqual("StructUnion { A { V = 0 } }", new StructUnion(new A { V = 0 }).ToString());
36+
Assert.AreEqual("StructUnion { B { V = 1 } }", new StructUnion(new B { V = 1 }).ToString());
37+
Assert.AreEqual("StructUnion { C { V = 2 } }", new StructUnion(new C { V = 2 }).ToString());
38+
Assert.AreEqual("StructUnion { D { V = 3 } }", new StructUnion(new D { V = 3 }).ToString());
39+
}
40+
41+
[TestMethod]
42+
public void UnionTables_ToString()
43+
{
44+
Assert.AreEqual("TableUnion { MyTable { FieldA = hello, FieldB = 10 } }", new TableUnion(new MyTable { FieldA = "hello", FieldB = 10 }).ToString());
45+
Assert.AreEqual("TableUnion { MyEmptyTable { } }", new TableUnion(new MyEmptyTable()).ToString());
46+
}
47+
48+
[TestMethod]
49+
public void UnionMixed_ToString()
50+
{
51+
Assert.AreEqual("MixedUnion { A { V = 0 } }", new MixedUnion(new A { V = 0 }).ToString());
52+
Assert.AreEqual("MixedUnion { A { V = 2 } }", new MixedUnion(new A { V = 2 }).ToString());
53+
Assert.AreEqual("MixedUnion { B { V = 0 } }", new MixedUnion(new B { V = 0 }).ToString());
54+
Assert.AreEqual("MixedUnion { MyTable { FieldA = hi, FieldB = 21 } }", new MixedUnion(new MyTable { FieldA = "hi", FieldB = 21 }).ToString());
55+
Assert.AreEqual("MixedUnion { MyEmptyTable { } }", new MixedUnion(new MyEmptyTable()).ToString());
56+
57+
}
58+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Declare FlatSharp attributes.
2+
3+
attribute "fs_serializer";
4+
attribute "fs_valueStruct";
5+
6+
namespace FlatSharpEndToEndTests.ToStringMethods;
7+
8+
9+
table MyTable (fs_serializer) {
10+
FieldA: string;
11+
FieldB: int;
12+
}
13+
14+
table MyEmptyTable (fs_serializer) {
15+
}
16+
17+
struct MyStruct {
18+
FieldA: int;
19+
FieldB: int;
20+
}
21+
22+
23+
struct MyValueStruct (fs_valueStruct) {
24+
FieldX: float;
25+
FieldY: float;
26+
}
27+
28+
struct A { V : uint; }
29+
struct B { V : uint; }
30+
struct C { V : uint; }
31+
struct D { V : uint; }
32+
33+
union StructUnion { A, B, C, D }
34+
35+
union TableUnion { MyTable, MyEmptyTable }
36+
37+
union MixedUnion { A, B, MyTable, MyEmptyTable }

0 commit comments

Comments
 (0)