Commit 76d31e1b authored by kostya-sh's avatar kostya-sh Committed by Robert

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
parent 20396a17
...@@ -49,7 +49,7 @@ namespace flatbuffers { ...@@ -49,7 +49,7 @@ namespace flatbuffers {
#define FLATBUFFERS_GEN_TYPES_SCALAR(TD) \ #define FLATBUFFERS_GEN_TYPES_SCALAR(TD) \
TD(NONE, "", uint8_t, byte, byte, byte, uint8, u8) \ TD(NONE, "", uint8_t, byte, byte, byte, uint8, u8) \
TD(UTYPE, "", uint8_t, byte, byte, byte, uint8, u8) /* begin scalar/int */ \ 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(CHAR, "byte", int8_t, byte, int8, sbyte, int8, i8) \
TD(UCHAR, "ubyte", uint8_t, byte, byte, byte, uint8, u8) \ TD(UCHAR, "ubyte", uint8_t, byte, byte, byte, uint8, u8) \
TD(SHORT, "short", int16_t, short, int16, short, int16, i16) \ TD(SHORT, "short", int16_t, short, int16, short, int16, i16) \
......
...@@ -52,6 +52,7 @@ static const char * const g_golang_keywords[] = { ...@@ -52,6 +52,7 @@ static const char * const g_golang_keywords[] = {
static std::string GenGetter(const Type &type); static std::string GenGetter(const Type &type);
static std::string GenMethod(const FieldDef &field); static std::string GenMethod(const FieldDef &field);
static std::string GenConstant(const FieldDef &field);
static void GenStructBuilder(const StructDef &struct_def, static void GenStructBuilder(const StructDef &struct_def,
std::string *code_ptr); std::string *code_ptr);
static void GenReceiver(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, ...@@ -245,7 +246,7 @@ static void GetScalarFieldOfTable(const StructDef &struct_def,
code += "() " + TypeName(field) + " "; code += "() " + TypeName(field) + " ";
code += OffsetPrefix(field) + "\t\treturn " + getter; code += OffsetPrefix(field) + "\t\treturn " + getter;
code += "(o + rcv._tab.Pos)\n\t}\n"; code += "(o + rcv._tab.Pos)\n\t}\n";
code += "\treturn " + field.value.constant + "\n"; code += "\treturn " + GenConstant(field) + "\n";
code += "}\n\n"; code += "}\n\n";
} }
...@@ -361,6 +362,8 @@ static void GetMemberOfVectorOfNonStruct(const StructDef &struct_def, ...@@ -361,6 +362,8 @@ static void GetMemberOfVectorOfNonStruct(const StructDef &struct_def,
code += "\t}\n"; code += "\t}\n";
if (vectortype.base_type == BASE_TYPE_STRING) { if (vectortype.base_type == BASE_TYPE_STRING) {
code += "\treturn nil\n"; code += "\treturn nil\n";
} else if (vectortype.base_type == BASE_TYPE_BOOL) {
code += "\treturn false\n";
} else { } else {
code += "\treturn 0\n"; code += "\treturn 0\n";
} }
...@@ -471,7 +474,7 @@ static void BuildFieldOfTable(const StructDef &struct_def, ...@@ -471,7 +474,7 @@ static void BuildFieldOfTable(const StructDef &struct_def,
} else { } else {
code += GoIdentity(field.name); code += GoIdentity(field.name);
} }
code += ", " + field.value.constant; code += ", " + GenConstant(field);
code += ")\n}\n"; code += ")\n}\n";
} }
...@@ -721,6 +724,13 @@ static std::string TypeName(const FieldDef &field) { ...@@ -721,6 +724,13 @@ static std::string TypeName(const FieldDef &field) {
return GenTypeGet(field.value.type); 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. // Create a struct with a builder and the struct's arguments.
static void GenStructBuilder(const StructDef &struct_def, static void GenStructBuilder(const StructDef &struct_def,
std::string *code_ptr) { std::string *code_ptr) {
......
...@@ -97,7 +97,7 @@ namespace FlatBuffers.Test ...@@ -97,7 +97,7 @@ namespace FlatBuffers.Test
Monster.AddTest(fbb, mon2.Value); Monster.AddTest(fbb, mon2.Value);
Monster.AddTest4(fbb, test4); Monster.AddTest4(fbb, test4);
Monster.AddTestarrayofstring(fbb, testArrayOfString); Monster.AddTestarrayofstring(fbb, testArrayOfString);
Monster.AddTestbool(fbb, false); Monster.AddTestbool(fbb, true);
Monster.AddTestarrayoftables(fbb, sortMons); Monster.AddTestarrayoftables(fbb, sortMons);
var mon = Monster.EndMonster(fbb); var mon = Monster.EndMonster(fbb);
...@@ -246,7 +246,7 @@ namespace FlatBuffers.Test ...@@ -246,7 +246,7 @@ namespace FlatBuffers.Test
Assert.AreEqual("test1", monster.Testarrayofstring(0)); Assert.AreEqual("test1", monster.Testarrayofstring(0));
Assert.AreEqual("test2", monster.Testarrayofstring(1)); Assert.AreEqual("test2", monster.Testarrayofstring(1));
Assert.AreEqual(false, monster.Testbool); Assert.AreEqual(true, monster.Testbool);
#if ENABLE_SPAN_T #if ENABLE_SPAN_T
var nameBytes = monster.GetNameBytes(); var nameBytes = monster.GetNameBytes();
......
...@@ -52,7 +52,7 @@ function main() { ...@@ -52,7 +52,7 @@ function main() {
MyGame.Example.Monster.addTest(fbb, mon2); MyGame.Example.Monster.addTest(fbb, mon2);
MyGame.Example.Monster.addTest4(fbb, test4); MyGame.Example.Monster.addTest4(fbb, test4);
MyGame.Example.Monster.addTestarrayofstring(fbb, testArrayOfString); 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); var mon = MyGame.Example.Monster.endMonster(fbb);
MyGame.Example.Monster.finishMonsterBuffer(fbb, mon); MyGame.Example.Monster.finishMonsterBuffer(fbb, mon);
...@@ -140,7 +140,7 @@ function testBuffer(bb) { ...@@ -140,7 +140,7 @@ function testBuffer(bb) {
assert.strictEqual(monster.testarrayofstring(0), 'test1'); assert.strictEqual(monster.testarrayofstring(0), 'test1');
assert.strictEqual(monster.testarrayofstring(1), 'test2'); assert.strictEqual(monster.testarrayofstring(1), 'test2');
assert.strictEqual(monster.testbool(), false); assert.strictEqual(monster.testbool(), true);
} }
function test64bit() { function test64bit() {
......
...@@ -130,7 +130,7 @@ class JavaTest { ...@@ -130,7 +130,7 @@ class JavaTest {
TestEq(monster.testarrayofstring(0),"test1"); TestEq(monster.testarrayofstring(0),"test1");
TestEq(monster.testarrayofstring(1),"test2"); 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 // this method checks additional fields not present in the binary buffer read from file
...@@ -319,7 +319,7 @@ class JavaTest { ...@@ -319,7 +319,7 @@ class JavaTest {
Monster.addTest(fbb, mon2); Monster.addTest(fbb, mon2);
Monster.addTest4(fbb, test4); Monster.addTest4(fbb, test4);
Monster.addTestarrayofstring(fbb, testArrayOfString); Monster.addTestarrayofstring(fbb, testArrayOfString);
Monster.addTestbool(fbb, false); Monster.addTestbool(fbb, true);
Monster.addTesthashu32Fnv1(fbb, Integer.MAX_VALUE + 1L); Monster.addTesthashu32Fnv1(fbb, Integer.MAX_VALUE + 1L);
Monster.addTestarrayoftables(fbb, sortMons); Monster.addTestarrayoftables(fbb, sortMons);
int mon = Monster.endMonster(fbb); int mon = Monster.endMonster(fbb);
......
...@@ -241,16 +241,16 @@ func (rcv *Monster) Testempty(obj *Stat) *Stat { ...@@ -241,16 +241,16 @@ func (rcv *Monster) Testempty(obj *Stat) *Stat {
return nil return nil
} }
func (rcv *Monster) Testbool() byte { func (rcv *Monster) Testbool() bool {
o := flatbuffers.UOffsetT(rcv._tab.Offset(34)) o := flatbuffers.UOffsetT(rcv._tab.Offset(34))
if o != 0 { 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 { func (rcv *Monster) MutateTestbool(n bool) bool {
return rcv._tab.MutateByteSlot(34, n) return rcv._tab.MutateBoolSlot(34, n)
} }
func (rcv *Monster) Testhashs32Fnv1() int32 { func (rcv *Monster) Testhashs32Fnv1() int32 {
...@@ -349,13 +349,13 @@ func (rcv *Monster) MutateTesthashu64Fnv1a(n uint64) bool { ...@@ -349,13 +349,13 @@ func (rcv *Monster) MutateTesthashu64Fnv1a(n uint64) bool {
return rcv._tab.MutateUint64Slot(50, n) 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)) o := flatbuffers.UOffsetT(rcv._tab.Offset(52))
if o != 0 { if o != 0 {
a := rcv._tab.Vector(o) 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 { func (rcv *Monster) TestarrayofboolsLength() int {
...@@ -716,8 +716,8 @@ func MonsterStartTestnestedflatbufferVector(builder *flatbuffers.Builder, numEle ...@@ -716,8 +716,8 @@ func MonsterStartTestnestedflatbufferVector(builder *flatbuffers.Builder, numEle
func MonsterAddTestempty(builder *flatbuffers.Builder, testempty flatbuffers.UOffsetT) { func MonsterAddTestempty(builder *flatbuffers.Builder, testempty flatbuffers.UOffsetT) {
builder.PrependUOffsetTSlot(14, flatbuffers.UOffsetT(testempty), 0) builder.PrependUOffsetTSlot(14, flatbuffers.UOffsetT(testempty), 0)
} }
func MonsterAddTestbool(builder *flatbuffers.Builder, testbool byte) { func MonsterAddTestbool(builder *flatbuffers.Builder, testbool bool) {
builder.PrependByteSlot(15, testbool, 0) builder.PrependBoolSlot(15, testbool, false)
} }
func MonsterAddTesthashs32Fnv1(builder *flatbuffers.Builder, testhashs32Fnv1 int32) { func MonsterAddTesthashs32Fnv1(builder *flatbuffers.Builder, testhashs32Fnv1 int32) {
builder.PrependInt32Slot(16, testhashs32Fnv1, 0) builder.PrependInt32Slot(16, testhashs32Fnv1, 0)
......
...@@ -160,6 +160,10 @@ func CheckReadBuffer(buf []byte, offset flatbuffers.UOffsetT, fail func(string, ...@@ -160,6 +160,10 @@ func CheckReadBuffer(buf []byte, offset flatbuffers.UOffsetT, fail func(string,
fail(FailString("color", example.ColorBlue, got)) fail(FailString("color", example.ColorBlue, got))
} }
if got := monster.Testbool(); true != got {
fail(FailString("testbool", true, got))
}
// initialize a Vec3 from Pos() // initialize a Vec3 from Pos()
vec := new(example.Vec3) vec := new(example.Vec3)
vec = monster.Pos(vec) vec = monster.Pos(vec)
...@@ -317,6 +321,7 @@ func CheckMutateBuffer(org []byte, offset flatbuffers.UOffsetT, fail func(string ...@@ -317,6 +321,7 @@ func CheckMutateBuffer(org []byte, offset flatbuffers.UOffsetT, fail func(string
testForOriginalValues := []testcase{ testForOriginalValues := []testcase{
testcase{"Hp", func() bool { return monster.Hp() == 80 }}, testcase{"Hp", func() bool { return monster.Hp() == 80 }},
testcase{"Mana", func() bool { return monster.Mana() == 150 }}, 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.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.Y'", func() bool { return monster.Pos(nil).Y() == float32(2.0) }},
testcase{"Pos.Z'", func() bool { return monster.Pos(nil).Z() == float32(3.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 ...@@ -329,6 +334,7 @@ func CheckMutateBuffer(org []byte, offset flatbuffers.UOffsetT, fail func(string
testMutability := []testcase{ testMutability := []testcase{
testcase{"Hp", func() bool { return monster.MutateHp(70) }}, testcase{"Hp", func() bool { return monster.MutateHp(70) }},
testcase{"Mana", func() bool { return !monster.MutateMana(140) }}, 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.X", func() bool { return monster.Pos(nil).MutateX(10.0) }},
testcase{"Pos.Y", func() bool { return monster.Pos(nil).MutateY(20.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) }}, 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 ...@@ -341,6 +347,7 @@ func CheckMutateBuffer(org []byte, offset flatbuffers.UOffsetT, fail func(string
testForMutatedValues := []testcase{ testForMutatedValues := []testcase{
testcase{"Hp", func() bool { return monster.Hp() == 70 }}, testcase{"Hp", func() bool { return monster.Hp() == 70 }},
testcase{"Mana", func() bool { return monster.Mana() == 150 }}, 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.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.Y'", func() bool { return monster.Pos(nil).Y() == float32(20.0) }},
testcase{"Pos.Z'", func() bool { return monster.Pos(nil).Z() == float32(30.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 ...@@ -392,6 +399,7 @@ func CheckMutateBuffer(org []byte, offset flatbuffers.UOffsetT, fail func(string
// any unnecessary changes to the buffer. // any unnecessary changes to the buffer.
monster = example.GetRootAsMonster(buf, offset) monster = example.GetRootAsMonster(buf, offset)
monster.MutateHp(80) monster.MutateHp(80)
monster.MutateTestbool(true)
monster.Pos(nil).MutateX(1.0) monster.Pos(nil).MutateX(1.0)
monster.Pos(nil).MutateY(2.0) monster.Pos(nil).MutateY(2.0)
monster.Pos(nil).MutateZ(3.0) monster.Pos(nil).MutateZ(3.0)
...@@ -1158,6 +1166,7 @@ func CheckGeneratedBuild(fail func(string, ...interface{})) ([]byte, flatbuffers ...@@ -1158,6 +1166,7 @@ func CheckGeneratedBuild(fail func(string, ...interface{})) ([]byte, flatbuffers
example.MonsterAddHp(b, 80) example.MonsterAddHp(b, 80)
example.MonsterAddName(b, str) example.MonsterAddName(b, str)
example.MonsterAddTestbool(b, true)
example.MonsterAddInventory(b, inv) example.MonsterAddInventory(b, inv)
example.MonsterAddTestType(b, 1) example.MonsterAddTestType(b, 1)
example.MonsterAddTest(b, mon2) example.MonsterAddTest(b, mon2)
......
...@@ -56,6 +56,7 @@ ...@@ -56,6 +56,7 @@
name: "Wilma" name: "Wilma"
} }
], ],
testbool: true,
testhashs32_fnv1: -579221183, testhashs32_fnv1: -579221183,
testhashu32_fnv1: 3715746113, testhashu32_fnv1: 3715746113,
testhashs64_fnv1: 7930699090847568257, testhashs64_fnv1: 7930699090847568257,
......
...@@ -66,6 +66,7 @@ ...@@ -66,6 +66,7 @@
testarrayofbools:[ testarrayofbools:[
true, false, true true, false, true
], ],
testbool: true,
testhashs32_fnv1: "This string is being hashed!", testhashs32_fnv1: "This string is being hashed!",
testhashu32_fnv1: "This string is being hashed!", testhashu32_fnv1: "This string is being hashed!",
testhashs64_fnv1: "This string is being hashed!", testhashs64_fnv1: "This string is being hashed!",
......
...@@ -67,7 +67,7 @@ function main() ...@@ -67,7 +67,7 @@ function main()
\MyGame\Example\Monster::AddTest4($fbb, $test4); \MyGame\Example\Monster::AddTest4($fbb, $test4);
\MyGame\Example\Monster::AddTestarrayofstring($fbb, $testArrayOfString); \MyGame\Example\Monster::AddTestarrayofstring($fbb, $testArrayOfString);
\MyGame\Example\Monster::AddEnemy($fbb, $enemy); \MyGame\Example\Monster::AddEnemy($fbb, $enemy);
\MyGame\Example\Monster::AddTestbool($fbb, false); \MyGame\Example\Monster::AddTestbool($fbb, true);
$mon = \MyGame\Example\Monster::EndMonster($fbb); $mon = \MyGame\Example\Monster::EndMonster($fbb);
\MyGame\Example\Monster::FinishMonsterBuffer($fbb, $mon); \MyGame\Example\Monster::FinishMonsterBuffer($fbb, $mon);
...@@ -142,7 +142,7 @@ function test_buffer(Assert $assert, Google\FlatBuffers\ByteBuffer $bb) { ...@@ -142,7 +142,7 @@ function test_buffer(Assert $assert, Google\FlatBuffers\ByteBuffer $bb) {
$fred = $monster->getEnemy(); $fred = $monster->getEnemy();
$assert->Equal('Fred', $fred->getName()); $assert->Equal('Fred', $fred->getName());
$assert->strictEqual($monster->GetTestbool(), false); $assert->strictEqual($monster->GetTestbool(), true);
} }
//function testUnicode(Assert $assert) { //function testUnicode(Assert $assert) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment