Categorygithub.com/chaimhong/gobuf
repositorypackage
0.0.0-20180609094436-a4e3ef684791
Repository: https://github.com/chaimhong/gobuf.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# README

What's is Gobuf?

Gobuf is a binary serialization format like protobuf.

But it didn't need any DSL file to define messages.

It just needs you write down data format by Go structs.

It can analize Go struct and generate marshaller and unmarshaller code automaticlly.

And it not only supports generate Go code but also C#.

It has a good plugin mechanism, let you can add different programming language generation easily.

Pipeline

 +-----------+           +-----------------+        +----------+          +-------------------+
 |  Go code  <-----------+  Gobuf Command  +-------->  Plugin  +---------->  Target Language  |
 +-----------+  analyze  +-----------------+  JSON  +----------+  output  +-------------------+
  1. Gobuf command line tool analyze a Go source file.
  2. Gobuf output type informations to command line standard output.
  3. Plugin use type informations to generate target language source code.

Example:

gobuf protocol.go | gobuf-cs > protocol.gb.cs

Types

Gobuf supports almost all kinds of Go data type, but it has some limits.

This is all the data types that Gobuf supports:

  • Type = Scalar | Composite
  • Scalar = Numberic | "string" | "bool"
  • Numeric = "int" | "uint" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32" | "int64" | "uint64" | "float32" | "float64"
  • Composite = Pointer | Array | Map | Bytes | Struct
  • Pointer = "*" ( Scalar | Struct )
  • Array = "[" "]" Type
  • Bytes = "[" "]" "byte"
  • Map = "map" "[" Scalar "]" Type
  • Struct = "type" Name "struct" "{" Name Type [";" Name Type] "}"

Type Maping

Gobuf maping Go type to different programming languages.

This is the type maping rule:

GoC#
intlong
uintulong
int8sbyte
uint8byte
int16short
uint16ushort
int32int
uint32uint
int64long
uint64ulong
float32float
float64double
boolbool
stringutf8 string
[]bytebyte[]
[]TypeList<Type>
map[Scalar]TypeDictionary<Scalar, Type>
*ScalarNullable<Scalar>
*StructClass
StructClass

Wired Format

TypeFormat
intbase128 varint + zig-zag
uintbase128 varint
int8, uint8, bool1byte
int16, uint162byte little-endian
int32, uint324byte little-endian
int64, uint648byte little-endian
float32convert to uint32 bits
float64convert to uint64 bits
stringuint(utf8_length) + utf8_length
[]byteuint(length) + length
[]Typeuint(count) + items
map[Scalar]Typeuint(count) + items(key + value)
*Scalaris_null ? uint8(0) : uint8(1) + element
*Structis_null ? uint8(0) : uint8(1) + fields
Structfields