mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-13 00:04:29 +00:00
[C#] Handle keywords (#6775)
* Handle keywords in C# C# should handle keywords similarly to other languages by appending an underscore after the keyword. * Fix clang-format for idl_gen_csharp.cpp * Fix spacing clang-format issue
This commit is contained in:
@@ -137,6 +137,18 @@
|
||||
<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>
|
||||
<Compile Include="..\keyword_test\public.cs">
|
||||
<Link>keyword_test\public.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\keyword_test\KeywordsInTable.cs">
|
||||
<Link>keyword_test\KeywordsInTable.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\keyword_test\KeywordsInUnion.cs">
|
||||
<Link>keyword_test\KeywordsInUnion.cs</Link>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -168,6 +168,18 @@
|
||||
<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>
|
||||
<Compile Include="..\keyword_test\public.cs">
|
||||
<Link>keyword_test\public.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\keyword_test\KeywordsInTable.cs">
|
||||
<Link>keyword_test\KeywordsInTable.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\keyword_test\KeywordsInUnion.cs">
|
||||
<Link>keyword_test\KeywordsInUnion.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="Assert.cs" />
|
||||
<Compile Include="ByteBufferTests.cs" />
|
||||
<Compile Include="FlatBufferBuilderTests.cs" />
|
||||
|
||||
@@ -71,6 +71,9 @@ set TEST_NOINCL_FLAGS=%TEST_BASE_FLAGS% --no-includes
|
||||
@rem Generate the schema evolution tests
|
||||
..\%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
|
||||
|
||||
if NOT "%MONSTER_EXTRA%"=="skip" (
|
||||
@echo Generate MosterExtra
|
||||
..\%buildtype%\flatc.exe --cpp --java --csharp %TEST_NOINCL_FLAGS% %TEST_CPP_FLAGS% %TEST_CS_FLAGS% monster_extra.fbs monsterdata_extra.json || goto FAIL
|
||||
|
||||
@@ -73,6 +73,9 @@ $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS $TEST_TS_FLAGS -o namespace_te
|
||||
# Generate the schema evolution tests
|
||||
../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
|
||||
|
||||
working_dir=`pwd`
|
||||
cd FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests
|
||||
$working_dir/../flatc --bfbs-filenames $working_dir --swift --grpc $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS $TEST_CS_FLAGS -I ${working_dir}/include_test ${working_dir}/monster_test.fbs
|
||||
|
||||
14
tests/keyword_test.fbs
Normal file
14
tests/keyword_test.fbs
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
enum ABC: int { void, where, stackalloc }
|
||||
|
||||
enum public: int { }
|
||||
|
||||
table KeywordsInTable {
|
||||
is: ABC = void;
|
||||
private: public;
|
||||
}
|
||||
|
||||
union KeywordsInUnion {
|
||||
static: KeywordsInTable,
|
||||
internal: string
|
||||
}
|
||||
12
tests/keyword_test/ABC.cs
Normal file
12
tests/keyword_test/ABC.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
// <auto-generated>
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// </auto-generated>
|
||||
|
||||
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
|
||||
public enum ABC : int
|
||||
{
|
||||
void_ = 0,
|
||||
where = 1,
|
||||
stackalloc_ = 2,
|
||||
};
|
||||
|
||||
70
tests/keyword_test/KeywordsInTable.cs
Normal file
70
tests/keyword_test/KeywordsInTable.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
// <auto-generated>
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// </auto-generated>
|
||||
|
||||
using global::System;
|
||||
using global::System.Collections.Generic;
|
||||
using global::FlatBuffers;
|
||||
|
||||
public struct KeywordsInTable : IFlatbufferObject
|
||||
{
|
||||
private Table __p;
|
||||
public ByteBuffer ByteBuffer { get { return __p.bb; } }
|
||||
public static void ValidateVersion() { FlatBufferConstants.FLATBUFFERS_2_0_0(); }
|
||||
public static KeywordsInTable GetRootAsKeywordsInTable(ByteBuffer _bb) { return GetRootAsKeywordsInTable(_bb, new KeywordsInTable()); }
|
||||
public static KeywordsInTable GetRootAsKeywordsInTable(ByteBuffer _bb, KeywordsInTable obj) { return (obj.__assign(_bb.GetInt(_bb.Position) + _bb.Position, _bb)); }
|
||||
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 static Offset<KeywordsInTable> CreateKeywordsInTable(FlatBufferBuilder builder,
|
||||
ABC is_ = ABC.void_,
|
||||
public_ private_ = public_.NONE) {
|
||||
builder.StartTable(2);
|
||||
KeywordsInTable.AddPrivate(builder, private_);
|
||||
KeywordsInTable.AddIs(builder, is_);
|
||||
return KeywordsInTable.EndKeywordsInTable(builder);
|
||||
}
|
||||
|
||||
public static void StartKeywordsInTable(FlatBufferBuilder builder) { builder.StartTable(2); }
|
||||
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 Offset<KeywordsInTable> EndKeywordsInTable(FlatBufferBuilder builder) {
|
||||
int o = builder.EndTable();
|
||||
return new Offset<KeywordsInTable>(o);
|
||||
}
|
||||
public KeywordsInTableT UnPack() {
|
||||
var _o = new KeywordsInTableT();
|
||||
this.UnPackTo(_o);
|
||||
return _o;
|
||||
}
|
||||
public void UnPackTo(KeywordsInTableT _o) {
|
||||
_o.Is = this.Is;
|
||||
_o.Private = this.Private;
|
||||
}
|
||||
public static Offset<KeywordsInTable> Pack(FlatBufferBuilder builder, KeywordsInTableT _o) {
|
||||
if (_o == null) return default(Offset<KeywordsInTable>);
|
||||
return CreateKeywordsInTable(
|
||||
builder,
|
||||
_o.Is,
|
||||
_o.Private);
|
||||
}
|
||||
};
|
||||
|
||||
public class KeywordsInTableT
|
||||
{
|
||||
[Newtonsoft.Json.JsonProperty("is")]
|
||||
public ABC Is { get; set; }
|
||||
[Newtonsoft.Json.JsonProperty("private")]
|
||||
public public_ Private { get; set; }
|
||||
|
||||
public KeywordsInTableT() {
|
||||
this.Is = ABC.void_;
|
||||
this.Private = public_.NONE;
|
||||
}
|
||||
}
|
||||
|
||||
78
tests/keyword_test/KeywordsInUnion.cs
Normal file
78
tests/keyword_test/KeywordsInUnion.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
// <auto-generated>
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// </auto-generated>
|
||||
|
||||
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
|
||||
public enum KeywordsInUnion : byte
|
||||
{
|
||||
NONE = 0,
|
||||
static_ = 1,
|
||||
internal_ = 2,
|
||||
};
|
||||
|
||||
public class KeywordsInUnionUnion {
|
||||
public KeywordsInUnion Type { get; set; }
|
||||
public object Value { get; set; }
|
||||
|
||||
public KeywordsInUnionUnion() {
|
||||
this.Type = KeywordsInUnion.NONE;
|
||||
this.Value = null;
|
||||
}
|
||||
|
||||
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 string Asinternal() { return this.As<string>(); }
|
||||
public static KeywordsInUnionUnion Frominternal(string _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 builder.CreateString(_o.Asinternal()).Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class KeywordsInUnionUnion_JsonConverter : Newtonsoft.Json.JsonConverter {
|
||||
public override bool CanConvert(System.Type objectType) {
|
||||
return objectType == typeof(KeywordsInUnionUnion) || objectType == typeof(System.Collections.Generic.List<KeywordsInUnionUnion>);
|
||||
}
|
||||
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) {
|
||||
var _olist = value as System.Collections.Generic.List<KeywordsInUnionUnion>;
|
||||
if (_olist != null) {
|
||||
writer.WriteStartArray();
|
||||
foreach (var _o in _olist) { this.WriteJson(writer, _o, serializer); }
|
||||
writer.WriteEndArray();
|
||||
} else {
|
||||
this.WriteJson(writer, value as KeywordsInUnionUnion, serializer);
|
||||
}
|
||||
}
|
||||
public void WriteJson(Newtonsoft.Json.JsonWriter writer, KeywordsInUnionUnion _o, Newtonsoft.Json.JsonSerializer serializer) {
|
||||
if (_o == null) return;
|
||||
serializer.Serialize(writer, _o.Value);
|
||||
}
|
||||
public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) {
|
||||
var _olist = existingValue as System.Collections.Generic.List<KeywordsInUnionUnion>;
|
||||
if (_olist != null) {
|
||||
for (var _j = 0; _j < _olist.Count; ++_j) {
|
||||
reader.Read();
|
||||
_olist[_j] = this.ReadJson(reader, _olist[_j], serializer);
|
||||
}
|
||||
reader.Read();
|
||||
return _olist;
|
||||
} else {
|
||||
return this.ReadJson(reader, existingValue as KeywordsInUnionUnion, serializer);
|
||||
}
|
||||
}
|
||||
public KeywordsInUnionUnion ReadJson(Newtonsoft.Json.JsonReader reader, KeywordsInUnionUnion _o, Newtonsoft.Json.JsonSerializer serializer) {
|
||||
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<string>(reader); break;
|
||||
}
|
||||
return _o;
|
||||
}
|
||||
}
|
||||
|
||||
10
tests/keyword_test/public.cs
Normal file
10
tests/keyword_test/public.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
// <auto-generated>
|
||||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
// </auto-generated>
|
||||
|
||||
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
|
||||
public enum public_ : int
|
||||
{
|
||||
NONE = 0,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user