Normalize field names by default (#326)

* Normalize field names by default

* Fix tests
This commit is contained in:
James Courtney 2022-10-26 00:42:01 -07:00 коммит произвёл GitHub
Родитель 1f0241e892
Коммит 7993a58ce9
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
16 изменённых файлов: 104 добавлений и 66 удалений

Просмотреть файл

@ -29,7 +29,7 @@ public record CompilerOptions
[Option('I', "includes", HelpText = "Includes search directory path(s)")]
public string? IncludesDirectory { get; set; }
[Option("normalize-field-names", Default = false, HelpText = "Normalize snake_case and lowerPascalCase field names to UpperPascalCase.")]
[Option("normalize-field-names", Default = true, HelpText = "Normalize snake_case and lowerPascalCase field names to UpperPascalCase.")]
public bool? NormalizeFieldNames { get; set; }
[Option("nullable-warnings", Default = false, HelpText = "Emit full nullable annotations and enable warnings.")]

Просмотреть файл

@ -96,7 +96,7 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(FlatSharpNameNormalization)' == '' ">
<FlatSharpNameNormalization>false</FlatSharpNameNormalization>
<FlatSharpNameNormalization>true</FlatSharpNameNormalization>
</PropertyGroup>
<!-- Query the installed SDKs. -->

Просмотреть файл

@ -552,7 +552,7 @@ public class FlatSharpCompiler
var schema = mutableSerializer.Parse(bfbs);
// Modify
if (options.NormalizeFieldNames == true)
if (options.NormalizeFieldNames != false)
{
foreach (Schema.FlatBufferObject item in schema.Objects)
{

Просмотреть файл

@ -110,7 +110,7 @@ public static class MetadataKeys
/// - Struct (as a default for all fields in the struct, overridden by the setting on a field)
/// - Field
/// </summary>
public const string PreserveFieldCasing = "fs_preserveFieldCasing";
public const string LiteralName = "fs_literalName";
/// <summary>
/// Marks a table field as deprecated. Deprecated fields do not have their values serialized or parsed.

Просмотреть файл

@ -41,7 +41,7 @@ public class FlatSharpAttributes : IFlatSharpAttributes
public bool? NonVirtual => this.TryParseBoolean(MetadataKeys.NonVirtualProperty);
public bool? PreserveFieldCasing => this.TryParseBoolean(MetadataKeys.PreserveFieldCasing);
public bool? PreserveFieldCasing => this.TryParseBoolean(MetadataKeys.LiteralName);
public bool? SortedVector => this.TryParseBoolean(MetadataKeys.SortedVector);

Просмотреть файл

@ -127,7 +127,7 @@ table InnerTable {{
byte[] data = new byte[FlatBufferSerializer.Default.GetMaxSize(original)];
int bytesWritten = FlatBufferSerializer.Default.Serialize(original, data);
Assembly asm = FlatSharpCompiler.CompileAndLoadAssembly(schema, new());
Assembly asm = FlatSharpCompiler.CompileAndLoadAssembly(schema, new() { NormalizeFieldNames = false });
Type outerTableType = asm.GetType("CopyConstructorTest.OuterTable");
dynamic serializer = outerTableType.GetProperty("Serializer", BindingFlags.Public | BindingFlags.Static).GetValue(null);

Просмотреть файл

@ -28,25 +28,22 @@ public class FieldNameNormalizationTests
table Table {{
item_one : int32;
item_two : int32;
item__three : int32;
lowerPascalCase : int32;
item_f : int32;
____item__three__ : int32;
lowerPascal_Case : int32;
_item_f_ : int32;
}}
struct Struct {{
item_one : int32;
item_two : int32;
____item__three__ : int32;
lowerPascalCase : int32;
item_f : int32;
lowerPascal_Case : int32;
_item_f_ : int32;
}}";
Assembly asm = FlatSharpCompiler.CompileAndLoadAssembly(
schema,
new CompilerOptions
{
NormalizeFieldNames = true,
});
new CompilerOptions());
foreach (string typeName in new[] { "FieldNameNormalizationTests.Table", "FieldNameNormalizationTests.Struct" })
{
@ -61,6 +58,46 @@ public class FieldNameNormalizationTests
}
}
[Fact]
public void NormalizeFieldNames_Disabled()
{
string schema = $@"
{MetadataHelpers.AllAttributes}
namespace FieldNameNormalizationTests;
table Table {{
item_one : int32;
item_two : int32;
____item__three__ : int32;
lowerPascal_Case : int32;
_item_f_ : int32;
}}
struct Struct {{
item_one : int32;
item_two : int32;
____item__three__ : int32;
lowerPascal_Case : int32;
_item_f_ : int32;
}}";
Assembly asm = FlatSharpCompiler.CompileAndLoadAssembly(
schema,
new CompilerOptions() { NormalizeFieldNames = false });
foreach (string typeName in new[] { "FieldNameNormalizationTests.Table", "FieldNameNormalizationTests.Struct" })
{
Type type = asm.GetType(typeName);
Assert.NotNull(type);
Assert.NotNull(type.GetProperty("item_one"));
Assert.NotNull(type.GetProperty("item_two"));
Assert.NotNull(type.GetProperty("____item__three__"));
Assert.NotNull(type.GetProperty("lowerPascal_Case"));
Assert.NotNull(type.GetProperty("_item_f_"));
}
}
[Fact]
public void PreserveFieldCasingOnField()
{
@ -70,12 +107,12 @@ public class FieldNameNormalizationTests
table Table {{
item_one : int32;
item_two : int32 ({MetadataKeys.PreserveFieldCasing});
item_two : int32 ({MetadataKeys.LiteralName});
}}
struct Struct {{
item_one : int32;
item_two : int32 ({MetadataKeys.PreserveFieldCasing});
item_two : int32 ({MetadataKeys.LiteralName});
}}";
Assembly asm = FlatSharpCompiler.CompileAndLoadAssembly(
@ -102,13 +139,13 @@ public class FieldNameNormalizationTests
{MetadataHelpers.AllAttributes}
namespace FieldNameNormalizationTests;
table Table ({MetadataKeys.PreserveFieldCasing}) {{
item_one : int32 ({MetadataKeys.PreserveFieldCasing}:""false"");
table Table ({MetadataKeys.LiteralName}) {{
item_one : int32 ({MetadataKeys.LiteralName}:""false"");
item_two : int32;
}}
struct Struct ({MetadataKeys.PreserveFieldCasing}) {{
item_one : int32 ({MetadataKeys.PreserveFieldCasing}:""false"");
struct Struct ({MetadataKeys.LiteralName}) {{
item_one : int32 ({MetadataKeys.LiteralName}:""false"");
item_two : int32;
}}";

Просмотреть файл

@ -40,7 +40,7 @@ public class MetadataHelpers
names.Add(MetadataKeys.ForceWrite);
names.Add(MetadataKeys.WriteThrough);
names.Add(MetadataKeys.RpcInterface);
names.Add(MetadataKeys.PreserveFieldCasing);
names.Add(MetadataKeys.LiteralName);
names.Add(string.Empty);
AllAttributes = string.Join("\r\n", names.Select(x => $"attribute \"{x}\";"));

Просмотреть файл

@ -70,18 +70,18 @@ public class PrecompiledSerializerTests
object vec = Activator.CreateInstance(vecType);
dynamic dVec = vec;
Assert.Equal((short)150, dMonster.mana);
Assert.Equal((short)100, dMonster.hp);
Assert.False(dMonster.friendly);
Assert.Equal("Blue", dMonster.color.ToString());
Assert.Null(dMonster.pos);
Assert.Equal((short)150, dMonster.Mana);
Assert.Equal((short)100, dMonster.Hp);
Assert.False(dMonster.Friendly);
Assert.Equal("Blue", dMonster.Color.ToString());
Assert.Null(dMonster.Pos);
Assert.Equal(typeof(IList<byte>), monsterType.GetProperty("inventory").PropertyType);
Assert.Equal(typeof(IList<>).MakeGenericType(vecType), monsterType.GetProperty("path").PropertyType);
Assert.Equal(typeof(IList<>).MakeGenericType(weaponType), monsterType.GetProperty("weapons").PropertyType);
Assert.True(typeof(IFlatBufferUnion<,>).MakeGenericType(weaponType, vecType).IsAssignableFrom(Nullable.GetUnderlyingType(monsterType.GetProperty("equipped").PropertyType)));
Assert.Equal(typeof(string), monsterType.GetProperty("name").PropertyType);
Assert.True(monsterType.GetProperty("friendly").GetCustomAttribute<FlatBufferItemAttribute>().Deprecated);
Assert.Equal(typeof(IList<byte>), monsterType.GetProperty("Inventory").PropertyType);
Assert.Equal(typeof(IList<>).MakeGenericType(vecType), monsterType.GetProperty("Path").PropertyType);
Assert.Equal(typeof(IList<>).MakeGenericType(weaponType), monsterType.GetProperty("Weapons").PropertyType);
Assert.True(typeof(IFlatBufferUnion<,>).MakeGenericType(weaponType, vecType).IsAssignableFrom(Nullable.GetUnderlyingType(monsterType.GetProperty("Equipped").PropertyType)));
Assert.Equal(typeof(string), monsterType.GetProperty("Name").PropertyType);
Assert.True(monsterType.GetProperty("Friendly").GetCustomAttribute<FlatBufferItemAttribute>().Deprecated);
byte[] data = new byte[1024];
@ -89,7 +89,7 @@ public class PrecompiledSerializerTests
compiled.Write(data, monster);
dynamic parsedMonster = compiled.Parse(data);
Assert.Equal("Blue", parsedMonster.color.ToString());
Assert.Equal("Blue", parsedMonster.Color.ToString());
}
[Fact]

Просмотреть файл

@ -105,18 +105,18 @@ table TableThatStartsItAll {{
object vec = Activator.CreateInstance(vec3Type);
dynamic dVec = vec;
Assert.Equal((short)150, dMonster.mana);
Assert.Equal((short)100, dMonster.hp);
Assert.False(dMonster.friendly);
Assert.Equal("Blue", dMonster.color.ToString());
Assert.Null(dMonster.pos);
Assert.Equal((short)150, dMonster.Mana);
Assert.Equal((short)100, dMonster.Hp);
Assert.False(dMonster.Friendly);
Assert.Equal("Blue", dMonster.Color.ToString());
Assert.Null(dMonster.Pos);
Assert.Equal(typeof(IList<byte>), monsterType.GetProperty("inventory").PropertyType);
Assert.Equal(typeof(IList<>).MakeGenericType(vec3Type), monsterType.GetProperty("path").PropertyType);
Assert.Equal(typeof(IList<>).MakeGenericType(weaponType), monsterType.GetProperty("weapons").PropertyType);
Assert.True(typeof(IFlatBufferUnion<,,,>).MakeGenericType(weaponType, vec3Type, vec4Type, monsterType).IsAssignableFrom(Nullable.GetUnderlyingType(monsterType.GetProperty("equipped").PropertyType)));
Assert.Equal(typeof(string), monsterType.GetProperty("name").PropertyType);
Assert.True(monsterType.GetProperty("friendly").GetCustomAttribute<FlatBufferItemAttribute>().Deprecated);
Assert.Equal(typeof(IList<byte>), monsterType.GetProperty("Inventory").PropertyType);
Assert.Equal(typeof(IList<>).MakeGenericType(vec3Type), monsterType.GetProperty("Path").PropertyType);
Assert.Equal(typeof(IList<>).MakeGenericType(weaponType), monsterType.GetProperty("Weapons").PropertyType);
Assert.True(typeof(IFlatBufferUnion<,,,>).MakeGenericType(weaponType, vec3Type, vec4Type, monsterType).IsAssignableFrom(Nullable.GetUnderlyingType(monsterType.GetProperty("Equipped").PropertyType)));
Assert.Equal(typeof(string), monsterType.GetProperty("Name").PropertyType);
Assert.True(monsterType.GetProperty("Friendly").GetCustomAttribute<FlatBufferItemAttribute>().Deprecated);
Assert.Equal(arrayVectorType, monsterType.GetProperty("FakeVector2").PropertyType);

Просмотреть файл

@ -33,15 +33,15 @@ public class SharedStringCompilerTests
Assembly asm = FlatSharpCompiler.CompileAndLoadAssembly(schema, new());
Type tableType = asm.GetTypes().Single(x => x.FullName == "SharedStringTests.Table");
var property = tableType.GetProperty("foo");
var property = tableType.GetProperty("Foo");
Assert.Equal(typeof(string), property.PropertyType);
Assert.True(property.GetCustomAttribute<FlatBufferItemAttribute>().SharedString);
Assert.Equal(typeof(string[]), tableType.GetProperty("bar").PropertyType);
Assert.True(tableType.GetProperty("bar").GetCustomAttribute<FlatBufferItemAttribute>().SharedString);
Assert.Equal(typeof(string[]), tableType.GetProperty("Bar").PropertyType);
Assert.True(tableType.GetProperty("Bar").GetCustomAttribute<FlatBufferItemAttribute>().SharedString);
Assert.Equal(typeof(IList<string>), tableType.GetProperty("baz").PropertyType);
Assert.True(tableType.GetProperty("baz").GetCustomAttribute<FlatBufferItemAttribute>().SharedString);
Assert.Equal(typeof(IList<string>), tableType.GetProperty("Baz").PropertyType);
Assert.True(tableType.GetProperty("Baz").GetCustomAttribute<FlatBufferItemAttribute>().SharedString);
}
}

Просмотреть файл

@ -42,7 +42,7 @@ public class StructTests
size:ushort;
}";
Assembly asm = FlatSharpCompiler.CompileAndLoadAssembly(schema, new());
Assembly asm = FlatSharpCompiler.CompileAndLoadAssembly(schema, new() { NormalizeFieldNames = false });
Type tableType = asm.GetTypes().Single(x => x.FullName == "StructTests.Table");
object table = Activator.CreateInstance(tableType);

Просмотреть файл

@ -122,7 +122,7 @@ public class StructVectorTests
Assembly asm = FlatSharpCompiler.CompileAndLoadAssembly(
schema,
new());
new() { NormalizeFieldNames = false });
Type tableType = asm.GetType("StructVectorTests.Table");
Type fooType = asm.GetType("StructVectorTests.Foo");
@ -231,7 +231,7 @@ public class StructVectorTests
dynamic table = Activator.CreateInstance(tableType);
dynamic foo = Activator.CreateInstance(fooType);
table.foo = foo;
table.Foo = foo;
Assert.Equal(length, foo.V.Count);
@ -242,10 +242,10 @@ public class StructVectorTests
items.Add(GetRandom<T>());
}
table.foo.V.CopyFrom((IReadOnlyList<T>)items);
table.Foo.V.CopyFrom((IReadOnlyList<T>)items);
for (int i = 0; i < length; ++i)
{
CheckRandom<T>(items[i], table.foo.V[i]);
CheckRandom<T>(items[i], table.Foo.V[i]);
}
for (int i = 0; i < length; ++i)
@ -264,11 +264,11 @@ public class StructVectorTests
dynamic copy = Activator.CreateInstance(tableType, (object)parsed);
Assert.Equal(length, parsed.foo.V.Count);
Assert.Equal(length, parsed.Foo.V.Count);
for (int i = 0; i < length; ++i)
{
CheckRandom<T>(foo.V[i], parsed.foo.V[i]);
CheckRandom<T>(foo.V[i], copy.foo.V[i]);
CheckRandom<T>(foo.V[i], parsed.Foo.V[i]);
CheckRandom<T>(foo.V[i], copy.Foo.V[i]);
}
bool isMutable = option is FlatBufferDeserializationOption.GreedyMutable;
@ -282,7 +282,7 @@ public class StructVectorTests
{
for (int i = 0; i < length; ++i)
{
parsed.foo.V[i] = GetRandom<T>();
parsed.Foo.V[i] = GetRandom<T>();
}
Assert.True(isMutable);

Просмотреть файл

@ -124,7 +124,7 @@ public class TableMemberTests
Assembly asm = FlatSharpCompiler.CompileAndLoadAssembly(schema, new());
Type tableType = asm.GetType("TableMemberTests.Table");
PropertyInfo property = tableType.GetProperty("member");
PropertyInfo property = tableType.GetProperty("Member");
Assert.Equal(typeof(T), property.PropertyType);
var attribute = property.GetCustomAttribute<FlatBufferItemAttribute>();

Просмотреть файл

@ -37,20 +37,20 @@ public class WriteThroughTests
ISerializer serializer = (ISerializer)tableType.GetProperty("Serializer", BindingFlags.Public | BindingFlags.Static).GetValue(null);
dynamic table = Activator.CreateInstance(tableType);
table.Struct = (dynamic)Activator.CreateInstance(structType);
table.Struct.foo = 42;
table.Struct.bar = 65;
table.Struct.Foo = 42;
table.Struct.Bar = 65;
byte[] data = new byte[100];
serializer.Write(data, (object)table);
dynamic parsed = serializer.Parse(data);
parsed.Struct.foo = 100;
parsed.Struct.Foo = 100;
Assert.Throws<NotMutableException>(() => parsed.Struct.bar = 22);
Assert.Throws<NotMutableException>(() => parsed.Struct.Bar = 22);
dynamic parsed2 = serializer.Parse(data);
Assert.Equal(100, (int)parsed2.Struct.foo);
Assert.Equal(65, (int)parsed2.Struct.bar);
Assert.Equal(100, (int)parsed2.Struct.Foo);
Assert.Equal(65, (int)parsed2.Struct.Bar);
}
Test(FlatBufferDeserializationOption.Lazy);

Просмотреть файл

@ -11,6 +11,7 @@
<DocumentationFile>$(OutputPath)$(AssemblyName).xml</DocumentationFile>
<NoWarn>CS1591</NoWarn>
<FlatSharpCompilerPath>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)\..\..\FlatSharp.Compiler\bin\$(Configuration)\net7.0\FlatSharp.Compiler.dll'))</FlatSharpCompilerPath>
<FlatSharpNameNormalization>false</FlatSharpNameNormalization>
</PropertyGroup>
<ItemGroup>