Skip to content

Commit

Permalink
Add ToString methods
Browse files Browse the repository at this point in the history
  • Loading branch information
trumully committed May 14, 2024
1 parent b8110a1 commit 337cff2
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ protected sealed override void OnWriteCode(CodeWriter writer, CompileContext con
writer.AppendLine("this.OnInitialized(context);");
}

foreach (var property in this.properties.OrderBy(x => x.Key))
var orderedProperties = this.properties.OrderBy(x => x.Key);
foreach (var property in orderedProperties)
{
int index = property.Key;
PropertyFieldModel model = property.Value;
Expand All @@ -100,6 +101,11 @@ protected sealed override void OnWriteCode(CodeWriter writer, CompileContext con
sv.WriteCode(writer, context);
}

// This matches C# records
string fieldStrings = string.Join(", ", orderedProperties.Select(p => p.Value.FieldName).Select(n => $"{n} = {{this.{n}}}"));
string fieldStringsWithSpace = this.properties.Count == 0 ? " " : $" {fieldStrings} ";
writer.AppendLine($"public override string ToString() => $\"{this.Name} {{{{{fieldStringsWithSpace}}}}}\";");

this.EmitExtraData(writer, context);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/FlatSharp.Compiler/SchemaModel/ValueStructSchemaModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ protected override void OnWriteCode(CodeWriter writer, CompileContext context)
writer.AppendLine();
}

// This matches C# records
string fieldStrings = string.Join(", ", this.fields.Select(x => $"{x.Name} = {{this.{x.Name}}}"));
string fieldStringsWithSpace = this.fields.Count == 0 ? " " : $" {fieldStrings} ";
writer.AppendLine($"public override string ToString() => $\"{this.Name} {{{{{fieldStringsWithSpace}}}}}\";");

foreach (var sv in this.structVectors)
{
writer.AppendSummaryComment($"Gets the number of items in the {sv.Name} vector.");
Expand Down
6 changes: 6 additions & 0 deletions src/FlatSharp.Compiler/SchemaModel/ValueUnionSchemaModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ protected override void OnWriteCode(CodeWriter writer, CompileContext context)
writer.AppendLine();
writer.AppendLine("public byte Discriminator { get; }");

if (!generateUnsafeItems)
{
string item = this.union.Values.Count == 0 ? " " : $" this.value ";
writer.AppendLine($"public override string ToString() => $\"{this.Name} {{{{ {{{item}}} }}}}\";");
}

foreach (var item in innerTypes)
{
Type? propertyClrType = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using FlatSharp.Internal;

namespace FlatSharpEndToEndTests.ToStringMethods;

[TestClass]
public class ToStringTests
{
[TestMethod]
public void Table_ToString()
{
Assert.AreEqual("MyTable { FieldA = hello, FieldB = 123 }", new MyTable { FieldA = "hello", FieldB = 123}.ToString());
}

[TestMethod]
public void EmptyTable_ToString()
{
Assert.AreEqual("MyEmptyTable { }", new MyEmptyTable().ToString());
}

[TestMethod]
public void Struct_ToString()
{
Assert.AreEqual("MyStruct { FieldA = 456, FieldB = 123 }", new MyStruct { FieldA = 456, FieldB = 123}.ToString());
}

[TestMethod]
public void ValueStruct_ToString()
{
Assert.AreEqual("MyValueStruct { FieldX = 1, FieldY = 2 }", new MyValueStruct { FieldX = 1f, FieldY = 2f}.ToString());
}

[TestMethod]
public void UnionStructs_ToString()
{
Assert.AreEqual("StructUnion { A { V = 0 } }", new StructUnion(new A { V = 0 }).ToString());
Assert.AreEqual("StructUnion { B { V = 1 } }", new StructUnion(new B { V = 1 }).ToString());
Assert.AreEqual("StructUnion { C { V = 2 } }", new StructUnion(new C { V = 2 }).ToString());
Assert.AreEqual("StructUnion { D { V = 3 } }", new StructUnion(new D { V = 3 }).ToString());
}

[TestMethod]
public void UnionTables_ToString()
{
Assert.AreEqual("TableUnion { MyTable { FieldA = hello, FieldB = 10 } }", new TableUnion(new MyTable { FieldA = "hello", FieldB = 10 }).ToString());
Assert.AreEqual("TableUnion { MyEmptyTable { } }", new TableUnion(new MyEmptyTable()).ToString());
}

[TestMethod]
public void UnionMixed_ToString()
{
Assert.AreEqual("MixedUnion { A { V = 0 } }", new MixedUnion(new A { V = 0 }).ToString());
Assert.AreEqual("MixedUnion { A { V = 2 } }", new MixedUnion(new A { V = 2 }).ToString());
Assert.AreEqual("MixedUnion { B { V = 0 } }", new MixedUnion(new B { V = 0 }).ToString());
Assert.AreEqual("MixedUnion { MyTable { FieldA = hi, FieldB = 21 } }", new MixedUnion(new MyTable { FieldA = "hi", FieldB = 21 }).ToString());
Assert.AreEqual("MixedUnion { MyEmptyTable { } }", new MixedUnion(new MyEmptyTable()).ToString());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Declare FlatSharp attributes.

attribute "fs_serializer";
attribute "fs_valueStruct";

namespace FlatSharpEndToEndTests.ToStringMethods;


table MyTable (fs_serializer) {
FieldA: string;
FieldB: int;
}

table MyEmptyTable (fs_serializer) {
}

struct MyStruct {
FieldA: int;
FieldB: int;
}


struct MyValueStruct (fs_valueStruct) {
FieldX: float;
FieldY: float;
}

struct A { V : uint; }
struct B { V : uint; }
struct C { V : uint; }
struct D { V : uint; }

union StructUnion { A, B, C, D }

union TableUnion { MyTable, MyEmptyTable }

union MixedUnion { A, B, MyTable, MyEmptyTable }

0 comments on commit 337cff2

Please sign in to comment.