Skip to content

Commit

Permalink
[C#] Use @ for keyword escaping (#6834)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaileychess authored Sep 9, 2021
1 parent 1d063d8 commit 8fb8c2c
Show file tree
Hide file tree
Showing 19 changed files with 111 additions and 164 deletions.
2 changes: 1 addition & 1 deletion src/idl_gen_csharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class CSharpGenerator : public BaseGenerator {
std::unordered_set<std::string> keywords_;

std::string EscapeKeyword(const std::string &name) const {
return keywords_.find(name) == keywords_.end() ? name : name + "_";
return keywords_.find(name) == keywords_.end() ? name : "@" + name;
}

std::string Name(const FieldDef &field) const {
Expand Down
16 changes: 8 additions & 8 deletions tests/FlatBuffers.Test/FlatBuffers.Core.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,17 @@
<Compile Include="..\optional_scalars\ScalarStuff.cs">
<Link>optional_scalars\ScalarStuff.cs</Link>
</Compile>
<Compile Include="..\keyword_test\ABC.cs">
<Link>keyword_test\ABC.cs</Link>
<Compile Include="..\KeywordTest\ABC.cs">
<Link>KeywordTest\ABC.cs</Link>
</Compile>
<Compile Include="..\keyword_test\public.cs">
<Link>keyword_test\public.cs</Link>
<Compile Include="..\KeywordTest\public.cs">
<Link>KeywordTest\public.cs</Link>
</Compile>
<Compile Include="..\keyword_test\KeywordsInTable.cs">
<Link>keyword_test\KeywordsInTable.cs</Link>
<Compile Include="..\KeywordTest\KeywordsInTable.cs">
<Link>KeywordTest\KeywordsInTable.cs</Link>
</Compile>
<Compile Include="..\keyword_test\KeywordsInUnion.cs">
<Link>keyword_test\KeywordsInUnion.cs</Link>
<Compile Include="..\KeywordTest\KeywordsInUnion.cs">
<Link>KeywordTest\KeywordsInUnion.cs</Link>
</Compile>
</ItemGroup>

Expand Down
16 changes: 8 additions & 8 deletions tests/FlatBuffers.Test/FlatBuffers.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,17 @@
<Compile Include="..\optional_scalars\ScalarStuff.cs">
<Link>optional_scalars\ScalarStuff.cs</Link>
</Compile>
<Compile Include="..\keyword_test\ABC.cs">
<Link>keyword_test\ABC.cs</Link>
<Compile Include="..\KeywordTest\ABC.cs">
<Link>KeywordTest\ABC.cs</Link>
</Compile>
<Compile Include="..\keyword_test\public.cs">
<Link>keyword_test\public.cs</Link>
<Compile Include="..\KeywordTest\public.cs">
<Link>KeywordTest\public.cs</Link>
</Compile>
<Compile Include="..\keyword_test\KeywordsInTable.cs">
<Link>keyword_test\KeywordsInTable.cs</Link>
<Compile Include="..\KeywordTest\KeywordsInTable.cs">
<Link>KeywordTest\KeywordsInTable.cs</Link>
</Compile>
<Compile Include="..\keyword_test\KeywordsInUnion.cs">
<Link>keyword_test\KeywordsInUnion.cs</Link>
<Compile Include="..\KeywordTest\KeywordsInUnion.cs">
<Link>KeywordTest\KeywordsInUnion.cs</Link>
</Compile>
<Compile Include="Assert.cs" />
<Compile Include="ByteBufferTests.cs" />
Expand Down
22 changes: 22 additions & 0 deletions tests/FlatBuffers.Test/FlatBuffersExampleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Threading;
using MyGame.Example;
using optional_scalars;
using KeywordTest;

namespace FlatBuffers.Test
{
Expand Down Expand Up @@ -1117,5 +1118,26 @@ public void TestScalarOptional_Construction_CreatorMethod() {
Assert.AreEqual(OptionalByte.Two, scalarStuff.MaybeEnum);
Assert.AreEqual(OptionalByte.Two, scalarStuff.DefaultEnum);
}


[FlatBuffersTestMethod]
public void TestKeywordEscaping() {
Assert.AreEqual((int)KeywordTest.@public.NONE, 0);

Assert.AreEqual((int)KeywordTest.ABC.@void, 0);
Assert.AreEqual((int)KeywordTest.ABC.where, 1);
Assert.AreEqual((int)KeywordTest.ABC.@stackalloc, 2);

var fbb = new FlatBufferBuilder(1);
var offset = KeywordsInTable.CreateKeywordsInTable(
fbb, KeywordTest.ABC.@stackalloc, KeywordTest.@public.NONE);
fbb.Finish(offset.Value);

KeywordsInTable keywordsInTable =
KeywordsInTable.GetRootAsKeywordsInTable(fbb.DataBuffer);

Assert.AreEqual(keywordsInTable.Is, KeywordTest.ABC.@stackalloc);
Assert.AreEqual(keywordsInTable.Private, KeywordTest.@public.NONE);
}
}
}
10 changes: 5 additions & 5 deletions tests/FlatBuffers.Test/NetTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ $DOTNET sln add $PROJ_FILE
$DOTNET restore -r linux-x64 $PROJ_FILE

# Testing C# on Linux using Mono.
msbuild -property:Configuration=Release,OutputPath=$TEMP_BIN -verbosity:minimal $PROJ_FILE
msbuild -property:Configuration=Release,OutputPath=$TEMP_BIN -verbosity:quiet $PROJ_FILE
mono $TEMP_BIN/FlatBuffers.Test.exe
rm -fr $TEMP_BIN

# Repeat with unsafe versions
msbuild -property:Configuration=Release,UnsafeByteBuffer=true,OutputPath=$TEMP_BIN -verbosity:minimal $PROJ_FILE
msbuild -property:Configuration=Release,UnsafeByteBuffer=true,OutputPath=$TEMP_BIN -verbosity:quiet $PROJ_FILE
mono $TEMP_BIN/FlatBuffers.Test.exe
rm -fr $TEMP_BIN

Expand All @@ -36,17 +36,17 @@ $DOTNET sln add $CORE_PROJ_FILE
$DOTNET restore -r linux-x64 $CORE_PROJ_FILE

# Testing C# on Linux using .Net Core.
msbuild -property:Configuration=Release,OutputPath=$TEMP_BIN -verbosity:minimal $CORE_PROJ_FILE
msbuild -property:Configuration=Release,OutputPath=$TEMP_BIN -verbosity:quiet $CORE_PROJ_FILE
$TEMP_BIN/FlatBuffers.Core.Test.exe
rm -fr $TEMP_BIN

# Repeat with unsafe versions
msbuild -property:Configuration=Release,UnsafeByteBuffer=true,OutputPath=$TEMP_BIN -verbosity:minimal $CORE_PROJ_FILE
msbuild -property:Configuration=Release,UnsafeByteBuffer=true,OutputPath=$TEMP_BIN -verbosity:quiet $CORE_PROJ_FILE
$TEMP_BIN/FlatBuffers.Core.Test.exe
rm -fr $TEMP_BIN

# Repeat with SpanT versions
msbuild -property:Configuration=Release,EnableSpanT=true,OutputPath=$TEMP_BIN -verbosity:minimal $CORE_PROJ_FILE
msbuild -property:Configuration=Release,EnableSpanT=true,OutputPath=$TEMP_BIN -verbosity:quiet $CORE_PROJ_FILE
$TEMP_BIN/FlatBuffers.Core.Test.exe
rm -fr $TEMP_BIN

Expand Down
9 changes: 7 additions & 2 deletions tests/keyword_test/ABC.cs → tests/KeywordTest/ABC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>

namespace KeywordTest
{

[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public enum ABC : int
{
void_ = 0,
@void = 0,
where = 1,
stackalloc_ = 2,
@stackalloc = 2,
};


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>

namespace KeywordTest
{

using global::System;
using global::System.Collections.Generic;
using global::FlatBuffers;
Expand All @@ -16,31 +19,31 @@ public struct KeywordsInTable : IFlatbufferObject
public void __init(int _i, ByteBuffer _bb) { __p = new Table(_i, _bb); }
public KeywordsInTable __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }

public ABC Is { get { int o = __p.__offset(4); return o != 0 ? (ABC)__p.bb.GetInt(o + __p.bb_pos) : ABC.void_; } }
public bool MutateIs(ABC is_) { int o = __p.__offset(4); if (o != 0) { __p.bb.PutInt(o + __p.bb_pos, (int)is_); return true; } else { return false; } }
public public_ Private { get { int o = __p.__offset(6); return o != 0 ? (public_)__p.bb.GetInt(o + __p.bb_pos) : public_.NONE; } }
public bool MutatePrivate(public_ private_) { int o = __p.__offset(6); if (o != 0) { __p.bb.PutInt(o + __p.bb_pos, (int)private_); return true; } else { return false; } }
public KeywordTest.ABC Is { get { int o = __p.__offset(4); return o != 0 ? (KeywordTest.ABC)__p.bb.GetInt(o + __p.bb_pos) : KeywordTest.ABC.@void; } }
public bool MutateIs(KeywordTest.ABC @is) { int o = __p.__offset(4); if (o != 0) { __p.bb.PutInt(o + __p.bb_pos, (int)@is); return true; } else { return false; } }
public KeywordTest.@public Private { get { int o = __p.__offset(6); return o != 0 ? (KeywordTest.@public)__p.bb.GetInt(o + __p.bb_pos) : KeywordTest.@public.NONE; } }
public bool MutatePrivate(KeywordTest.@public @private) { int o = __p.__offset(6); if (o != 0) { __p.bb.PutInt(o + __p.bb_pos, (int)@private); return true; } else { return false; } }
public int Type { get { int o = __p.__offset(8); return o != 0 ? __p.bb.GetInt(o + __p.bb_pos) : (int)0; } }
public bool MutateType(int type) { int o = __p.__offset(8); if (o != 0) { __p.bb.PutInt(o + __p.bb_pos, type); return true; } else { return false; } }

public static Offset<KeywordsInTable> CreateKeywordsInTable(FlatBufferBuilder builder,
ABC is_ = ABC.void_,
public_ private_ = public_.NONE,
public static Offset<KeywordTest.KeywordsInTable> CreateKeywordsInTable(FlatBufferBuilder builder,
KeywordTest.ABC @is = KeywordTest.ABC.@void,
KeywordTest.@public @private = KeywordTest.@public.NONE,
int type = 0) {
builder.StartTable(3);
KeywordsInTable.AddType(builder, type);
KeywordsInTable.AddPrivate(builder, private_);
KeywordsInTable.AddIs(builder, is_);
KeywordsInTable.AddPrivate(builder, @private);
KeywordsInTable.AddIs(builder, @is);
return KeywordsInTable.EndKeywordsInTable(builder);
}

public static void StartKeywordsInTable(FlatBufferBuilder builder) { builder.StartTable(3); }
public static void AddIs(FlatBufferBuilder builder, ABC is_) { builder.AddInt(0, (int)is_, 0); }
public static void AddPrivate(FlatBufferBuilder builder, public_ private_) { builder.AddInt(1, (int)private_, 0); }
public static void AddIs(FlatBufferBuilder builder, KeywordTest.ABC @is) { builder.AddInt(0, (int)@is, 0); }
public static void AddPrivate(FlatBufferBuilder builder, KeywordTest.@public @private) { builder.AddInt(1, (int)@private, 0); }
public static void AddType(FlatBufferBuilder builder, int type) { builder.AddInt(2, type, 0); }
public static Offset<KeywordsInTable> EndKeywordsInTable(FlatBufferBuilder builder) {
public static Offset<KeywordTest.KeywordsInTable> EndKeywordsInTable(FlatBufferBuilder builder) {
int o = builder.EndTable();
return new Offset<KeywordsInTable>(o);
return new Offset<KeywordTest.KeywordsInTable>(o);
}
public KeywordsInTableT UnPack() {
var _o = new KeywordsInTableT();
Expand All @@ -52,8 +55,8 @@ public void UnPackTo(KeywordsInTableT _o) {
_o.Private = this.Private;
_o.Type = this.Type;
}
public static Offset<KeywordsInTable> Pack(FlatBufferBuilder builder, KeywordsInTableT _o) {
if (_o == null) return default(Offset<KeywordsInTable>);
public static Offset<KeywordTest.KeywordsInTable> Pack(FlatBufferBuilder builder, KeywordsInTableT _o) {
if (_o == null) return default(Offset<KeywordTest.KeywordsInTable>);
return CreateKeywordsInTable(
builder,
_o.Is,
Expand All @@ -65,16 +68,18 @@ public static Offset<KeywordsInTable> Pack(FlatBufferBuilder builder, KeywordsIn
public class KeywordsInTableT
{
[Newtonsoft.Json.JsonProperty("is")]
public ABC Is { get; set; }
public KeywordTest.ABC Is { get; set; }
[Newtonsoft.Json.JsonProperty("private")]
public public_ Private { get; set; }
public KeywordTest.@public Private { get; set; }
[Newtonsoft.Json.JsonProperty("type")]
public int Type { get; set; }

public KeywordsInTableT() {
this.Is = ABC.void_;
this.Private = public_.NONE;
this.Is = KeywordTest.ABC.@void;
this.Private = KeywordTest.@public.NONE;
this.Type = 0;
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>

namespace KeywordTest
{

[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public enum KeywordsInUnion : byte
{
NONE = 0,
static_ = 1,
internal_ = 2,
@static = 1,
@internal = 2,
};

public class KeywordsInUnionUnion {
Expand All @@ -20,16 +23,16 @@ public KeywordsInUnionUnion() {
}

public T As<T>() where T : class { return this.Value as T; }
public KeywordsInTableT Asstatic() { return this.As<KeywordsInTableT>(); }
public static KeywordsInUnionUnion Fromstatic(KeywordsInTableT _static) { return new KeywordsInUnionUnion{ Type = KeywordsInUnion.static_, Value = _static }; }
public KeywordsInTableT Asinternal() { return this.As<KeywordsInTableT>(); }
public static KeywordsInUnionUnion Frominternal(KeywordsInTableT _internal) { return new KeywordsInUnionUnion{ Type = KeywordsInUnion.internal_, Value = _internal }; }
public KeywordTest.KeywordsInTableT Asstatic() { return this.As<KeywordTest.KeywordsInTableT>(); }
public static KeywordsInUnionUnion Fromstatic(KeywordTest.KeywordsInTableT _static) { return new KeywordsInUnionUnion{ Type = KeywordsInUnion.@static, Value = _static }; }
public KeywordTest.KeywordsInTableT Asinternal() { return this.As<KeywordTest.KeywordsInTableT>(); }
public static KeywordsInUnionUnion Frominternal(KeywordTest.KeywordsInTableT _internal) { return new KeywordsInUnionUnion{ Type = KeywordsInUnion.@internal, Value = _internal }; }

public static int Pack(FlatBuffers.FlatBufferBuilder builder, KeywordsInUnionUnion _o) {
switch (_o.Type) {
default: return 0;
case KeywordsInUnion.static_: return KeywordsInTable.Pack(builder, _o.Asstatic()).Value;
case KeywordsInUnion.internal_: return KeywordsInTable.Pack(builder, _o.Asinternal()).Value;
case KeywordsInUnion.@static: return KeywordTest.KeywordsInTable.Pack(builder, _o.Asstatic()).Value;
case KeywordsInUnion.@internal: return KeywordTest.KeywordsInTable.Pack(builder, _o.Asinternal()).Value;
}
}
}
Expand Down Expand Up @@ -69,10 +72,12 @@ public KeywordsInUnionUnion ReadJson(Newtonsoft.Json.JsonReader reader, Keywords
if (_o == null) return null;
switch (_o.Type) {
default: break;
case KeywordsInUnion.static_: _o.Value = serializer.Deserialize<KeywordsInTableT>(reader); break;
case KeywordsInUnion.internal_: _o.Value = serializer.Deserialize<KeywordsInTableT>(reader); break;
case KeywordsInUnion.@static: _o.Value = serializer.Deserialize<KeywordTest.KeywordsInTableT>(reader); break;
case KeywordsInUnion.@internal: _o.Value = serializer.Deserialize<KeywordTest.KeywordsInTableT>(reader); break;
}
return _o;
}
}


}
7 changes: 6 additions & 1 deletion tests/keyword_test/public.cs → tests/KeywordTest/public.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
// automatically generated by the FlatBuffers compiler, do not modify
// </auto-generated>

namespace KeywordTest
{

[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public enum public_ : int
public enum @public : int
{
NONE = 0,
};


}
2 changes: 1 addition & 1 deletion tests/generate_code.bat
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ set TEST_NOINCL_FLAGS=%TEST_BASE_FLAGS% --no-includes
..\%buildtype%\flatc.exe --cpp --scoped-enums %TEST_CPP_FLAGS% -o evolution_test ./evolution_test/evolution_v1.fbs ./evolution_test/evolution_v2.fbs || goto FAIL

@rem Generate the keywords tests
..\%buildtype%\flatc.exe --csharp --scoped-enums %TEST_BASE_FLAGS% %TEST_CS_FLAGS% -o keyword_test keyword_test.fbs || goto FAIL
..\%buildtype%\flatc.exe --csharp --scoped-enums %TEST_BASE_FLAGS% %TEST_CS_FLAGS% keyword_test.fbs || goto FAIL

if NOT "%MONSTER_EXTRA%"=="skip" (
@echo Generate MosterExtra
Expand Down
2 changes: 1 addition & 1 deletion tests/generate_code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS $TEST_TS_FLAGS -o namespace_te
../flatc --cpp --scoped-enums $TEST_CPP_FLAGS -o evolution_test ./evolution_test/evolution_v*.fbs

# Generate the keywords tests
../flatc --csharp $TEST_BASE_FLAGS $TEST_CS_FLAGS -o keyword_test ./keyword_test.fbs
../flatc --csharp $TEST_BASE_FLAGS $TEST_CS_FLAGS ./keyword_test.fbs
../flatc --rust $TEST_RUST_FLAGS -o keyword_test ./keyword_test.fbs

working_dir=`pwd`
Expand Down
1 change: 1 addition & 0 deletions tests/keyword_test.fbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
namespace KeywordTest;

enum ABC: int { void, where, stackalloc }

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl<'a> KeywordsInTable<'a> {
pub const VT_TYPE_: flatbuffers::VOffsetT = 8;

pub const fn get_fully_qualified_name() -> &'static str {
"KeywordsInTable"
"KeywordTest.KeywordsInTable"
}

#[inline]
Expand Down
File renamed without changes.
19 changes: 11 additions & 8 deletions tests/keyword_test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Automatically generated by the Flatbuffers compiler. Do not modify.
mod abc_generated;
pub use self::abc_generated::*;
mod public_generated;
pub use self::public_generated::*;
mod keywords_in_union_generated;
pub use self::keywords_in_union_generated::*;
mod keywords_in_table_generated;
pub use self::keywords_in_table_generated::*;
pub mod keyword_test {
use super::*;
mod abc_generated;
pub use self::abc_generated::*;
mod public_generated;
pub use self::public_generated::*;
mod keywords_in_union_generated;
pub use self::keywords_in_union_generated::*;
mod keywords_in_table_generated;
pub use self::keywords_in_table_generated::*;
} // keyword_test
10 changes: 0 additions & 10 deletions tests/keyword_test/type.cs

This file was deleted.

Loading

0 comments on commit 8fb8c2c

Please sign in to comment.