From ef03cf46e47a74abd3ee0a4d7942ce46b8b6ea08 Mon Sep 17 00:00:00 2001 From: rw Date: Tue, 23 Sep 2014 13:55:05 -0700 Subject: [PATCH] Fix vector of strings for Go Mirrors Java patch 39d4b7e2bf Change-Id: If8d0ab29f6eb508a156d468aeb0a9d5410149e53 --- go/table.go | 1 - src/idl_gen_go.cpp | 2 +- tests/MyGame/Example/Monster.go | 2 +- tests/go_test.go | 21 +++++++++++++++++++++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/go/table.go b/go/table.go index 21c112181..dc5c39908 100644 --- a/go/table.go +++ b/go/table.go @@ -26,7 +26,6 @@ func (t *Table) Indirect(off UOffsetT) UOffsetT { // String gets a string from data stored inside the flatbuffer. func (t *Table) String(off UOffsetT) string { - off += t.Pos off += GetUOffsetT(t.Bytes[off:]) start := off + UOffsetT(SizeUOffsetT) length := GetUOffsetT(t.Bytes[off:]) diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index b71c09b7a..8b7afde4d 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -235,7 +235,7 @@ static void GetStringField(const StructDef &struct_def, code += " " + MakeCamel(field.name); code += "() " + TypeName(field) + " "; code += OffsetPrefix(field) + "\t\treturn " + GenGetter(field.value.type); - code += "(o)\n\t}\n\treturn \"\"\n"; + code += "(o + rcv._tab.Pos)\n\t}\n\treturn \"\"\n"; code += "}\n\n"; } diff --git a/tests/MyGame/Example/Monster.go b/tests/MyGame/Example/Monster.go index 16b1053ef..688553c5a 100644 --- a/tests/MyGame/Example/Monster.go +++ b/tests/MyGame/Example/Monster.go @@ -53,7 +53,7 @@ func (rcv *Monster) Hp() int16 { func (rcv *Monster) Name() string { o := flatbuffers.UOffsetT(rcv._tab.Offset(10)) if o != 0 { - return rcv._tab.String(o) + return rcv._tab.String(o + rcv._tab.Pos) } return "" } diff --git a/tests/go_test.go b/tests/go_test.go index 313ee6401..a9ad01d37 100644 --- a/tests/go_test.go +++ b/tests/go_test.go @@ -235,6 +235,18 @@ func CheckReadBuffer(buf []byte, offset flatbuffers.UOffsetT, fail func(string, if 100 != sum { fail(FailString("test0 and test1 sum", 100, sum)) } + + if got := monster.TestarrayofstringLength(); 2 != got { + fail(FailString("Testarrayofstring length", 2, got)) + } + + if got := monster.Testarrayofstring(0); "test1" != got { + fail(FailString("Testarrayofstring(0)", "test1", got)) + } + + if got := monster.Testarrayofstring(1); "test2" != got { + fail(FailString("Testarrayofstring(1)", "test2", got)) + } } // Low level stress/fuzz test: serialize/deserialize a variety of @@ -891,6 +903,9 @@ func CheckManualBuild(fail func(string, ...interface{})) ([]byte, flatbuffers.UO func CheckGeneratedBuild(fail func(string, ...interface{})) ([]byte, flatbuffers.UOffsetT) { b := flatbuffers.NewBuilder(0) str := b.CreateString("MyMonster") + test1 := b.CreateString("test1") + test2 := b.CreateString("test2") + example.MonsterStartInventoryVector(b, 5) b.PrependByte(4) b.PrependByte(3) @@ -908,6 +923,11 @@ func CheckGeneratedBuild(fail func(string, ...interface{})) ([]byte, flatbuffers example.CreateTest(b, 30, 40) test4 := b.EndVector(2) + example.MonsterStartTestarrayofstringVector(b, 2) + b.PrependUOffsetT(test2) + b.PrependUOffsetT(test1) + testArrayOfString := b.EndVector(2) + example.MonsterStart(b) pos := example.CreateVec3(b, 1.0, 2.0, 3.0, 3.0, 4, 5, 6) @@ -919,6 +939,7 @@ func CheckGeneratedBuild(fail func(string, ...interface{})) ([]byte, flatbuffers example.MonsterAddTestType(b, 1) example.MonsterAddTest(b, mon2) example.MonsterAddTest4(b, test4) + example.MonsterAddTestarrayofstring(b, testArrayOfString) mon := example.MonsterEnd(b) b.Finish(mon)