diff options
author | Wouter van Oortmerssen <wvo@google.com> | 2014-09-25 15:53:56 -0700 |
---|---|---|
committer | Wouter van Oortmerssen <wvo@google.com> | 2014-09-25 15:53:56 -0700 |
commit | 18cf19f87629b54e961e2b81542aaca5bf956ada (patch) | |
tree | d07d44b8714a1dcc2c564f74a027b295caf63baf | |
parent | 730c0cadde2302efa1487d672a1e2f53680ce2ea (diff) | |
download | flatbuffers-18cf19f87629b54e961e2b81542aaca5bf956ada.tar.gz flatbuffers-18cf19f87629b54e961e2b81542aaca5bf956ada.tar.bz2 flatbuffers-18cf19f87629b54e961e2b81542aaca5bf956ada.zip |
Fixed bug in convenient constructors for Java/C#
Also fixed Go unit tests not being up to date with recent schema
changes.
Change-Id: I42e619f9c5ea05f6f937c68a5c8a92462c46bce3
Tested: on Linux and Windows.
-rw-r--r-- | src/idl_gen_general.cpp | 7 | ||||
-rw-r--r-- | tests/FlatBuffers.Test/FlatBuffers.Test.csproj | 1 | ||||
-rw-r--r-- | tests/GoTest.sh | 4 | ||||
-rw-r--r-- | tests/MyGame/Example/Monster.cs | 4 | ||||
-rw-r--r-- | tests/MyGame/Example/Monster.go | 7 | ||||
-rw-r--r-- | tests/MyGame/Example/Monster.java | 4 | ||||
-rw-r--r-- | tests/MyGame/Example/Stat.cs | 34 | ||||
-rw-r--r-- | tests/MyGame/Example/Stat.go | 36 | ||||
-rw-r--r-- | tests/MyGame/Example/Stat.java | 35 | ||||
-rw-r--r-- | tests/go_test.go | 17 | ||||
-rwxr-xr-x | tests/monster_test.fbs | 7 | ||||
-rwxr-xr-x | tests/monster_test_generated.h | 41 |
12 files changed, 172 insertions, 25 deletions
diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp index 51270ad2..fa9bd133 100644 --- a/src/idl_gen_general.cpp +++ b/src/idl_gen_general.cpp @@ -463,7 +463,7 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, if (has_no_struct_fields && num_fields) { // Generate a table constructor of the form: // public static void createName(FlatBufferBuilder builder, args...) - code += " public static void " + FunctionStart(lang, 'C') + "reate"; + code += " public static int " + FunctionStart(lang, 'C') + "reate"; code += struct_def.name; code += "(FlatBufferBuilder builder"; for (auto it = struct_def.fields.vec.begin(); @@ -495,8 +495,9 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, } } } - code += " builder."; - code += FunctionStart(lang, 'E') + "ndObject();\n }\n\n"; + code += " return " + struct_def.name + "."; + code += FunctionStart(lang, 'E') + "nd" + struct_def.name; + code += "(builder);\n }\n\n"; } // Generate a set of static methods that allow table construction, // of the form: diff --git a/tests/FlatBuffers.Test/FlatBuffers.Test.csproj b/tests/FlatBuffers.Test/FlatBuffers.Test.csproj index fd3f61f2..f5f7d5cf 100644 --- a/tests/FlatBuffers.Test/FlatBuffers.Test.csproj +++ b/tests/FlatBuffers.Test/FlatBuffers.Test.csproj @@ -55,6 +55,7 @@ </Compile> <Compile Include="Assert.cs" /> <Compile Include="ByteBufferTests.cs" /> + <Compile Include="MyGame\Example\Stat.cs" /> <Compile Include="Program.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="FlatBuffersExampleTests.cs" /> diff --git a/tests/GoTest.sh b/tests/GoTest.sh index 2e7b5c7a..ba4835a2 100644 --- a/tests/GoTest.sh +++ b/tests/GoTest.sh @@ -41,8 +41,8 @@ cp -u ./go_test.go ./go_gen/src/flatbuffers_test/ # go -test -test.bench=. ... GOPATH=${go_path} go test flatbuffers_test \ --test.coverpkg=github.com/google/flatbuffers/go \ - --cpp_data=${test_dir}/monsterdata_test.bin \ - --out_data=${test_dir}/monsterdata_go_wire.bin \ + --cpp_data=${test_dir}/monsterdata_test.mon \ + --out_data=${test_dir}/monsterdata_go_wire.mon \ --fuzz=true \ --fuzz_fields=4 \ --fuzz_objects=10000 diff --git a/tests/MyGame/Example/Monster.cs b/tests/MyGame/Example/Monster.cs index a389cb82..2b36eed3 100644 --- a/tests/MyGame/Example/Monster.cs +++ b/tests/MyGame/Example/Monster.cs @@ -34,8 +34,8 @@ public class Monster : Table { public Monster Enemy(Monster obj) { int o = __offset(28); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; } public byte Testnestedflatbuffer(int j) { int o = __offset(30); return o != 0 ? bb.Get(__vector(o) + j * 1) : (byte)0; } public int TestnestedflatbufferLength() { int o = __offset(30); return o != 0 ? __vector_len(o) : 0; } - public Monster Testempty() { return Testempty(new Monster()); } - public Monster Testempty(Monster obj) { int o = __offset(32); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; } + public Stat Testempty() { return Testempty(new Stat()); } + public Stat Testempty(Stat obj) { int o = __offset(32); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; } public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(15); } public static void AddPos(FlatBufferBuilder builder, int posOffset) { builder.AddStruct(0, posOffset, 0); } diff --git a/tests/MyGame/Example/Monster.go b/tests/MyGame/Example/Monster.go index 74bf7b5a..ed0f6061 100644 --- a/tests/MyGame/Example/Monster.go +++ b/tests/MyGame/Example/Monster.go @@ -139,8 +139,7 @@ func (rcv *Monster) TestarrayofstringLength() int { return 0 } -/// an example documentation comment: this will end up in the generated code -/// multiline too +/// an example documentation comment: this will end up in the generated code multiline too func (rcv *Monster) Testarrayoftables(obj *Monster, j int) bool { o := flatbuffers.UOffsetT(rcv._tab.Offset(26)) if o != 0 { @@ -194,12 +193,12 @@ func (rcv *Monster) TestnestedflatbufferLength() int { return 0 } -func (rcv *Monster) Testempty(obj *Monster) *Monster { +func (rcv *Monster) Testempty(obj *Stat) *Stat { o := flatbuffers.UOffsetT(rcv._tab.Offset(32)) if o != 0 { x := rcv._tab.Indirect(o + rcv._tab.Pos) if obj == nil { - obj = new(Monster) + obj = new(Stat) } obj.Init(rcv._tab.Bytes, x) return obj diff --git a/tests/MyGame/Example/Monster.java b/tests/MyGame/Example/Monster.java index 5f8da24b..f7411d9d 100644 --- a/tests/MyGame/Example/Monster.java +++ b/tests/MyGame/Example/Monster.java @@ -42,8 +42,8 @@ public class Monster extends Table { public byte testnestedflatbuffer(int j) { int o = __offset(30); return o != 0 ? bb.get(__vector(o) + j * 1) : 0; } public int testnestedflatbufferLength() { int o = __offset(30); return o != 0 ? __vector_len(o) : 0; } public ByteBuffer testnestedflatbufferAsByteBuffer() { return __vector_as_bytebuffer(30, 1); } - public Monster testempty() { return testempty(new Monster()); } - public Monster testempty(Monster obj) { int o = __offset(32); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; } + public Stat testempty() { return testempty(new Stat()); } + public Stat testempty(Stat obj) { int o = __offset(32); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; } public static void startMonster(FlatBufferBuilder builder) { builder.startObject(15); } public static void addPos(FlatBufferBuilder builder, int posOffset) { builder.addStruct(0, posOffset, 0); } diff --git a/tests/MyGame/Example/Stat.cs b/tests/MyGame/Example/Stat.cs new file mode 100644 index 00000000..1a554f29 --- /dev/null +++ b/tests/MyGame/Example/Stat.cs @@ -0,0 +1,34 @@ +// automatically generated, do not modify + +namespace MyGame.Example +{ + +using FlatBuffers; + +public class Stat : Table { + public static Stat GetRootAsStat(ByteBuffer _bb) { return (new Stat()).__init(_bb.GetInt(_bb.position()) + _bb.position(), _bb); } + public Stat __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; } + + public string Id() { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; } + public long Val() { int o = __offset(6); return o != 0 ? bb.GetLong(o + bb_pos) : (long)0; } + + public static int CreateStat(FlatBufferBuilder builder, + int id = 0, + long val = 0) { + builder.StartObject(2); + Stat.AddVal(builder, val); + Stat.AddId(builder, id); + return Stat.EndStat(builder); + } + + public static void StartStat(FlatBufferBuilder builder) { builder.StartObject(2); } + public static void AddId(FlatBufferBuilder builder, int idOffset) { builder.AddOffset(0, idOffset, 0); } + public static void AddVal(FlatBufferBuilder builder, long val) { builder.AddLong(1, val, 0); } + public static int EndStat(FlatBufferBuilder builder) { + int o = builder.EndObject(); + return o; + } +}; + + +} diff --git a/tests/MyGame/Example/Stat.go b/tests/MyGame/Example/Stat.go new file mode 100644 index 00000000..4c07f56b --- /dev/null +++ b/tests/MyGame/Example/Stat.go @@ -0,0 +1,36 @@ +// automatically generated, do not modify + +package Example + +import ( + flatbuffers "github.com/google/flatbuffers/go" +) +type Stat struct { + _tab flatbuffers.Table +} + +func (rcv *Stat) Init(buf []byte, i flatbuffers.UOffsetT) { + rcv._tab.Bytes = buf + rcv._tab.Pos = i +} + +func (rcv *Stat) Id() string { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + return rcv._tab.String(o + rcv._tab.Pos) + } + return "" +} + +func (rcv *Stat) Val() int64 { + o := flatbuffers.UOffsetT(rcv._tab.Offset(6)) + if o != 0 { + return rcv._tab.GetInt64(o + rcv._tab.Pos) + } + return 0 +} + +func StatStart(builder *flatbuffers.Builder) { builder.StartObject(2) } +func StatAddId(builder *flatbuffers.Builder, id flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(id), 0) } +func StatAddVal(builder *flatbuffers.Builder, val int64) { builder.PrependInt64Slot(1, val, 0) } +func StatEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } diff --git a/tests/MyGame/Example/Stat.java b/tests/MyGame/Example/Stat.java new file mode 100644 index 00000000..66be12f1 --- /dev/null +++ b/tests/MyGame/Example/Stat.java @@ -0,0 +1,35 @@ +// automatically generated, do not modify + +package MyGame.Example; + +import java.nio.*; +import java.lang.*; +import java.util.*; +import com.google.flatbuffers.*; + +public class Stat extends Table { + public static Stat getRootAsStat(ByteBuffer _bb) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (new Stat()).__init(_bb.getInt(_bb.position()) + _bb.position(), _bb); } + public Stat __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; } + + public String id() { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; } + public ByteBuffer idAsByteBuffer() { return __vector_as_bytebuffer(4, 1); } + public long val() { int o = __offset(6); return o != 0 ? bb.getLong(o + bb_pos) : 0; } + + public static int createStat(FlatBufferBuilder builder, + int id, + long val) { + builder.startObject(2); + Stat.addVal(builder, val); + Stat.addId(builder, id); + return Stat.endStat(builder); + } + + public static void startStat(FlatBufferBuilder builder) { builder.startObject(2); } + public static void addId(FlatBufferBuilder builder, int idOffset) { builder.addOffset(0, idOffset, 0); } + public static void addVal(FlatBufferBuilder builder, long val) { builder.addLong(1, val, 0); } + public static int endStat(FlatBufferBuilder builder) { + int o = builder.endObject(); + return o; + } +}; + diff --git a/tests/go_test.go b/tests/go_test.go index a9ad01d3..351b31eb 100644 --- a/tests/go_test.go +++ b/tests/go_test.go @@ -21,9 +21,9 @@ var ( func init() { flag.StringVar(&cppData, "cpp_data", "", - "location of monsterdata_test.bin to verify against (required)") + "location of monsterdata_test.mon to verify against (required)") flag.StringVar(&javaData, "java_data", "", - "location of monsterdata_java_wire.bin to verify against (optional)") + "location of monsterdata_java_wire.mon to verify against (optional)") flag.StringVar(&outData, "out_data", "", "location to write generated Go data") flag.BoolVar(&fuzz, "fuzz", false, "perform fuzzing") @@ -146,8 +146,8 @@ func CheckReadBuffer(buf []byte, offset flatbuffers.UOffsetT, fail func(string, fail(FailString("Pos.Test1", float64(3.0), got)) } - if got := vec.Test2(); int8(4) != got { - fail(FailString("Pos.Test2", int8(4), got)) + if got := vec.Test2(); int8(2) != got { + fail(FailString("Pos.Test2", int8(2), got)) } // initialize a Test from Test3(...) @@ -190,8 +190,8 @@ func CheckReadBuffer(buf []byte, offset flatbuffers.UOffsetT, fail func(string, var monster2 example.Monster monster2.Init(table2.Bytes, table2.Pos) - if got := monster2.Hp(); int16(20) != got { - fail(FailString("monster2.Hp()", int16(20), got)) + if got := monster2.Name(); "Fred" != got { + fail(FailString("monster2.Name()", "Fred", got)) } if got := monster.InventoryLength(); 5 != got { @@ -905,6 +905,7 @@ func CheckGeneratedBuild(fail func(string, ...interface{})) ([]byte, flatbuffers str := b.CreateString("MyMonster") test1 := b.CreateString("test1") test2 := b.CreateString("test2") + fred := b.CreateString("Fred") example.MonsterStartInventoryVector(b, 5) b.PrependByte(4) @@ -915,7 +916,7 @@ func CheckGeneratedBuild(fail func(string, ...interface{})) ([]byte, flatbuffers inv := b.EndVector(5) example.MonsterStart(b) - example.MonsterAddHp(b, int16(20)) + example.MonsterAddName(b, fred) mon2 := example.MonsterEnd(b) example.MonsterStartTest4Vector(b, 2) @@ -930,7 +931,7 @@ func CheckGeneratedBuild(fail func(string, ...interface{})) ([]byte, flatbuffers example.MonsterStart(b) - pos := example.CreateVec3(b, 1.0, 2.0, 3.0, 3.0, 4, 5, 6) + pos := example.CreateVec3(b, 1.0, 2.0, 3.0, 3.0, 2, 5, 6) example.MonsterAddPos(b, pos) example.MonsterAddHp(b, 80) diff --git a/tests/monster_test.fbs b/tests/monster_test.fbs index 8de9c033..12749cbd 100755 --- a/tests/monster_test.fbs +++ b/tests/monster_test.fbs @@ -19,6 +19,11 @@ struct Vec3 (force_align: 16) { test3:Test; } +table Stat { + id:string; + val:long; +} + table Monster { pos:Vec3 (id: 0); hp:short = 100 (id: 2); @@ -35,7 +40,7 @@ table Monster { test:Any (id: 8); test4:[Test] (id: 9); testnestedflatbuffer:[ubyte] (id:13, nested_flatbuffer: "Monster"); - testempty:Monster (id:14); + testempty:Stat (id:14); } root_type Monster; diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 7b23c217..10d881c6 100755 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -16,6 +16,7 @@ namespace Example { struct Test; struct Vec3; +struct Stat; struct Monster; enum Color { @@ -85,6 +86,40 @@ MANUALLY_ALIGNED_STRUCT(16) Vec3 { }; STRUCT_END(Vec3, 32); +struct Stat : private flatbuffers::Table { + const flatbuffers::String *id() const { return GetPointer<const flatbuffers::String *>(4); } + int64_t val() const { return GetField<int64_t>(6, 0); } + bool Verify(flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + VerifyField<flatbuffers::uoffset_t>(verifier, 4 /* id */) && + verifier.Verify(id()) && + VerifyField<int64_t>(verifier, 6 /* val */) && + verifier.EndTable(); + } +}; + +struct StatBuilder { + flatbuffers::FlatBufferBuilder &fbb_; + flatbuffers::uoffset_t start_; + void add_id(flatbuffers::Offset<flatbuffers::String> id) { fbb_.AddOffset(4, id); } + void add_val(int64_t val) { fbb_.AddElement<int64_t>(6, val, 0); } + StatBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } + StatBuilder &operator=(const StatBuilder &); + flatbuffers::Offset<Stat> Finish() { + auto o = flatbuffers::Offset<Stat>(fbb_.EndTable(start_, 2)); + return o; + } +}; + +inline flatbuffers::Offset<Stat> CreateStat(flatbuffers::FlatBufferBuilder &_fbb, + flatbuffers::Offset<flatbuffers::String> id = 0, + int64_t val = 0) { + StatBuilder builder_(_fbb); + builder_.add_val(val); + builder_.add_id(id); + return builder_.Finish(); +} + struct Monster : private flatbuffers::Table { const Vec3 *pos() const { return GetStruct<const Vec3 *>(4); } int16_t mana() const { return GetField<int16_t>(6, 150); } @@ -102,7 +137,7 @@ struct Monster : private flatbuffers::Table { const Monster *enemy() const { return GetPointer<const Monster *>(28); } const flatbuffers::Vector<uint8_t> *testnestedflatbuffer() const { return GetPointer<const flatbuffers::Vector<uint8_t> *>(30); } const Monster *testnestedflatbuffer_nested_root() { return flatbuffers::GetRoot<Monster>(testnestedflatbuffer()->Data()); } - const Monster *testempty() const { return GetPointer<const Monster *>(32); } + const Stat *testempty() const { return GetPointer<const Stat *>(32); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField<Vec3>(verifier, 4 /* pos */) && @@ -150,7 +185,7 @@ struct MonsterBuilder { void add_testarrayoftables(flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Monster>>> testarrayoftables) { fbb_.AddOffset(26, testarrayoftables); } void add_enemy(flatbuffers::Offset<Monster> enemy) { fbb_.AddOffset(28, enemy); } void add_testnestedflatbuffer(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> testnestedflatbuffer) { fbb_.AddOffset(30, testnestedflatbuffer); } - void add_testempty(flatbuffers::Offset<Monster> testempty) { fbb_.AddOffset(32, testempty); } + void add_testempty(flatbuffers::Offset<Stat> testempty) { fbb_.AddOffset(32, testempty); } MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } MonsterBuilder &operator=(const MonsterBuilder &); flatbuffers::Offset<Monster> Finish() { @@ -174,7 +209,7 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Monster>>> testarrayoftables = 0, flatbuffers::Offset<Monster> enemy = 0, flatbuffers::Offset<flatbuffers::Vector<uint8_t>> testnestedflatbuffer = 0, - flatbuffers::Offset<Monster> testempty = 0) { + flatbuffers::Offset<Stat> testempty = 0) { MonsterBuilder builder_(_fbb); builder_.add_testempty(testempty); builder_.add_testnestedflatbuffer(testnestedflatbuffer); |