Fix go generator undefined Package name, also throwing exception (#7632)

* Fix go generator undefined Package, also throw exception in specific examples.

* Add test for go generator import problem

* Add new version of generated go file. Fix conflict.

* Add executable permission to generate_code.py script.

* Improve test quality, remove unwanted generated files, better naming

* Fix comments

* clang format

Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
Saman
2022-11-22 16:21:25 -05:00
committed by GitHub
parent eead6c6219
commit 1cba8b2b49
9 changed files with 264 additions and 39 deletions

View File

@@ -20,26 +20,18 @@ go_path=${test_dir}/go_gen
go_src=${go_path}/src
# Emit Go code for the example schemas in the test dir:
../flatc -g --gen-object-api -I include_test monster_test.fbs optional_scalars.fbs
../flatc -g --gen-object-api -I include_test -o ${go_src} monster_test.fbs optional_scalars.fbs
../flatc -g --gen-object-api -I include_test/sub -o ${go_src} include_test/order.fbs
../flatc -g --gen-object-api -o ${go_src}/Pizza include_test/sub/no_namespace.fbs
# Go requires a particular layout of files in order to link multiple packages.
# Copy flatbuffer Go files to their own package directories to compile the
# test binary:
mkdir -p ${go_src}/MyGame/Example
mkdir -p ${go_src}/MyGame/Example2
mkdir -p ${go_src}/github.com/google/flatbuffers/go
mkdir -p ${go_src}/flatbuffers_test
mkdir -p ${go_src}/optional_scalars
cp -a MyGame/*.go ./go_gen/src/MyGame/
cp -a MyGame/Example/*.go ./go_gen/src/MyGame/Example/
cp -a MyGame/Example2/*.go ./go_gen/src/MyGame/Example2/
# do not compile the gRPC generated files, which are not tested by go_test.go
# below, but have their own test.
rm ./go_gen/src/MyGame/Example/*_grpc.go
cp -a ../go/* ./go_gen/src/github.com/google/flatbuffers/go
cp -a ./go_test.go ./go_gen/src/flatbuffers_test/
cp -a optional_scalars/*.go ./go_gen/src/optional_scalars
# https://stackoverflow.com/a/63545857/7024978
# We need to turn off go modules for this script
@@ -72,7 +64,7 @@ else
exit 1
fi
NOT_FMT_FILES=$(gofmt -l MyGame)
NOT_FMT_FILES=$(gofmt -l .)
if [[ ${NOT_FMT_FILES} != "" ]]; then
echo "These files are not well gofmt'ed:"
echo

78
tests/Pizza.go Normal file
View File

@@ -0,0 +1,78 @@
// Code generated by the FlatBuffers compiler. DO NOT EDIT.
package Pizza
import (
flatbuffers "github.com/google/flatbuffers/go"
)
type PizzaT struct {
Size int32 `json:"size"`
}
func (t *PizzaT) Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
if t == nil { return 0 }
PizzaStart(builder)
PizzaAddSize(builder, t.Size)
return PizzaEnd(builder)
}
func (rcv *Pizza) UnPackTo(t *PizzaT) {
t.Size = rcv.Size()
}
func (rcv *Pizza) UnPack() *PizzaT {
if rcv == nil { return nil }
t := &PizzaT{}
rcv.UnPackTo(t)
return t
}
type Pizza struct {
_tab flatbuffers.Table
}
func GetRootAsPizza(buf []byte, offset flatbuffers.UOffsetT) *Pizza {
n := flatbuffers.GetUOffsetT(buf[offset:])
x := &Pizza{}
x.Init(buf, n+offset)
return x
}
func GetSizePrefixedRootAsPizza(buf []byte, offset flatbuffers.UOffsetT) *Pizza {
n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:])
x := &Pizza{}
x.Init(buf, n+offset+flatbuffers.SizeUint32)
return x
}
func (rcv *Pizza) Init(buf []byte, i flatbuffers.UOffsetT) {
rcv._tab.Bytes = buf
rcv._tab.Pos = i
}
func (rcv *Pizza) Table() flatbuffers.Table {
return rcv._tab
}
func (rcv *Pizza) Size() int32 {
o := flatbuffers.UOffsetT(rcv._tab.Offset(4))
if o != 0 {
return rcv._tab.GetInt32(o + rcv._tab.Pos)
}
return 0
}
func (rcv *Pizza) MutateSize(n int32) bool {
return rcv._tab.MutateInt32Slot(4, n)
}
func PizzaStart(builder *flatbuffers.Builder) {
builder.StartObject(1)
}
func PizzaAddSize(builder *flatbuffers.Builder, size int32) {
builder.PrependInt32Slot(0, size, 0)
}
func PizzaEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
return builder.EndObject()
}

View File

@@ -17,6 +17,8 @@
package main
import (
order "order"
pizza "Pizza"
mygame "MyGame" // refers to generated code
example "MyGame/Example" // refers to generated code
"encoding/json"
@@ -98,6 +100,24 @@ func TestTextParsing(t *testing.T) {
}
}
func CheckNoNamespaceImport(fail func(string, ...interface{})) {
const size = 13
// Order a pizza with specific size
builder := flatbuffers.NewBuilder(0)
ordered_pizza := pizza.PizzaT{Size: size}
food := order.FoodT{Pizza: &ordered_pizza}
builder.Finish(food.Pack(builder))
// Receive order
received_food := order.GetRootAsFood(builder.FinishedBytes(), 0)
received_pizza := received_food.Pizza(nil).UnPack()
// Check if received pizza is equal to ordered pizza
if !reflect.DeepEqual(ordered_pizza, *received_pizza) {
fail(FailString("no namespace import", ordered_pizza, received_pizza))
}
}
// TestAll runs all checks, failing if any errors occur.
func TestAll(t *testing.T) {
// Verify that the Go FlatBuffers runtime library generates the
@@ -160,6 +180,9 @@ func TestAll(t *testing.T) {
// Check a parent namespace import
CheckParentNamespace(t.Fatalf)
// Check a no namespace import
CheckNoNamespaceImport(t.Fatalf)
// Check size-prefixed flatbuffers
CheckSizePrefixedBuffer(t.Fatalf)

View File

@@ -0,0 +1,8 @@
include "no_namespace.fbs";
namespace order;
table Food {
pizza: Pizza (id: 0);
pizza_test:Pizza(id:1);
}

View File

@@ -0,0 +1,3 @@
table Pizza {
size: int;
}

102
tests/order/Food.go Normal file
View File

@@ -0,0 +1,102 @@
// Code generated by the FlatBuffers compiler. DO NOT EDIT.
package order
import (
flatbuffers "github.com/google/flatbuffers/go"
Pizza "Pizza"
)
type FoodT struct {
Pizza *Pizza.PizzaT `json:"pizza"`
PizzaTest *Pizza.PizzaT `json:"pizza_test"`
}
func (t *FoodT) Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
if t == nil { return 0 }
pizzaOffset := t.Pizza.Pack(builder)
pizzaTestOffset := t.PizzaTest.Pack(builder)
FoodStart(builder)
FoodAddPizza(builder, pizzaOffset)
FoodAddPizzaTest(builder, pizzaTestOffset)
return FoodEnd(builder)
}
func (rcv *Food) UnPackTo(t *FoodT) {
t.Pizza = rcv.Pizza(nil).UnPack()
t.PizzaTest = rcv.PizzaTest(nil).UnPack()
}
func (rcv *Food) UnPack() *FoodT {
if rcv == nil { return nil }
t := &FoodT{}
rcv.UnPackTo(t)
return t
}
type Food struct {
_tab flatbuffers.Table
}
func GetRootAsFood(buf []byte, offset flatbuffers.UOffsetT) *Food {
n := flatbuffers.GetUOffsetT(buf[offset:])
x := &Food{}
x.Init(buf, n+offset)
return x
}
func GetSizePrefixedRootAsFood(buf []byte, offset flatbuffers.UOffsetT) *Food {
n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:])
x := &Food{}
x.Init(buf, n+offset+flatbuffers.SizeUint32)
return x
}
func (rcv *Food) Init(buf []byte, i flatbuffers.UOffsetT) {
rcv._tab.Bytes = buf
rcv._tab.Pos = i
}
func (rcv *Food) Table() flatbuffers.Table {
return rcv._tab
}
func (rcv *Food) Pizza(obj *Pizza.Pizza) *Pizza.Pizza {
o := flatbuffers.UOffsetT(rcv._tab.Offset(4))
if o != 0 {
x := rcv._tab.Indirect(o + rcv._tab.Pos)
if obj == nil {
obj = new(Pizza.Pizza)
}
obj.Init(rcv._tab.Bytes, x)
return obj
}
return nil
}
func (rcv *Food) PizzaTest(obj *Pizza.Pizza) *Pizza.Pizza {
o := flatbuffers.UOffsetT(rcv._tab.Offset(6))
if o != 0 {
x := rcv._tab.Indirect(o + rcv._tab.Pos)
if obj == nil {
obj = new(Pizza.Pizza)
}
obj.Init(rcv._tab.Bytes, x)
return obj
}
return nil
}
func FoodStart(builder *flatbuffers.Builder) {
builder.StartObject(2)
}
func FoodAddPizza(builder *flatbuffers.Builder, pizza flatbuffers.UOffsetT) {
builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(pizza), 0)
}
func FoodAddPizzaTest(builder *flatbuffers.Builder, pizzaTest flatbuffers.UOffsetT) {
builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(pizzaTest), 0)
}
func FoodEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
return builder.EndObject()
}