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
a56c6e51
Commit
a56c6e51
authored
Jul 22, 2016
by
Robert
Committed by
GitHub
Jul 22, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #363 from danring/fix-341
Emit GetRootAs methods for all types in Go and Python
parents
5efa2244
48322387
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
80 additions
and
10 deletions
+80
-10
idl_gen_go.cpp
src/idl_gen_go.cpp
+3
-4
idl_gen_python.cpp
src/idl_gen_python.cpp
+4
-5
Monster.py
tests/MyGame/Example/Monster.py
+0
-1
Stat.go
tests/MyGame/Example/Stat.go
+7
-0
Stat.py
tests/MyGame/Example/Stat.py
+7
-0
TestSimpleTableWithEnum.go
tests/MyGame/Example/TestSimpleTableWithEnum.go
+7
-0
TestSimpleTableWithEnum.py
tests/MyGame/Example/TestSimpleTableWithEnum.py
+7
-0
go_test.go
tests/go_test.go
+28
-0
py_test.py
tests/py_test.py
+17
-0
No files found.
src/idl_gen_go.cpp
View file @
a56c6e51
...
...
@@ -551,13 +551,12 @@ static void GenTableBuilders(const StructDef &struct_def,
// Generate struct or table methods.
static
void
GenStruct
(
const
StructDef
&
struct_def
,
std
::
string
*
code_ptr
,
StructDef
*
root_struct_def
)
{
std
::
string
*
code_ptr
)
{
if
(
struct_def
.
generated
)
return
;
GenComment
(
struct_def
.
doc_comment
,
code_ptr
,
nullptr
);
BeginClass
(
struct_def
,
code_ptr
);
if
(
&
struct_def
==
root_struct_def
)
{
if
(
!
struct_def
.
fixed
)
{
// Generate a special accessor for the table that has been declared as
// the root type.
NewRootTypeFromBuffer
(
struct_def
,
code_ptr
);
...
...
@@ -681,7 +680,7 @@ class GoGenerator : public BaseGenerator {
for
(
auto
it
=
parser_
.
structs_
.
vec
.
begin
();
it
!=
parser_
.
structs_
.
vec
.
end
();
++
it
)
{
std
::
string
declcode
;
go
::
GenStruct
(
**
it
,
&
declcode
,
parser_
.
root_struct_def_
);
go
::
GenStruct
(
**
it
,
&
declcode
);
if
(
!
SaveType
(
**
it
,
declcode
,
true
))
return
false
;
}
...
...
src/idl_gen_python.cpp
View file @
a56c6e51
...
...
@@ -94,7 +94,7 @@ static void NewRootTypeFromBuffer(const StructDef &struct_def,
code
+=
Indent
+
Indent
+
"x = "
+
struct_def
.
name
+
"()
\n
"
;
code
+=
Indent
+
Indent
+
"x.Init(buf, n + offset)
\n
"
;
code
+=
Indent
+
Indent
+
"return x
\n
"
;
code
+=
"
\n
\n
"
;
code
+=
"
\n
"
;
}
// Initialize an existing object with other data, to avoid an allocation.
...
...
@@ -478,13 +478,12 @@ static void GenTableBuilders(const StructDef &struct_def,
// Generate struct or table methods.
static
void
GenStruct
(
const
StructDef
&
struct_def
,
std
::
string
*
code_ptr
,
StructDef
*
root_struct_def
)
{
std
::
string
*
code_ptr
)
{
if
(
struct_def
.
generated
)
return
;
GenComment
(
struct_def
.
doc_comment
,
code_ptr
,
nullptr
,
"# "
);
BeginClass
(
struct_def
,
code_ptr
);
if
(
&
struct_def
==
root_struct_def
)
{
if
(
!
struct_def
.
fixed
)
{
// Generate a special accessor for the table that has been declared as
// the root type.
NewRootTypeFromBuffer
(
struct_def
,
code_ptr
);
...
...
@@ -621,7 +620,7 @@ class PythonGenerator : public BaseGenerator {
it
!=
parser_
.
structs_
.
vec
.
end
();
++
it
)
{
auto
&
struct_def
=
**
it
;
std
::
string
declcode
;
GenStruct
(
struct_def
,
&
declcode
,
parser_
.
root_struct_def_
);
GenStruct
(
struct_def
,
&
declcode
);
if
(
!
SaveType
(
struct_def
,
declcode
,
true
))
return
false
;
}
return
true
;
...
...
tests/MyGame/Example/Monster.py
View file @
a56c6e51
...
...
@@ -15,7 +15,6 @@ class Monster(object):
x
.
Init
(
buf
,
n
+
offset
)
return
x
# Monster
def
Init
(
self
,
buf
,
pos
):
self
.
_tab
=
flatbuffers
.
table
.
Table
(
buf
,
pos
)
...
...
tests/MyGame/Example/Stat.go
View file @
a56c6e51
...
...
@@ -9,6 +9,13 @@ type Stat struct {
_tab
flatbuffers
.
Table
}
func
GetRootAsStat
(
buf
[]
byte
,
offset
flatbuffers
.
UOffsetT
)
*
Stat
{
n
:=
flatbuffers
.
GetUOffsetT
(
buf
[
offset
:
])
x
:=
&
Stat
{}
x
.
Init
(
buf
,
n
+
offset
)
return
x
}
func
(
rcv
*
Stat
)
Init
(
buf
[]
byte
,
i
flatbuffers
.
UOffsetT
)
{
rcv
.
_tab
.
Bytes
=
buf
rcv
.
_tab
.
Pos
=
i
...
...
tests/MyGame/Example/Stat.py
View file @
a56c6e51
...
...
@@ -7,6 +7,13 @@ import flatbuffers
class
Stat
(
object
):
__slots__
=
[
'_tab'
]
@classmethod
def
GetRootAsStat
(
cls
,
buf
,
offset
):
n
=
flatbuffers
.
encode
.
Get
(
flatbuffers
.
packer
.
uoffset
,
buf
,
offset
)
x
=
Stat
()
x
.
Init
(
buf
,
n
+
offset
)
return
x
# Stat
def
Init
(
self
,
buf
,
pos
):
self
.
_tab
=
flatbuffers
.
table
.
Table
(
buf
,
pos
)
...
...
tests/MyGame/Example/TestSimpleTableWithEnum.go
View file @
a56c6e51
...
...
@@ -9,6 +9,13 @@ type TestSimpleTableWithEnum struct {
_tab
flatbuffers
.
Table
}
func
GetRootAsTestSimpleTableWithEnum
(
buf
[]
byte
,
offset
flatbuffers
.
UOffsetT
)
*
TestSimpleTableWithEnum
{
n
:=
flatbuffers
.
GetUOffsetT
(
buf
[
offset
:
])
x
:=
&
TestSimpleTableWithEnum
{}
x
.
Init
(
buf
,
n
+
offset
)
return
x
}
func
(
rcv
*
TestSimpleTableWithEnum
)
Init
(
buf
[]
byte
,
i
flatbuffers
.
UOffsetT
)
{
rcv
.
_tab
.
Bytes
=
buf
rcv
.
_tab
.
Pos
=
i
...
...
tests/MyGame/Example/TestSimpleTableWithEnum.py
View file @
a56c6e51
...
...
@@ -7,6 +7,13 @@ import flatbuffers
class
TestSimpleTableWithEnum
(
object
):
__slots__
=
[
'_tab'
]
@classmethod
def
GetRootAsTestSimpleTableWithEnum
(
cls
,
buf
,
offset
):
n
=
flatbuffers
.
encode
.
Get
(
flatbuffers
.
packer
.
uoffset
,
buf
,
offset
)
x
=
TestSimpleTableWithEnum
()
x
.
Init
(
buf
,
n
+
offset
)
return
x
# TestSimpleTableWithEnum
def
Init
(
self
,
buf
,
pos
):
self
.
_tab
=
flatbuffers
.
table
.
Table
(
buf
,
pos
)
...
...
tests/go_test.go
View file @
a56c6e51
...
...
@@ -76,6 +76,9 @@ func TestAll(t *testing.T) {
CheckStructIsNotInlineError
(
t
.
Fatalf
)
CheckFinishedBytesError
(
t
.
Fatalf
)
// Verify that GetRootAs works for non-root tables
CheckGetRootAsForNonRootTable
(
t
.
Fatalf
)
// Verify that using the generated Go code builds a buffer without
// returning errors:
generated
,
off
:=
CheckGeneratedBuild
(
t
.
Fatalf
)
...
...
@@ -1081,6 +1084,31 @@ func CheckManualBuild(fail func(string, ...interface{})) ([]byte, flatbuffers.UO
return
b
.
Bytes
,
b
.
Head
()
}
func
CheckGetRootAsForNonRootTable
(
fail
func
(
string
,
...
interface
{}))
{
b
:=
flatbuffers
.
NewBuilder
(
0
)
str
:=
b
.
CreateString
(
"MyStat"
)
example
.
StatStart
(
b
)
example
.
StatAddId
(
b
,
str
)
example
.
StatAddVal
(
b
,
12345678
)
example
.
StatAddCount
(
b
,
12345
)
stat_end
:=
example
.
StatEnd
(
b
)
b
.
Finish
(
stat_end
)
stat
:=
example
.
GetRootAsStat
(
b
.
Bytes
,
b
.
Head
())
if
got
:=
stat
.
Id
();
!
bytes
.
Equal
([]
byte
(
"MyStat"
),
got
)
{
fail
(
FailString
(
"stat.Id()"
,
"MyStat"
,
got
))
}
if
got
:=
stat
.
Val
();
12345678
!=
got
{
fail
(
FailString
(
"stat.Val()"
,
12345678
,
got
))
}
if
got
:=
stat
.
Count
();
12345
!=
got
{
fail
(
FailString
(
"stat.Count()"
,
12345
,
got
))
}
}
// CheckGeneratedBuild uses generated code to build the example Monster.
func
CheckGeneratedBuild
(
fail
func
(
string
,
...
interface
{}))
([]
byte
,
flatbuffers
.
UOffsetT
)
{
b
:=
flatbuffers
.
NewBuilder
(
0
)
...
...
tests/py_test.py
View file @
a56c6e51
...
...
@@ -1034,6 +1034,23 @@ class TestAllCodePathsOfExampleSchema(unittest.TestCase):
self
.
assertEqual
(
7
,
mon2
.
Testhashs64Fnv1a
())
self
.
assertEqual
(
8
,
mon2
.
Testhashu64Fnv1a
())
def
test_getrootas_for_nonroot_table
(
self
):
b
=
flatbuffers
.
Builder
(
0
)
string
=
b
.
CreateString
(
"MyStat"
)
MyGame
.
Example
.
Stat
.
StatStart
(
b
)
MyGame
.
Example
.
Stat
.
StatAddId
(
b
,
string
)
MyGame
.
Example
.
Stat
.
StatAddVal
(
b
,
12345678
)
MyGame
.
Example
.
Stat
.
StatAddCount
(
b
,
12345
)
stat
=
MyGame
.
Example
.
Stat
.
StatEnd
(
b
)
b
.
Finish
(
stat
)
stat2
=
MyGame
.
Example
.
Stat
.
Stat
.
GetRootAsStat
(
b
.
Bytes
,
b
.
Head
())
self
.
assertEqual
(
b
"MyStat"
,
stat2
.
Id
())
self
.
assertEqual
(
12345678
,
stat2
.
Val
())
self
.
assertEqual
(
12345
,
stat2
.
Count
())
class
TestVtableDeduplication
(
unittest
.
TestCase
):
''' TestVtableDeduplication verifies that vtables are deduplicated. '''
...
...
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