Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
flatbuffers
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
flatbuffers
Commits
b55f1864
Commit
b55f1864
authored
Jan 10, 2017
by
Pascal S. de Kloe
Committed by
Wouter van Oortmerssen
Jan 10, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Resolve Go fmt and vet warnings (#4134)
* Resolve Go fmt and vet warnings. * Undo generated code gofmt.
parent
e2373668
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
139 additions
and
139 deletions
+139
-139
sizes.go
go/sizes.go
+1
-1
sample_binary.go
samples/sample_binary.go
+135
-135
go_test.go
tests/go_test.go
+3
-3
No files found.
go/sizes.go
View file @
b55f1864
package
flatbuffers
import
(
"unsafe"
"unsafe"
)
const
(
...
...
samples/sample_binary.go
View file @
b55f1864
...
...
@@ -19,147 +19,147 @@
package
main
import
(
flatbuffers
"github.com/google/flatbuffers/go
"
"fmt"
"strconv
"
sample
"MyGame/Sample
"
sample
"MyGame/Sample
"
"fmt"
flatbuffers
"github.com/google/flatbuffers/go
"
"strconv
"
)
// Example how to use Flatbuffers to create and read binary buffers.
func
main
()
{
builder
:=
flatbuffers
.
NewBuilder
(
0
)
// Create some weapons for our Monster ("Sword" and "Axe").
weaponOne
:=
builder
.
CreateString
(
"Sword"
)
weaponTwo
:=
builder
.
CreateString
(
"Axe"
)
sample
.
WeaponStart
(
builder
)
sample
.
WeaponAddName
(
builder
,
weaponOne
)
sample
.
WeaponAddDamage
(
builder
,
3
)
sword
:=
sample
.
WeaponEnd
(
builder
)
sample
.
WeaponStart
(
builder
)
sample
.
WeaponAddName
(
builder
,
weaponTwo
)
sample
.
WeaponAddDamage
(
builder
,
5
)
axe
:=
sample
.
WeaponEnd
(
builder
)
// Serialize the FlatBuffer data.
name
:=
builder
.
CreateString
(
"Orc"
)
sample
.
MonsterStartInventoryVector
(
builder
,
10
)
// Note: Since we prepend the bytes, this loop iterates in reverse.
for
i
:=
9
;
i
>=
0
;
i
--
{
builder
.
PrependByte
(
byte
(
i
))
}
inv
:=
builder
.
EndVector
(
10
)
sample
.
MonsterStartWeaponsVector
(
builder
,
2
)
// Note: Since we prepend the weapons, prepend in reverse order.
builder
.
PrependUOffsetT
(
axe
)
builder
.
PrependUOffsetT
(
sword
)
weapons
:=
builder
.
EndVector
(
2
)
pos
:=
sample
.
CreateVec3
(
builder
,
1.0
,
2.0
,
3.0
)
sample
.
MonsterStart
(
builder
)
sample
.
MonsterAddPos
(
builder
,
pos
)
sample
.
MonsterAddHp
(
builder
,
300
)
sample
.
MonsterAddName
(
builder
,
name
)
sample
.
MonsterAddInventory
(
builder
,
inv
)
sample
.
MonsterAddColor
(
builder
,
sample
.
ColorRed
)
sample
.
MonsterAddWeapons
(
builder
,
weapons
)
sample
.
MonsterAddEquippedType
(
builder
,
sample
.
EquipmentWeapon
)
sample
.
MonsterAddEquipped
(
builder
,
axe
)
orc
:=
sample
.
MonsterEnd
(
builder
)
builder
.
Finish
(
orc
)
// 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...
// Instead, we are going to access this buffer right away (as if we just received it).
buf
:=
builder
.
FinishedBytes
()
// 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
// 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
// backwards.
monster
:=
sample
.
GetRootAsMonster
(
buf
,
0
)
// Note: We did not set the `mana` field explicitly, so we get the
// default value.
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
(
string
(
monster
.
Name
())
==
"Orc"
,
"`string(monster.Name())`"
,
string
(
monster
.
Name
()),
"
\"
Orc
\"
"
)
assert
(
monster
.
Color
()
==
sample
.
ColorRed
,
"`monster.Color()`"
,
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
// 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
// the amount of object allocation/garbage collection.
assert
(
monster
.
Pos
(
nil
)
.
X
()
==
1.0
,
"`monster.Pos(nil).X()`"
,
strconv
.
FormatFloat
(
float64
(
monster
.
Pos
(
nil
)
.
X
()),
'f'
,
1
,
32
),
"1.0"
)
assert
(
monster
.
Pos
(
nil
)
.
Y
()
==
2.0
,
"`monster.Pos(nil).Y()`"
,
strconv
.
FormatFloat
(
float64
(
monster
.
Pos
(
nil
)
.
Y
()),
'f'
,
1
,
32
),
"2.0"
)
assert
(
monster
.
Pos
(
nil
)
.
Z
()
==
3.0
,
"`monster.Pos(nil).Z()`"
,
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
// to query the length of the vector. You can index the vector by passing an index value
// into the accessor.
for
i
:=
0
;
i
<
monster
.
InventoryLength
();
i
++
{
assert
(
monster
.
Inventory
(
i
)
==
byte
(
i
),
"`monster.Inventory(i)`"
,
strconv
.
Itoa
(
int
(
monster
.
Inventory
(
i
))),
strconv
.
Itoa
(
int
(
byte
(
i
))))
}
expectedWeaponNames
:=
[]
string
{
"Sword"
,
"Axe"
}
expectedWeaponDamages
:=
[]
int
{
3
,
5
}
weapon
:=
new
(
sample
.
Weapon
)
// We need a `sample.Weapon` to pass into `monster.Weapons()`
// to capture the output of that function.
for
i
:=
0
;
i
<
monster
.
WeaponsLength
();
i
++
{
if
monster
.
Weapons
(
weapon
,
i
)
{
assert
(
string
(
weapon
.
Name
())
==
expectedWeaponNames
[
i
],
"`weapon.Name()`"
,
string
(
weapon
.
Name
()),
expectedWeaponNames
[
i
])
assert
(
int
(
weapon
.
Damage
())
==
expectedWeaponDamages
[
i
],
"`weapon.Damage()`"
,
strconv
.
Itoa
(
int
(
weapon
.
Damage
())),
strconv
.
Itoa
(
expectedWeaponDamages
[
i
]))
}
}
// For FlatBuffer `union`s, you can get the type of the union, as well as the union
// data itself.
assert
(
monster
.
EquippedType
()
==
sample
.
EquipmentWeapon
,
"`monster.EquippedType()`"
,
strconv
.
Itoa
(
int
(
monster
.
EquippedType
())),
strconv
.
Itoa
(
int
(
sample
.
EquipmentWeapon
)))
unionTable
:=
new
(
flatbuffers
.
Table
)
if
monster
.
Equipped
(
unionTable
)
{
// 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
// other FlatBuffer `union` types for this field. (Similarly, this could be
// done in a switch statement.)
if
monster
.
EquippedType
()
==
sample
.
EquipmentWeapon
{
unionWeapon
:=
new
(
sample
.
Weapon
)
unionWeapon
.
Init
(
unionTable
.
Bytes
,
unionTable
.
Pos
)
assert
(
string
(
unionWeapon
.
Name
())
==
"Axe"
,
"`unionWeapon.Name()`"
,
string
(
unionWeapon
.
Name
()),
"Axe"
)
assert
(
int
(
unionWeapon
.
Damage
())
==
5
,
"`unionWeapon.Damage()`"
,
strconv
.
Itoa
(
int
(
unionWeapon
.
Damage
())),
strconv
.
Itoa
(
5
))
}
}
fmt
.
Printf
(
"The FlatBuffer was successfully created and verified!
\n
"
)
builder
:=
flatbuffers
.
NewBuilder
(
0
)
// Create some weapons for our Monster ("Sword" and "Axe").
weaponOne
:=
builder
.
CreateString
(
"Sword"
)
weaponTwo
:=
builder
.
CreateString
(
"Axe"
)
sample
.
WeaponStart
(
builder
)
sample
.
WeaponAddName
(
builder
,
weaponOne
)
sample
.
WeaponAddDamage
(
builder
,
3
)
sword
:=
sample
.
WeaponEnd
(
builder
)
sample
.
WeaponStart
(
builder
)
sample
.
WeaponAddName
(
builder
,
weaponTwo
)
sample
.
WeaponAddDamage
(
builder
,
5
)
axe
:=
sample
.
WeaponEnd
(
builder
)
// Serialize the FlatBuffer data.
name
:=
builder
.
CreateString
(
"Orc"
)
sample
.
MonsterStartInventoryVector
(
builder
,
10
)
// Note: Since we prepend the bytes, this loop iterates in reverse.
for
i
:=
9
;
i
>=
0
;
i
--
{
builder
.
PrependByte
(
byte
(
i
))
}
inv
:=
builder
.
EndVector
(
10
)
sample
.
MonsterStartWeaponsVector
(
builder
,
2
)
// Note: Since we prepend the weapons, prepend in reverse order.
builder
.
PrependUOffsetT
(
axe
)
builder
.
PrependUOffsetT
(
sword
)
weapons
:=
builder
.
EndVector
(
2
)
pos
:=
sample
.
CreateVec3
(
builder
,
1.0
,
2.0
,
3.0
)
sample
.
MonsterStart
(
builder
)
sample
.
MonsterAddPos
(
builder
,
pos
)
sample
.
MonsterAddHp
(
builder
,
300
)
sample
.
MonsterAddName
(
builder
,
name
)
sample
.
MonsterAddInventory
(
builder
,
inv
)
sample
.
MonsterAddColor
(
builder
,
sample
.
ColorRed
)
sample
.
MonsterAddWeapons
(
builder
,
weapons
)
sample
.
MonsterAddEquippedType
(
builder
,
sample
.
EquipmentWeapon
)
sample
.
MonsterAddEquipped
(
builder
,
axe
)
orc
:=
sample
.
MonsterEnd
(
builder
)
builder
.
Finish
(
orc
)
// 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...
// Instead, we are going to access this buffer right away (as if we just received it).
buf
:=
builder
.
FinishedBytes
()
// 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
// 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
// backwards.
monster
:=
sample
.
GetRootAsMonster
(
buf
,
0
)
// Note: We did not set the `mana` field explicitly, so we get the
// default value.
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
(
string
(
monster
.
Name
())
==
"Orc"
,
"`string(monster.Name())`"
,
string
(
monster
.
Name
()),
"
\"
Orc
\"
"
)
assert
(
monster
.
Color
()
==
sample
.
ColorRed
,
"`monster.Color()`"
,
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
// 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
// the amount of object allocation/garbage collection.
assert
(
monster
.
Pos
(
nil
)
.
X
()
==
1.0
,
"`monster.Pos(nil).X()`"
,
strconv
.
FormatFloat
(
float64
(
monster
.
Pos
(
nil
)
.
X
()),
'f'
,
1
,
32
),
"1.0"
)
assert
(
monster
.
Pos
(
nil
)
.
Y
()
==
2.0
,
"`monster.Pos(nil).Y()`"
,
strconv
.
FormatFloat
(
float64
(
monster
.
Pos
(
nil
)
.
Y
()),
'f'
,
1
,
32
),
"2.0"
)
assert
(
monster
.
Pos
(
nil
)
.
Z
()
==
3.0
,
"`monster.Pos(nil).Z()`"
,
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
// to query the length of the vector. You can index the vector by passing an index value
// into the accessor.
for
i
:=
0
;
i
<
monster
.
InventoryLength
();
i
++
{
assert
(
monster
.
Inventory
(
i
)
==
byte
(
i
),
"`monster.Inventory(i)`"
,
strconv
.
Itoa
(
int
(
monster
.
Inventory
(
i
))),
strconv
.
Itoa
(
int
(
byte
(
i
))))
}
expectedWeaponNames
:=
[]
string
{
"Sword"
,
"Axe"
}
expectedWeaponDamages
:=
[]
int
{
3
,
5
}
weapon
:=
new
(
sample
.
Weapon
)
// We need a `sample.Weapon` to pass into `monster.Weapons()`
// to capture the output of that function.
for
i
:=
0
;
i
<
monster
.
WeaponsLength
();
i
++
{
if
monster
.
Weapons
(
weapon
,
i
)
{
assert
(
string
(
weapon
.
Name
())
==
expectedWeaponNames
[
i
],
"`weapon.Name()`"
,
string
(
weapon
.
Name
()),
expectedWeaponNames
[
i
])
assert
(
int
(
weapon
.
Damage
())
==
expectedWeaponDamages
[
i
],
"`weapon.Damage()`"
,
strconv
.
Itoa
(
int
(
weapon
.
Damage
())),
strconv
.
Itoa
(
expectedWeaponDamages
[
i
]))
}
}
// For FlatBuffer `union`s, you can get the type of the union, as well as the union
// data itself.
assert
(
monster
.
EquippedType
()
==
sample
.
EquipmentWeapon
,
"`monster.EquippedType()`"
,
strconv
.
Itoa
(
int
(
monster
.
EquippedType
())),
strconv
.
Itoa
(
int
(
sample
.
EquipmentWeapon
)))
unionTable
:=
new
(
flatbuffers
.
Table
)
if
monster
.
Equipped
(
unionTable
)
{
// 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
// other FlatBuffer `union` types for this field. (Similarly, this could be
// done in a switch statement.)
if
monster
.
EquippedType
()
==
sample
.
EquipmentWeapon
{
unionWeapon
:=
new
(
sample
.
Weapon
)
unionWeapon
.
Init
(
unionTable
.
Bytes
,
unionTable
.
Pos
)
assert
(
string
(
unionWeapon
.
Name
())
==
"Axe"
,
"`unionWeapon.Name()`"
,
string
(
unionWeapon
.
Name
()),
"Axe"
)
assert
(
int
(
unionWeapon
.
Damage
())
==
5
,
"`unionWeapon.Damage()`"
,
strconv
.
Itoa
(
int
(
unionWeapon
.
Damage
())),
strconv
.
Itoa
(
5
))
}
}
fmt
.
Printf
(
"The FlatBuffer was successfully created and verified!
\n
"
)
}
// A helper function to print out if an assertion failed.
func
assert
(
assertPassed
bool
,
codeExecuted
string
,
actualValue
string
,
expectedValue
string
)
{
if
assertPassed
==
false
{
panic
(
"Assert failed! "
+
codeExecuted
+
" ("
+
actualValue
+
") was not equal to "
+
expectedValue
+
"."
)
}
if
assertPassed
==
false
{
panic
(
"Assert failed! "
+
codeExecuted
+
" ("
+
actualValue
+
") was not equal to "
+
expectedValue
+
"."
)
}
}
tests/go_test.go
View file @
b55f1864
...
...
@@ -1251,9 +1251,9 @@ func CheckVtableDeduplication(fail func(string, ...interface{})) {
len
(
want
),
want
,
len
(
got
),
got
)
}
table0
:=
&
flatbuffers
.
Table
{
b
.
Bytes
,
flatbuffers
.
UOffsetT
(
len
(
b
.
Bytes
))
-
obj0
}
table1
:=
&
flatbuffers
.
Table
{
b
.
Bytes
,
flatbuffers
.
UOffsetT
(
len
(
b
.
Bytes
))
-
obj1
}
table2
:=
&
flatbuffers
.
Table
{
b
.
Bytes
,
flatbuffers
.
UOffsetT
(
len
(
b
.
Bytes
))
-
obj2
}
table0
:=
&
flatbuffers
.
Table
{
Bytes
:
b
.
Bytes
,
UOffset
:
flatbuffers
.
UOffsetT
(
len
(
b
.
Bytes
))
-
obj0
}
table1
:=
&
flatbuffers
.
Table
{
Bytes
:
b
.
Bytes
,
UOffset
:
flatbuffers
.
UOffsetT
(
len
(
b
.
Bytes
))
-
obj1
}
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
)
{
// vtable size
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment