forked from BigfootDev/flatbuffers
Resolve Go fmt and vet warnings (#4134)
* Resolve Go fmt and vet warnings. * Undo generated code gofmt.
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
e2373668d9
commit
b55f18649a
@@ -1,7 +1,7 @@
|
|||||||
package flatbuffers
|
package flatbuffers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
@@ -19,147 +19,147 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
flatbuffers "github.com/google/flatbuffers/go"
|
sample "MyGame/Sample"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
flatbuffers "github.com/google/flatbuffers/go"
|
||||||
sample "MyGame/Sample"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Example how to use Flatbuffers to create and read binary buffers.
|
// Example how to use Flatbuffers to create and read binary buffers.
|
||||||
func main() {
|
func main() {
|
||||||
builder := flatbuffers.NewBuilder(0)
|
builder := flatbuffers.NewBuilder(0)
|
||||||
|
|
||||||
// Create some weapons for our Monster ("Sword" and "Axe").
|
// Create some weapons for our Monster ("Sword" and "Axe").
|
||||||
weaponOne := builder.CreateString("Sword")
|
weaponOne := builder.CreateString("Sword")
|
||||||
weaponTwo := builder.CreateString("Axe")
|
weaponTwo := builder.CreateString("Axe")
|
||||||
|
|
||||||
sample.WeaponStart(builder)
|
sample.WeaponStart(builder)
|
||||||
sample.WeaponAddName(builder, weaponOne)
|
sample.WeaponAddName(builder, weaponOne)
|
||||||
sample.WeaponAddDamage(builder, 3)
|
sample.WeaponAddDamage(builder, 3)
|
||||||
sword := sample.WeaponEnd(builder)
|
sword := sample.WeaponEnd(builder)
|
||||||
|
|
||||||
sample.WeaponStart(builder)
|
sample.WeaponStart(builder)
|
||||||
sample.WeaponAddName(builder, weaponTwo)
|
sample.WeaponAddName(builder, weaponTwo)
|
||||||
sample.WeaponAddDamage(builder, 5)
|
sample.WeaponAddDamage(builder, 5)
|
||||||
axe := sample.WeaponEnd(builder)
|
axe := sample.WeaponEnd(builder)
|
||||||
|
|
||||||
// Serialize the FlatBuffer data.
|
// Serialize the FlatBuffer data.
|
||||||
name := builder.CreateString("Orc")
|
name := builder.CreateString("Orc")
|
||||||
|
|
||||||
sample.MonsterStartInventoryVector(builder, 10)
|
sample.MonsterStartInventoryVector(builder, 10)
|
||||||
// Note: Since we prepend the bytes, this loop iterates in reverse.
|
// Note: Since we prepend the bytes, this loop iterates in reverse.
|
||||||
for i := 9; i >= 0; i-- {
|
for i := 9; i >= 0; i-- {
|
||||||
builder.PrependByte(byte(i))
|
builder.PrependByte(byte(i))
|
||||||
}
|
}
|
||||||
inv := builder.EndVector(10)
|
inv := builder.EndVector(10)
|
||||||
|
|
||||||
sample.MonsterStartWeaponsVector(builder, 2)
|
sample.MonsterStartWeaponsVector(builder, 2)
|
||||||
// Note: Since we prepend the weapons, prepend in reverse order.
|
// Note: Since we prepend the weapons, prepend in reverse order.
|
||||||
builder.PrependUOffsetT(axe)
|
builder.PrependUOffsetT(axe)
|
||||||
builder.PrependUOffsetT(sword)
|
builder.PrependUOffsetT(sword)
|
||||||
weapons := builder.EndVector(2)
|
weapons := builder.EndVector(2)
|
||||||
|
|
||||||
pos := sample.CreateVec3(builder, 1.0, 2.0, 3.0)
|
pos := sample.CreateVec3(builder, 1.0, 2.0, 3.0)
|
||||||
|
|
||||||
sample.MonsterStart(builder)
|
sample.MonsterStart(builder)
|
||||||
sample.MonsterAddPos(builder, pos)
|
sample.MonsterAddPos(builder, pos)
|
||||||
sample.MonsterAddHp(builder, 300)
|
sample.MonsterAddHp(builder, 300)
|
||||||
sample.MonsterAddName(builder, name)
|
sample.MonsterAddName(builder, name)
|
||||||
sample.MonsterAddInventory(builder, inv)
|
sample.MonsterAddInventory(builder, inv)
|
||||||
sample.MonsterAddColor(builder, sample.ColorRed)
|
sample.MonsterAddColor(builder, sample.ColorRed)
|
||||||
sample.MonsterAddWeapons(builder, weapons)
|
sample.MonsterAddWeapons(builder, weapons)
|
||||||
sample.MonsterAddEquippedType(builder, sample.EquipmentWeapon)
|
sample.MonsterAddEquippedType(builder, sample.EquipmentWeapon)
|
||||||
sample.MonsterAddEquipped(builder, axe)
|
sample.MonsterAddEquipped(builder, axe)
|
||||||
orc := sample.MonsterEnd(builder)
|
orc := sample.MonsterEnd(builder)
|
||||||
|
|
||||||
builder.Finish(orc)
|
builder.Finish(orc)
|
||||||
|
|
||||||
// We now have a FlatBuffer that we could store on disk or send over a network.
|
// We now have a FlatBuffer that we could store on disk or send over a network.
|
||||||
|
|
||||||
// ...Saving to file or sending over a network code goes here...
|
// ...Saving to file or sending over a network code goes here...
|
||||||
|
|
||||||
// Instead, we are going to access this buffer right away (as if we just received it).
|
// Instead, we are going to access this buffer right away (as if we just received it).
|
||||||
|
|
||||||
buf := builder.FinishedBytes()
|
buf := builder.FinishedBytes()
|
||||||
|
|
||||||
// Note: We use `0` for the offset here, since we got the data using the
|
// Note: We use `0` for the offset here, since we got the data using the
|
||||||
// `builder.FinishedBytes()` method. This simulates the data you would store/receive in your
|
// `builder.FinishedBytes()` method. This simulates the data you would store/receive in your
|
||||||
// FlatBuffer. If you wanted to read from the `builder.Bytes` directly, you would need to
|
// FlatBuffer. If you wanted to read from the `builder.Bytes` directly, you would need to
|
||||||
// pass in the offset of `builder.Head()`, as the builder actually constructs the buffer
|
// pass in the offset of `builder.Head()`, as the builder actually constructs the buffer
|
||||||
// backwards.
|
// backwards.
|
||||||
monster := sample.GetRootAsMonster(buf, 0)
|
monster := sample.GetRootAsMonster(buf, 0)
|
||||||
|
|
||||||
// Note: We did not set the `mana` field explicitly, so we get the
|
// Note: We did not set the `mana` field explicitly, so we get the
|
||||||
// default value.
|
// default value.
|
||||||
assert(monster.Mana() == 150, "`monster.Mana()`", strconv.Itoa(int(monster.Mana())), "150")
|
assert(monster.Mana() == 150, "`monster.Mana()`", strconv.Itoa(int(monster.Mana())), "150")
|
||||||
assert(monster.Hp() == 300, "`monster.Hp()`", strconv.Itoa(int(monster.Hp())), "300")
|
assert(monster.Hp() == 300, "`monster.Hp()`", strconv.Itoa(int(monster.Hp())), "300")
|
||||||
assert(string(monster.Name()) == "Orc", "`string(monster.Name())`", string(monster.Name()),
|
assert(string(monster.Name()) == "Orc", "`string(monster.Name())`", string(monster.Name()),
|
||||||
"\"Orc\"")
|
"\"Orc\"")
|
||||||
assert(monster.Color() == sample.ColorRed, "`monster.Color()`",
|
assert(monster.Color() == sample.ColorRed, "`monster.Color()`",
|
||||||
strconv.Itoa(int(monster.Color())), strconv.Itoa(int(sample.ColorRed)))
|
strconv.Itoa(int(monster.Color())), strconv.Itoa(int(sample.ColorRed)))
|
||||||
|
|
||||||
// Note: Whenever you access a new object, like in `Pos()`, a new temporary accessor object
|
// Note: Whenever you access a new object, like in `Pos()`, a new temporary accessor object
|
||||||
// gets created. If your code is very performance sensitive, you can pass in a pointer to an
|
// gets created. If your code is very performance sensitive, you can pass in a pointer to an
|
||||||
// existing `Vec3` instead of `nil`. This allows you to reuse it across many calls to reduce
|
// existing `Vec3` instead of `nil`. This allows you to reuse it across many calls to reduce
|
||||||
// the amount of object allocation/garbage collection.
|
// the amount of object allocation/garbage collection.
|
||||||
assert(monster.Pos(nil).X() == 1.0, "`monster.Pos(nil).X()`",
|
assert(monster.Pos(nil).X() == 1.0, "`monster.Pos(nil).X()`",
|
||||||
strconv.FormatFloat(float64(monster.Pos(nil).X()), 'f', 1, 32), "1.0")
|
strconv.FormatFloat(float64(monster.Pos(nil).X()), 'f', 1, 32), "1.0")
|
||||||
assert(monster.Pos(nil).Y() == 2.0, "`monster.Pos(nil).Y()`",
|
assert(monster.Pos(nil).Y() == 2.0, "`monster.Pos(nil).Y()`",
|
||||||
strconv.FormatFloat(float64(monster.Pos(nil).Y()), 'f', 1, 32), "2.0")
|
strconv.FormatFloat(float64(monster.Pos(nil).Y()), 'f', 1, 32), "2.0")
|
||||||
assert(monster.Pos(nil).Z() == 3.0, "`monster.Pos(nil).Z()`",
|
assert(monster.Pos(nil).Z() == 3.0, "`monster.Pos(nil).Z()`",
|
||||||
strconv.FormatFloat(float64(monster.Pos(nil).Z()), 'f', 1, 32), "3.0")
|
strconv.FormatFloat(float64(monster.Pos(nil).Z()), 'f', 1, 32), "3.0")
|
||||||
|
|
||||||
// For vectors, like `Inventory`, they have a method suffixed with 'Length' that can be used
|
// For vectors, like `Inventory`, they have a method suffixed with 'Length' that can be used
|
||||||
// to query the length of the vector. You can index the vector by passing an index value
|
// to query the length of the vector. You can index the vector by passing an index value
|
||||||
// into the accessor.
|
// into the accessor.
|
||||||
for i := 0; i < monster.InventoryLength(); i++ {
|
for i := 0; i < monster.InventoryLength(); i++ {
|
||||||
assert(monster.Inventory(i) == byte(i), "`monster.Inventory(i)`",
|
assert(monster.Inventory(i) == byte(i), "`monster.Inventory(i)`",
|
||||||
strconv.Itoa(int(monster.Inventory(i))), strconv.Itoa(int(byte(i))))
|
strconv.Itoa(int(monster.Inventory(i))), strconv.Itoa(int(byte(i))))
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedWeaponNames := []string{"Sword", "Axe"}
|
expectedWeaponNames := []string{"Sword", "Axe"}
|
||||||
expectedWeaponDamages := []int{3, 5}
|
expectedWeaponDamages := []int{3, 5}
|
||||||
weapon := new(sample.Weapon) // We need a `sample.Weapon` to pass into `monster.Weapons()`
|
weapon := new(sample.Weapon) // We need a `sample.Weapon` to pass into `monster.Weapons()`
|
||||||
// to capture the output of that function.
|
// to capture the output of that function.
|
||||||
for i := 0; i < monster.WeaponsLength(); i++ {
|
for i := 0; i < monster.WeaponsLength(); i++ {
|
||||||
if monster.Weapons(weapon, i) {
|
if monster.Weapons(weapon, i) {
|
||||||
assert(string(weapon.Name()) == expectedWeaponNames[i], "`weapon.Name()`",
|
assert(string(weapon.Name()) == expectedWeaponNames[i], "`weapon.Name()`",
|
||||||
string(weapon.Name()), expectedWeaponNames[i])
|
string(weapon.Name()), expectedWeaponNames[i])
|
||||||
assert(int(weapon.Damage()) == expectedWeaponDamages[i],
|
assert(int(weapon.Damage()) == expectedWeaponDamages[i],
|
||||||
"`weapon.Damage()`", strconv.Itoa(int(weapon.Damage())),
|
"`weapon.Damage()`", strconv.Itoa(int(weapon.Damage())),
|
||||||
strconv.Itoa(expectedWeaponDamages[i]))
|
strconv.Itoa(expectedWeaponDamages[i]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// For FlatBuffer `union`s, you can get the type of the union, as well as the union
|
// For FlatBuffer `union`s, you can get the type of the union, as well as the union
|
||||||
// data itself.
|
// data itself.
|
||||||
assert(monster.EquippedType() == sample.EquipmentWeapon, "`monster.EquippedType()`",
|
assert(monster.EquippedType() == sample.EquipmentWeapon, "`monster.EquippedType()`",
|
||||||
strconv.Itoa(int(monster.EquippedType())), strconv.Itoa(int(sample.EquipmentWeapon)))
|
strconv.Itoa(int(monster.EquippedType())), strconv.Itoa(int(sample.EquipmentWeapon)))
|
||||||
|
|
||||||
unionTable := new(flatbuffers.Table)
|
unionTable := new(flatbuffers.Table)
|
||||||
if monster.Equipped(unionTable) {
|
if monster.Equipped(unionTable) {
|
||||||
// An example of how you can appropriately convert the table depending on the
|
// An example of how you can appropriately convert the table depending on the
|
||||||
// FlatBuffer `union` type. You could add `else if` and `else` clauses to handle
|
// FlatBuffer `union` type. You could add `else if` and `else` clauses to handle
|
||||||
// other FlatBuffer `union` types for this field. (Similarly, this could be
|
// other FlatBuffer `union` types for this field. (Similarly, this could be
|
||||||
// done in a switch statement.)
|
// done in a switch statement.)
|
||||||
if monster.EquippedType() == sample.EquipmentWeapon {
|
if monster.EquippedType() == sample.EquipmentWeapon {
|
||||||
unionWeapon := new(sample.Weapon)
|
unionWeapon := new(sample.Weapon)
|
||||||
unionWeapon.Init(unionTable.Bytes, unionTable.Pos)
|
unionWeapon.Init(unionTable.Bytes, unionTable.Pos)
|
||||||
|
|
||||||
assert(string(unionWeapon.Name()) == "Axe", "`unionWeapon.Name()`",
|
assert(string(unionWeapon.Name()) == "Axe", "`unionWeapon.Name()`",
|
||||||
string(unionWeapon.Name()), "Axe")
|
string(unionWeapon.Name()), "Axe")
|
||||||
assert(int(unionWeapon.Damage()) == 5, "`unionWeapon.Damage()`",
|
assert(int(unionWeapon.Damage()) == 5, "`unionWeapon.Damage()`",
|
||||||
strconv.Itoa(int(unionWeapon.Damage())), strconv.Itoa(5))
|
strconv.Itoa(int(unionWeapon.Damage())), strconv.Itoa(5))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("The FlatBuffer was successfully created and verified!\n")
|
fmt.Printf("The FlatBuffer was successfully created and verified!\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// A helper function to print out if an assertion failed.
|
// A helper function to print out if an assertion failed.
|
||||||
func assert(assertPassed bool, codeExecuted string, actualValue string, expectedValue string) {
|
func assert(assertPassed bool, codeExecuted string, actualValue string, expectedValue string) {
|
||||||
if assertPassed == false {
|
if assertPassed == false {
|
||||||
panic("Assert failed! " + codeExecuted + " (" + actualValue +
|
panic("Assert failed! " + codeExecuted + " (" + actualValue +
|
||||||
") was not equal to " + expectedValue + ".")
|
") was not equal to " + expectedValue + ".")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1251,9 +1251,9 @@ func CheckVtableDeduplication(fail func(string, ...interface{})) {
|
|||||||
len(want), want, len(got), got)
|
len(want), want, len(got), got)
|
||||||
}
|
}
|
||||||
|
|
||||||
table0 := &flatbuffers.Table{b.Bytes, flatbuffers.UOffsetT(len(b.Bytes)) - obj0}
|
table0 := &flatbuffers.Table{Bytes: b.Bytes, UOffset: flatbuffers.UOffsetT(len(b.Bytes)) - obj0}
|
||||||
table1 := &flatbuffers.Table{b.Bytes, flatbuffers.UOffsetT(len(b.Bytes)) - obj1}
|
table1 := &flatbuffers.Table{Bytes: b.Bytes, UOffset: flatbuffers.UOffsetT(len(b.Bytes)) - obj1}
|
||||||
table2 := &flatbuffers.Table{b.Bytes, flatbuffers.UOffsetT(len(b.Bytes)) - obj2}
|
table2 := &flatbuffers.Table{Bytes: b.Bytes, UOffset: flatbuffers.UOffsetT(len(b.Bytes)) - obj2}
|
||||||
|
|
||||||
testTable := func(tab *flatbuffers.Table, a flatbuffers.VOffsetT, b, c, d byte) {
|
testTable := func(tab *flatbuffers.Table, a flatbuffers.VOffsetT, b, c, d byte) {
|
||||||
// vtable size
|
// vtable size
|
||||||
|
|||||||
Reference in New Issue
Block a user