English | 中文
A parser for flatbuffers in Go.
- Import
package fbs
:
import "trpc.group/trpc-go/fbs"
- Pass in the flatbuffers file list you want to parse. Create a
Parser
and callParseFiles
.
filenames := []string{
"./file1.fbs", // Pass in a list of flatbuffers files.
"./file2.fbs",
}
p := fbs.NewParser()
fds, err := p.ParseFiles(filenames...)
fd1 := fds[0] // The parsed result(descriptor) of file1.fbs
fd2 := fds[1] // The parsed result(descriptor) of file2.fbs
You can access the resulting descriptors from fbs
and get rich information about every method definition of rpc
services, e.g. method names, input/output type, client/server streaming, etc.
The descriptor SchemaDesc
for every file has the following fields:
// SchemaDesc describes a complete .fbs file.
type SchemaDesc struct {
// Schema stores the root node of the AST.
Schema *ast.SchemaNode
// Name stores the .fbs file name.
Name string
// Namespace of schema will change in the course of processing each decl, these will be stored
// in this slice. Decls of table/struct/enum/union will set their own namespaces to be the last
// namespace in this slice.
// See parseResult.createSchemaDescriptor.
Namespaces []string
// Root stores root_type declaration in flatbuffers file.
Root string
// FileExt stores file_extension declaration in flatbuffers file.
FileExt string
// FileIdent stores file_identifier declaration in flatbuffers file.
FileIdent string
// Attrs stores attributes definitions in flatbuffers file.
Attrs []string
// Includes stores all included file names.
Includes []string
// Dependencies stores all descriptors corresponding to included files.
Dependencies []*SchemaDesc
// Tables stores all table descriptors.
Tables []*TableDesc
// Structs stores all struct descriptors.
Structs []*StructDesc
// Enums stores all enum descriptors.
Enums []*EnumDesc
// Unions stores all union descriptors.
Unions []*UnionDesc
// RPCs stores all rpc service descriptors.
RPCs []*RPCDesc
}
Through accessing these fields such as RPCs
, you can get information defined in flatbuffers to get related
work done(e.g. generate stub files for rpc services).
.
├── desc.go # Definitions of descriptors for all kinds of node.
├── desc_test.go
├── doc.go
├── docs # Documents of implementation.
├── errors.go # Error handling.
├── errors_test.go
├── fbsfiles # Places .fbs for testing.
├── fbs.y # Hand-written according to the grammar of flatbuffers.
├── fbs.y.go # Generated by fbs.y, used to parse token stream to AST.
├── go.mod
├── go.sum
├── internal
│ └── ast # Stores definitions and constructions of nodes in AST.
├── lexer.go # lexer implementation.
├── lexer_test.go
├── linker.go # linker implementation.
├── linker_test.go
├── parse_result.go # Definition for parsed result.
├── parser.go # parser implementation.
├── parser_test.go
├── reader.go # reader implementation.
├── README.md
└── scope.go # scope implementation.
Further information see implementation details
This project enables trpc-go to have flatbuffers support.