mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-26 03:32:40 +00:00
[Go] Object API support (#5339)
* start * works for current usages! * unpack: vector of struct * optimize byte slice * support nested struct * support null table * support struct * support union * update generated code * grumble * fix compiler warning * update generated code * wrap type in namespace * bug * wrap in namespace * enum byte arrays * generate struct for unions * basic testing * remove branching * fix assert * pack vector of fixed structs correctly * omit null vectors * Refactor Union Pack and UnPack methods Remove append usage to increase code efficiency when dealing with large vectors * generate goldens
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
521e255ad9
commit
32254b7acd
@@ -90,6 +90,7 @@ func TestAll(t *testing.T) {
|
||||
// generated Go code:
|
||||
CheckReadBuffer(generated, off, t.Fatalf)
|
||||
CheckMutateBuffer(generated, off, t.Fatalf)
|
||||
CheckObjectAPI(generated, off, t.Fatalf)
|
||||
|
||||
// Verify that the buffer generated by C++ code is readable by the
|
||||
// generated Go code:
|
||||
@@ -99,6 +100,7 @@ func TestAll(t *testing.T) {
|
||||
}
|
||||
CheckReadBuffer(monsterDataCpp, 0, t.Fatalf)
|
||||
CheckMutateBuffer(monsterDataCpp, 0, t.Fatalf)
|
||||
CheckObjectAPI(monsterDataCpp, 0, t.Fatalf)
|
||||
|
||||
// Verify that vtables are deduplicated when written:
|
||||
CheckVtableDeduplication(t.Fatalf)
|
||||
@@ -448,6 +450,26 @@ func CheckMutateBuffer(org []byte, offset flatbuffers.UOffsetT, fail func(string
|
||||
}
|
||||
}
|
||||
|
||||
func CheckObjectAPI(buf []byte, offset flatbuffers.UOffsetT, fail func(string, ...interface{})) {
|
||||
monster := example.GetRootAsMonster(buf, offset).UnPack()
|
||||
|
||||
if got := monster.Hp; 80 != got {
|
||||
fail(FailString("hp", 80, got))
|
||||
}
|
||||
|
||||
// default
|
||||
if got := monster.Mana; 150 != got {
|
||||
fail(FailString("mana", 150, got))
|
||||
}
|
||||
|
||||
builder := flatbuffers.NewBuilder(0)
|
||||
builder.Finish(example.MonsterPack(builder, monster))
|
||||
monster2 := example.GetRootAsMonster(builder.FinishedBytes(), 0).UnPack()
|
||||
if !reflect.DeepEqual(monster, monster2) {
|
||||
fail(FailString("Pack/Unpack()", monster, monster2))
|
||||
}
|
||||
}
|
||||
|
||||
// Low level stress/fuzz test: serialize/deserialize a variety of
|
||||
// different kinds of data in different combinations
|
||||
func checkFuzz(fuzzFields, fuzzObjects int, fail func(string, ...interface{})) {
|
||||
|
||||
Reference in New Issue
Block a user