From 76d31e1b5e223e4600bf8298ef9224d2642f51ca Mon Sep 17 00:00:00 2001 From: kostya-sh Date: Tue, 16 Oct 2018 00:55:59 +0100 Subject: [PATCH] Go - Use Go bool type for bool fields (#4962) * Use Go bool type for bool fields, and store non-default bool field to test data --- include/flatbuffers/idl.h | 2 +- src/idl_gen_go.cpp | 14 ++++++++++-- .../FlatBuffersExampleTests.cs | 4 ++-- .../Resources/monsterdata_test.mon | Bin 288 -> 448 bytes tests/JavaScriptTest.js | 4 ++-- tests/JavaTest.java | 4 ++-- tests/MyGame/Example/Monster.go | 20 +++++++++--------- tests/go_test.go | 9 ++++++++ tests/monsterdata_test.golden | 1 + tests/monsterdata_test.json | 1 + tests/monsterdata_test.mon | Bin 448 -> 448 bytes tests/phpTest.php | 4 ++-- 12 files changed, 42 insertions(+), 21 deletions(-) diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index e1c759895..c00bdb9c2 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -49,7 +49,7 @@ namespace flatbuffers { #define FLATBUFFERS_GEN_TYPES_SCALAR(TD) \ TD(NONE, "", uint8_t, byte, byte, byte, uint8, u8) \ TD(UTYPE, "", uint8_t, byte, byte, byte, uint8, u8) /* begin scalar/int */ \ - TD(BOOL, "bool", uint8_t, boolean,byte, bool, bool, bool) \ + TD(BOOL, "bool", uint8_t, boolean,bool, bool, bool, bool) \ TD(CHAR, "byte", int8_t, byte, int8, sbyte, int8, i8) \ TD(UCHAR, "ubyte", uint8_t, byte, byte, byte, uint8, u8) \ TD(SHORT, "short", int16_t, short, int16, short, int16, i16) \ diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index 356035f96..fe4939edf 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -52,6 +52,7 @@ static const char * const g_golang_keywords[] = { static std::string GenGetter(const Type &type); static std::string GenMethod(const FieldDef &field); +static std::string GenConstant(const FieldDef &field); static void GenStructBuilder(const StructDef &struct_def, std::string *code_ptr); static void GenReceiver(const StructDef &struct_def, std::string *code_ptr); @@ -245,7 +246,7 @@ static void GetScalarFieldOfTable(const StructDef &struct_def, code += "() " + TypeName(field) + " "; code += OffsetPrefix(field) + "\t\treturn " + getter; code += "(o + rcv._tab.Pos)\n\t}\n"; - code += "\treturn " + field.value.constant + "\n"; + code += "\treturn " + GenConstant(field) + "\n"; code += "}\n\n"; } @@ -361,6 +362,8 @@ static void GetMemberOfVectorOfNonStruct(const StructDef &struct_def, code += "\t}\n"; if (vectortype.base_type == BASE_TYPE_STRING) { code += "\treturn nil\n"; + } else if (vectortype.base_type == BASE_TYPE_BOOL) { + code += "\treturn false\n"; } else { code += "\treturn 0\n"; } @@ -471,7 +474,7 @@ static void BuildFieldOfTable(const StructDef &struct_def, } else { code += GoIdentity(field.name); } - code += ", " + field.value.constant; + code += ", " + GenConstant(field); code += ")\n}\n"; } @@ -721,6 +724,13 @@ static std::string TypeName(const FieldDef &field) { return GenTypeGet(field.value.type); } +static std::string GenConstant(const FieldDef &field) { + switch (field.value.type.base_type) { + case BASE_TYPE_BOOL: return field.value.constant == "0" ? "false" : "true";; + default: return field.value.constant; + } +} + // Create a struct with a builder and the struct's arguments. static void GenStructBuilder(const StructDef &struct_def, std::string *code_ptr) { diff --git a/tests/FlatBuffers.Test/FlatBuffersExampleTests.cs b/tests/FlatBuffers.Test/FlatBuffersExampleTests.cs index 603de347a..1a8a08190 100644 --- a/tests/FlatBuffers.Test/FlatBuffersExampleTests.cs +++ b/tests/FlatBuffers.Test/FlatBuffersExampleTests.cs @@ -97,7 +97,7 @@ namespace FlatBuffers.Test Monster.AddTest(fbb, mon2.Value); Monster.AddTest4(fbb, test4); Monster.AddTestarrayofstring(fbb, testArrayOfString); - Monster.AddTestbool(fbb, false); + Monster.AddTestbool(fbb, true); Monster.AddTestarrayoftables(fbb, sortMons); var mon = Monster.EndMonster(fbb); @@ -246,7 +246,7 @@ namespace FlatBuffers.Test Assert.AreEqual("test1", monster.Testarrayofstring(0)); Assert.AreEqual("test2", monster.Testarrayofstring(1)); - Assert.AreEqual(false, monster.Testbool); + Assert.AreEqual(true, monster.Testbool); #if ENABLE_SPAN_T var nameBytes = monster.GetNameBytes(); diff --git a/tests/FlatBuffers.Test/Resources/monsterdata_test.mon b/tests/FlatBuffers.Test/Resources/monsterdata_test.mon index 2bf6d15865519c67c53427e86969f817c9ce35b1..3f8397236a1076d560a6b2da7c0728d8e9403fc8 100644 GIT binary patch literal 448 zcma!GU|{g|_X`G6VBo>f!oUG!u`y@>NgW_&VK87YVXy$QY=D@7ftA65!G$4#A%nq# z!H1!Mp@Ja*EDj?=7$O*AfN~5BjEo?e274gk07O6x0~`)aKy%oDTmwc11_dDA0c6hs z;tU`L>2f?-c^8BWmo)Z)unADC0EmwO@d6-joLK$oYy*EDjQ)5x{rDY^pUyBEsDyzT zh#7%!!~g&PS%7R1b}LFv0a6T1Kr8~pATd@TE=eseF+yS+0@)xnTtHQFz_37O^MFGL zC<)Zf113?;2H6X=5(wV^2dM%9D8C-W2H6MnG8ljz17r!PL--C({1C>&uOMY$0A?^U VF@xO73BfZbhjnAYoPz!N|l65(BYIQj1HBkl2PmHYX7KR{G}W6_=zI0oedd5E4`X diff --git a/tests/JavaScriptTest.js b/tests/JavaScriptTest.js index 7ed313af4..1186cdd41 100644 --- a/tests/JavaScriptTest.js +++ b/tests/JavaScriptTest.js @@ -52,7 +52,7 @@ function main() { MyGame.Example.Monster.addTest(fbb, mon2); MyGame.Example.Monster.addTest4(fbb, test4); MyGame.Example.Monster.addTestarrayofstring(fbb, testArrayOfString); - MyGame.Example.Monster.addTestbool(fbb, false); + MyGame.Example.Monster.addTestbool(fbb, true); var mon = MyGame.Example.Monster.endMonster(fbb); MyGame.Example.Monster.finishMonsterBuffer(fbb, mon); @@ -140,7 +140,7 @@ function testBuffer(bb) { assert.strictEqual(monster.testarrayofstring(0), 'test1'); assert.strictEqual(monster.testarrayofstring(1), 'test2'); - assert.strictEqual(monster.testbool(), false); + assert.strictEqual(monster.testbool(), true); } function test64bit() { diff --git a/tests/JavaTest.java b/tests/JavaTest.java index a9dedf979..3aead2917 100644 --- a/tests/JavaTest.java +++ b/tests/JavaTest.java @@ -130,7 +130,7 @@ class JavaTest { TestEq(monster.testarrayofstring(0),"test1"); TestEq(monster.testarrayofstring(1),"test2"); - TestEq(monster.testbool(), false); + TestEq(monster.testbool(), true); } // this method checks additional fields not present in the binary buffer read from file @@ -319,7 +319,7 @@ class JavaTest { Monster.addTest(fbb, mon2); Monster.addTest4(fbb, test4); Monster.addTestarrayofstring(fbb, testArrayOfString); - Monster.addTestbool(fbb, false); + Monster.addTestbool(fbb, true); Monster.addTesthashu32Fnv1(fbb, Integer.MAX_VALUE + 1L); Monster.addTestarrayoftables(fbb, sortMons); int mon = Monster.endMonster(fbb); diff --git a/tests/MyGame/Example/Monster.go b/tests/MyGame/Example/Monster.go index 7bf10ce61..29ca9c0d0 100644 --- a/tests/MyGame/Example/Monster.go +++ b/tests/MyGame/Example/Monster.go @@ -241,16 +241,16 @@ func (rcv *Monster) Testempty(obj *Stat) *Stat { return nil } -func (rcv *Monster) Testbool() byte { +func (rcv *Monster) Testbool() bool { o := flatbuffers.UOffsetT(rcv._tab.Offset(34)) if o != 0 { - return rcv._tab.GetByte(o + rcv._tab.Pos) + return rcv._tab.GetBool(o + rcv._tab.Pos) } - return 0 + return false } -func (rcv *Monster) MutateTestbool(n byte) bool { - return rcv._tab.MutateByteSlot(34, n) +func (rcv *Monster) MutateTestbool(n bool) bool { + return rcv._tab.MutateBoolSlot(34, n) } func (rcv *Monster) Testhashs32Fnv1() int32 { @@ -349,13 +349,13 @@ func (rcv *Monster) MutateTesthashu64Fnv1a(n uint64) bool { return rcv._tab.MutateUint64Slot(50, n) } -func (rcv *Monster) Testarrayofbools(j int) byte { +func (rcv *Monster) Testarrayofbools(j int) bool { o := flatbuffers.UOffsetT(rcv._tab.Offset(52)) if o != 0 { a := rcv._tab.Vector(o) - return rcv._tab.GetByte(a + flatbuffers.UOffsetT(j*1)) + return rcv._tab.GetBool(a + flatbuffers.UOffsetT(j*1)) } - return 0 + return false } func (rcv *Monster) TestarrayofboolsLength() int { @@ -716,8 +716,8 @@ func MonsterStartTestnestedflatbufferVector(builder *flatbuffers.Builder, numEle func MonsterAddTestempty(builder *flatbuffers.Builder, testempty flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(14, flatbuffers.UOffsetT(testempty), 0) } -func MonsterAddTestbool(builder *flatbuffers.Builder, testbool byte) { - builder.PrependByteSlot(15, testbool, 0) +func MonsterAddTestbool(builder *flatbuffers.Builder, testbool bool) { + builder.PrependBoolSlot(15, testbool, false) } func MonsterAddTesthashs32Fnv1(builder *flatbuffers.Builder, testhashs32Fnv1 int32) { builder.PrependInt32Slot(16, testhashs32Fnv1, 0) diff --git a/tests/go_test.go b/tests/go_test.go index 6119be309..f93788097 100644 --- a/tests/go_test.go +++ b/tests/go_test.go @@ -160,6 +160,10 @@ func CheckReadBuffer(buf []byte, offset flatbuffers.UOffsetT, fail func(string, fail(FailString("color", example.ColorBlue, got)) } + if got := monster.Testbool(); true != got { + fail(FailString("testbool", true, got)) + } + // initialize a Vec3 from Pos() vec := new(example.Vec3) vec = monster.Pos(vec) @@ -317,6 +321,7 @@ func CheckMutateBuffer(org []byte, offset flatbuffers.UOffsetT, fail func(string testForOriginalValues := []testcase{ testcase{"Hp", func() bool { return monster.Hp() == 80 }}, testcase{"Mana", func() bool { return monster.Mana() == 150 }}, + testcase{"Testbool", func() bool { return monster.Testbool() == true }}, testcase{"Pos.X'", func() bool { return monster.Pos(nil).X() == float32(1.0) }}, testcase{"Pos.Y'", func() bool { return monster.Pos(nil).Y() == float32(2.0) }}, testcase{"Pos.Z'", func() bool { return monster.Pos(nil).Z() == float32(3.0) }}, @@ -329,6 +334,7 @@ func CheckMutateBuffer(org []byte, offset flatbuffers.UOffsetT, fail func(string testMutability := []testcase{ testcase{"Hp", func() bool { return monster.MutateHp(70) }}, testcase{"Mana", func() bool { return !monster.MutateMana(140) }}, + testcase{"Testbool", func() bool { return monster.MutateTestbool(false) }}, testcase{"Pos.X", func() bool { return monster.Pos(nil).MutateX(10.0) }}, testcase{"Pos.Y", func() bool { return monster.Pos(nil).MutateY(20.0) }}, testcase{"Pos.Z", func() bool { return monster.Pos(nil).MutateZ(30.0) }}, @@ -341,6 +347,7 @@ func CheckMutateBuffer(org []byte, offset flatbuffers.UOffsetT, fail func(string testForMutatedValues := []testcase{ testcase{"Hp", func() bool { return monster.Hp() == 70 }}, testcase{"Mana", func() bool { return monster.Mana() == 150 }}, + testcase{"Testbool", func() bool { return monster.Testbool() == false }}, testcase{"Pos.X'", func() bool { return monster.Pos(nil).X() == float32(10.0) }}, testcase{"Pos.Y'", func() bool { return monster.Pos(nil).Y() == float32(20.0) }}, testcase{"Pos.Z'", func() bool { return monster.Pos(nil).Z() == float32(30.0) }}, @@ -392,6 +399,7 @@ func CheckMutateBuffer(org []byte, offset flatbuffers.UOffsetT, fail func(string // any unnecessary changes to the buffer. monster = example.GetRootAsMonster(buf, offset) monster.MutateHp(80) + monster.MutateTestbool(true) monster.Pos(nil).MutateX(1.0) monster.Pos(nil).MutateY(2.0) monster.Pos(nil).MutateZ(3.0) @@ -1158,6 +1166,7 @@ func CheckGeneratedBuild(fail func(string, ...interface{})) ([]byte, flatbuffers example.MonsterAddHp(b, 80) example.MonsterAddName(b, str) + example.MonsterAddTestbool(b, true) example.MonsterAddInventory(b, inv) example.MonsterAddTestType(b, 1) example.MonsterAddTest(b, mon2) diff --git a/tests/monsterdata_test.golden b/tests/monsterdata_test.golden index 6def28f76..ab401544f 100644 --- a/tests/monsterdata_test.golden +++ b/tests/monsterdata_test.golden @@ -56,6 +56,7 @@ name: "Wilma" } ], + testbool: true, testhashs32_fnv1: -579221183, testhashu32_fnv1: 3715746113, testhashs64_fnv1: 7930699090847568257, diff --git a/tests/monsterdata_test.json b/tests/monsterdata_test.json index fb2d2448d..d3028b55a 100644 --- a/tests/monsterdata_test.json +++ b/tests/monsterdata_test.json @@ -66,6 +66,7 @@ testarrayofbools:[ true, false, true ], + testbool: true, testhashs32_fnv1: "This string is being hashed!", testhashu32_fnv1: "This string is being hashed!", testhashs64_fnv1: "This string is being hashed!", diff --git a/tests/monsterdata_test.mon b/tests/monsterdata_test.mon index 8cb9cafd80b02c9f4f0e8325a0c8113a330f88c9..3f8397236a1076d560a6b2da7c0728d8e9403fc8 100644 GIT binary patch delta 33 ncmX@We1Lg^k{}C%0fPyH1p@getEnemy(); $assert->Equal('Fred', $fred->getName()); - $assert->strictEqual($monster->GetTestbool(), false); + $assert->strictEqual($monster->GetTestbool(), true); } //function testUnicode(Assert $assert) {