mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-30 12:30:01 +00:00
Add key lookup support for tables in Go (#7644)
* Add support for key lookup for tables in Go * Run clang format * Run go fmt on tests * Remove TODO in tests * Update LookupByKey API * Update LookupByKey API * Don't use resolvePointer in expectEq * Use generated getters instead of reading values directly from buffer * Fix typo Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
@@ -186,9 +186,12 @@ func TestAll(t *testing.T) {
|
||||
// Check size-prefixed flatbuffers
|
||||
CheckSizePrefixedBuffer(t.Fatalf)
|
||||
|
||||
// Check that optional scalars work
|
||||
// Check that optional scalars works
|
||||
CheckOptionalScalars(t.Fatalf)
|
||||
|
||||
// Check that getting vector element by key works
|
||||
CheckByKey(t.Fatalf)
|
||||
|
||||
// If the filename of the FlatBuffers file generated by the Java test
|
||||
// is given, check that Go code can read it, and that Go code
|
||||
// generates an identical buffer when used to create the example data:
|
||||
@@ -2215,6 +2218,78 @@ func CheckOptionalScalars(fail func(string, ...interface{})) {
|
||||
expectEq("defaultEnum", obj.DefaultEnum, optional_scalars.OptionalByteTwo)
|
||||
}
|
||||
|
||||
func CheckByKey(fail func(string, ...interface{})) {
|
||||
expectEq := func(what string, a, b interface{}) {
|
||||
if a != b {
|
||||
fail(FailString("Lookup by key: "+what, b, a))
|
||||
}
|
||||
}
|
||||
|
||||
b := flatbuffers.NewBuilder(0)
|
||||
name := b.CreateString("Boss")
|
||||
|
||||
slime := &example.MonsterT{Name: "Slime"}
|
||||
pig := &example.MonsterT{Name: "Pig"}
|
||||
slimeBoss := &example.MonsterT{Name: "SlimeBoss"}
|
||||
mushroom := &example.MonsterT{Name: "Mushroom"}
|
||||
ironPig := &example.MonsterT{Name: "Iron Pig"}
|
||||
|
||||
monsterOffsets := make([]flatbuffers.UOffsetT, 5)
|
||||
monsterOffsets[0] = slime.Pack(b)
|
||||
monsterOffsets[1] = pig.Pack(b)
|
||||
monsterOffsets[2] = slimeBoss.Pack(b)
|
||||
monsterOffsets[3] = mushroom.Pack(b)
|
||||
monsterOffsets[4] = ironPig.Pack(b)
|
||||
testarrayoftables := b.CreateVectorOfSortedTables(monsterOffsets, example.MonsterKeyCompare)
|
||||
|
||||
str := &example.StatT{Id: "Strength", Count: 42}
|
||||
luk := &example.StatT{Id: "Luck", Count: 51}
|
||||
hp := &example.StatT{Id: "Health", Count: 12}
|
||||
// Test default count value of 0
|
||||
mp := &example.StatT{Id: "Mana"}
|
||||
|
||||
statOffsets := make([]flatbuffers.UOffsetT, 4)
|
||||
statOffsets[0] = str.Pack(b)
|
||||
statOffsets[1] = luk.Pack(b)
|
||||
statOffsets[2] = hp.Pack(b)
|
||||
statOffsets[3] = mp.Pack(b)
|
||||
scalarKeySortedTablesOffset := b.CreateVectorOfSortedTables(statOffsets, example.StatKeyCompare)
|
||||
|
||||
example.MonsterStart(b)
|
||||
example.MonsterAddName(b, name)
|
||||
example.MonsterAddTestarrayoftables(b, testarrayoftables)
|
||||
example.MonsterAddScalarKeySortedTables(b, scalarKeySortedTablesOffset)
|
||||
moff := example.MonsterEnd(b)
|
||||
b.Finish(moff)
|
||||
|
||||
monster := example.GetRootAsMonster(b.Bytes, b.Head())
|
||||
slimeMon := &example.Monster{}
|
||||
monster.TestarrayoftablesByKey(slimeMon, slime.Name)
|
||||
mushroomMon := &example.Monster{}
|
||||
monster.TestarrayoftablesByKey(mushroomMon, mushroom.Name)
|
||||
slimeBossMon := &example.Monster{}
|
||||
monster.TestarrayoftablesByKey(slimeBossMon, slimeBoss.Name)
|
||||
|
||||
strStat := &example.Stat{}
|
||||
monster.ScalarKeySortedTablesByKey(strStat, str.Count)
|
||||
lukStat := &example.Stat{}
|
||||
monster.ScalarKeySortedTablesByKey(lukStat, luk.Count)
|
||||
mpStat := &example.Stat{}
|
||||
monster.ScalarKeySortedTablesByKey(mpStat, mp.Count)
|
||||
|
||||
expectEq("Boss name", string(monster.Name()), "Boss")
|
||||
expectEq("Slime name", string(slimeMon.Name()), slime.Name)
|
||||
expectEq("Mushroom name", string(mushroomMon.Name()), mushroom.Name)
|
||||
expectEq("SlimeBoss name", string(slimeBossMon.Name()), slimeBoss.Name)
|
||||
expectEq("Strength Id", string(strStat.Id()), str.Id)
|
||||
expectEq("Strength Count", strStat.Count(), str.Count)
|
||||
expectEq("Luck Id", string(lukStat.Id()), luk.Id)
|
||||
expectEq("Luck Count", lukStat.Count(), luk.Count)
|
||||
expectEq("Mana Id", string(mpStat.Id()), mp.Id)
|
||||
// Use default count value as key
|
||||
expectEq("Mana Count", mpStat.Count(), uint16(0))
|
||||
}
|
||||
|
||||
// BenchmarkVtableDeduplication measures the speed of vtable deduplication
|
||||
// by creating prePop vtables, then populating b.N objects with a
|
||||
// different single vtable.
|
||||
|
||||
Reference in New Issue
Block a user