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
8fb6c4f7
Commit
8fb6c4f7
authored
Apr 01, 2015
by
Ben Harper
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add byte slice accessor to Go code
parent
ca5c9e74
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
48 additions
and
1 deletion
+48
-1
table.go
go/table.go
+6
-1
idl_gen_go.cpp
src/idl_gen_go.cpp
+16
-0
Monster.go
tests/MyGame/Example/Monster.go
+18
-0
go_test.go
tests/go_test.go
+8
-0
No files found.
go/table.go
View file @
8fb6c4f7
...
@@ -26,10 +26,15 @@ func (t *Table) Indirect(off UOffsetT) UOffsetT {
...
@@ -26,10 +26,15 @@ func (t *Table) Indirect(off UOffsetT) UOffsetT {
// String gets a string from data stored inside the flatbuffer.
// String gets a string from data stored inside the flatbuffer.
func
(
t
*
Table
)
String
(
off
UOffsetT
)
string
{
func
(
t
*
Table
)
String
(
off
UOffsetT
)
string
{
return
string
(
t
.
ByteVector
(
off
))
}
// ByteVector gets a byte slice from data stored inside the flatbuffer.
func
(
t
*
Table
)
ByteVector
(
off
UOffsetT
)
[]
byte
{
off
+=
GetUOffsetT
(
t
.
Bytes
[
off
:
])
off
+=
GetUOffsetT
(
t
.
Bytes
[
off
:
])
start
:=
off
+
UOffsetT
(
SizeUOffsetT
)
start
:=
off
+
UOffsetT
(
SizeUOffsetT
)
length
:=
GetUOffsetT
(
t
.
Bytes
[
off
:
])
length
:=
GetUOffsetT
(
t
.
Bytes
[
off
:
])
return
string
(
t
.
Bytes
[
start
:
start
+
length
])
return
t
.
Bytes
[
start
:
start
+
length
]
}
}
// VectorLen retrieves the length of the vector whose offset is stored at
// VectorLen retrieves the length of the vector whose offset is stored at
...
...
src/idl_gen_go.cpp
View file @
8fb6c4f7
...
@@ -144,6 +144,19 @@ static void GetVectorLen(const StructDef &struct_def,
...
@@ -144,6 +144,19 @@ static void GetVectorLen(const StructDef &struct_def,
code
+=
"
\t
return 0
\n
}
\n\n
"
;
code
+=
"
\t
return 0
\n
}
\n\n
"
;
}
}
// Get a [ubyte] vector as a byte slice.
static
void
GetUByteSlice
(
const
StructDef
&
struct_def
,
const
FieldDef
&
field
,
std
::
string
*
code_ptr
)
{
std
::
string
&
code
=
*
code_ptr
;
GenReceiver
(
struct_def
,
code_ptr
);
code
+=
" "
+
MakeCamel
(
field
.
name
)
+
"Bytes("
;
code
+=
") []byte "
+
OffsetPrefix
(
field
);
code
+=
"
\t\t
return rcv._tab.ByteVector(o + rcv._tab.Pos)
\n\t
}
\n
"
;
code
+=
"
\t
return nil
\n
}
\n\n
"
;
}
// Get the value of a struct's scalar.
// Get the value of a struct's scalar.
static
void
GetScalarFieldOfStruct
(
const
StructDef
&
struct_def
,
static
void
GetScalarFieldOfStruct
(
const
StructDef
&
struct_def
,
const
FieldDef
&
field
,
const
FieldDef
&
field
,
...
@@ -480,6 +493,9 @@ static void GenStructAccessor(const StructDef &struct_def,
...
@@ -480,6 +493,9 @@ static void GenStructAccessor(const StructDef &struct_def,
}
}
if
(
field
.
value
.
type
.
base_type
==
BASE_TYPE_VECTOR
)
{
if
(
field
.
value
.
type
.
base_type
==
BASE_TYPE_VECTOR
)
{
GetVectorLen
(
struct_def
,
field
,
code_ptr
);
GetVectorLen
(
struct_def
,
field
,
code_ptr
);
if
(
field
.
value
.
type
.
element
==
BASE_TYPE_UCHAR
)
{
GetUByteSlice
(
struct_def
,
field
,
code_ptr
);
}
}
}
}
}
...
...
tests/MyGame/Example/Monster.go
View file @
8fb6c4f7
...
@@ -75,6 +75,14 @@ func (rcv *Monster) InventoryLength() int {
...
@@ -75,6 +75,14 @@ func (rcv *Monster) InventoryLength() int {
return
0
return
0
}
}
func
(
rcv
*
Monster
)
InventoryBytes
()
[]
byte
{
o
:=
flatbuffers
.
UOffsetT
(
rcv
.
_tab
.
Offset
(
14
))
if
o
!=
0
{
return
rcv
.
_tab
.
ByteVector
(
o
+
rcv
.
_tab
.
Pos
)
}
return
nil
}
func
(
rcv
*
Monster
)
Color
()
int8
{
func
(
rcv
*
Monster
)
Color
()
int8
{
o
:=
flatbuffers
.
UOffsetT
(
rcv
.
_tab
.
Offset
(
16
))
o
:=
flatbuffers
.
UOffsetT
(
rcv
.
_tab
.
Offset
(
16
))
if
o
!=
0
{
if
o
!=
0
{
...
@@ -140,7 +148,9 @@ func (rcv *Monster) TestarrayofstringLength() int {
...
@@ -140,7 +148,9 @@ func (rcv *Monster) TestarrayofstringLength() int {
}
}
/// an example documentation comment: this will end up in the generated code
/// an example documentation comment: this will end up in the generated code
/// multiline too
/// multiline too
func
(
rcv
*
Monster
)
Testarrayoftables
(
obj
*
Monster
,
j
int
)
bool
{
func
(
rcv
*
Monster
)
Testarrayoftables
(
obj
*
Monster
,
j
int
)
bool
{
o
:=
flatbuffers
.
UOffsetT
(
rcv
.
_tab
.
Offset
(
26
))
o
:=
flatbuffers
.
UOffsetT
(
rcv
.
_tab
.
Offset
(
26
))
if
o
!=
0
{
if
o
!=
0
{
...
@@ -194,6 +204,14 @@ func (rcv *Monster) TestnestedflatbufferLength() int {
...
@@ -194,6 +204,14 @@ func (rcv *Monster) TestnestedflatbufferLength() int {
return
0
return
0
}
}
func
(
rcv
*
Monster
)
TestnestedflatbufferBytes
()
[]
byte
{
o
:=
flatbuffers
.
UOffsetT
(
rcv
.
_tab
.
Offset
(
30
))
if
o
!=
0
{
return
rcv
.
_tab
.
ByteVector
(
o
+
rcv
.
_tab
.
Pos
)
}
return
nil
}
func
(
rcv
*
Monster
)
Testempty
(
obj
*
Stat
)
*
Stat
{
func
(
rcv
*
Monster
)
Testempty
(
obj
*
Stat
)
*
Stat
{
o
:=
flatbuffers
.
UOffsetT
(
rcv
.
_tab
.
Offset
(
32
))
o
:=
flatbuffers
.
UOffsetT
(
rcv
.
_tab
.
Offset
(
32
))
if
o
!=
0
{
if
o
!=
0
{
...
...
tests/go_test.go
View file @
8fb6c4f7
...
@@ -213,6 +213,11 @@ func CheckReadBuffer(buf []byte, offset flatbuffers.UOffsetT, fail func(string,
...
@@ -213,6 +213,11 @@ func CheckReadBuffer(buf []byte, offset flatbuffers.UOffsetT, fail func(string,
fail
(
FailString
(
"monster2.Name()"
,
"Fred"
,
got
))
fail
(
FailString
(
"monster2.Name()"
,
"Fred"
,
got
))
}
}
inventorySlice
:=
monster
.
InventoryBytes
()
if
len
(
inventorySlice
)
!=
monster
.
InventoryLength
()
{
fail
(
FailString
(
"len(monster.InventoryBytes) != monster.InventoryLength"
,
len
(
inventorySlice
),
monster
.
InventoryLength
()))
}
if
got
:=
monster
.
InventoryLength
();
5
!=
got
{
if
got
:=
monster
.
InventoryLength
();
5
!=
got
{
fail
(
FailString
(
"monster.InventoryLength"
,
5
,
got
))
fail
(
FailString
(
"monster.InventoryLength"
,
5
,
got
))
}
}
...
@@ -221,6 +226,9 @@ func CheckReadBuffer(buf []byte, offset flatbuffers.UOffsetT, fail func(string,
...
@@ -221,6 +226,9 @@ func CheckReadBuffer(buf []byte, offset flatbuffers.UOffsetT, fail func(string,
l
:=
monster
.
InventoryLength
()
l
:=
monster
.
InventoryLength
()
for
i
:=
0
;
i
<
l
;
i
++
{
for
i
:=
0
;
i
<
l
;
i
++
{
v
:=
monster
.
Inventory
(
i
)
v
:=
monster
.
Inventory
(
i
)
if
v
!=
inventorySlice
[
i
]
{
fail
(
FailString
(
"monster inventory slice[i] != Inventory(i)"
,
v
,
inventorySlice
[
i
]))
}
invsum
+=
int
(
v
)
invsum
+=
int
(
v
)
}
}
if
invsum
!=
10
{
if
invsum
!=
10
{
...
...
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