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

Use nullable operator (?) in type names. #312

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 12 additions & 4 deletions src/FlatSharp/Serialization/CSharpHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@ internal static string GetCompilableTypeName(this Type t)
string name;
if (t.IsGenericType)
{
List<string> parameters = new List<string>();
foreach (var generic in t.GetGenericArguments())
var nullableElementType = Nullable.GetUnderlyingType(t);
if (nullableElementType != null)
{
parameters.Add(GetCompilableTypeName(generic));
name = $"{GetCompilableTypeName(nullableElementType)}?";
}
else
{
List<string> parameters = new List<string>();
foreach (var generic in t.GetGenericArguments())
{
parameters.Add(GetCompilableTypeName(generic));
}

name = $"{t.FullName.Split('`')[0]}<{string.Join(", ", parameters)}>";
name = $"{t.FullName.Split('`')[0]}<{string.Join(", ", parameters)}>";
}
}
else if (t.IsArray)
{
Expand Down
44 changes: 22 additions & 22 deletions src/Tests/FlatSharpTests/ClassLib/TypeModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,15 +477,15 @@ public void TypeModel_Struct_OptionalField_NotAllowed()
{
var ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() =>
RuntimeTypeModel.CreateFrom(typeof(GenericStruct<int?>)));
Assert.Equal("Struct 'FlatSharpTests.TypeModelTests.GenericStruct<System.Nullable<System.Int32>>' property Value (Index 0) with type System.Nullable<System.Int32> cannot be part of a flatbuffer struct.", ex.Message);
Assert.Equal("Struct 'FlatSharpTests.TypeModelTests.GenericStruct<System.Int32?>' property Value (Index 0) with type System.Int32? cannot be part of a flatbuffer struct.", ex.Message);
}

[Fact]
public void TypeModel_Struct_OptionalEnum_NotAllowed()
{
var ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() =>
RuntimeTypeModel.CreateFrom(typeof(GenericStruct<TaggedEnum?>)));
Assert.Equal("Struct 'FlatSharpTests.TypeModelTests.GenericStruct<System.Nullable<FlatSharpTests.TypeModelTests.TaggedEnum>>' property Value (Index 0) with type System.Nullable<FlatSharpTests.TypeModelTests.TaggedEnum> cannot be part of a flatbuffer struct.", ex.Message);
Assert.Equal("Struct 'FlatSharpTests.TypeModelTests.GenericStruct<FlatSharpTests.TypeModelTests.TaggedEnum?>' property Value (Index 0) with type FlatSharpTests.TypeModelTests.TaggedEnum? cannot be part of a flatbuffer struct.", ex.Message);
}

[Fact]
Expand Down Expand Up @@ -588,15 +588,15 @@ public void TypeModel_Vector_OptionalScalarNotAllowed()
{
var ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() =>
RuntimeTypeModel.CreateFrom(typeof(GenericTable<IList<int?>>)));
Assert.Equal("Type 'System.Nullable<System.Int32>' is not a valid vector member.", ex.Message);
Assert.Equal("Type 'System.Int32?' is not a valid vector member.", ex.Message);
}

[Fact]
public void TypeModel_Vector_OptionalEnumNotAllowed()
{
var ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() =>
RuntimeTypeModel.CreateFrom(typeof(GenericTable<IList<TaggedEnum?>>)));
Assert.Equal("Type 'System.Nullable<FlatSharpTests.TypeModelTests.TaggedEnum>' is not a valid vector member.", ex.Message);
Assert.Equal("Type 'FlatSharpTests.TypeModelTests.TaggedEnum?' is not a valid vector member.", ex.Message);
}

[Fact]
Expand Down Expand Up @@ -777,7 +777,7 @@ public void TypeModel_SortedVector_OfNullableEnum_NotAllowed()
{
var ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() =>
RuntimeTypeModel.CreateFrom(typeof(SortedVector<TaggedEnum?[]>)));
Assert.Equal("Type 'System.Nullable<FlatSharpTests.TypeModelTests.TaggedEnum>' is not a valid vector member.", ex.Message);
Assert.Equal("Type 'FlatSharpTests.TypeModelTests.TaggedEnum?' is not a valid vector member.", ex.Message);
}

[Fact]
Expand All @@ -801,7 +801,7 @@ public void TypeModel_SortedVector_OfNullableScalar_NotAllowed()
{
var ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() =>
RuntimeTypeModel.CreateFrom(typeof(SortedVector<int?[]>)));
Assert.Equal("Type 'System.Nullable<System.Int32>' is not a valid vector member.", ex.Message);
Assert.Equal("Type 'System.Int32?' is not a valid vector member.", ex.Message);
}

[Fact]
Expand Down Expand Up @@ -835,40 +835,40 @@ public void TypeModel_SortedVector_DeprecatedKey_NotAllowed()
public void TypeModel_SortedVector_OfTableWithOptionalKey_NotAllowed()
{
var ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() => RuntimeTypeModel.CreateFrom(typeof(SortedVector<SortedVectorKeyTable<bool?>>)));
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Nullable<System.Boolean>> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Boolean?> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);

ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() => RuntimeTypeModel.CreateFrom(typeof(SortedVector<SortedVectorKeyTable<byte?>[]>)));
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Nullable<System.Byte>> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Byte?> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);

ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() => RuntimeTypeModel.CreateFrom(typeof(SortedVector<SortedVectorKeyTable<sbyte?>[]>)));
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Nullable<System.SByte>> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.SByte?> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);

ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() => RuntimeTypeModel.CreateFrom(typeof(SortedVector<SortedVectorKeyTable<ushort?>[]>)));
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Nullable<System.UInt16>> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.UInt16?> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);

ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() => RuntimeTypeModel.CreateFrom(typeof(SortedVector<SortedVectorKeyTable<short?>[]>)));
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Nullable<System.Int16>> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Int16?> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);

ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() => RuntimeTypeModel.CreateFrom(typeof(SortedVector<SortedVectorKeyTable<uint?>[]>)));
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Nullable<System.UInt32>> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.UInt32?> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);

ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() => RuntimeTypeModel.CreateFrom(typeof(SortedVector<SortedVectorKeyTable<int?>[]>)));
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Nullable<System.Int32>> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Int32?> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);

ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() => RuntimeTypeModel.CreateFrom(typeof(SortedVector<SortedVectorKeyTable<ulong?>[]>)));
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Nullable<System.UInt64>> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.UInt64?> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);

ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() => RuntimeTypeModel.CreateFrom(typeof(SortedVector<SortedVectorKeyTable<long?>[]>)));
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Nullable<System.Int64>> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Int64?> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);

ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() => RuntimeTypeModel.CreateFrom(typeof(SortedVector<SortedVectorKeyTable<float?>[]>)));
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Nullable<System.Single>> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Single?> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);

ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() => RuntimeTypeModel.CreateFrom(typeof(SortedVector<SortedVectorKeyTable<double?>[]>)));
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Nullable<System.Double>> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Double?> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);

ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() => RuntimeTypeModel.CreateFrom(typeof(SortedVector<SortedVectorKeyTable<TaggedEnum?>[]>)));
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Nullable<FlatSharpTests.TypeModelTests.TaggedEnum>> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<FlatSharpTests.TypeModelTests.TaggedEnum?> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);
}

[Fact]
Expand Down Expand Up @@ -908,7 +908,7 @@ public void TypeModel_SortedVector_OfTableWithNullableEnumKey_NotAllowed()
{
var ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() =>
RuntimeTypeModel.CreateFrom(typeof(SortedVector<IList<SortedVectorKeyTable<TaggedEnum?>>>)));
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<System.Nullable<FlatSharpTests.TypeModelTests.TaggedEnum>> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);
Assert.Equal("Table FlatSharpTests.TypeModelTests.SortedVectorKeyTable<FlatSharpTests.TypeModelTests.TaggedEnum?> declares a key property on a type that that does not support being a key in a sorted vector.", ex.Message);
}

[Fact]
Expand Down Expand Up @@ -974,7 +974,7 @@ public void TypeModel_Union_UnionsNotAllowed()
{
var ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() =>
RuntimeTypeModel.CreateFrom(typeof(GenericTable<FlatBufferUnion<string, FlatBufferUnion<string, GenericStruct<int>>?>?>)));
Assert.Equal("Unions may not store 'System.Nullable<FlatSharp.FlatBufferUnion<System.String, FlatSharpTests.TypeModelTests.GenericStruct<System.Int32>>>'.", ex.Message);
Assert.Equal("Unions may not store 'FlatSharp.FlatBufferUnion<System.String, FlatSharpTests.TypeModelTests.GenericStruct<System.Int32>>?'.", ex.Message);
}

[Fact]
Expand All @@ -990,7 +990,7 @@ public void TypeModel_Union_OptionalScalarsNotAllowed()
{
var ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() =>
RuntimeTypeModel.CreateFrom(typeof(GenericTable<FlatBufferUnion<string, int?>>)));
Assert.Equal("Unions may not store 'System.Nullable<System.Int32>'.", ex.Message);
Assert.Equal("Unions may not store 'System.Int32?'.", ex.Message);
}

[Fact]
Expand All @@ -1006,7 +1006,7 @@ public void TypeModel_Union_OptionalEnumsNotAllowed()
{
var ex = Assert.Throws<InvalidFlatBufferDefinitionException>(() =>
RuntimeTypeModel.CreateFrom(typeof(GenericTable<FlatBufferUnion<string, TaggedEnum?>?>)));
Assert.Equal("Unions may not store 'System.Nullable<FlatSharpTests.TypeModelTests.TaggedEnum>'.", ex.Message);
Assert.Equal("Unions may not store 'FlatSharpTests.TypeModelTests.TaggedEnum?'.", ex.Message);
}

[Fact]
Expand Down