From 74d5f3701fd19ca13b8fe69d1cf54002e11416da Mon Sep 17 00:00:00 2001 From: rw Date: Fri, 11 Jul 2014 16:12:35 -0700 Subject: Port FlatBuffers to Go. Implement code generation and runtime library for Go, derived from the Java implementation. Additionally, the test suite verifies: - the exact bytes in the Builder buffer during object construction, - vtable deduplication, and - table construction, via a fuzzer derived from the C++ implementation. Change-Id: Ib95a019c684891def2b50281e570b4843fea7baa --- .gitignore | 2 +- CMakeLists.txt | 1 + build/VS2010/flatc.vcxproj | 9 +- build/VS2010/flatc.vcxproj.user | 7 +- build/VS2010/flatsamplebinary.vcxproj | 6 +- build/VS2010/flatsampletext.vcxproj | 6 +- build/VS2010/flattests.vcxproj | 8 +- build/Xcode/FlatBuffers.xcodeproj/project.pbxproj | 4 + docs/html/index.html | 9 +- docs/html/md__go_usage.html | 106 ++ docs/html/navtree.js | 1 + docs/html/navtreeindex0.js | 11 +- docs/html/pages.html | 11 +- docs/source/FlatBuffers.md | 14 +- docs/source/GoUsage.md | 97 ++ docs/source/doxyfile | 1 + go/builder.go | 635 ++++++++++++ go/doc.go | 3 + go/encode.go | 216 ++++ go/struct.go | 8 + go/table.go | 290 ++++++ go/unsafe.go | 45 + include/flatbuffers/idl.h | 64 +- src/flatc.cpp | 4 +- src/idl_gen_cpp.cpp | 2 +- src/idl_gen_go.cpp | 680 +++++++++++++ src/idl_gen_java.cpp | 17 +- src/idl_gen_text.cpp | 6 +- src/idl_parser.cpp | 16 +- tests/GoTest.sh | 54 + tests/MyGame/Example/Any.go | 8 + tests/MyGame/Example/Color.go | 9 + tests/MyGame/Example/Monster.go | 196 ++++ tests/MyGame/Example/Test.go | 26 + tests/MyGame/Example/Vec3.go | 45 + tests/go_test.go | 1096 +++++++++++++++++++++ 36 files changed, 3631 insertions(+), 82 deletions(-) create mode 100644 docs/html/md__go_usage.html create mode 100755 docs/source/GoUsage.md create mode 100644 go/builder.go create mode 100644 go/doc.go create mode 100644 go/encode.go create mode 100644 go/struct.go create mode 100644 go/table.go create mode 100644 go/unsafe.go create mode 100755 src/idl_gen_go.cpp create mode 100755 tests/GoTest.sh create mode 100644 tests/MyGame/Example/Any.go create mode 100644 tests/MyGame/Example/Color.go create mode 100644 tests/MyGame/Example/Monster.go create mode 100644 tests/MyGame/Example/Test.go create mode 100644 tests/MyGame/Example/Vec3.go create mode 100644 tests/go_test.go diff --git a/.gitignore b/.gitignore index c6bcf20d..dd945932 100755 --- a/.gitignore +++ b/.gitignore @@ -33,4 +33,4 @@ flattests flatsamplebinary flatsampletext snapshot.sh - +tests/go_gen diff --git a/CMakeLists.txt b/CMakeLists.txt index 2fa4160b..fc0363aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ set(FlatBuffers_Compiler_SRCS src/idl_parser.cpp src/idl_gen_cpp.cpp src/idl_gen_java.cpp + src/idl_gen_go.cpp src/idl_gen_text.cpp src/flatc.cpp ) diff --git a/build/VS2010/flatc.vcxproj b/build/VS2010/flatc.vcxproj index d63edc48..e9313cfe 100755 --- a/build/VS2010/flatc.vcxproj +++ b/build/VS2010/flatc.vcxproj @@ -91,11 +91,12 @@ NotUsing MultiThreadedDebugDLL true - Level3 + Level4 WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR="Debug";%(PreprocessorDefinitions) Debug $(IntDir) ../../Debug/flatc.pdb + true WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR=\"Debug\";%(PreprocessorDefinitions) @@ -180,13 +181,14 @@ NotUsing MultiThreadedDLL true - Level3 + Level4 WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="Release";%(PreprocessorDefinitions) Release $(IntDir) ../../Release/flatc.pdb + true WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\";%(PreprocessorDefinitions) @@ -264,6 +266,9 @@ + + Level4 + diff --git a/build/VS2010/flatc.vcxproj.user b/build/VS2010/flatc.vcxproj.user index 53bc18a0..b7bcc9a5 100755 --- a/build/VS2010/flatc.vcxproj.user +++ b/build/VS2010/flatc.vcxproj.user @@ -3,21 +3,22 @@ ..\..\tests WindowsLocalDebugger - -j -c -b -t monster_test.fbs monsterdata_test.golden + -j -c -g -b -t monster_test.fbs monsterdata_test.golden ..\.. WindowsLocalDebugger + -j -c -g -b -t monster_test.fbs monsterdata_test.golden - -j -c -b -t monster_test.fbs monsterdata_test.golden + -j -c -g -b -t monster_test.fbs monsterdata_test.golden ..\..\tests WindowsLocalDebugger - -j -c -b -t monster_test.fbs monsterdata_test.json + -j -c -g -b -t monster_test.fbs monsterdata_test.json ..\..\tests diff --git a/build/VS2010/flatsamplebinary.vcxproj b/build/VS2010/flatsamplebinary.vcxproj index dfa710c1..9d5fb107 100755 --- a/build/VS2010/flatsamplebinary.vcxproj +++ b/build/VS2010/flatsamplebinary.vcxproj @@ -91,11 +91,12 @@ NotUsing MultiThreadedDebugDLL true - Level3 + Level4 WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR="Debug";%(PreprocessorDefinitions) Debug $(IntDir) ../../Debug/flatsamplebinary.pdb + true WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR=\"Debug\";%(PreprocessorDefinitions) @@ -180,13 +181,14 @@ NotUsing MultiThreadedDLL true - Level3 + Level4 WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="Release";%(PreprocessorDefinitions) Release $(IntDir) ../../Release/flatsamplebinary.pdb + true WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\";%(PreprocessorDefinitions) diff --git a/build/VS2010/flatsampletext.vcxproj b/build/VS2010/flatsampletext.vcxproj index 5b81e6df..2519e50c 100755 --- a/build/VS2010/flatsampletext.vcxproj +++ b/build/VS2010/flatsampletext.vcxproj @@ -91,11 +91,12 @@ NotUsing MultiThreadedDebugDLL true - Level3 + Level4 WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR="Debug";%(PreprocessorDefinitions) Debug $(IntDir) ../../Debug/flatsampletext.pdb + true WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR=\"Debug\";%(PreprocessorDefinitions) @@ -180,13 +181,14 @@ NotUsing MultiThreadedDLL true - Level3 + Level4 WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="Release";%(PreprocessorDefinitions) Release $(IntDir) ../../Release/flatsampletext.pdb + true WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\";%(PreprocessorDefinitions) diff --git a/build/VS2010/flattests.vcxproj b/build/VS2010/flattests.vcxproj index 694a81cb..e4c2746c 100755 --- a/build/VS2010/flattests.vcxproj +++ b/build/VS2010/flattests.vcxproj @@ -91,11 +91,12 @@ NotUsing MultiThreadedDebugDLL true - Level3 + Level4 WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR="Debug";%(PreprocessorDefinitions) Debug $(IntDir) ../../Debug/flattests.pdb + true WIN32;_WINDOWS;_DEBUG;CMAKE_INTDIR=\"Debug\";%(PreprocessorDefinitions) @@ -180,13 +181,14 @@ NotUsing MultiThreadedDLL true - Level3 + Level4 WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="Release";%(PreprocessorDefinitions) Release $(IntDir) ../../Release/flattests.pdb + true WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\";%(PreprocessorDefinitions) @@ -225,7 +227,7 @@ NotUsing MultiThreadedDLL true - EnableAllWarnings + Level4 WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="Release";%(PreprocessorDefinitions) diff --git a/build/Xcode/FlatBuffers.xcodeproj/project.pbxproj b/build/Xcode/FlatBuffers.xcodeproj/project.pbxproj index ead87f8d..edf486cc 100644 --- a/build/Xcode/FlatBuffers.xcodeproj/project.pbxproj +++ b/build/Xcode/FlatBuffers.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 3343DD4ED370434BBA148FAB /* idl_gen_java.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3803689175184C7E8CB3EED0 /* idl_gen_java.cpp */; settings = {COMPILER_FLAGS = ""; }; }; 61823BBC53544106B6DBC38E /* idl_parser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3709AC883348409592530AE6 /* idl_parser.cpp */; settings = {COMPILER_FLAGS = ""; }; }; 61FF3C34FBEC4819A1C30F92 /* sample_text.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ECCEBFFA6977404F858F9739 /* sample_text.cpp */; settings = {COMPILER_FLAGS = ""; }; }; + 8C303C591975D6A700D7C1C5 /* idl_gen_go.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8C303C581975D6A700D7C1C5 /* idl_gen_go.cpp */; }; A9C9A99F719A4ED58DC2D2FC /* idl_parser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3709AC883348409592530AE6 /* idl_parser.cpp */; settings = {COMPILER_FLAGS = ""; }; }; AA9BACF55EB3456BA2F633BB /* flatc.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0DFD29781D8E490284B06504 /* flatc.cpp */; settings = {COMPILER_FLAGS = ""; }; }; AD71FEBEE4E846529002C1F0 /* idl_gen_text.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6C5D81DBF864365B12E269D /* idl_gen_text.cpp */; settings = {COMPILER_FLAGS = ""; }; }; @@ -32,6 +33,7 @@ 423CA92401AE442B91546E63 /* CMakeLists.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = CMakeLists.txt; path = /Users/wvo/flatbuffers_snapshot9/CMakeLists.txt; sourceTree = ""; }; 5EE44BFFAF8E43F485859145 /* sample_binary.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = sample_binary.cpp; path = samples/sample_binary.cpp; sourceTree = SOURCE_ROOT; }; 6AD24EEB3D024825A37741FF /* test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = test.cpp; path = tests/test.cpp; sourceTree = SOURCE_ROOT; }; + 8C303C581975D6A700D7C1C5 /* idl_gen_go.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = idl_gen_go.cpp; path = src/idl_gen_go.cpp; sourceTree = ""; }; A13F25CDAD23435DA293690D /* flattests */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; path = flattests; sourceTree = BUILT_PRODUCTS_DIR; }; AB70F1FBA50E4120BCF37C8D /* monster_test_generated.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = monster_test_generated.h; path = tests/monster_test_generated.h; sourceTree = SOURCE_ROOT; }; AD3682C6E1DD4EABB822C0CC /* monster_generated.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = monster_generated.h; path = samples/monster_generated.h; sourceTree = SOURCE_ROOT; }; @@ -53,6 +55,7 @@ 28237E300FE042DEADA302D3 /* Source Files */ = { isa = PBXGroup; children = ( + 8C303C581975D6A700D7C1C5 /* idl_gen_go.cpp */, 0DFD29781D8E490284B06504 /* flatc.cpp */, CD90A7F6B2BE4D0384294DD1 /* idl_gen_cpp.cpp */, 3803689175184C7E8CB3EED0 /* idl_gen_java.cpp */, @@ -314,6 +317,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 8C303C591975D6A700D7C1C5 /* idl_gen_go.cpp in Sources */, AA9BACF55EB3456BA2F633BB /* flatc.cpp in Sources */, BE03D7B0C9584DD58B50ED34 /* idl_gen_cpp.cpp in Sources */, 3343DD4ED370434BBA148FAB /* idl_gen_java.cpp in Sources */, diff --git a/docs/html/index.html b/docs/html/index.html index f005a565..ab9ec47b 100644 --- a/docs/html/index.html +++ b/docs/html/index.html @@ -53,7 +53,7 @@ $(document).ready(function(){initNavTree('index.html','');});
FlatBuffers Documentation
-

FlatBuffers is an efficient cross platform serialization library in for C++ and Java. It was created at Google specifically for game development and other performance-critical applications.

+

FlatBuffers is an efficient cross platform serialization library for C++, with support for Java and Go. It was created at Google specifically for game development and other performance-critical applications.

It is available as open source under the Apache license, v2 (see LICENSE.txt).

Why use FlatBuffers?

    @@ -63,9 +63,9 @@ $(document).ready(function(){initNavTree('index.html','');});
  • Tiny code footprint - Small amounts of generated code, and just a single small header as the minimum dependency, which is very easy to integrate. Again, see the benchmark section for details.
  • Strongly typed - Errors happen at compile time rather than manually having to write repetitive and error prone run-time checks. Useful code can be generated for you.
  • Convenient to use - Generated C++ code allows for terse access & construction code. Then there's optional functionality for parsing schemas and JSON-like text representations at runtime efficiently if needed (faster and more memory efficient than other JSON parsers).

    -

    Java code supports object-reuse.

    +

    Java and Go code supports object-reuse.

  • -
  • Cross platform C++11/Java code with no dependencies - will work with any recent gcc/clang and VS2010. Comes with build files for the tests & samples (Android .mk files, and cmake for all other platforms).
  • +
  • Cross platform C++11/Java/Go code with no dependencies - will work with any recent gcc/clang and VS2010. Comes with build files for the tests & samples (Android .mk files, and cmake for all other platforms).

Why not use Protocol Buffers, or .. ?

Protocol Buffers is indeed relatively similar to FlatBuffers, with the primary difference being that FlatBuffers does not need a parsing/ unpacking step to a secondary representation before you can access data, often coupled with per-object memory allocation. The code is an order of magnitude bigger, too. Protocol Buffers has neither optional text import/export nor schema language features like unions.

@@ -76,7 +76,7 @@ $(document).ready(function(){initNavTree('index.html','');});

This section is a quick rundown of how to use this system. Subsequent sections provide a more in-depth usage guide.

  • Write a schema file that allows you to define the data structures you may want to serialize. Fields can have a scalar type (ints/floats of all sizes), or they can be a: string; array of any type; reference to yet another object; or, a set of possible objects (unions). Fields are optional and have defaults, so they don't need to be present for every object instance.
  • -
  • Use flatc (the FlatBuffer compiler) to generate a C++ header (or Java classes) with helper classes to access and construct serialized data. This header (say mydata_generated.h) only depends on flatbuffers.h, which defines the core functionality.
  • +
  • Use flatc (the FlatBuffer compiler) to generate a C++ header (or Java/Go classes) with helper classes to access and construct serialized data. This header (say mydata_generated.h) only depends on flatbuffers.h, which defines the core functionality.
  • Use the FlatBufferBuilder class to construct a flat binary buffer. The generated functions allow you to add objects to this buffer recursively, often as simply as making a single function call.
  • Store or send your buffer somewhere!
  • When reading it back, you can obtain the pointer to the root object from the binary buffer, and from there traverse it conveniently in-place with object->field().
  • @@ -88,6 +88,7 @@ $(document).ready(function(){initNavTree('index.html','');});
  • How to write a schema.
  • How to use the generated C++ code in your own programs.
  • How to use the generated Java code in your own programs.
  • +
  • How to use the generated Go code in your own programs.
  • Some benchmarks showing the advantage of using FlatBuffers.
  • A white paper explaining the "why" of FlatBuffers.
  • A description of the internals of FlatBuffers.
  • diff --git a/docs/html/md__go_usage.html b/docs/html/md__go_usage.html new file mode 100644 index 00000000..8a662508 --- /dev/null +++ b/docs/html/md__go_usage.html @@ -0,0 +1,106 @@ + + + + + + +FlatBuffers: Use in Go + + + + + + + + + + +
    +
    + + + + + + +
    +
    FlatBuffers +
    +
    +
    + + +
    +
    + +
    +
    +
    + +
    +
    +
    +
    Use in Go
    +
    +
    +

    There's experimental support for reading FlatBuffers in Go. Generate code for Go with the -g option to flatc.

    +

    See go_test.go for an example. You import the generated code, read a FlatBuffer binary file into a []byte, which you pass to the GetRootAsMonster function:

    import (
    +   example "MyGame/Example"
    +   flatbuffers "github.com/google/flatbuffers/go"
    +
    +   io/ioutil
    +)
    +
    +buf, err := ioutil.ReadFile("monster.dat")
    +// handle err
    +monster := example.GetRootAsMonster(buf, 0)
    +

    Now you can access values like this:

    hp := monster.Hp()
    +pos := monster.Pos(nil)
    +

    Note that whenever you access a new object like in the Pos example above, a new temporary accessor object gets created. If your code is very performance sensitive (you iterate through a lot of objects), you can replace nil with a pointer to a Vec3 object you've already created. This allows you to reuse it across many calls and reduce the amount of object allocation (and thus garbage collection) your program does.

    +

    To access vectors you pass an extra index to the vector field accessor. Then a second method with the same name suffixed by Length let's you know the number of elements you can access:

    for i := 0; i < monster.InventoryLength(); i++ {
    +    monster.Inventory(i) // do something here
    +}
    +

    You can also construct these buffers in Go using the functions found in the generated code, and the FlatBufferBuilder class:

    builder := flatbuffers.NewBuilder(0)
    +

    Create strings:

    str := builder.CreateString("MyMonster")
    +

    Create a table with a struct contained therein:

    example.MonsterStart(builder)
    +example.MonsterAddPos(builder, example.CreateVec3(builder, 1.0, 2.0, 3.0, 3.0, 4, 5, 6))
    +example.MonsterAddHp(builder, 80)
    +example.MonsterAddName(builder, str)
    +example.MonsterAddInventory(builder, inv)
    +example.MonsterAddTest_Type(builder, 1)
    +example.MonsterAddTest(builder, mon2)
    +example.MonsterAddTest4(builder, test4s)
    +mon := example.MonsterEnd(builder)
    +

    Unlike C++, Go does not support table creation functions like 'createMonster()'. This is to create the buffer without using temporary object allocation (since the Vec3 is an inline component of Monster, it has to be created right where it is added, whereas the name and the inventory are not inline). Structs do have convenient methods that allow you to construct them in one call. These even have arguments for nested structs, e.g. if a struct has a field a and a nested struct field b (which has fields c and d), then the arguments will be a, c and d.

    +

    Vectors also use this start/end pattern to allow vectors of both scalar types and structs:

    example.MonsterStartInventoryVector(builder, 5)
    +for i := 4; i >= 0; i-- {
    +    builder.PrependByte(byte(i))
    +}
    +inv := builder.EndVector(5)
    +

    The generated method 'StartInventoryVector' is provided as a convenience function which calls 'StartVector' with the correct element size of the vector type which in this case is 'ubyte' or 1 byte per vector element. You pass the number of elements you want to write. You write the elements backwards since the buffer is being constructed back to front.

    +

    There are Prepend functions for all the scalar types. You use PrependUOffset for any previously constructed objects (such as other tables, strings, vectors). For structs, you use the appropriate create function in-line, as shown above in the Monster example.

    +

    Text Parsing

    +

    There currently is no support for parsing text (Schema's and JSON) directly from Go, though you could use the C++ parser through cgo. Please see the C++ documentation for more on text parsing.

    +
    +
    + + + + diff --git a/docs/html/navtree.js b/docs/html/navtree.js index abdc1d0a..9ff8c25e 100644 --- a/docs/html/navtree.js +++ b/docs/html/navtree.js @@ -5,6 +5,7 @@ var NAVTREE = [ "Using the schema compiler", "md__compiler.html", null ], [ "Writing a schema", "md__schemas.html", null ], [ "Use in C++", "md__cpp_usage.html", null ], + [ "Use in Go", "md__go_usage.html", null ], [ "Use in Java", "md__java_usage.html", null ], [ "Benchmarks", "md__benchmarks.html", null ], [ "FlatBuffers white paper", "md__white_paper.html", null ], diff --git a/docs/html/navtreeindex0.js b/docs/html/navtreeindex0.js index 45204501..7dda4761 100644 --- a/docs/html/navtreeindex0.js +++ b/docs/html/navtreeindex0.js @@ -1,14 +1,15 @@ var NAVTREEINDEX0 = { "index.html":[], -"md__benchmarks.html":[5], +"md__benchmarks.html":[6], "md__building.html":[0], "md__compiler.html":[1], "md__cpp_usage.html":[3], -"md__grammar.html":[8], -"md__internals.html":[7], -"md__java_usage.html":[4], +"md__go_usage.html":[4], +"md__grammar.html":[9], +"md__internals.html":[8], +"md__java_usage.html":[5], "md__schemas.html":[2], -"md__white_paper.html":[6], +"md__white_paper.html":[7], "pages.html":[] }; diff --git a/docs/html/pages.html b/docs/html/pages.html index 4d71a95e..5810c722 100644 --- a/docs/html/pages.html +++ b/docs/html/pages.html @@ -59,11 +59,12 @@ $(document).ready(function(){initNavTree('pages.html','');});  Using the schema compiler  Writing a schema  Use in C++ - Use in Java - Benchmarks - FlatBuffers white paper - FlatBuffer Internals - Formal Grammar of the schema language + Use in Go + Use in Java + Benchmarks + FlatBuffers white paper + FlatBuffer Internals + Formal Grammar of the schema language
diff --git a/docs/source/FlatBuffers.md b/docs/source/FlatBuffers.md index 8f0401e4..fd308555 100644 --- a/docs/source/FlatBuffers.md +++ b/docs/source/FlatBuffers.md @@ -1,8 +1,8 @@ # FlatBuffers -FlatBuffers is an efficient cross platform serialization library in for C++ and -Java. It was created at Google specifically for game development and other -performance-critical applications. +FlatBuffers is an efficient cross platform serialization library for C++, +with support for Java and Go. It was created at Google specifically for game +development and other performance-critical applications. It is available as open source under the Apache license, v2 (see LICENSE.txt). @@ -46,9 +46,9 @@ It is available as open source under the Apache license, v2 (see LICENSE.txt). needed (faster and more memory efficient than other JSON parsers). - Java code supports object-reuse. + Java and Go code supports object-reuse. -- **Cross platform C++11/Java code with no dependencies** - will work with +- **Cross platform C++11/Java/Go code with no dependencies** - will work with any recent gcc/clang and VS2010. Comes with build files for the tests & samples (Android .mk files, and cmake for all other platforms). @@ -87,7 +87,7 @@ sections provide a more in-depth usage guide. Fields are optional and have defaults, so they don't need to be present for every object instance. -- Use `flatc` (the FlatBuffer compiler) to generate a C++ header (or Java +- Use `flatc` (the FlatBuffer compiler) to generate a C++ header (or Java/Go classes) with helper classes to access and construct serialized data. This header (say `mydata_generated.h`) only depends on `flatbuffers.h`, which defines the core functionality. @@ -112,6 +112,8 @@ sections provide a more in-depth usage guide. programs. - How to [use the generated Java code](md__java_usage.html) in your own programs. +- How to [use the generated Go code](md__go_usage.html) in your own + programs. - Some [benchmarks](md__benchmarks.html) showing the advantage of using FlatBuffers. - A [white paper](md__white_paper.html) explaining the "why" of FlatBuffers. diff --git a/docs/source/GoUsage.md b/docs/source/GoUsage.md new file mode 100755 index 00000000..0c2370b8 --- /dev/null +++ b/docs/source/GoUsage.md @@ -0,0 +1,97 @@ +# Use in Go + +There's experimental support for reading FlatBuffers in Go. Generate code +for Go with the `-g` option to `flatc`. + +See `go_test.go` for an example. You import the generated code, read a +FlatBuffer binary file into a `[]byte`, which you pass to the +`GetRootAsMonster` function: + + import ( + example "MyGame/Example" + flatbuffers "github.com/google/flatbuffers/go" + + io/ioutil + ) + + buf, err := ioutil.ReadFile("monster.dat") + // handle err + monster := example.GetRootAsMonster(buf, 0) + +Now you can access values like this: + + hp := monster.Hp() + pos := monster.Pos(nil) + +Note that whenever you access a new object like in the `Pos` example above, +a new temporary accessor object gets created. If your code is very performance +sensitive (you iterate through a lot of objects), you can replace nil with a +pointer to a `Vec3` object you've already created. This allows +you to reuse it across many calls and reduce the amount of object allocation +(and thus garbage collection) your program does. + +To access vectors you pass an extra index to the +vector field accessor. Then a second method with the same name suffixed +by `Length` let's you know the number of elements you can access: + + for i := 0; i < monster.InventoryLength(); i++ { + monster.Inventory(i) // do something here + } + +You can also construct these buffers in Go using the functions found in the +generated code, and the FlatBufferBuilder class: + + builder := flatbuffers.NewBuilder(0) + +Create strings: + + str := builder.CreateString("MyMonster") + +Create a table with a struct contained therein: + + example.MonsterStart(builder) + example.MonsterAddPos(builder, example.CreateVec3(builder, 1.0, 2.0, 3.0, 3.0, 4, 5, 6)) + example.MonsterAddHp(builder, 80) + example.MonsterAddName(builder, str) + example.MonsterAddInventory(builder, inv) + example.MonsterAddTest_Type(builder, 1) + example.MonsterAddTest(builder, mon2) + example.MonsterAddTest4(builder, test4s) + mon := example.MonsterEnd(builder) + +Unlike C++, Go does not support table creation functions like 'createMonster()'. +This is to create the buffer without +using temporary object allocation (since the `Vec3` is an inline component of +`Monster`, it has to be created right where it is added, whereas the name and +the inventory are not inline). +Structs do have convenient methods that allow you to construct them in one call. +These also have arguments for nested structs, e.g. if a struct has a field `a` +and a nested struct field `b` (which has fields `c` and `d`), then the arguments +will be `a`, `c` and `d`. + +Vectors also use this start/end pattern to allow vectors of both scalar types +and structs: + + example.MonsterStartInventoryVector(builder, 5) + for i := 4; i >= 0; i-- { + builder.PrependByte(byte(i)) + } + inv := builder.EndVector(5) + +The generated method 'StartInventoryVector' is provided as a convenience +function which calls 'StartVector' with the correct element size of the vector +type which in this case is 'ubyte' or 1 byte per vector element. +You pass the number of elements you want to write. +You write the elements backwards since the buffer +is being constructed back to front. + +There are `Prepend` functions for all the scalar types. You use +`PrependUOffset` for any previously constructed objects (such as other tables, +strings, vectors). For structs, you use the appropriate `create` function +in-line, as shown above in the `Monster` example. + +## Text Parsing + +There currently is no support for parsing text (Schema's and JSON) directly +from Go, though you could use the C++ parser through cgo. Please see the +C++ documentation for more on text parsing. diff --git a/docs/source/doxyfile b/docs/source/doxyfile index f1e1981b..e0ece1d2 100755 --- a/docs/source/doxyfile +++ b/docs/source/doxyfile @@ -748,6 +748,7 @@ INPUT = "FlatBuffers.md" \ "Compiler.md" \ "Schemas.md" \ "CppUsage.md" \ + "GoUsage.md" \ "JavaUsage.md" \ "Benchmarks.md" \ "WhitePaper.md" \ diff --git a/go/builder.go b/go/builder.go new file mode 100644 index 00000000..423620b9 --- /dev/null +++ b/go/builder.go @@ -0,0 +1,635 @@ +package flatbuffers + +// Builder is a state machine for creating FlatBuffer objects. +// Use a Builder to construct object(s) starting from leaf nodes. +// +// A Builder constructs byte buffers in a last-first manner for simplicity and +// performance. +type Builder struct { + Bytes []byte + + minalign int + vtable []UOffsetT + objectEnd UOffsetT + vtables []UOffsetT + head UOffsetT +} + +// NewBuilder initializes a Builder of size `initial_size`. +// The internal buffer is grown as needed. +func NewBuilder(initialSize int) *Builder { + if initialSize <= 0 { + initialSize = 0 + } + + b := &Builder{} + b.Bytes = make([]byte, initialSize) + b.head = UOffsetT(initialSize) + b.minalign = 1 + b.vtables = make([]UOffsetT, 0, 16) // sensible default capacity + + return b +} + +// StartObject initializes bookkeeping for writing a new object. +func (b *Builder) StartObject(numfields int) { + b.notNested() + // use 32-bit offsets so that arithmetic doesn't overflow. + b.vtable = make([]UOffsetT, numfields) + b.objectEnd = b.Offset() + b.minalign = 1 +} + +// WriteVtable serializes the vtable for the current object, if applicable. +// +// Before writing out the vtable, this checks pre-existing vtables for equality +// to this one. If an equal vtable is found, point the object to the existing +// vtable and return. +// +// Because vtable values are sensitive to alignment of object data, not all +// logically-equal vtables will be deduplicated. +// +// A vtable has the following format: +// +// +// * N, where N is the number of fields in +// the schema for this type. Includes deprecated fields. +// Thus, a vtable is made of 2 + N elements, each SizeVOffsetT bytes wide. +// +// An object has the following format: +// +// + +func (b *Builder) WriteVtable() (n UOffsetT) { + // Prepend a zero scalar to the object. Later in this function we'll + // write an offset here that points to the object's vtable: + b.PrependSOffsetT(0) + + objectOffset := b.Offset() + existingVtable := UOffsetT(0) + + // Search backwards through existing vtables, because similar vtables + // are likely to have been recently appended. See + // BenchmarkVtableDeduplication for a case in which this heuristic + // saves about 30% of the time used in writing objects with duplicate + // tables. + for i := len(b.vtables) - 1; i >= 0; i-- { + // Find the other vtable, which is associated with `i`: + vt2Offset := b.vtables[i] + vt2Start := len(b.Bytes) - int(vt2Offset) + vt2Len := GetVOffsetT(b.Bytes[vt2Start:]) + + metadata := VtableMetadataFields * SizeVOffsetT + vt2End := vt2Start + int(vt2Len) + vt2 := b.Bytes[vt2Start+metadata : vt2End] + + // Compare the other vtable to the one under consideration. + // If they are equal, store the offset and break: + if vtableEqual(b.vtable, objectOffset, vt2) { + existingVtable = vt2Offset + break + } + } + + if existingVtable == 0 { + // Did not find a vtable, so write this one to the buffer. + + // Write out the current vtable in reverse , because + // serialization occurs in last-first order: + for i := len(b.vtable) - 1; i >= 0; i-- { + var off UOffsetT + if b.vtable[i] != 0 { + // Forward reference to field; + // use 32bit number to ensure no overflow: + off = objectOffset - b.vtable[i] + } + + b.PrependVOffsetT(VOffsetT(off)) + } + + // The two metadata fields are written last. + + // First, store the object bytesize: + objectSize := objectOffset - b.objectEnd + b.PrependVOffsetT(VOffsetT(objectSize)) + + // Second, store the vtable bytesize: + vBytes := (len(b.vtable) + VtableMetadataFields) * SizeVOffsetT + b.PrependVOffsetT(VOffsetT(vBytes)) + + // Next, write the offset to the new vtable in the + // already-allocated SOffsetT at the beginning of this object: + objectStart := SOffsetT(len(b.Bytes)) - SOffsetT(objectOffset) + WriteSOffsetT(b.Bytes[objectStart:], + SOffsetT(b.Offset())-SOffsetT(objectOffset)) + + // Finally, store this vtable in memory for future + // deduplication: + b.vtables = append(b.vtables, b.Offset()) + } else { + // Found a duplicate vtable. + + objectStart := SOffsetT(len(b.Bytes)) - SOffsetT(objectOffset) + b.head = UOffsetT(objectStart) + + // Write the offset to the found vtable in the + // already-allocated SOffsetT at the beginning of this object: + WriteSOffsetT(b.Bytes[b.head:], + SOffsetT(existingVtable)-SOffsetT(objectOffset)) + } + + b.vtable = nil + return objectOffset +} + +// EndObject writes data necessary to finish object construction. +func (b *Builder) EndObject() UOffsetT { + if b.vtable == nil { + panic("not in object") + } + return b.WriteVtable() +} + +// Doubles the size of the byteslice, and copies the old data towards the +// end of the new byteslice (since we build the buffer backwards). +func (b *Builder) growByteBuffer() { + if (len(b.Bytes) & 0xC0000000) != 0 { + panic("cannot grow buffer beyond 2 gigabytes") + } + newSize := len(b.Bytes) * 2 + if newSize == 0 { + newSize = 1 + } + bytes2 := make([]byte, newSize) + copy(bytes2[newSize-len(b.Bytes):], b.Bytes) + b.Bytes = bytes2 +} + +// Head gives the start of useful data in the underlying byte buffer. +// Note: unlike other functions, this value is interpreted as from the left. +func (b *Builder) Head() UOffsetT { + return b.head +} + +// Offset relative to the end of the buffer. +func (b *Builder) Offset() UOffsetT { + return UOffsetT(len(b.Bytes)) - b.head +} + +// Pad places zeros at the current offset. +func (b *Builder) Pad(n int) { + for i := 0; i < n; i++ { + b.PlaceByte(0) + } +} + +// Prep prepares to write an element of `size` after `additional_bytes` +// have been written, e.g. if you write a string, you need to align such +// the int length field is aligned to SizeInt32, and the string data follows it +// directly. +// If all you need to do is align, `additionalBytes` will be 0. +func (b *Builder) Prep(size, additionalBytes int) { + // Track the biggest thing we've ever aligned to. + if size > b.minalign { + b.minalign = size + } + // Find the amount of alignment needed such that `size` is properly + // aligned after `additionalBytes`: + alignSize := (^(len(b.Bytes) - int(b.Head()) + additionalBytes)) + 1 + alignSize &= (size - 1) + + // Reallocate the buffer if needed: + for int(b.head) < alignSize+size+additionalBytes { + oldBufSize := len(b.Bytes) + b.growByteBuffer() + b.head += UOffsetT(len(b.Bytes) - oldBufSize) + } + b.Pad(alignSize) +} + +// PrependSOffsetT prepends an SOffsetT, relative to where it will be written. +func (b *Builder) PrependSOffsetT(off SOffsetT) { + b.Prep(SizeSOffsetT, 0) // Ensure alignment is already done. + if !(UOffsetT(off) <= b.Offset()) { + panic("unreachable: off <= b.Offset()") + } + off2 := SOffsetT(b.Offset()) - off + SOffsetT(SizeSOffsetT) + b.PlaceSOffsetT(off2) +} + +// PrependUOffsetT prepends an UOffsetT, relative to where it will be written. +func (b *Builder) PrependUOffsetT(off UOffsetT) { + b.Prep(SizeUOffsetT, 0) // Ensure alignment is already done. + if !(off <= b.Offset()) { + panic("unreachable: off <= b.Offset()") + } + off2 := b.Offset() - off + UOffsetT(SizeUOffsetT) + b.PlaceUOffsetT(off2) +} + +// StartVector initializes bookkeeping for writing a new vector. +// +// A vector has the following format: +// +// +, where T is the type of elements of this vector. +func (b *Builder) StartVector(elemSize, numElems int) UOffsetT { + b.notNested() + b.Prep(SizeUint32, elemSize*numElems) + return b.Offset() +} + +// EndVector writes data necessary to finish vector construction. +func (b *Builder) EndVector(vectorNumElems int) UOffsetT { + // we already made space for this, so write without PrependUint32 + b.PlaceUOffsetT(UOffsetT(vectorNumElems)) + return b.Offset() +} + +// CreateString writes a null-terminated string as a vector. +func (b *Builder) CreateString(s string) UOffsetT { + b.Prep(int(SizeUOffsetT), (len(s)+1)*SizeByte) + b.PlaceByte(0) + + x := []byte(s) + l := UOffsetT(len(x)) + + b.head -= l + copy(b.Bytes[b.head:b.head+l], x) + + return b.EndVector(len(x)) +} + +func (b *Builder) notNested() { + // Check that no other objects are being built while making this + // object. If not, panic: + if b.vtable != nil { + panic("non-inline data write inside of object") + } +} + +func (b *Builder) nested(obj UOffsetT) { + // Structs are always stored inline, so need to be created right + // where they are used. You'll get this panic if you created it + // elsewhere: + if obj != b.Offset() { + panic("inline data write outside of object") + } +} + +// PrependBoolSlot prepends a bool onto the object at vtable slot `o`. +// If value `x` equals default `d`, then the slot will be set to zero and no +// other data will be written. +func (b *Builder) PrependBoolSlot(o int, x, d bool) { + val := byte(0) + if x { + val = 1 + } + def := byte(0) + if d { + def = 1 + } + b.PrependByteSlot(o, val, def) +} + +// PrependByteSlot prepends a byte onto the object at vtable slot `o`. +// If value `x` equals default `d`, then the slot will be set to zero and no +// other data will be written. +func (b *Builder) PrependByteSlot(o int, x, d byte) { + if x != d { + b.PrependByte(x) + b.Slot(o) + } +} + +// PrependUint8Slot prepends a uint8 onto the object at vtable slot `o`. +// If value `x` equals default `d`, then the slot will be set to zero and no +// other data will be written. +func (b *Builder) PrependUint8Slot(o int, x, d uint8) { + if x != d { + b.PrependUint8(x) + b.Slot(o) + } +} + +// PrependUint16Slot prepends a uint16 onto the object at vtable slot `o`. +// If value `x` equals default `d`, then the slot will be set to zero and no +// other data will be written. +func (b *Builder) PrependUint16Slot(o int, x, d uint16) { + if x != d { + b.PrependUint16(x) + b.Slot(o) + } +} + +// PrependUint32Slot prepends a uint32 onto the object at vtable slot `o`. +// If value `x` equals default `d`, then the slot will be set to zero and no +// other data will be written. +func (b *Builder) PrependUint32Slot(o int, x, d uint32) { + if x != d { + b.PrependUint32(x) + b.Slot(o) + } +} + +// PrependUint64Slot prepends a uint64 onto the object at vtable slot `o`. +// If value `x` equals default `d`, then the slot will be set to zero and no +// other data will be written. +func (b *Builder) PrependUint64Slot(o int, x, d uint64) { + if x != d { + b.PrependUint64(x) + b.Slot(o) + } +} + +// PrependInt8Slot prepends a int8 onto the object at vtable slot `o`. +// If value `x` equals default `d`, then the slot will be set to zero and no +// other data will be written. +func (b *Builder) PrependInt8Slot(o int, x, d int8) { + if x != d { + b.PrependInt8(x) + b.Slot(o) + } +} + +// PrependInt16Slot prepends a int16 onto the object at vtable slot `o`. +// If value `x` equals default `d`, then the slot will be set to zero and no +// other data will be written. +func (b *Builder) PrependInt16Slot(o int, x, d int16) { + if x != d { + b.PrependInt16(x) + b.Slot(o) + } +} + +// PrependInt32Slot prepends a int32 onto the object at vtable slot `o`. +// If value `x` equals default `d`, then the slot will be set to zero and no +// other data will be written. +func (b *Builder) PrependInt32Slot(o int, x, d int32) { + if x != d { + b.PrependInt32(x) + b.Slot(o) + } +} + +// PrependInt64Slot prepends a int64 onto the object at vtable slot `o`. +// If value `x` equals default `d`, then the slot will be set to zero and no +// other data will be written. +func (b *Builder) PrependInt64Slot(o int, x, d int64) { + if x != d { + b.PrependInt64(x) + b.Slot(o) + } +} + +// PrependFloat32Slot prepends a float32 onto the object at vtable slot `o`. +// If value `x` equals default `d`, then the slot will be set to zero and no +// other data will be written. +func (b *Builder) PrependFloat32Slot(o int, x, d float32) { + if x != d { + b.PrependFloat32(x) + b.Slot(o) + } +} + +// PrependFloat64Slot prepends a float64 onto the object at vtable slot `o`. +// If value `x` equals default `d`, then the slot will be set to zero and no +// other data will be written. +func (b *Builder) PrependFloat64Slot(o int, x, d float64) { + if x != d { + b.PrependFloat64(x) + b.Slot(o) + } +} + +// PrependUOffsetTSlot prepends an UOffsetT onto the object at vtable slot `o`. +// If value `x` equals default `d`, then the slot will be set to zero and no +// other data will be written. +func (b *Builder) PrependUOffsetTSlot(o int, x, d UOffsetT) { + if x != d { + b.PrependUOffsetT(x) + b.Slot(o) + } +} + +// PrependStructSlot prepends a struct onto the object at vtable slot `o`. +// Structs are stored inline, so nothing additional is being added. +// In generated code, `d` is always 0. +func (b *Builder) PrependStructSlot(voffset int, x, d UOffsetT) { + if x != d { + b.nested(x) + b.Slot(voffset) + } +} + +// Slot sets the vtable key `voffset` to the current location in the buffer. +func (b *Builder) Slot(slotnum int) { + b.vtable[slotnum] = UOffsetT(b.Offset()) +} + +// Finish finalizes a buffer, pointing to the given `rootTable`. +func (b *Builder) Finish(rootTable UOffsetT) { + b.Prep(b.minalign, SizeUOffsetT) + b.PrependUOffsetT(rootTable) +} + +// vtableEqual compares an unwritten vtable to a written vtable. +func vtableEqual(a []UOffsetT, objectStart UOffsetT, b []byte) bool { + if len(a)*SizeVOffsetT != len(b) { + return false + } + + for i := 0; i < len(a); i++ { + x := GetVOffsetT(b[i*SizeVOffsetT : (i+1)*SizeVOffsetT]) + + // Skip vtable entries that indicate a default value. + if x == 0 && a[i] == 0 { + continue + } + + y := SOffsetT(objectStart) - SOffsetT(a[i]) + if SOffsetT(x) != y { + return false + } + } + return true +} + +// PrependBool prepends a bool to the Builder buffer. +// Aligns and checks for space. +func (b *Builder) PrependBool(x bool) { + b.Prep(SizeBool, 0) + b.PlaceBool(x) +} + +// PrependUint8 prepends a uint8 to the Builder buffer. +// Aligns and checks for space. +func (b *Builder) PrependUint8(x uint8) { + b.Prep(SizeUint8, 0) + b.PlaceUint8(x) +} + +// PrependUint16 prepends a uint16 to the Builder buffer. +// Aligns and checks for space. +func (b *Builder) PrependUint16(x uint16) { + b.Prep(SizeUint16, 0) + b.PlaceUint16(x) +} + +// PrependUint32 prepends a uint32 to the Builder buffer. +// Aligns and checks for space. +func (b *Builder) PrependUint32(x uint32) { + b.Prep(SizeUint32, 0) + b.PlaceUint32(x) +} + +// PrependUint64 prepends a uint64 to the Builder buffer. +// Aligns and checks for space. +func (b *Builder) PrependUint64(x uint64) { + b.Prep(SizeUint64, 0) + b.PlaceUint64(x) +} + +// PrependInt8 prepends a int8 to the Builder buffer. +// Aligns and checks for space. +func (b *Builder) PrependInt8(x int8) { + b.Prep(SizeInt8, 0) + b.PlaceInt8(x) +} + +// PrependInt16 prepends a int16 to the Builder buffer. +// Aligns and checks for space. +func (b *Builder) PrependInt16(x int16) { + b.Prep(SizeInt16, 0) + b.PlaceInt16(x) +} + +// PrependInt32 prepends a int32 to the Builder buffer. +// Aligns and checks for space. +func (b *Builder) PrependInt32(x int32) { + b.Prep(SizeInt32, 0) + b.PlaceInt32(x) +} + +// PrependInt64 prepends a int64 to the Builder buffer. +// Aligns and checks for space. +func (b *Builder) PrependInt64(x int64) { + b.Prep(SizeInt64, 0) + b.PlaceInt64(x) +} + +// PrependFloat32 prepends a float32 to the Builder buffer. +// Aligns and checks for space. +func (b *Builder) PrependFloat32(x float32) { + b.Prep(SizeFloat32, 0) + b.PlaceFloat32(x) +} + +// PrependFloat64 prepends a float64 to the Builder buffer. +// Aligns and checks for space. +func (b *Builder) PrependFloat64(x float64) { + b.Prep(SizeFloat64, 0) + b.PlaceFloat64(x) +} + +// PrependByte prepends a byte to the Builder buffer. +// Aligns and checks for space. +func (b *Builder) PrependByte(x byte) { + b.Prep(SizeByte, 0) + b.PlaceByte(x) +} + +// PrependVOffsetT prepends a VOffsetT to the Builder buffer. +// Aligns and checks for space. +func (b *Builder) PrependVOffsetT(x VOffsetT) { + b.Prep(SizeVOffsetT, 0) + b.PlaceVOffsetT(x) +} + +// PlaceBool prepends a bool to the Builder, without checking for space. +func (b *Builder) PlaceBool(x bool) { + b.head -= UOffsetT(SizeBool) + WriteBool(b.Bytes[b.head:], x) +} + +// PlaceUint8 prepends a uint8 to the Builder, without checking for space. +func (b *Builder) PlaceUint8(x uint8) { + b.head -= UOffsetT(SizeUint8) + WriteUint8(b.Bytes[b.head:], x) +} + +// PlaceUint16 prepends a uint16 to the Builder, without checking for space. +func (b *Builder) PlaceUint16(x uint16) { + b.head -= UOffsetT(SizeUint16) + WriteUint16(b.Bytes[b.head:], x) +} + +// PlaceUint32 prepends a uint32 to the Builder, without checking for space. +func (b *Builder) PlaceUint32(x uint32) { + b.head -= UOffsetT(SizeUint32) + WriteUint32(b.Bytes[b.head:], x) +} + +// PlaceUint64 prepends a uint64 to the Builder, without checking for space. +func (b *Builder) PlaceUint64(x uint64) { + b.head -= UOffsetT(SizeUint64) + WriteUint64(b.Bytes[b.head:], x) +} + +// PlaceInt8 prepends a int8 to the Builder, without checking for space. +func (b *Builder) PlaceInt8(x int8) { + b.head -= UOffsetT(SizeInt8) + WriteInt8(b.Bytes[b.head:], x) +} + +// PlaceInt16 prepends a int16 to the Builder, without checking for space. +func (b *Builder) PlaceInt16(x int16) { + b.head -= UOffsetT(SizeInt16) + WriteInt16(b.Bytes[b.head:], x) +} + +// PlaceInt32 prepends a int32 to the Builder, without checking for space. +func (b *Builder) PlaceInt32(x int32) { + b.head -= UOffsetT(SizeInt32) + WriteInt32(b.Bytes[b.head:], x) +} + +// PlaceInt64 prepends a int64 to the Builder, without checking for space. +func (b *Builder) PlaceInt64(x int64) { + b.head -= UOffsetT(SizeInt64) + WriteInt64(b.Bytes[b.head:], x) +} + +// PlaceFloat32 prepends a float32 to the Builder, without checking for space. +func (b *Builder) PlaceFloat32(x float32) { + b.head -= UOffsetT(SizeFloat32) + WriteFloat32(b.Bytes[b.head:], x) +} + +// PlaceFloat64 prepends a float64 to the Builder, without checking for space. +func (b *Builder) PlaceFloat64(x float64) { + b.head -= UOffsetT(SizeFloat64) + WriteFloat64(b.Bytes[b.head:], x) +} + +// PlaceByte prepends a byte to the Builder, without checking for space. +func (b *Builder) PlaceByte(x byte) { + b.head -= UOffsetT(SizeByte) + WriteByte(b.Bytes[b.head:], x) +} + +// PlaceVOffsetT prepends a VOffsetT to the Builder, without checking for space. +func (b *Builder) PlaceVOffsetT(x VOffsetT) { + b.head -= UOffsetT(SizeVOffsetT) + WriteVOffsetT(b.Bytes[b.head:], x) +} + +// PlaceSOffsetT prepends a SOffsetT to the Builder, without checking for space. +func (b *Builder) PlaceSOffsetT(x SOffsetT) { + b.head -= UOffsetT(SizeSOffsetT) + WriteSOffsetT(b.Bytes[b.head:], x) +} + +// PlaceUOffsetT prepends a UOffsetT to the Builder, without checking for space. +func (b *Builder) PlaceUOffsetT(x UOffsetT) { + b.head -= UOffsetT(SizeUOffsetT) + WriteUOffsetT(b.Bytes[b.head:], x) +} diff --git a/go/doc.go b/go/doc.go new file mode 100644 index 00000000..694edc76 --- /dev/null +++ b/go/doc.go @@ -0,0 +1,3 @@ +// Package flatbuffers provides facilities to read and write flatbuffers +// objects. +package flatbuffers diff --git a/go/encode.go b/go/encode.go new file mode 100644 index 00000000..48ff36ef --- /dev/null +++ b/go/encode.go @@ -0,0 +1,216 @@ +package flatbuffers + +import ( + "math" +) + +type ( + // A SOffsetT stores a signed offset into arbitrary data. + SOffsetT int32 + // A UOffsetT stores an unsigned offset into vector data. + UOffsetT uint32 + // A VOffsetT stores an unsigned offset in a vtable. + VOffsetT uint16 +) + +const ( + // VtableMetadataFields is the count of metadata fields in each vtable. + VtableMetadataFields = 2 +) + +// GetByte decodes a little-endian byte from a byte slice. +func GetByte(buf []byte) byte { + return byte(GetUint8(buf)) +} + +// GetBool decodes a little-endian bool from a byte slice. +func GetBool(buf []byte) bool { + return buf[0] == 1 +} + +// GetUint8 decodes a little-endian uint8 from a byte slice. +func GetUint8(buf []byte) (n uint8) { + n = uint8(buf[0]) + return +} + +// GetUint16 decodes a little-endian uint16 from a byte slice. +func GetUint16(buf []byte) (n uint16) { + n |= uint16(buf[0]) + n |= uint16(buf[1]) << 8 + return +} + +// GetUint32 decodes a little-endian uint32 from a byte slice. +func GetUint32(buf []byte) (n uint32) { + n |= uint32(buf[0]) + n |= uint32(buf[1]) << 8 + n |= uint32(buf[2]) << 16 + n |= uint32(buf[3]) << 24 + return +} + +// GetUint64 decodes a little-endian uint64 from a byte slice. +func GetUint64(buf []byte) (n uint64) { + n |= uint64(buf[0]) + n |= uint64(buf[1]) << 8 + n |= uint64(buf[2]) << 16 + n |= uint64(buf[3]) << 24 + n |= uint64(buf[4]) << 32 + n |= uint64(buf[5]) << 40 + n |= uint64(buf[6]) << 48 + n |= uint64(buf[7]) << 56 + return +} + +// GetInt8 decodes a little-endian int8 from a byte slice. +func GetInt8(buf []byte) (n int8) { + n = int8(buf[0]) + return +} + +// GetInt16 decodes a little-endian int16 from a byte slice. +func GetInt16(buf []byte) (n int16) { + n |= int16(buf[0]) + n |= int16(buf[1]) << 8 + return +} + +// GetInt32 decodes a little-endian int32 from a byte slice. +func GetInt32(buf []byte) (n int32) { + n |= int32(buf[0]) + n |= int32(buf[1]) << 8 + n |= int32(buf[2]) << 16 + n |= int32(buf[3]) << 24 + return +} + +// GetInt64 decodes a little-endian int64 from a byte slice. +func GetInt64(buf []byte) (n int64) { + n |= int64(buf[0]) + n |= int64(buf[1]) << 8 + n |= int64(buf[2]) << 16 + n |= int64(buf[3]) << 24 + n |= int64(buf[4]) << 32 + n |= int64(buf[5]) << 40 + n |= int64(buf[6]) << 48 + n |= int64(buf[7]) << 56 + return +} + +// GetFloat32 decodes a little-endian float32 from a byte slice. +func GetFloat32(buf []byte) float32 { + x := GetUint32(buf) + return math.Float32frombits(x) +} + +// GetFloat64 decodes a little-endian float64 from a byte slice. +func GetFloat64(buf []byte) float64 { + x := GetUint64(buf) + return math.Float64frombits(x) +} + +// GetUOffsetT decodes a little-endian UOffsetT from a byte slice. +func GetUOffsetT(buf []byte) UOffsetT { + return UOffsetT(GetInt32(buf)) +} + +// GetSOffsetT decodes a little-endian SOffsetT from a byte slice. +func GetSOffsetT(buf []byte) SOffsetT { + return SOffsetT(GetInt32(buf)) +} + +// GetVOffsetT decodes a little-endian VOffsetT from a byte slice. +func GetVOffsetT(buf []byte) VOffsetT { + return VOffsetT(GetUint16(buf)) +} + +// WriteByte encodes a little-endian uint8 into a byte slice. +func WriteByte(buf []byte, n byte) { + WriteUint8(buf, uint8(n)) +} + +// WriteBool encodes a little-endian bool into a byte slice. +func WriteBool(buf []byte, b bool) { + buf[0] = 0 + if b { + buf[0] = 1 + } +} + +// WriteUint8 encodes a little-endian uint8 into a byte slice. +func WriteUint8(buf []byte, n uint8) { + buf[0] = byte(n) +} + +// WriteUint16 encodes a little-endian uint16 into a byte slice. +func WriteUint16(buf []byte, n uint16) { + buf[0] = byte(n) + buf[1] = byte(n >> 8) +} + +// WriteUint32 encodes a little-endian uint32 into a byte slice. +func WriteUint32(buf []byte, n uint32) { + buf[0] = byte(n) + buf[1] = byte(n >> 8) + buf[2] = byte(n >> 16) + buf[3] = byte(n >> 24) +} + +// WriteUint64 encodes a little-endian uint64 into a byte slice. +func WriteUint64(buf []byte, n uint64) { + for i := uint(0); i < uint(SizeUint64); i++ { + buf[i] = byte(n >> (i * 8)) + } +} + +// WriteInt8 encodes a little-endian int8 into a byte slice. +func WriteInt8(buf []byte, n int8) { + buf[0] = byte(n) +} + +// WriteInt16 encodes a little-endian int16 into a byte slice. +func WriteInt16(buf []byte, n int16) { + buf[0] = byte(n) + buf[1] = byte(n >> 8) +} + +// WriteInt32 encodes a little-endian int32 into a byte slice. +func WriteInt32(buf []byte, n int32) { + buf[0] = byte(n) + buf[1] = byte(n >> 8) + buf[2] = byte(n >> 16) + buf[3] = byte(n >> 24) +} + +// WriteInt64 encodes a little-endian int64 into a byte slice. +func WriteInt64(buf []byte, n int64) { + for i := uint(0); i < uint(SizeInt64); i++ { + buf[i] = byte(n >> (i * 8)) + } +} + +// WriteFloat32 encodes a little-endian float32 into a byte slice. +func WriteFloat32(buf []byte, n float32) { + WriteUint32(buf, math.Float32bits(n)) +} + +// WriteFloat64 encodes a little-endian float64 into a byte slice. +func WriteFloat64(buf []byte, n float64) { + WriteUint64(buf, math.Float64bits(n)) +} + +// WriteVOffsetT encodes a little-endian VOffsetT into a byte slice. +func WriteVOffsetT(buf []byte, n VOffsetT) { + WriteUint16(buf, uint16(n)) +} + +// WriteSOffsetT encodes a little-endian SOffsetT into a byte slice. +func WriteSOffsetT(buf []byte, n SOffsetT) { + WriteInt32(buf, int32(n)) +} + +// WriteUOffsetT encodes a little-endian UOffsetT into a byte slice. +func WriteUOffsetT(buf []byte, n UOffsetT) { + WriteUint32(buf, uint32(n)) +} diff --git a/go/struct.go b/go/struct.go new file mode 100644 index 00000000..11258f71 --- /dev/null +++ b/go/struct.go @@ -0,0 +1,8 @@ +package flatbuffers + +// Struct wraps a byte slice and provides read access to its data. +// +// Structs do not have a vtable. +type Struct struct { + Table +} diff --git a/go/table.go b/go/table.go new file mode 100644 index 00000000..21c11218 --- /dev/null +++ b/go/table.go @@ -0,0 +1,290 @@ +package flatbuffers + +// Table wraps a byte slice and provides read access to its data. +// +// The variable `Pos` indicates the root of the FlatBuffers object therein. +type Table struct { + Bytes []byte + Pos UOffsetT // Always < 1<<31. +} + +// Offset provides access into the Table's vtable. +// +// Deprecated fields are ignored by checking against the vtable's length. +func (t *Table) Offset(vtableOffset VOffsetT) VOffsetT { + vtable := UOffsetT(SOffsetT(t.Pos) - t.GetSOffsetT(t.Pos)) + if vtableOffset < t.GetVOffsetT(vtable) { + return t.GetVOffsetT(vtable + UOffsetT(vtableOffset)) + } + return 0 +} + +// Indirect retrieves the relative offset stored at `offset`. +func (t *Table) Indirect(off UOffsetT) UOffsetT { + return off + GetUOffsetT(t.Bytes[off:]) +} + +// String gets a string from data stored inside the flatbuffer. +func (t *Table) String(off UOffsetT) string { + off += t.Pos + off += GetUOffsetT(t.Bytes[off:]) + start := off + UOffsetT(SizeUOffsetT) + length := GetUOffsetT(t.Bytes[off:]) + return string(t.Bytes[start : start+length]) +} + +// VectorLen retrieves the length of the vector whose offset is stored at +// "off" in this object. +func (t *Table) VectorLen(off UOffsetT) int { + off += t.Pos + off += GetUOffsetT(t.Bytes[off:]) + return int(GetUOffsetT(t.Bytes[off:])) +} + +// Vector retrieves the start of data of the vector whose offset is stored +// at "off" in this object. +func (t *Table) Vector(off UOffsetT) UOffsetT { + off += t.Pos + x := off + GetUOffsetT(t.Bytes[off:]) + // data starts after metadata containing the vector length + x += UOffsetT(SizeUOffsetT) + return x +} + +// Union initializes any Table-derived type to point to the union at the given +// offset. +func (t *Table) Union(t2 *Table, off UOffsetT) { + off += t.Pos + t2.Pos = off + t.GetUOffsetT(off) + t2.Bytes = t.Bytes +} + +// GetBool retrieves a bool at the given offset. +func (t *Table) GetBool(off UOffsetT) bool { + return GetBool(t.Bytes[off:]) +} + +// GetByte retrieves a byte at the given offset. +func (t *Table) GetByte(off UOffsetT) byte { + return GetByte(t.Bytes[off:]) +} + +// GetUint8 retrieves a uint8 at the given offset. +func (t *Table) GetUint8(off UOffsetT) uint8 { + return GetUint8(t.Bytes[off:]) +} + +// GetUint16 retrieves a uint16 at the given offset. +func (t *Table) GetUint16(off UOffsetT) uint16 { + return GetUint16(t.Bytes[off:]) +} + +// GetUint32 retrieves a uint32 at the given offset. +func (t *Table) GetUint32(off UOffsetT) uint32 { + return GetUint32(t.Bytes[off:]) +} + +// GetUint64 retrieves a uint64 at the given offset. +func (t *Table) GetUint64(off UOffsetT) uint64 { + return GetUint64(t.Bytes[off:]) +} + +// GetInt8 retrieves a int8 at the given offset. +func (t *Table) GetInt8(off UOffsetT) int8 { + return GetInt8(t.Bytes[off:]) +} + +// GetInt16 retrieves a int16 at the given offset. +func (t *Table) GetInt16(off UOffsetT) int16 { + return GetInt16(t.Bytes[off:]) +} + +// GetInt32 retrieves a int32 at the given offset. +func (t *Table) GetInt32(off UOffsetT) int32 { + return GetInt32(t.Bytes[off:]) +} + +// GetInt64 retrieves a int64 at the given offset. +func (t *Table) GetInt64(off UOffsetT) int64 { + return GetInt64(t.Bytes[off:]) +} + +// GetFloat32 retrieves a float32 at the given offset. +func (t *Table) GetFloat32(off UOffsetT) float32 { + return GetFloat32(t.Bytes[off:]) +} + +// GetFloat64 retrieves a float64 at the given offset. +func (t *Table) GetFloat64(off UOffsetT) float64 { + return GetFloat64(t.Bytes[off:]) +} + +// GetUOffsetT retrieves a UOffsetT at the given offset. +func (t *Table) GetUOffsetT(off UOffsetT) UOffsetT { + return GetUOffsetT(t.Bytes[off:]) +} + +// GetVOffsetT retrieves a VOffsetT at the given offset. +func (t *Table) GetVOffsetT(off UOffsetT) VOffsetT { + return GetVOffsetT(t.Bytes[off:]) +} + +// GetSOffsetT retrieves a SOffsetT at the given offset. +func (t *Table) GetSOffsetT(off UOffsetT) SOffsetT { + return GetSOffsetT(t.Bytes[off:]) +} + +// GetBoolSlot retrieves the bool that the given vtable location +// points to. If the vtable value is zero, the default value `d` +// will be returned. +func (t *Table) GetBoolSlot(slot VOffsetT, d bool) bool { + off := t.Offset(slot) + if off == 0 { + return d + } + + return t.GetBool(t.Pos + UOffsetT(off)) +} + +// GetByteSlot retrieves the byte that the given vtable location +// points to. If the vtable value is zero, the default value `d` +// will be returned. +func (t *Table) GetByteSlot(slot VOffsetT, d byte) byte { + off := t.Offset(slot) + if off == 0 { + return d + } + + return t.GetByte(t.Pos + UOffsetT(off)) +} + +// GetInt8Slot retrieves the int8 that the given vtable location +// points to. If the vtable value is zero, the default value `d` +// will be returned. +func (t *Table) GetInt8Slot(slot VOffsetT, d int8) int8 { + off := t.Offset(slot) + if off == 0 { + return d + } + + return t.GetInt8(t.Pos + UOffsetT(off)) +} + +// GetUint8Slot retrieves the uint8 that the given vtable location +// points to. If the vtable value is zero, the default value `d` +// will be returned. +func (t *Table) GetUint8Slot(slot VOffsetT, d uint8) uint8 { + off := t.Offset(slot) + if off == 0 { + return d + } + + return t.GetUint8(t.Pos + UOffsetT(off)) +} + +// GetInt16Slot retrieves the int16 that the given vtable location +// points to. If the vtable value is zero, the default value `d` +// will be returned. +func (t *Table) GetInt16Slot(slot VOffsetT, d int16) int16 { + off := t.Offset(slot) + if off == 0 { + return d + } + + return t.GetInt16(t.Pos + UOffsetT(off)) +} + +// GetUint16Slot retrieves the uint16 that the given vtable location +// points to. If the vtable value is zero, the default value `d` +// will be returned. +func (t *Table) GetUint16Slot(slot VOffsetT, d uint16) uint16 { + off := t.Offset(slot) + if off == 0 { + return d + } + + return t.GetUint16(t.Pos + UOffsetT(off)) +} + +// GetInt32Slot retrieves the int32 that the given vtable location +// points to. If the vtable value is zero, the default value `d` +// will be returned. +func (t *Table) GetInt32Slot(slot VOffsetT, d int32) int32 { + off := t.Offset(slot) + if off == 0 { + return d + } + + return t.GetInt32(t.Pos + UOffsetT(off)) +} + +// GetUint32Slot retrieves the uint32 that the given vtable location +// points to. If the vtable value is zero, the default value `d` +// will be returned. +func (t *Table) GetUint32Slot(slot VOffsetT, d uint32) uint32 { + off := t.Offset(slot) + if off == 0 { + return d + } + + return t.GetUint32(t.Pos + UOffsetT(off)) +} + +// GetInt64Slot retrieves the int64 that the given vtable location +// points to. If the vtable value is zero, the default value `d` +// will be returned. +func (t *Table) GetInt64Slot(slot VOffsetT, d int64) int64 { + off := t.Offset(slot) + if off == 0 { + return d + } + + return t.GetInt64(t.Pos + UOffsetT(off)) +} + +// GetUint64Slot retrieves the uint64 that the given vtable location +// points to. If the vtable value is zero, the default value `d` +// will be returned. +func (t *Table) GetUint64Slot(slot VOffsetT, d uint64) uint64 { + off := t.Offset(slot) + if off == 0 { + return d + } + + return t.GetUint64(t.Pos + UOffsetT(off)) +} + +// GetFloat32Slot retrieves the float32 that the given vtable location +// points to. If the vtable value is zero, the default value `d` +// will be returned. +func (t *Table) GetFloat32Slot(slot VOffsetT, d float32) float32 { + off := t.Offset(slot) + if off == 0 { + return d + } + + return t.GetFloat32(t.Pos + UOffsetT(off)) +} + +// GetFloat64Slot retrieves the float64 that the given vtable location +// points to. If the vtable value is zero, the default value `d` +// will be returned. +func (t *Table) GetFloat64Slot(slot VOffsetT, d float64) float64 { + off := t.Offset(slot) + if off == 0 { + return d + } + + return t.GetFloat64(t.Pos + UOffsetT(off)) +} + +// GetVOffsetTSlot retrieves the VOffsetT that the given vtable location +// points to. If the vtable value is zero, the default value `d` +// will be returned. +func (t *Table) GetVOffsetTSlot(slot VOffsetT, d VOffsetT) VOffsetT { + off := t.Offset(slot) + if off == 0 { + return d + } + return VOffsetT(off) +} diff --git a/go/unsafe.go b/go/unsafe.go new file mode 100644 index 00000000..e90ccb09 --- /dev/null +++ b/go/unsafe.go @@ -0,0 +1,45 @@ +package flatbuffers + +import "unsafe" + +var ( + // See http://golang.org/ref/spec#Numeric_types + + // SizeUint8 is the byte size of a uint8. + SizeUint8 = int(unsafe.Sizeof(uint8(0))) + // SizeUint16 is the byte size of a uint16. + SizeUint16 = int(unsafe.Sizeof(uint16(0))) + // SizeUint32 is the byte size of a uint32. + SizeUint32 = int(unsafe.Sizeof(uint32(0))) + // SizeUint64 is the byte size of a uint64. + SizeUint64 = int(unsafe.Sizeof(uint64(0))) + + // SizeInt8 is the byte size of a int8. + SizeInt8 = int(unsafe.Sizeof(int8(0))) + // SizeInt16 is the byte size of a int16. + SizeInt16 = int(unsafe.Sizeof(int16(0))) + // SizeInt32 is the byte size of a int32. + SizeInt32 = int(unsafe.Sizeof(int32(0))) + // SizeInt64 is the byte size of a int64. + SizeInt64 = int(unsafe.Sizeof(int64(0))) + + // SizeFloat32 is the byte size of a float32. + SizeFloat32 = int(unsafe.Sizeof(float32(0))) + // SizeFloat64 is the byte size of a float64. + SizeFloat64 = int(unsafe.Sizeof(float64(0))) + + // SizeByte is the byte size of a byte. + // The `byte` type is aliased (by Go definition) to uint8. + SizeByte = SizeUint8 + + // SizeBool is the byte size of a bool. + // The `bool` type is aliased (by flatbuffers convention) to uint8. + SizeBool = SizeUint8 + + // SizeSOffsetT is the byte size of an SOffsetT. + SizeSOffsetT = int(unsafe.Sizeof(SOffsetT(0))) + // SizeUOffsetT is the byte size of an UOffsetT. + SizeUOffsetT = int(unsafe.Sizeof(UOffsetT(0))) + // SizeVOffsetT is the byte size of an VOffsetT. + SizeVOffsetT = int(unsafe.Sizeof(VOffsetT(0))) +) diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 53657d73..cdec8bb7 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -31,31 +31,31 @@ namespace flatbuffers { // Additionally, Parser::ParseType assumes bool..string is a contiguous range // of type tokens. #define FLATBUFFERS_GEN_TYPES_SCALAR(TD) \ - TD(NONE, "", uint8_t, byte ) \ - TD(UTYPE, "", uint8_t, byte ) /* begin scalars, ints */ \ - TD(BOOL, "bool", uint8_t, byte ) \ - TD(CHAR, "byte", int8_t, byte ) \ - TD(UCHAR, "ubyte", uint8_t, byte ) \ - TD(SHORT, "short", int16_t, short ) \ - TD(USHORT, "ushort", uint16_t, short ) \ - TD(INT, "int", int32_t, int ) \ - TD(UINT, "uint", uint32_t, int ) \ - TD(LONG, "long", int64_t, long ) \ - TD(ULONG, "ulong", uint64_t, long ) /* end ints */ \ - TD(FLOAT, "float", float, float ) /* begin floats */ \ - TD(DOUBLE, "double", double, double) /* end floats, scalars */ + TD(NONE, "", uint8_t, byte, byte) \ + TD(UTYPE, "", uint8_t, byte, byte) /* begin scalars, ints */ \ + TD(BOOL, "bool", uint8_t, byte, byte) \ + TD(CHAR, "byte", int8_t, byte, int8) \ + TD(UCHAR, "ubyte", uint8_t, byte, byte) \ + TD(SHORT, "short", int16_t, short, int16) \ + TD(USHORT, "ushort", uint16_t, short, uint16) \ + TD(INT, "int", int32_t, int, int32) \ + TD(UINT, "uint", uint32_t, int, uint32) \ + TD(LONG, "long", int64_t, long, int64) \ + TD(ULONG, "ulong", uint64_t, long, uint64) /* end ints */ \ + TD(FLOAT, "float", float, float, float32) /* begin floats */ \ + TD(DOUBLE, "double", double, double, float64) /* end floats, scalars */ #define FLATBUFFERS_GEN_TYPES_POINTER(TD) \ - TD(STRING, "string", Offset, int) \ - TD(VECTOR, "", Offset, int) \ - TD(STRUCT, "", Offset, int) \ - TD(UNION, "", Offset, int) + TD(STRING, "string", Offset, int, int) \ + TD(VECTOR, "", Offset, int, int) \ + TD(STRUCT, "", Offset, int, int) \ + TD(UNION, "", Offset, int, int) // using these macros, we can now write code dealing with types just once, e.g. /* switch (type) { - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \ + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \ case BASE_TYPE_ ## ENUM: \ // do something specific to CTYPE here FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) @@ -69,12 +69,12 @@ switch (type) { // Create an enum for all the types above enum BaseType { - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) BASE_TYPE_ ## ENUM, + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) BASE_TYPE_ ## ENUM, FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) #undef FLATBUFFERS_TD }; -#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \ +#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \ static_assert(sizeof(CTYPE) <= sizeof(largest_scalar_t), \ "define largest_scalar_t as " #CTYPE); FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) @@ -291,6 +291,23 @@ class Parser { std::vector struct_stack_; }; +// Utility functions for generators: + +// Convert an underscore_based_indentifier in to camelCase. +// Also uppercases the first character if first is true. +inline std::string MakeCamel(const std::string &in, bool first = true) { + std::string s; + for (size_t i = 0; i < in.length(); i++) { + if (!i && first) + s += static_cast(toupper(in[0])); + else if (in[i] == '_' && i + 1 < in.length()) + s += static_cast(toupper(in[++i])); + else + s += in[i]; + } + return s; +} + // Container of options that may apply to any of the source/text generators. struct GeneratorOptions { bool strict_json; @@ -321,6 +338,13 @@ extern bool GenerateCPP(const Parser &parser, const std::string &file_name, const GeneratorOptions &opts); +// Generate Go files from the definitions in the Parser object. +// See idl_gen_go.cpp. +extern bool GenerateGo(const Parser &parser, + const std::string &path, + const std::string &file_name, + const GeneratorOptions &opts); + // Generate Java files from the definitions in the Parser object. // See idl_gen_java.cpp. extern bool GenerateJava(const Parser &parser, diff --git a/src/flatc.cpp b/src/flatc.cpp index 4327de77..ab873474 100755 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -71,6 +71,8 @@ const Generator generators[] = { "Generate text output for any data definitions" }, { flatbuffers::GenerateCPP, "c", "C++", "Generate C++ headers for tables/structs" }, + { flatbuffers::GenerateGo, "g", "Go", + "Generate Go files for tables/structs" }, { flatbuffers::GenerateJava, "j", "Java", "Generate Java classes for tables/structs" }, }; @@ -162,7 +164,7 @@ int main(int argc, const char *argv[]) { if (!any_generator) Error("no options: no output files generated.", - "specify one of -c -j -t -b etc.", true); + "specify one of -c -g -j -t -b etc.", true); // Now process the files: for (auto file_it = filenames.begin(); diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index c711bb7f..cd560c5c 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -26,7 +26,7 @@ namespace cpp { // Return a C++ type from the table in idl.h static std::string GenTypeBasic(const Type &type) { static const char *ctypename[] = { - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) #CTYPE, + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) #CTYPE, FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) #undef FLATBUFFERS_TD }; diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp new file mode 100755 index 00000000..f15955be --- /dev/null +++ b/src/idl_gen_go.cpp @@ -0,0 +1,680 @@ +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// independent from idl_parser, since this code is not needed for most clients + +#include + +#include "flatbuffers/flatbuffers.h" +#include "flatbuffers/idl.h" +#include "flatbuffers/util.h" + +#ifdef _WIN32 +#include +#define PATH_SEPARATOR "\\" +#define mkdir(n, m) _mkdir(n) +#else +#include +#define PATH_SEPARATOR "/" +#endif + +namespace flatbuffers { +namespace go { + +static std::string GenGetter(const Type &type); +static std::string GenMethod(const FieldDef &field); +static void GenStructBuilder(const StructDef &struct_def, + std::string *code_ptr); +static void GenReceiver(const StructDef &struct_def, std::string *code_ptr); +static std::string GenTypeBasic(const Type &type); +static std::string GenTypeGet(const Type &type); +static std::string TypeName(const FieldDef &field); + + +// Write a comment. +static void Comment(const std::string &dc, + std::string *code_ptr, + const char *prefix = "") { + std::string &code = *code_ptr; + if (dc.length()) { + code += std::string(prefix) + "///" + dc + "\n"; + } +} + +// Most field accessors need to retrieve and test the field offset first, +// this is the prefix code for that. +std::string OffsetPrefix(const FieldDef &field) { + return "{\n\to := flatbuffers.UOffsetT(rcv._tab.Offset(" + + NumToString(field.value.offset) + + "))\n\tif o != 0 {\n"; +} + +// Begin by declaring namespace and imports. +static void BeginFile(const std::string name_space_name, + const bool needs_imports, + std::string *code_ptr) { + std::string &code = *code_ptr; + code += "// automatically generated, do not modify\n\n"; + code += "package " + name_space_name + "\n\n"; + if (needs_imports) { + code += "import (\n"; + code += "\tflatbuffers \"github.com/google/flatbuffers/go\"\n"; + code += ")\n"; + } +} + +// Begin a class declaration. +static void BeginClass(const StructDef &struct_def, std::string *code_ptr) { + std::string &code = *code_ptr; + + code += "type " + struct_def.name + " struct {\n\t"; + + // _ is reserved in flatbuffers field names, so no chance of name conflict: + code += "_tab "; + code += struct_def.fixed ? "flatbuffers.Struct" : "flatbuffers.Table"; + code += "\n}\n\n"; +} + +// Begin enum code with a class declaration. +static void BeginEnum(std::string *code_ptr) { + std::string &code = *code_ptr; + code += "const (\n"; +} + +// A single enum member. +static void EnumMember(const EnumDef &enum_def, const EnumVal ev, + std::string *code_ptr) { + std::string &code = *code_ptr; + code += "\t"; + code += enum_def.name; + code += ev.name; + code += " = "; + code += NumToString(ev.value) + "\n"; +} + +// End enum code. +static void EndEnum(std::string *code_ptr) { + std::string &code = *code_ptr; + code += ")\n"; +} + +// Initialize a new struct or table from existing data. +static void NewRootTypeFromBuffer(const StructDef &struct_def, + std::string *code_ptr) { + std::string &code = *code_ptr; + + code += "func GetRootAs"; + code += struct_def.name; + code += "(buf []byte, offset flatbuffers.UOffsetT) "; + code += "*" + struct_def.name + ""; + code += " {\n"; + code += "\tn := flatbuffers.GetUOffsetT(buf[offset:])\n"; + code += "\tx := &" + struct_def.name + "{}\n"; + code += "\tx.Init(buf, n + offset)\n"; + code += "\treturn x\n"; + code += "}\n\n"; +} + +// Initialize an existing object with other data, to avoid an allocation. +static void InitializeExisting(const StructDef &struct_def, + std::string *code_ptr) { + std::string &code = *code_ptr; + + GenReceiver(struct_def, code_ptr); + code += " Init(buf []byte, i flatbuffers.UOffsetT) "; + code += "{\n"; + code += "\trcv._tab.Bytes = buf\n"; + code += "\trcv._tab.Pos = i\n"; + code += "}\n\n"; +} + +// Get the length of a vector. +static void GetVectorLen(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) + "Length("; + code += ") int " + OffsetPrefix(field); + code += "\t\treturn rcv._tab.VectorLen(o)\n\t}\n"; + code += "\treturn 0\n}\n\n"; +} + +// Get the value of a struct's scalar. +static void GetScalarFieldOfStruct(const StructDef &struct_def, + const FieldDef &field, + std::string *code_ptr) { + std::string &code = *code_ptr; + std::string getter = GenGetter(field.value.type); + GenReceiver(struct_def, code_ptr); + code += " " + MakeCamel(field.name); + code += "() " + TypeName(field) + " { return " + getter; + code += "(rcv._tab.Pos + flatbuffers.UOffsetT("; + code += NumToString(field.value.offset) + ")) }\n"; +} + +// Get the value of a table's scalar. +static void GetScalarFieldOfTable(const StructDef &struct_def, + const FieldDef &field, + std::string *code_ptr) { + std::string &code = *code_ptr; + std::string getter = GenGetter(field.value.type); + GenReceiver(struct_def, code_ptr); + code += " " + MakeCamel(field.name); + code += "() " + TypeName(field) + " "; + code += OffsetPrefix(field) + "\t\treturn " + getter; + code += "(o + rcv._tab.Pos)\n\t}\n"; + code += "\treturn " + field.value.constant + "\n"; + code += "}\n\n"; +} + +// Get a struct by initializing an existing struct. +// Specific to Struct. +static void GetStructFieldOfStruct(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); + code += "(obj *" + TypeName(field); + code += ") *" + TypeName(field); + code += " {\n"; + code += "\tif obj == nil {\n"; + code += "\t\tobj = new(" + TypeName(field) + ")\n"; + code += "\t}\n"; + code += "\tobj.Init(rcv._tab.Bytes, rcv._tab.Pos + "; + code += NumToString(field.value.offset) + ")"; + code += "\n\treturn obj\n"; + code += "}\n"; +} + +// Get a struct by initializing an existing struct. +// Specific to Table. +static void GetStructFieldOfTable(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); + code += "(obj *"; + code += TypeName(field); + code += ") *" + TypeName(field) + " " + OffsetPrefix(field); + if (field.value.type.struct_def->fixed) { + code += "\t\tx := o + rcv._tab.Pos\n"; + } else { + code += "\t\tx := rcv._tab.Indirect(o + rcv._tab.Pos)\n"; + } + code += "\t\tif obj == nil {\n"; + code += "\t\t\tobj = new(" + TypeName(field) + ")\n"; + code += "\t\t}\n"; + code += "\t\tobj.Init(rcv._tab.Bytes, x)\n"; + code += "\t\treturn obj\n\t}\n\treturn nil\n"; + code += "}\n\n"; +} + +// Get the value of a string. +static void GetStringField(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); + code += "() " + TypeName(field) + " "; + code += OffsetPrefix(field) + "\t\treturn " + GenGetter(field.value.type); + code += "(o)\n\t}\n\treturn \"\"\n"; + code += "}\n\n"; +} + +// Get the value of a union from an object. +static void GetUnionField(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) + "("; + code += "obj " + TypeName(field) + ") bool "; + code += OffsetPrefix(field); + code += "\t\t" + GenGetter(field.value.type); + code += "(obj, o)\n\t\treturn true\n\t}\n"; + code += "\treturn false\n"; + code += "}\n\n"; +} + +// Get the value of a vector's struct member. +static void GetMemberOfVectorOfStruct(const StructDef &struct_def, + const FieldDef &field, + std::string *code_ptr) { + std::string &code = *code_ptr; + auto vectortype = field.value.type.VectorType(); + + GenReceiver(struct_def, code_ptr); + code += " " + MakeCamel(field.name); + code += "(obj *" + TypeName(field); + code += ", j int) bool " + OffsetPrefix(field); + code += "\t\tx := rcv._tab.Vector(o)\n"; + code += "\t\tx += flatbuffers.UOffsetT(j) * "; + code += NumToString(InlineSize(vectortype)) + "\n"; + if (!(vectortype.struct_def->fixed)) { + code += "\t\tx = rcv._tab.Indirect(x)\n"; + } + code += "\tif obj == nil {\n"; + code += "\t\tobj = new(" + TypeName(field) + ")\n"; + code += "\t}\n"; + code += "\t\tobj.Init(rcv._tab.Bytes, x)\n"; + code += "\t\treturn true\n\t}\n"; + code += "\treturn false\n"; + code += "}\n\n"; +} + +// Get the value of a vector's non-struct member. Uses a named return +// argument to conveniently set the zero value for the result. +static void GetMemberOfVectorOfNonStruct(const StructDef &struct_def, + const FieldDef &field, + std::string *code_ptr) { + std::string &code = *code_ptr; + auto vectortype = field.value.type.VectorType(); + + GenReceiver(struct_def, code_ptr); + code += " " + MakeCamel(field.name); + code += "(j int) " + TypeName(field) + " "; + code += OffsetPrefix(field); + code += "\t\ta := rcv._tab.Vector(o)\n"; + code += "\t\treturn " + GenGetter(field.value.type) + "("; + code += "a + flatbuffers.UOffsetT(j * "; + code += NumToString(InlineSize(vectortype)) + "))\n"; + code += "\t}\n"; + if (vectortype.base_type == BASE_TYPE_STRING) { + code += "\treturn \"\"\n"; + } else { + code += "\treturn 0\n"; + } + code += "}\n\n"; +} + +// Begin the creator function signature. +static void BeginBuilderArgs(const StructDef &struct_def, + std::string *code_ptr) { + std::string &code = *code_ptr; + + code += "\n"; + code += "func Create" + struct_def.name; + code += "(builder *flatbuffers.Builder"; +} + +// Recursively generate arguments for a constructor, to deal with nested +// structs. +static void StructBuilderArgs(const StructDef &struct_def, + const char *nameprefix, + std::string *code_ptr) { + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); + ++it) { + auto &field = **it; + if (IsStruct(field.value.type)) { + // Generate arguments for a struct inside a struct. To ensure names + // don't clash, and to make it obvious these arguments are constructing + // a nested struct, prefix the name with the struct name. + StructBuilderArgs(*field.value.type.struct_def, + (field.value.type.struct_def->name + "_").c_str(), + code_ptr); + } else { + std::string &code = *code_ptr; + code += (std::string)", " + nameprefix; + code += MakeCamel(field.name, false); + code += " " + GenTypeBasic(field.value.type); + } + } +} + +// End the creator function signature. +static void EndBuilderArgs(std::string *code_ptr) { + std::string &code = *code_ptr; + code += ") flatbuffers.UOffsetT {\n"; +} + +// Recursively generate struct construction statements and instert manual +// padding. +static void StructBuilderBody(const StructDef &struct_def, + const char *nameprefix, + std::string *code_ptr) { + std::string &code = *code_ptr; + code += " builder.Prep(" + NumToString(struct_def.minalign) + ", "; + code += NumToString(struct_def.bytesize) + ")\n"; + for (auto it = struct_def.fields.vec.rbegin(); + it != struct_def.fields.vec.rend(); + ++it) { + auto &field = **it; + if (field.padding) + code += " builder.Pad(" + NumToString(field.padding) + ")\n"; + if (IsStruct(field.value.type)) { + StructBuilderBody(*field.value.type.struct_def, + (field.value.type.struct_def->name + "_").c_str(), + code_ptr); + } else { + code += " builder.Prepend" + GenMethod(field) + "("; + code += nameprefix + MakeCamel(field.name, false) + ")\n"; + } + } +} + +static void EndBuilderBody(std::string *code_ptr) { + std::string &code = *code_ptr; + code += " return builder.Offset()\n"; + code += "}\n"; +} + +// Get the value of a table's starting offset. +static void GetStartOfTable(const StructDef &struct_def, + std::string *code_ptr) { + std::string &code = *code_ptr; + code += "func " + struct_def.name + "Start"; + code += "(builder *flatbuffers.Builder) { "; + code += "builder.StartObject("; + code += NumToString(struct_def.fields.vec.size()); + code += ") }\n"; +} + +// Set the value of a table's field. +static void BuildFieldOfTable(const StructDef &struct_def, + const FieldDef &field, + const size_t offset, + std::string *code_ptr) { + std::string &code = *code_ptr; + code += "func " + struct_def.name + "Add" + MakeCamel(field.name); + code += "(builder *flatbuffers.Builder, "; + code += MakeCamel(field.name, false) + " "; + if (!IsScalar(field.value.type.base_type) && (!struct_def.fixed)) { + code += "flatbuffers.UOffsetT"; + } else { + code += GenTypeBasic(field.value.type); + } + code += ") "; + code += "{ builder.Prepend"; + code += GenMethod(field) + "Slot("; + code += NumToString(offset) + ", "; + if (!IsScalar(field.value.type.base_type) && (!struct_def.fixed)) { + code += "flatbuffers.UOffsetT"; + code += "("; + code += MakeCamel(field.name, false) + ")"; + } else { + code += MakeCamel(field.name, false); + } + code += ", " + field.value.constant; + code += ") }\n"; +} + +// Set the value of one of the members of a table's vector. +static void BuildVectorOfTable(const StructDef &struct_def, + const FieldDef &field, + std::string *code_ptr) { + std::string &code = *code_ptr; + code += "func " + struct_def.name + "Start"; + code += MakeCamel(field.name); + code += "Vector(builder *flatbuffers.Builder, numElems int) "; + code += "flatbuffers.UOffsetT { return builder.StartVector("; + code += NumToString(InlineSize(field.value.type.VectorType())); + code += ", numElems) }\n"; +} + +// Get the offset of the end of a table. +static void GetEndOffsetOnTable(const StructDef &struct_def, + std::string *code_ptr) { + std::string &code = *code_ptr; + code += "func " + struct_def.name + "End"; + code += "(builder *flatbuffers.Builder) flatbuffers.UOffsetT "; + code += "{ return builder.EndObject() }\n"; +} + +// Generate the receiver for function signatures. +static void GenReceiver(const StructDef &struct_def, std::string *code_ptr) { + std::string &code = *code_ptr; + code += "func (rcv *" + struct_def.name + ")"; +} + +// Generate a struct field, conditioned on its child type(s). +static void GenStructAccessor(const StructDef &struct_def, + const FieldDef &field, + std::string *code_ptr) { + Comment(field.doc_comment, code_ptr, ""); + if (IsScalar(field.value.type.base_type)) { + if (struct_def.fixed) { + GetScalarFieldOfStruct(struct_def, field, code_ptr); + } else { + GetScalarFieldOfTable(struct_def, field, code_ptr); + } + } else { + switch (field.value.type.base_type) { + case BASE_TYPE_STRUCT: + if (struct_def.fixed) { + GetStructFieldOfStruct(struct_def, field, code_ptr); + } else { + GetStructFieldOfTable(struct_def, field, code_ptr); + } + break; + case BASE_TYPE_STRING: + GetStringField(struct_def, field, code_ptr); + break; + case BASE_TYPE_VECTOR: { + auto vectortype = field.value.type.VectorType(); + if (vectortype.base_type == BASE_TYPE_STRUCT) { + GetMemberOfVectorOfStruct(struct_def, field, code_ptr); + } else { + GetMemberOfVectorOfNonStruct(struct_def, field, code_ptr); + } + break; + } + case BASE_TYPE_UNION: + GetUnionField(struct_def, field, code_ptr); + break; + default: + assert(0); + } + } + if (field.value.type.base_type == BASE_TYPE_VECTOR) { + GetVectorLen(struct_def, field, code_ptr); + } +} + +// Generate table constructors, conditioned on its members' types. +static void GenTableBuilders(const StructDef &struct_def, + std::string *code_ptr) { + GetStartOfTable(struct_def, code_ptr); + + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); + ++it) { + auto &field = **it; + if (field.deprecated) continue; + + auto offset = it - struct_def.fields.vec.begin(); + BuildFieldOfTable(struct_def, field, offset, code_ptr); + if (field.value.type.base_type == BASE_TYPE_VECTOR) { + BuildVectorOfTable(struct_def, field, code_ptr); + } + } + + GetEndOffsetOnTable(struct_def, code_ptr); +} + +// Generate struct or table methods. +static void GenStruct(const StructDef &struct_def, + std::string *code_ptr, + StructDef *root_struct_def) { + if (struct_def.generated) return; + + Comment(struct_def.doc_comment, code_ptr); + BeginClass(struct_def, code_ptr); + if (&struct_def == root_struct_def) { + // Generate a special accessor for the table that has been declared as + // the root type. + NewRootTypeFromBuffer(struct_def, code_ptr); + } + // Generate the Init method that sets the field in a pre-existing + // accessor object. This is to allow object reuse. + InitializeExisting(struct_def, code_ptr); + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); + ++it) { + auto &field = **it; + if (field.deprecated) continue; + + GenStructAccessor(struct_def, field, code_ptr); + } + + if (struct_def.fixed) { + // create a struct constructor function + GenStructBuilder(struct_def, code_ptr); + } else { + // Create a set of functions that allow table construction. + GenTableBuilders(struct_def, code_ptr); + } +} + +// Generate enum declarations. +static void GenEnum(const EnumDef &enum_def, std::string *code_ptr) { + if (enum_def.generated) return; + + Comment(enum_def.doc_comment, code_ptr); + BeginEnum(code_ptr); + for (auto it = enum_def.vals.vec.begin(); + it != enum_def.vals.vec.end(); + ++it) { + auto &ev = **it; + Comment(ev.doc_comment, code_ptr, " "); + EnumMember(enum_def, ev, code_ptr); + } + EndEnum(code_ptr); +} + +// Returns the function name that is able to read a value of the given type. +static std::string GenGetter(const Type &type) { + switch (type.base_type) { + case BASE_TYPE_STRING: return "rcv._tab.String"; + case BASE_TYPE_UNION: return "rcv._tab.Union"; + case BASE_TYPE_VECTOR: return GenGetter(type.VectorType()); + default: + return "rcv._tab.Get" + MakeCamel(GenTypeGet(type)); + } +} + +// Returns the method name for use with add/put calls. +static std::string GenMethod(const FieldDef &field) { + return IsScalar(field.value.type.base_type) + ? MakeCamel(GenTypeBasic(field.value.type)) + : (IsStruct(field.value.type) ? "Struct" : "UOffsetT"); +} + + +// Save out the generated code for a Go Table type. +static bool SaveType(const Parser &parser, const Definition &def, + const std::string &classcode, const std::string &path, + bool needs_imports) { + if (!classcode.length()) return true; + + std::string name_space_name; + std::string name_space_dir = path; + for (auto it = parser.name_space_.begin(); + it != parser.name_space_.end(); ++it) { + if (name_space_name.length()) { + name_space_name += "."; + name_space_dir += PATH_SEPARATOR; + } + name_space_name = *it; + name_space_dir += *it; + mkdir(name_space_dir.c_str(), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); + } + + + std::string code = ""; + BeginFile(name_space_name, needs_imports, &code); + code += classcode; + std::string filename = name_space_dir + PATH_SEPARATOR + def.name + ".go"; + return SaveFile(filename.c_str(), code, false); +} + +static std::string GenTypeBasic(const Type &type) { + static const char *ctypename[] = { + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) #GTYPE, + FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) + #undef FLATBUFFERS_TD + }; + return ctypename[type.base_type]; +} + +static std::string GenTypePointer(const Type &type) { + switch (type.base_type) { + case BASE_TYPE_STRING: + return "string"; + case BASE_TYPE_VECTOR: + return GenTypeGet(type.VectorType()); + case BASE_TYPE_STRUCT: + return type.struct_def->name; + case BASE_TYPE_UNION: + // fall through + default: + return "*flatbuffers.Table"; + } +} + +static std::string GenTypeGet(const Type &type) { + return IsScalar(type.base_type) + ? GenTypeBasic(type) + : GenTypePointer(type); +} + +static std::string TypeName(const FieldDef &field) { + return GenTypeGet(field.value.type); +} + +// Create a struct with a builder and the struct's arguments. +static void GenStructBuilder(const StructDef &struct_def, + std::string *code_ptr) { + BeginBuilderArgs(struct_def, code_ptr); + StructBuilderArgs(struct_def, "", code_ptr); + EndBuilderArgs(code_ptr); + + StructBuilderBody(struct_def, "", code_ptr); + EndBuilderBody(code_ptr); +} + +} // namespace go + +bool GenerateGo(const Parser &parser, + const std::string &path, + const std::string & /*file_name*/, + const GeneratorOptions & /*opts*/) { + for (auto it = parser.enums_.vec.begin(); + it != parser.enums_.vec.end(); ++it) { + std::string enumcode; + go::GenEnum(**it, &enumcode); + if (!go::SaveType(parser, **it, enumcode, path, false)) + return false; + } + + for (auto it = parser.structs_.vec.begin(); + it != parser.structs_.vec.end(); ++it) { + std::string declcode; + go::GenStruct(**it, &declcode, parser.root_struct_def); + if (!go::SaveType(parser, **it, declcode, path, true)) + return false; + } + + return true; +} + +} // namespace flatbuffers + diff --git a/src/idl_gen_java.cpp b/src/idl_gen_java.cpp index 35628cba..39281bb6 100755 --- a/src/idl_gen_java.cpp +++ b/src/idl_gen_java.cpp @@ -34,7 +34,7 @@ namespace java { static std::string GenTypeBasic(const Type &type) { static const char *ctypename[] = { - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) #JTYPE, + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) #JTYPE, FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) #undef FLATBUFFERS_TD }; @@ -73,21 +73,6 @@ static void GenComment(const std::string &dc, } } -// Convert an underscore_based_indentifier in to camelCase. -// Also uppercases the first character if first is true. -static std::string MakeCamel(const std::string &in, bool first = true) { - std::string s; - for (size_t i = 0; i < in.length(); i++) { - if (!i && first) - s += static_cast(toupper(in[0])); - else if (in[i] == '_' && i + 1 < in.length()) - s += static_cast(toupper(in[++i])); - else - s += in[i]; - } - return s; -} - static void GenEnum(EnumDef &enum_def, std::string *code_ptr) { std::string &code = *code_ptr; if (enum_def.generated) return; diff --git a/src/idl_gen_text.cpp b/src/idl_gen_text.cpp index e8759259..f35e8840 100644 --- a/src/idl_gen_text.cpp +++ b/src/idl_gen_text.cpp @@ -140,7 +140,7 @@ template<> void Print(const void *val, type = type.VectorType(); // Call PrintVector above specifically for each element type: switch (type.base_type) { - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \ + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \ case BASE_TYPE_ ## ENUM: \ PrintVector( \ *reinterpret_cast *>(val), \ @@ -207,7 +207,7 @@ static void GenStruct(const StructDef &struct_def, const Table *table, OutputIdentifier(fd.name, opts, _text); text += ": "; switch (fd.value.type.base_type) { - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \ + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \ case BASE_TYPE_ ## ENUM: \ GenField(fd, table, struct_def.fixed, \ opts, indent + opts.indent_step, _text); \ @@ -215,7 +215,7 @@ static void GenStruct(const StructDef &struct_def, const Table *table, FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD) #undef FLATBUFFERS_TD // Generate drop-thru case statements for all pointer types: - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \ + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \ case BASE_TYPE_ ## ENUM: FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD) #undef FLATBUFFERS_TD diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 4d9c4cf2..8be05914 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -23,14 +23,14 @@ namespace flatbuffers { const char *const kTypeNames[] = { - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) IDLTYPE, + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) IDLTYPE, FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) #undef FLATBUFFERS_TD nullptr }; const char kTypeSizes[] = { - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) sizeof(CTYPE), + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) sizeof(CTYPE), FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) #undef FLATBUFFERS_TD }; @@ -86,7 +86,7 @@ enum { #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) kToken ## NAME = VALUE, FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN) #undef FLATBUFFERS_TOKEN - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) kToken ## ENUM, + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) kToken ## ENUM, FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) #undef FLATBUFFERS_TD }; @@ -96,7 +96,7 @@ static std::string TokenToString(int t) { #define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) STRING, FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN) #undef FLATBUFFERS_TOKEN - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) IDLTYPE, + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) IDLTYPE, FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) #undef FLATBUFFERS_TD }; @@ -170,7 +170,7 @@ void Parser::Next() { attribute_.clear(); attribute_.append(start, cursor_); // First, see if it is a type keyword from the table of types: - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \ + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \ if (attribute_ == IDLTYPE) { \ token_ = kToken ## ENUM; \ return; \ @@ -429,7 +429,7 @@ uoffset_t Parser::ParseTable(const StructDef &struct_def) { auto field = it->second; if (!struct_def.sortbysize || size == SizeOf(value.type.base_type)) { switch (value.type.base_type) { - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \ + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \ case BASE_TYPE_ ## ENUM: \ builder_.Pad(field->padding); \ builder_.AddElement(value.offset, \ @@ -438,7 +438,7 @@ uoffset_t Parser::ParseTable(const StructDef &struct_def) { break; FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD); #undef FLATBUFFERS_TD - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \ + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \ case BASE_TYPE_ ## ENUM: \ builder_.Pad(field->padding); \ if (IsStruct(field->value.type)) { \ @@ -492,7 +492,7 @@ uoffset_t Parser::ParseVector(const Type &type) { // start at the back, since we're building the data backwards. auto &val = field_stack_.back().first; switch (val.type.base_type) { - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE) \ + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE) \ case BASE_TYPE_ ## ENUM: \ if (IsStruct(val.type)) SerializeStruct(*val.type.struct_def, val); \ else builder_.PushElement(atot(val.constant.c_str())); \ diff --git a/tests/GoTest.sh b/tests/GoTest.sh new file mode 100755 index 00000000..873f2bb1 --- /dev/null +++ b/tests/GoTest.sh @@ -0,0 +1,54 @@ +#!/bin/bash -eu +# Copyright 2014 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +pushd "$(dirname $0)" >/dev/null +test_dir="$(pwd)" +go_path=${test_dir}/go_gen +go_src=${go_path}/src + +# Emit Go code for the example schema in the test dir: +../flatc -g monster_test.fbs + +# Go requires a particular layout of files in order to link multiple packages. +# Copy flatbuffer Go files to their own package directories to compile the +# test binary: +mkdir -p ${go_src}/MyGame/Example +mkdir -p ${go_src}/github.com/google/flatbuffers/go +mkdir -p ${go_src}/flatbuffers_test + +cp -u MyGame/Example/*.go ./go_gen/src/MyGame/Example/ +cp -u ../go/* ./go_gen/src/github.com/google/flatbuffers/go +cp -u ./go_test.go ./go_gen/src/flatbuffers_test/ + +# Run tests with necessary flags. +# Developers may wish to see more detail by appending the verbosity flag +# -test.v to arguments for this command, as in: +# go -test -test.v ... +# Developers may also wish to run benchmarks, which may be achieved with the +# flag -test.bench and the wildcard regexp ".": +# go -test -test.bench=. ... +GOPATH=${go_path} go test flatbuffers_test \ + --test.coverpkg=github.com/google/flatbuffers/go \ + --cpp_data=${test_dir}/monsterdata_test.bin \ + --java_data=${test_dir}/monsterdata_java_wire.bin \ + --out_data=${test_dir}/monsterdata_go_wire.bin \ + --fuzz=true \ + --super_fuzz=false \ + --fuzz_fields=4 \ + --fuzz_objects=10000 + +rm -rf ./go_gen/{pkg,src} + +echo "OK: Go tests passed." diff --git a/tests/MyGame/Example/Any.go b/tests/MyGame/Example/Any.go new file mode 100644 index 00000000..0039bb8c --- /dev/null +++ b/tests/MyGame/Example/Any.go @@ -0,0 +1,8 @@ +// automatically generated, do not modify + +package Example + +const ( + AnyNONE = 0 + AnyMonster = 1 +) diff --git a/tests/MyGame/Example/Color.go b/tests/MyGame/Example/Color.go new file mode 100644 index 00000000..f4cc633f --- /dev/null +++ b/tests/MyGame/Example/Color.go @@ -0,0 +1,9 @@ +// automatically generated, do not modify + +package Example + +const ( + ColorRed = 0 + ColorGreen = 1 + ColorBlue = 2 +) diff --git a/tests/MyGame/Example/Monster.go b/tests/MyGame/Example/Monster.go new file mode 100644 index 00000000..2bceae58 --- /dev/null +++ b/tests/MyGame/Example/Monster.go @@ -0,0 +1,196 @@ +// automatically generated, do not modify + +package Example + +import ( + flatbuffers "github.com/google/flatbuffers/go" +) +type Monster struct { + _tab flatbuffers.Table +} + +func GetRootAsMonster(buf []byte, offset flatbuffers.UOffsetT) *Monster { + n := flatbuffers.GetUOffsetT(buf[offset:]) + x := &Monster{} + x.Init(buf, n + offset) + return x +} + +func (rcv *Monster) Init(buf []byte, i flatbuffers.UOffsetT) { + rcv._tab.Bytes = buf + rcv._tab.Pos = i +} + +func (rcv *Monster) Pos(obj *Vec3) *Vec3 { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + x := o + rcv._tab.Pos + if obj == nil { + obj = new(Vec3) + } + obj.Init(rcv._tab.Bytes, x) + return obj + } + return nil +} + +func (rcv *Monster) Mana() int16 { + o := flatbuffers.UOffsetT(rcv._tab.Offset(6)) + if o != 0 { + return rcv._tab.GetInt16(o + rcv._tab.Pos) + } + return 150 +} + +func (rcv *Monster) Hp() int16 { + o := flatbuffers.UOffsetT(rcv._tab.Offset(8)) + if o != 0 { + return rcv._tab.GetInt16(o + rcv._tab.Pos) + } + return 100 +} + +func (rcv *Monster) Name() string { + o := flatbuffers.UOffsetT(rcv._tab.Offset(10)) + if o != 0 { + return rcv._tab.String(o) + } + return "" +} + +func (rcv *Monster) Inventory(j int) byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(14)) + if o != 0 { + a := rcv._tab.Vector(o) + return rcv._tab.GetByte(a + flatbuffers.UOffsetT(j * 1)) + } + return 0 +} + +func (rcv *Monster) InventoryLength() int { + o := flatbuffers.UOffsetT(rcv._tab.Offset(14)) + if o != 0 { + return rcv._tab.VectorLen(o) + } + return 0 +} + +func (rcv *Monster) Color() int8 { + o := flatbuffers.UOffsetT(rcv._tab.Offset(16)) + if o != 0 { + return rcv._tab.GetInt8(o + rcv._tab.Pos) + } + return 2 +} + +func (rcv *Monster) TestType() byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(18)) + if o != 0 { + return rcv._tab.GetByte(o + rcv._tab.Pos) + } + return 0 +} + +func (rcv *Monster) Test(obj *flatbuffers.Table) bool { + o := flatbuffers.UOffsetT(rcv._tab.Offset(20)) + if o != 0 { + rcv._tab.Union(obj, o) + return true + } + return false +} + +func (rcv *Monster) Test4(obj *Test, j int) bool { + o := flatbuffers.UOffsetT(rcv._tab.Offset(22)) + if o != 0 { + x := rcv._tab.Vector(o) + x += flatbuffers.UOffsetT(j) * 4 + if obj == nil { + obj = new(Test) + } + obj.Init(rcv._tab.Bytes, x) + return true + } + return false +} + +func (rcv *Monster) Test4Length() int { + o := flatbuffers.UOffsetT(rcv._tab.Offset(22)) + if o != 0 { + return rcv._tab.VectorLen(o) + } + return 0 +} + +func (rcv *Monster) Testarrayofstring(j int) string { + o := flatbuffers.UOffsetT(rcv._tab.Offset(24)) + if o != 0 { + a := rcv._tab.Vector(o) + return rcv._tab.String(a + flatbuffers.UOffsetT(j * 4)) + } + return "" +} + +func (rcv *Monster) TestarrayofstringLength() int { + o := flatbuffers.UOffsetT(rcv._tab.Offset(24)) + if o != 0 { + return rcv._tab.VectorLen(o) + } + return 0 +} + +/// an example documentation comment: this will end up in the generated code multiline too +func (rcv *Monster) Testarrayoftables(obj *Monster, j int) bool { + o := flatbuffers.UOffsetT(rcv._tab.Offset(26)) + if o != 0 { + x := rcv._tab.Vector(o) + x += flatbuffers.UOffsetT(j) * 4 + x = rcv._tab.Indirect(x) + if obj == nil { + obj = new(Monster) + } + obj.Init(rcv._tab.Bytes, x) + return true + } + return false +} + +func (rcv *Monster) TestarrayoftablesLength() int { + o := flatbuffers.UOffsetT(rcv._tab.Offset(26)) + if o != 0 { + return rcv._tab.VectorLen(o) + } + return 0 +} + +func (rcv *Monster) Enemy(obj *Monster) *Monster { + o := flatbuffers.UOffsetT(rcv._tab.Offset(28)) + if o != 0 { + x := rcv._tab.Indirect(o + rcv._tab.Pos) + if obj == nil { + obj = new(Monster) + } + obj.Init(rcv._tab.Bytes, x) + return obj + } + return nil +} + +func MonsterStart(builder *flatbuffers.Builder) { builder.StartObject(13) } +func MonsterAddPos(builder *flatbuffers.Builder, pos flatbuffers.UOffsetT) { builder.PrependStructSlot(0, flatbuffers.UOffsetT(pos), 0) } +func MonsterAddMana(builder *flatbuffers.Builder, mana int16) { builder.PrependInt16Slot(1, mana, 150) } +func MonsterAddHp(builder *flatbuffers.Builder, hp int16) { builder.PrependInt16Slot(2, hp, 100) } +func MonsterAddName(builder *flatbuffers.Builder, name flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(3, flatbuffers.UOffsetT(name), 0) } +func MonsterAddInventory(builder *flatbuffers.Builder, inventory flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(5, flatbuffers.UOffsetT(inventory), 0) } +func MonsterStartInventoryVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(1, numElems) } +func MonsterAddColor(builder *flatbuffers.Builder, color int8) { builder.PrependInt8Slot(6, color, 2) } +func MonsterAddTestType(builder *flatbuffers.Builder, testType byte) { builder.PrependByteSlot(7, testType, 0) } +func MonsterAddTest(builder *flatbuffers.Builder, test flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(8, flatbuffers.UOffsetT(test), 0) } +func MonsterAddTest4(builder *flatbuffers.Builder, test4 flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(9, flatbuffers.UOffsetT(test4), 0) } +func MonsterStartTest4Vector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(4, numElems) } +func MonsterAddTestarrayofstring(builder *flatbuffers.Builder, testarrayofstring flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(10, flatbuffers.UOffsetT(testarrayofstring), 0) } +func MonsterStartTestarrayofstringVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(4, numElems) } +func MonsterAddTestarrayoftables(builder *flatbuffers.Builder, testarrayoftables flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(11, flatbuffers.UOffsetT(testarrayoftables), 0) } +func MonsterStartTestarrayoftablesVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(4, numElems) } +func MonsterAddEnemy(builder *flatbuffers.Builder, enemy flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(12, flatbuffers.UOffsetT(enemy), 0) } +func MonsterEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } diff --git a/tests/MyGame/Example/Test.go b/tests/MyGame/Example/Test.go new file mode 100644 index 00000000..dc4de8ee --- /dev/null +++ b/tests/MyGame/Example/Test.go @@ -0,0 +1,26 @@ +// automatically generated, do not modify + +package Example + +import ( + flatbuffers "github.com/google/flatbuffers/go" +) +type Test struct { + _tab flatbuffers.Struct +} + +func (rcv *Test) Init(buf []byte, i flatbuffers.UOffsetT) { + rcv._tab.Bytes = buf + rcv._tab.Pos = i +} + +func (rcv *Test) A() int16 { return rcv._tab.GetInt16(rcv._tab.Pos + flatbuffers.UOffsetT(0)) } +func (rcv *Test) B() int8 { return rcv._tab.GetInt8(rcv._tab.Pos + flatbuffers.UOffsetT(2)) } + +func CreateTest(builder *flatbuffers.Builder, a int16, b int8) flatbuffers.UOffsetT { + builder.Prep(2, 4) + builder.Pad(1) + builder.PrependInt8(b) + builder.PrependInt16(a) + return builder.Offset() +} diff --git a/tests/MyGame/Example/Vec3.go b/tests/MyGame/Example/Vec3.go new file mode 100644 index 00000000..d2bab2c9 --- /dev/null +++ b/tests/MyGame/Example/Vec3.go @@ -0,0 +1,45 @@ +// automatically generated, do not modify + +package Example + +import ( + flatbuffers "github.com/google/flatbuffers/go" +) +type Vec3 struct { + _tab flatbuffers.Struct +} + +func (rcv *Vec3) Init(buf []byte, i flatbuffers.UOffsetT) { + rcv._tab.Bytes = buf + rcv._tab.Pos = i +} + +func (rcv *Vec3) X() float32 { return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(0)) } +func (rcv *Vec3) Y() float32 { return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(4)) } +func (rcv *Vec3) Z() float32 { return rcv._tab.GetFloat32(rcv._tab.Pos + flatbuffers.UOffsetT(8)) } +func (rcv *Vec3) Test1() float64 { return rcv._tab.GetFloat64(rcv._tab.Pos + flatbuffers.UOffsetT(16)) } +func (rcv *Vec3) Test2() int8 { return rcv._tab.GetInt8(rcv._tab.Pos + flatbuffers.UOffsetT(24)) } +func (rcv *Vec3) Test3(obj *Test) *Test { + if obj == nil { + obj = new(Test) + } + obj.Init(rcv._tab.Bytes, rcv._tab.Pos + 26) + return obj +} + +func CreateVec3(builder *flatbuffers.Builder, x float32, y float32, z float32, test1 float64, test2 int8, Test_a int16, Test_b int8) flatbuffers.UOffsetT { + builder.Prep(16, 32) + builder.Pad(2) + builder.Prep(2, 4) + builder.Pad(1) + builder.PrependInt8(Test_b) + builder.PrependInt16(Test_a) + builder.Pad(1) + builder.PrependInt8(test2) + builder.PrependFloat64(test1) + builder.Pad(4) + builder.PrependFloat32(z) + builder.PrependFloat32(y) + builder.PrependFloat32(x) + return builder.Offset() +} diff --git a/tests/go_test.go b/tests/go_test.go new file mode 100644 index 00000000..069de811 --- /dev/null +++ b/tests/go_test.go @@ -0,0 +1,1096 @@ +package main + +import ( + example "MyGame/Example" // refers to generated code + "bytes" + "flag" + "fmt" + flatbuffers "github.com/google/flatbuffers/go" + "io/ioutil" + "os" + "reflect" + "sort" + "testing" +) + +var ( + cppData, javaData, outData string + fuzz, superFuzz bool + fuzzFields, fuzzObjects int +) + +func init() { + flag.StringVar(&cppData, "cpp_data", "", + "location of monsterdata_test.bin") + flag.StringVar(&javaData, "java_data", "", + "location of monsterdata_java_wire.bin") + flag.StringVar(&outData, "out_data", "", + "location to write generated Go data") + flag.BoolVar(&fuzz, "fuzz", false, "perform fuzzing") + flag.BoolVar(&superFuzz, "super_fuzz", false, + "perform fuzzing of 0..fuzz_fields fields") + flag.IntVar(&fuzzFields, "fuzz_fields", 4, "fields per fuzzer object") + flag.IntVar(&fuzzObjects, "fuzz_objects", 10000, + "number of fuzzer objects (higher is slower and more thorough") + flag.Parse() + if cppData == "" { + fmt.Fprintf(os.Stderr, "cpp_data argument is required\n") + os.Exit(1) + } + if javaData == "" { + fmt.Fprintf(os.Stderr, "java_data argument is required\n") + os.Exit(1) + } +} + +// Store specific byte patterns in these variables for the fuzzer. +var ( + overflowingInt32Val = flatbuffers.GetInt32([]byte{0x83, 0x33, 0x33, 0x33}) + overflowingInt64Val = flatbuffers.GetInt64([]byte{0x84, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}) +) + +// TestAll runs all checks, failing if any errors occur. +func TestAll(t *testing.T) { + monsterDataCpp, err := ioutil.ReadFile(cppData) + if err != nil { + t.Fatal(err) + } + + monsterDataJava, err := ioutil.ReadFile(javaData) + if err != nil { + t.Fatalf("run the java test once to generate javaData\n%s", err) + } + + // FIXME: this test is brittle and not maintainable when the test schema changes + CheckByteLayout(t.Fatalf) + + manual, off0 := CheckManualBuild(t.Fatalf) + helped, off1 := CheckGeneratedBuild(t.Fatalf) + + CheckEquality(manual[off0:], monsterDataJava, "manually", t.Fatalf) + CheckEquality(helped[off1:], monsterDataJava, "helper", t.Fatalf) + CheckVtableDeduplication(t.Fatalf) + + CheckInterpretBuffer(monsterDataCpp, 0, t.Fatalf) + CheckInterpretBuffer(monsterDataJava, 0, t.Fatalf) + + CheckDocExample(manual, off0, t.Fatalf) + + if superFuzz { + for i := 0; i <= fuzzFields; i++ { + checkFuzz(i, fuzzObjects, t.Fatalf) + } + } else if fuzz { + checkFuzz(fuzzFields, fuzzObjects, t.Fatalf) + } + + err = ioutil.WriteFile(outData, helped[off1:], os.FileMode(0644)) + if err != nil { + t.Fatal(err) + } +} + +// CheckInterpretBuffer checks that the given buffer is evaluated correctly +// as the example Monster. +func CheckInterpretBuffer(buf []byte, offset flatbuffers.UOffsetT, fail func(string, ...interface{})) { + monster := example.GetRootAsMonster(buf, offset) + + if got := monster.Hp(); 80 != got { + fail(FailString("hp", 80, got)) + } + + // default + if got := monster.Mana(); 150 != got { + fail(FailString("mana", 150, got)) + } + + if got := monster.Name(); "MyMonster" != got { + fail(FailString("name", "MyMonster", got)) + } + + // initialize a Vec3 from Pos() + vec := new(example.Vec3) + vec = monster.Pos(vec) + if vec == nil { + fail("vec3 initialization failed") + } + + // check that new allocs equal given ones: + vec2 := monster.Pos(nil) + if !reflect.DeepEqual(vec, vec2) { + fail("fresh allocation failed") + } + + // verify the properties of the Vec3 + if got := vec.X(); float32(1.0) != got { + fail(FailString("Pos.X", float32(1.0), got)) + } + + if got := vec.Y(); float32(2.0) != got { + fail(FailString("Pos.Y", float32(2.0), got)) + } + + if got := vec.Z(); float32(3.0) != got { + fail(FailString("Pos.Z", float32(3.0), got)) + } + + if got := vec.Test1(); float64(3.0) != got { + fail(FailString("Pos.Test1", float64(3.0), got)) + } + + if got := vec.Test2(); int8(4) != got { + fail(FailString("Pos.Test2", int8(4), got)) + } + + // initialize a Test from Test3(...) + t := new(example.Test) + t = vec.Test3(t) + if t == nil { + fail("vec.Test3(&t) failed") + } + + // check that new allocs equal given ones: + t2 := vec.Test3(nil) + if !reflect.DeepEqual(t, t2) { + fail("fresh allocation failed") + } + + // verify the properties of the Test + if got := t.A(); int16(5) != got { + fail(FailString("t.A()", int16(5), got)) + } + + if got := t.B(); int8(6) != got { + fail(FailString("t.B()", int8(6), got)) + } + + if got := monster.TestType(); example.AnyMonster != got { + fail(FailString("monster.TestType()", example.AnyMonster, got)) + } + + if unionType := monster.TestType(); unionType != example.AnyMonster { + fail("monster.TestType()") + } + + // initialize a Table from a union field Test(...) + var table2 flatbuffers.Table + if ok := monster.Test(&table2); !ok { + fail("monster.Test(&monster2) failed") + } + + // initialize a Monster from the Table from the union + var monster2 example.Monster + monster2.Init(table2.Bytes, table2.Pos) + + if got := monster2.Hp(); int16(20) != got { + fail(FailString("monster2.Hp()", int16(20), got)) + } + + if got := monster.InventoryLength(); 5 != got { + fail(FailString("monster.InventoryLength", 5, got)) + } + + invsum := 0 + l := monster.InventoryLength() + for i := 0; i < l; i++ { + v := monster.Inventory(i) + invsum += int(v) + } + if invsum != 10 { + fail(FailString("monster inventory sum", 10, invsum)) + } + + if got := monster.Test4Length(); 2 != got { + fail(FailString("monster.Test4Length()", 2, got)) + } + + var test0 example.Test + ok := monster.Test4(&test0, 0) + if !ok { + fail(FailString("monster.Test4(&test0, 0)", true, ok)) + } + + var test1 example.Test + ok = monster.Test4(&test1, 1) + if !ok { + fail(FailString("monster.Test4(&test1, 1)", true, ok)) + } + + // the position of test0 and test1 are swapped in monsterdata_java_wire + // and monsterdata_test_wire, so ignore ordering + v0 := test0.A() + v1 := test0.B() + v2 := test1.A() + v3 := test1.B() + sum := int(v0) + int(v1) + int(v2) + int(v3) + + if 100 != sum { + fail(FailString("test0 and test1 sum", 100, sum)) + } +} + +// Low level stress/fuzz test: serialize/deserialize a variety of +// different kinds of data in different combinations +func checkFuzz(fuzzFields, fuzzObjects int, fail func(string, ...interface{})) { + + // Values we're testing against: chosen to ensure no bits get chopped + // off anywhere, and also be different from eachother. + boolVal := true + int8Val := int8(-127) // 0x81 + uint8Val := uint8(0xFF) + int16Val := int16(-32222) // 0x8222 + uint16Val := uint16(0xFEEE) + int32Val := int32(overflowingInt32Val) + uint32Val := uint32(0xFDDDDDDD) + int64Val := int64(overflowingInt64Val) + uint64Val := uint64(0xFCCCCCCCCCCCCCCC) + float32Val := float32(3.14159) + float64Val := float64(3.14159265359) + + testValuesMax := 11 // hardcoded to the number of scalar types + + builder := flatbuffers.NewBuilder(0) + l := NewLCG() + + objects := make([]flatbuffers.UOffsetT, fuzzObjects) + + // Generate fuzzObjects random objects each consisting of + // fuzzFields fields, each of a random type. + for i := 0; i < fuzzObjects; i++ { + builder.StartObject(fuzzFields) + + for f := 0; f < fuzzFields; f++ { + choice := l.Next() % uint32(testValuesMax) + switch choice { + case 0: + builder.PrependBoolSlot(int(f), boolVal, false) + case 1: + builder.PrependInt8Slot(int(f), int8Val, 0) + case 2: + builder.PrependUint8Slot(int(f), uint8Val, 0) + case 3: + builder.PrependInt16Slot(int(f), int16Val, 0) + case 4: + builder.PrependUint16Slot(int(f), uint16Val, 0) + case 5: + builder.PrependInt32Slot(int(f), int32Val, 0) + case 6: + builder.PrependUint32Slot(int(f), uint32Val, 0) + case 7: + builder.PrependInt64Slot(int(f), int64Val, 0) + case 8: + builder.PrependUint64Slot(int(f), uint64Val, 0) + case 9: + builder.PrependFloat32Slot(int(f), float32Val, 0) + case 10: + builder.PrependFloat64Slot(int(f), float64Val, 0) + } + } + + off := builder.EndObject() + + // store the offset from the end of the builder buffer, + // since it will keep growing: + objects[i] = off + } + + // Do some bookkeeping to generate stats on fuzzes: + stats := map[string]int{} + check := func(desc string, want, got interface{}) { + stats[desc]++ + if want != got { + fail("%s want %v got %v", desc, want, got) + } + } + + l = NewLCG() // Reset. + + // Test that all objects we generated are readable and return the + // expected values. We generate random objects in the same order + // so this is deterministic. + for i := 0; i < fuzzObjects; i++ { + + table := &flatbuffers.Table{ + Bytes: builder.Bytes, + Pos: flatbuffers.UOffsetT(len(builder.Bytes)) - objects[i], + } + + for j := 0; j < fuzzFields; j++ { + f := flatbuffers.VOffsetT((flatbuffers.VtableMetadataFields + j) * flatbuffers.SizeVOffsetT) + choice := int(l.Next()) % testValuesMax + + switch choice { + case 0: + check("bool", boolVal, table.GetBoolSlot(f, false)) + case 1: + check("int8", int8Val, table.GetInt8Slot(f, 0)) + case 2: + check("uint8", uint8Val, table.GetUint8Slot(f, 0)) + case 3: + check("int16", int16Val, table.GetInt16Slot(f, 0)) + case 4: + check("uint16", uint16Val, table.GetUint16Slot(f, 0)) + case 5: + check("int32", int32Val, table.GetInt32Slot(f, 0)) + case 6: + check("uint32", uint32Val, table.GetUint32Slot(f, 0)) + case 7: + check("int64", int64Val, table.GetInt64Slot(f, 0)) + case 8: + check("uint64", uint64Val, table.GetUint64Slot(f, 0)) + case 9: + check("float32", float32Val, table.GetFloat32Slot(f, 0)) + case 10: + check("float64", float64Val, table.GetFloat64Slot(f, 0)) + } + } + } + + // If enough checks were made, verify that all scalar types were used: + if fuzzFields*fuzzObjects >= testValuesMax { + if len(stats) != testValuesMax { + fail("fuzzing failed to test all scalar types") + } + } + + // Print some counts, if needed: + if testing.Verbose() { + if fuzzFields == 0 || fuzzObjects == 0 { + fmt.Printf("fuzz\tfields: %d\tobjects: %d\t[none]\t%d\n", + fuzzFields, fuzzObjects, 0) + } else { + keys := make([]string, 0, len(stats)) + for k := range stats { + keys = append(keys, k) + } + sort.Strings(keys) + for _, k := range keys { + fmt.Printf("fuzz\tfields: %d\tobjects: %d\t%s\t%d\n", + fuzzFields, fuzzObjects, k, stats[k]) + } + } + } + + return +} + +// FailString makes a message for when expectations differ from reality. +func FailString(name string, want, got interface{}) string { + return fmt.Sprintf("bad %s: want %#v got %#v", name, want, got) +} + +// CheckByteLayout verifies the bytes of a Builder in various scenarios. +func CheckByteLayout(fail func(string, ...interface{})) { + var b *flatbuffers.Builder + + var i int + check := func(want []byte) { + i++ + got := b.Bytes[b.Head():] + if !bytes.Equal(want, got) { + fail("case %d: want\n%v\nbut got\n%v\n", i, want, got) + } + } + + // test 1: numbers + + b = flatbuffers.NewBuilder(0) + check([]byte{}) + b.PrependBool(true) + check([]byte{1}) + b.PrependInt8(-127) + check([]byte{129, 1}) + b.PrependUint8(255) + check([]byte{255, 129, 1}) + b.PrependInt16(-32222) + check([]byte{0x22, 0x82, 0, 255, 129, 1}) // first pad + b.PrependUint16(0xFEEE) + check([]byte{0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1}) // no pad this time + b.PrependInt32(-53687092) + check([]byte{204, 204, 204, 252, 0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1}) + b.PrependUint32(0x98765432) + check([]byte{0x32, 0x54, 0x76, 0x98, 204, 204, 204, 252, 0xEE, 0xFE, 0x22, 0x82, 0, 255, 129, 1}) + + // test 1b: numbers 2 + + b = flatbuffers.NewBuilder(0) + b.PrependUint64(0x1122334455667788) + check([]byte{0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11}) + + // test 2: 1xbyte vector + + b = flatbuffers.NewBuilder(0) + check([]byte{}) + b.StartVector(flatbuffers.SizeByte, 1) + check([]byte{0, 0, 0}) // align to 4bytes + b.PrependByte(1) + check([]byte{1, 0, 0, 0}) + b.EndVector(1) + check([]byte{1, 0, 0, 0, 1, 0, 0, 0}) // padding + + // test 3: 2xbyte vector + + b = flatbuffers.NewBuilder(0) + b.StartVector(flatbuffers.SizeByte, 2) + check([]byte{0, 0}) // align to 4bytes + b.PrependByte(1) + check([]byte{1, 0, 0}) + b.PrependByte(2) + check([]byte{2, 1, 0, 0}) + b.EndVector(2) + check([]byte{2, 0, 0, 0, 2, 1, 0, 0}) // padding + + // test 4: 1xuint16 vector + + b = flatbuffers.NewBuilder(0) + b.StartVector(flatbuffers.SizeUint16, 1) + check([]byte{0, 0}) // align to 4bytes + b.PrependUint16(1) + check([]byte{1, 0, 0, 0}) + b.EndVector(1) + check([]byte{1, 0, 0, 0, 1, 0, 0, 0}) // padding + + // test 5: 2xuint16 vector + + b = flatbuffers.NewBuilder(0) + b.StartVector(flatbuffers.SizeUint16, 2) + check([]byte{}) // align to 4bytes + b.PrependUint16(0xABCD) + check([]byte{0xCD, 0xAB}) + b.PrependUint16(0xDCBA) + check([]byte{0xBA, 0xDC, 0xCD, 0xAB}) + b.EndVector(2) + check([]byte{2, 0, 0, 0, 0xBA, 0xDC, 0xCD, 0xAB}) + + // test 6: CreateString + + b = flatbuffers.NewBuilder(0) + b.CreateString("foo") + check([]byte{3, 0, 0, 0, 'f', 'o', 'o', 0}) // 0-terminated, no pad + b.CreateString("moop") + check([]byte{4, 0, 0, 0, 'm', 'o', 'o', 'p', 0, 0, 0, 0, // 0-terminated, 3-byte pad + 3, 0, 0, 0, 'f', 'o', 'o', 0}) + + // test 7: empty vtable + b = flatbuffers.NewBuilder(0) + b.StartObject(0) + check([]byte{}) + b.EndObject() + check([]byte{4, 0, 4, 0, 4, 0, 0, 0}) + + // test 8: vtable with one true bool + b = flatbuffers.NewBuilder(0) + check([]byte{}) + b.StartObject(1) + check([]byte{}) + b.PrependBoolSlot(0, true, false) + b.EndObject() + check([]byte{ + 6, 0, // vtable bytes + 8, 0, // length of object including vtable offset + 7, 0, // start of bool value + 6, 0, 0, 0, // offset for start of vtable (int32) + 0, 0, 0, // padded to 4 bytes + 1, // bool value + }) + + // test 9: vtable with one default bool + b = flatbuffers.NewBuilder(0) + check([]byte{}) + b.StartObject(1) + check([]byte{}) + b.PrependBoolSlot(0, false, false) + b.EndObject() + check([]byte{ + 6, 0, // vtable bytes + 4, 0, // end of object from here + 0, 0, // entry 1 is zero + 6, 0, 0, 0, // offset for start of vtable (int32) + }) + + // test 10: vtable with one int16 + b = flatbuffers.NewBuilder(0) + b.StartObject(1) + b.PrependInt16Slot(0, 0x789A, 0) + b.EndObject() + check([]byte{ + 6, 0, // vtable bytes + 8, 0, // end of object from here + 6, 0, // offset to value + 6, 0, 0, 0, // offset for start of vtable (int32) + 0, 0, // padding to 4 bytes + 0x9A, 0x78, + }) + + // test 11: vtable with two int16 + b = flatbuffers.NewBuilder(0) + b.StartObject(2) + b.PrependInt16Slot(0, 0x3456, 0) + b.PrependInt16Slot(1, 0x789A, 0) + b.EndObject() + check([]byte{ + 8, 0, // vtable bytes + 8, 0, // end of object from here + 6, 0, // offset to value 0 + 4, 0, // offset to value 1 + 8, 0, 0, 0, // offset for start of vtable (int32) + 0x9A, 0x78, // value 1 + 0x56, 0x34, // value 0 + }) + + // test 12: vtable with int16 and bool + b = flatbuffers.NewBuilder(0) + b.StartObject(2) + b.PrependInt16Slot(0, 0x3456, 0) + b.PrependBoolSlot(1, true, false) + b.EndObject() + check([]byte{ + 8, 0, // vtable bytes + 8, 0, // end of object from here + 6, 0, // offset to value 0 + 5, 0, // offset to value 1 + 8, 0, 0, 0, // offset for start of vtable (int32) + 0, // padding + 1, // value 1 + 0x56, 0x34, // value 0 + }) + + // test 12: vtable with empty vector + b = flatbuffers.NewBuilder(0) + b.StartVector(flatbuffers.SizeByte, 0) + vecend := b.EndVector(0) + b.StartObject(1) + b.PrependUOffsetTSlot(0, vecend, 0) + b.EndObject() + check([]byte{ + 6, 0, // vtable bytes + 8, 0, + 4, 0, // offset to vector offset + 6, 0, 0, 0, // offset for start of vtable (int32) + 4, 0, 0, 0, + 0, 0, 0, 0, // length of vector (not in struct) + }) + + // test 12b: vtable with empty vector of byte and some scalars + b = flatbuffers.NewBuilder(0) + b.StartVector(flatbuffers.SizeByte, 0) + vecend = b.EndVector(0) + b.StartObject(2) + b.PrependInt16Slot(0, 55, 0) + b.PrependUOffsetTSlot(1, vecend, 0) + b.EndObject() + check([]byte{ + 8, 0, // vtable bytes + 12, 0, + 10, 0, // offset to value 0 + 4, 0, // offset to vector offset + 8, 0, 0, 0, // vtable loc + 8, 0, 0, 0, // value 1 + 0, 0, 55, 0, // value 0 + + 0, 0, 0, 0, // length of vector (not in struct) + }) + + // test 13: vtable with 1 int16 and 2-vector of int16 + b = flatbuffers.NewBuilder(0) + b.StartVector(flatbuffers.SizeInt16, 2) + b.PrependInt16(0x1234) + b.PrependInt16(0x5678) + vecend = b.EndVector(2) + b.StartObject(2) + b.PrependUOffsetTSlot(1, vecend, 0) + b.PrependInt16Slot(0, 55, 0) + b.EndObject() + check([]byte{ + 8, 0, // vtable bytes + 12, 0, // length of object + 6, 0, // start of value 0 from end of vtable + 8, 0, // start of value 1 from end of buffer + 8, 0, 0, 0, // offset for start of vtable (int32) + 0, 0, // padding + 55, 0, // value 0 + 4, 0, 0, 0, // vector position from here + 2, 0, 0, 0, // length of vector (uint32) + 0x78, 0x56, // vector value 1 + 0x34, 0x12, // vector value 0 + }) + + // test 14: vtable with 1 struct of 1 int8, 1 int16, 1 int32 + b = flatbuffers.NewBuilder(0) + b.StartObject(1) + b.Prep(4+4+4, 0) + b.PrependInt8(55) + b.Pad(3) + b.PrependInt16(0x1234) + b.Pad(2) + b.PrependInt32(0x12345678) + structStart := b.Offset() + b.PrependStructSlot(0, structStart, 0) + b.EndObject() + check([]byte{ + 6, 0, // vtable bytes + 16, 0, // end of object from here + 4, 0, // start of struct from here + 6, 0, 0, 0, // offset for start of vtable (int32) + 0x78, 0x56, 0x34, 0x12, // value 2 + 0, 0, // padding + 0x34, 0x12, // value 1 + 0, 0, 0, // padding + 55, // value 0 + }) + + // test 15: vtable with 1 vector of 2 struct of 2 int8 + b = flatbuffers.NewBuilder(0) + b.StartVector(flatbuffers.SizeInt8*2, 2) + b.PrependInt8(33) + b.PrependInt8(44) + b.PrependInt8(55) + b.PrependInt8(66) + vecend = b.EndVector(2) + b.StartObject(1) + b.PrependUOffsetTSlot(0, vecend, 0) + b.EndObject() + check([]byte{ + 6, 0, // vtable bytes + 8, 0, + 4, 0, // offset of vector offset + 6, 0, 0, 0, // offset for start of vtable (int32) + 4, 0, 0, 0, // vector start offset + + 2, 0, 0, 0, // vector length + 66, // vector value 1,1 + 55, // vector value 1,0 + 44, // vector value 0,1 + 33, // vector value 0,0 + }) + + // test 16: table with some elements + b = flatbuffers.NewBuilder(0) + b.StartObject(2) + b.PrependInt8Slot(0, 33, 0) + b.PrependInt16Slot(1, 66, 0) + off := b.EndObject() + b.Finish(off) + + check([]byte{ + 12, 0, 0, 0, // root of table: points to vtable offset + + 8, 0, // vtable bytes + 8, 0, // end of object from here + 7, 0, // start of value 0 + 4, 0, // start of value 1 + + 8, 0, 0, 0, // offset for start of vtable (int32) + + 66, 0, // value 1 + 0, // padding + 33, // value 0 + }) + + // test 17: one unfinished table and one finished table + b = flatbuffers.NewBuilder(0) + b.StartObject(2) + b.PrependInt8Slot(0, 33, 0) + b.PrependInt8Slot(1, 44, 0) + off = b.EndObject() + b.Finish(off) + + b.StartObject(3) + b.PrependInt8Slot(0, 55, 0) + b.PrependInt8Slot(1, 66, 0) + b.PrependInt8Slot(2, 77, 0) + off = b.EndObject() + b.Finish(off) + + check([]byte{ + 16, 0, 0, 0, // root of table: points to object + 0, 0, // padding + + 10, 0, // vtable bytes + 8, 0, // size of object + 7, 0, // start of value 0 + 6, 0, // start of value 1 + 5, 0, // start of value 2 + 10, 0, 0, 0, // offset for start of vtable (int32) + 0, // padding + 77, // value 2 + 66, // value 1 + 55, // value 0 + + 12, 0, 0, 0, // root of table: points to object + + 8, 0, // vtable bytes + 8, 0, // size of object + 7, 0, // start of value 0 + 6, 0, // start of value 1 + 8, 0, 0, 0, // offset for start of vtable (int32) + 0, 0, // padding + 44, // value 1 + 33, // value 0 + }) + + // test 18: a bunch of bools + b = flatbuffers.NewBuilder(0) + b.StartObject(8) + b.PrependBoolSlot(0, true, false) + b.PrependBoolSlot(1, true, false) + b.PrependBoolSlot(2, true, false) + b.PrependBoolSlot(3, true, false) + b.PrependBoolSlot(4, true, false) + b.PrependBoolSlot(5, true, false) + b.PrependBoolSlot(6, true, false) + b.PrependBoolSlot(7, true, false) + off = b.EndObject() + b.Finish(off) + + check([]byte{ + 24, 0, 0, 0, // root of table: points to vtable offset + + 20, 0, // vtable bytes + 12, 0, // size of object + 11, 0, // start of value 0 + 10, 0, // start of value 1 + 9, 0, // start of value 2 + 8, 0, // start of value 3 + 7, 0, // start of value 4 + 6, 0, // start of value 5 + 5, 0, // start of value 6 + 4, 0, // start of value 7 + 20, 0, 0, 0, // vtable offset + + 1, // value 7 + 1, // value 6 + 1, // value 5 + 1, // value 4 + 1, // value 3 + 1, // value 2 + 1, // value 1 + 1, // value 0 + }) + + // test 19: three bools + b = flatbuffers.NewBuilder(0) + b.StartObject(3) + b.PrependBoolSlot(0, true, false) + b.PrependBoolSlot(1, true, false) + b.PrependBoolSlot(2, true, false) + off = b.EndObject() + b.Finish(off) + + check([]byte{ + 16, 0, 0, 0, // root of table: points to vtable offset + + 0, 0, // padding + + 10, 0, // vtable bytes + 8, 0, // size of object + 7, 0, // start of value 0 + 6, 0, // start of value 1 + 5, 0, // start of value 2 + 10, 0, 0, 0, // vtable offset from here + + 0, // padding + 1, // value 2 + 1, // value 1 + 1, // value 0 + }) + + // test 20: some floats + b = flatbuffers.NewBuilder(0) + b.StartObject(1) + b.PrependFloat32Slot(0, 1.0, 0.0) + off = b.EndObject() + + check([]byte{ + 6, 0, // vtable bytes + 8, 0, // size of object + 4, 0, // start of value 0 + 6, 0, 0, 0, // vtable offset + + 0, 0, 128, 63, // value 0 + }) +} + +// CheckManualBuild builds a Monster manually. +func CheckManualBuild(fail func(string, ...interface{})) ([]byte, flatbuffers.UOffsetT) { + b := flatbuffers.NewBuilder(0) + str := b.CreateString("MyMonster") + + b.StartVector(1, 5) + b.PrependByte(4) + b.PrependByte(3) + b.PrependByte(2) + b.PrependByte(1) + b.PrependByte(0) + inv := b.EndVector(5) + + b.StartObject(13) + b.PrependInt16Slot(2, 20, 100) + mon2 := b.EndObject() + + // Test4Vector + b.StartVector(4, 2) + + // Test 0 + b.Prep(2, 4) + b.Pad(1) + b.PlaceInt8(20) + b.PlaceInt16(10) + + // Test 1 + b.Prep(2, 4) + b.Pad(1) + b.PlaceInt8(40) + b.PlaceInt16(30) + + // end testvector + test4 := b.EndVector(2) + + b.StartObject(13) + + // a vec3 + b.Prep(16, 32) + b.Pad(2) + b.Prep(2, 4) + b.Pad(1) + b.PlaceByte(6) + b.PlaceInt16(5) + b.Pad(1) + b.PlaceByte(4) + b.PlaceFloat64(3.0) + b.Pad(4) + b.PlaceFloat32(3.0) + b.PlaceFloat32(2.0) + b.PlaceFloat32(1.0) + vec3Loc := b.Offset() + // end vec3 + + b.PrependStructSlot(0, vec3Loc, 0) // vec3. noop + b.PrependInt16Slot(2, 80, 100) // hp + b.PrependUOffsetTSlot(3, str, 0) + b.PrependUOffsetTSlot(5, inv, 0) // inventory + b.PrependByteSlot(7, 1, 0) + b.PrependUOffsetTSlot(8, mon2, 0) + b.PrependUOffsetTSlot(9, test4, 0) + mon := b.EndObject() + + b.Finish(mon) + + return b.Bytes, b.Head() +} + +// CheckGeneratedBuild uses generated code to build the example Monster. +func CheckGeneratedBuild(fail func(string, ...interface{})) ([]byte, flatbuffers.UOffsetT) { + b := flatbuffers.NewBuilder(0) + str := b.CreateString("MyMonster") + example.MonsterStartInventoryVector(b, 5) + b.PrependByte(4) + b.PrependByte(3) + b.PrependByte(2) + b.PrependByte(1) + b.PrependByte(0) + inv := b.EndVector(5) + + example.MonsterStart(b) + example.MonsterAddHp(b, int16(20)) + mon2 := example.MonsterEnd(b) + + example.MonsterStartTest4Vector(b, 2) + example.CreateTest(b, 10, 20) + example.CreateTest(b, 30, 40) + test4 := b.EndVector(2) + + example.MonsterStart(b) + + pos := example.CreateVec3(b, 1.0, 2.0, 3.0, 3.0, 4, 5, 6) + example.MonsterAddPos(b, pos) + + example.MonsterAddHp(b, 80) + example.MonsterAddName(b, str) + example.MonsterAddInventory(b, inv) + example.MonsterAddTestType(b, 1) + example.MonsterAddTest(b, mon2) + example.MonsterAddTest4(b, test4) + mon := example.MonsterEnd(b) + + b.Finish(mon) + + return b.Bytes, b.Head() +} + +// CheckVtableDeduplication verifies that vtables are deduplicated. +func CheckVtableDeduplication(fail func(string, ...interface{})) { + b := flatbuffers.NewBuilder(0) + + b.StartObject(4) + b.PrependByteSlot(0, 0, 0) + b.PrependByteSlot(1, 11, 0) + b.PrependByteSlot(2, 22, 0) + b.PrependInt16Slot(3, 33, 0) + obj0 := b.EndObject() + + b.StartObject(4) + b.PrependByteSlot(0, 0, 0) + b.PrependByteSlot(1, 44, 0) + b.PrependByteSlot(2, 55, 0) + b.PrependInt16Slot(3, 66, 0) + obj1 := b.EndObject() + + b.StartObject(4) + b.PrependByteSlot(0, 0, 0) + b.PrependByteSlot(1, 77, 0) + b.PrependByteSlot(2, 88, 0) + b.PrependInt16Slot(3, 99, 0) + obj2 := b.EndObject() + + got := b.Bytes[b.Head():] + + want := []byte{ + 240, 255, 255, 255, // == -12. offset to dedupped vtable. + 99, 0, + 88, + 77, + 248, 255, 255, 255, // == -8. offset to dedupped vtable. + 66, 0, + 55, + 44, + 12, 0, + 8, 0, + 0, 0, + 7, 0, + 6, 0, + 4, 0, + 12, 0, 0, 0, + 33, 0, + 22, + 11, + } + + if !bytes.Equal(want, got) { + fail("testVtableDeduplication want:\n%d %v\nbut got:\n%d %v\n", + 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} + + testTable := func(tab *flatbuffers.Table, a flatbuffers.VOffsetT, b, c, d byte) { + // vtable size + if got := tab.GetVOffsetTSlot(0, 0); 12 != got { + fail("failed 0, 0: %d", got) + } + // object size + if got := tab.GetVOffsetTSlot(2, 0); 8 != got { + fail("failed 2, 0: %d", got) + } + // default value + if got := tab.GetVOffsetTSlot(4, 0); a != got { + fail("failed 4, 0: %d", got) + } + if got := tab.GetByteSlot(6, 0); b != got { + fail("failed 6, 0: %d", got) + } + if val := tab.GetByteSlot(8, 0); c != val { + fail("failed 8, 0: %d", got) + } + if got := tab.GetByteSlot(10, 0); d != got { + fail("failed 10, 0: %d", got) + } + } + + testTable(table0, 0, 11, 22, 33) + testTable(table1, 0, 44, 55, 66) + testTable(table2, 0, 77, 88, 99) +} + +// CheckDocExample checks that the code given in FlatBuffers documentation +// is syntactically correct. +func CheckDocExample(buf []byte, off flatbuffers.UOffsetT, fail func(string, ...interface{})) { + monster := example.GetRootAsMonster(buf, off) + _ = monster.Hp() + _ = monster.Pos(nil) + for i := 0; i < monster.InventoryLength(); i++ { + _ = monster.Inventory(i) // do something here + } + + builder := flatbuffers.NewBuilder(0) + + example.MonsterStartInventoryVector(builder, 5) + for i := 4; i >= 0; i-- { + builder.PrependByte(byte(i)) + } + inv := builder.EndVector(5) + + str := builder.CreateString("MyMonster") + example.MonsterStart(builder) + example.MonsterAddPos(builder, example.CreateVec3(builder, 1.0, 2.0, 3.0, 3.0, 4, 5, 6)) + example.MonsterAddHp(builder, 80) + example.MonsterAddName(builder, str) + example.MonsterAddInventory(builder, inv) + example.MonsterAddTestType(builder, 1) + // example.MonsterAddTest(builder, mon2) + // example.MonsterAddTest4(builder, test4s) + _ = example.MonsterEnd(builder) +} + +// Include simple random number generator to ensure results will be the +// same cross platform. +// http://en.wikipedia.org/wiki/Park%E2%80%93Miller_random_number_generator +type LCG uint32 + +const InitialLCGSeed = 48271 + +func NewLCG() *LCG { + n := uint32(InitialLCGSeed) + l := LCG(n) + return &l +} + +func (lcg *LCG) Reset() { + *lcg = InitialLCGSeed +} + +func (lcg *LCG) Next() uint32 { + n := uint32((uint64(*lcg) * uint64(279470273)) % uint64(4294967291)) + *lcg = LCG(n) + return n +} + +// CheckEquality verifies that two byte buffers are the same. +func CheckEquality(a, b []byte, msg string, fail func(string, ...interface{})) { + if !bytes.Equal(a, b) { + fail("%s constructed object does not pass byte equality", msg) + } +} + +// BenchmarkVtableDeduplication measures the speed of vtable deduplication +// by creating prePop vtables, then populating b.N objects with a +// different single vtable. +// +// When b.N is large (as in long benchmarks), memory usage may be high. +func BenchmarkVtableDeduplication(b *testing.B) { + prePop := 10 + builder := flatbuffers.NewBuilder(0) + + // pre-populate some vtables: + for i := 0; i < prePop; i++ { + builder.StartObject(i) + for j := 0; j < i; j++ { + builder.PrependInt16Slot(j, int16(j), 0) + } + builder.EndObject() + } + + // benchmark deduplication of a new vtable: + b.ResetTimer() + for i := 0; i < b.N; i++ { + lim := prePop + + builder.StartObject(lim) + for j := 0; j < lim; j++ { + builder.PrependInt16Slot(j, int16(j), 0) + } + builder.EndObject() + } +} -- cgit v1.2.3