Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Match method to Union types #416

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ protected override void OnWriteCode(CodeWriter writer, CompileContext context)
this.WriteDefaultConstructor(writer);
this.WriteReturnToPool(writer);
this.WriteAcceptMethod(writer, innerTypes);
this.WriteMatchMethod(writer, innerTypes);
}
}

Expand Down Expand Up @@ -187,7 +188,32 @@ private void WriteAcceptMethod(
foreach (var item in components)
{
long index = item.value.Value;
writer.AppendLine($"case {index}: return visitor.Visit(this.value_{item.value.Value});");
writer.AppendLine($"case {index}: return visitor.Visit(this.value_{index});");
}

writer.AppendLine($"default: throw new {typeof(InvalidOperationException).GetCompilableTypeName()}(\"Unexpected discriminator: \" + disc);");
}
}
}

private void WriteMatchMethod(
CodeWriter writer,
List<(string resolvedType, EnumVal value, Type? propertyType)> components)
{
string parameters = string.Join(", ", components.Select((x, i) => $"Func<{x.resolvedType}, TReturn> f{i + 1}"));

writer.AppendSummaryComment("Matches this FlatBufferUnion with a function for each value.");
writer.AppendLine($"public TReturn Match<TReturn>({parameters})");
using (writer.WithBlock())
{
writer.AppendLine("var disc = this.Discriminator;");
writer.AppendLine("switch (disc)");
using (writer.WithBlock())
{
foreach (var item in components)
{
long index = item.value.Value;
writer.AppendLine($"case {index}: return f{index}(this.value_{index});");
}

writer.AppendLine($"default: throw new {typeof(InvalidOperationException).GetCompilableTypeName()}(\"Unexpected discriminator: \" + disc);");
Expand Down
27 changes: 27 additions & 0 deletions src/FlatSharp.Compiler/SchemaModel/ValueUnionSchemaModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ protected override void OnWriteCode(CodeWriter writer, CompileContext context)
}

this.WriteAcceptMethod(writer, innerTypes);
this.WriteMatchMethod(writer, innerTypes);
}
}

Expand Down Expand Up @@ -262,6 +263,32 @@ private void WriteAcceptMethod(
}
}

private void WriteMatchMethod(
CodeWriter writer,
List<(string resolvedType, EnumVal value, int? size)> components)
{
string parameters = string.Join(", ", components.Select(x => $"Func<{x.resolvedType}, TReturn> f{x.value.Value}"));

writer.AppendSummaryComment("Matches this FlatBufferUnion with a function for each value.");
writer.AppendLine($"public TReturn Match<TReturn>({parameters})");
using (writer.WithBlock())
{
writer.AppendLine("var disc = this.Discriminator;");
writer.AppendLine("switch (disc)");
using (writer.WithBlock())
{
foreach (var item in components)
{
long index = item.value.Value;
writer.AppendLine($"case {index}: return f{index}(this.UncheckedGetItem{index}());");
}

writer.AppendLine($"default: throw new {typeof(InvalidOperationException).GetCompilableTypeName()}(\"Unexpected discriminator: \" + disc);");
}
}
}


private void WriteUncheckedGetItemMethod(CodeWriter writer, string resolvedType, EnumVal unionValue, Type? propertyType, bool generateUnsafeItems)
{
if (propertyType?.IsValueType == true && generateUnsafeItems)
Expand Down
Loading