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 implicit operator to Union types #415

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
10 changes: 10 additions & 0 deletions src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
foreach (var item in innerTypes)
{
this.WriteConstructor(writer, item.resolvedType, item.value, item.propertyType);
this.WriteImplicitOperator(writer, resolvedType);

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The name 'resolvedType' does not exist in the current context

Check failure on line 104 in src/FlatSharp.Compiler/SchemaModel/ReferenceUnionSchemaModel.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The name 'resolvedType' does not exist in the current context
this.AddUnionMember(writer, item.resolvedType, item.value, item.propertyType, context);
}

Expand Down Expand Up @@ -214,6 +215,15 @@
}
}

private void WriteImplicitOperator(CodeWriter writer, string resolvedType)
{
writer.AppendLine($"public static implicit operator {this.Name}({resolvedType} value)");
using (writer.WithBlock())
{
writer.AppendLine($"return new {this.Name}(value);");
}
}

private void WriteDefaultConstructor(CodeWriter writer)
{
writer.AppendLine($"protected {this.Name}()");
Expand Down
10 changes: 10 additions & 0 deletions src/FlatSharp.Compiler/SchemaModel/ValueUnionSchemaModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ protected override void OnWriteCode(CodeWriter writer, CompileContext context)
}

this.WriteConstructor(writer, item.resolvedType, item.value, propertyClrType, generateUnsafeItems);
this.WriteImplicitOperator(writer, item.resolvedType);
this.WriteUncheckedGetItemMethod(writer, item.resolvedType, item.value, propertyClrType, generateUnsafeItems);

writer.AppendLine();
Expand Down Expand Up @@ -324,4 +325,13 @@ private void WriteConstructor(CodeWriter writer, string resolvedType, EnumVal un
}
}
}

private void WriteImplicitOperator(CodeWriter writer, string resolvedType)
{
writer.AppendLine($"public static implicit operator {this.Name}({resolvedType} value)");
using (writer.WithBlock())
{
writer.AppendLine($"return new {Name}(value);");
}
}
}
Loading