Swap type assert and fix it (#418) * swap type assert back * type assert rather than check for marshal function at compile time * Fix swap type assert to only call Size when Marshal is generated. * added Sizer type assert at cachedsize method. The sizecache is not calculated if the struct conforms to the Sizer interface * fixed go vet issue. removed err variable shadowing * refactor pointer interface type conversion out in order to implement both reflect and unsafe cases * added minimal test example * added sizer option when computing the marshalinfo and generator now either use MarshalTo or default marhsaling. hassizer property now checked in the marshalinfo size method if the type has a Size() method. removed the Sizer interface check in the generated XXX_Size methods. * remove unnecessary variable and just return size. * added a check if the message have a protosize method. changed the size and cached size methods to just return the message size without looking at any other struct fields. * removed the sizer checks in the caching method. only use size mehtods of the message is also a marshaler * revert the bench result accidental update * do a generate time check for XXX_unmarshal. Special thanks to @jmarais for doing all the work for this commit.
gogoprotobuf is a fork of golang/protobuf with extra code generation features.
This code generation is used to achieve:
Keeping track of how up to date gogoprotobuf is relative to golang/protobuf is done in this issue
These projects use gogoprotobuf:
Please let us know if you are using gogoprotobuf by posting on our GoogleGroup.
There are several ways to use gogoprotobuf, but for all you need to install go and protoc. After that you can choose:
To install it, you must first have Go (at least version 1.6.3 or 1.9 if you are using gRPC) installed (see http://golang.org/doc/install). Latest patch versions of 1.9 and 1.10 are continuously tested.
Next, install the standard protocol buffer implementation from https://github.com/google/protobuf. Most versions from 2.3.1 should not give any problems, but 2.6.1, 3.0.2 and 3.5.1 are continuously tested.
Install the protoc-gen-gofast binary
go get github.com/gogo/protobuf/protoc-gen-gofast
Use it to generate faster marshaling and unmarshaling go code for your protocol buffers.
protoc --gofast_out=. myproto.proto
This does not allow you to use any of the other gogoprotobuf extensions.
Fields without pointers cause less time in the garbage collector. More code generation results in more convenient methods.
Other binaries are also included:
protoc-gen-gogofast (same as gofast, but imports gogoprotobuf) protoc-gen-gogofaster (same as gogofast, without XXX_unrecognized, less pointer fields) protoc-gen-gogoslick (same as gogofaster, but with generated string, gostring and equal methods)
Installing any of these binaries is easy. Simply run:
go get github.com/gogo/protobuf/proto
go get github.com/gogo/protobuf/{binary}
go get github.com/gogo/protobuf/gogoproto
These binaries allow you to use gogoprotobuf extensions. You can also use your own binary.
To generate the code, you also need to set the include path properly.
protoc -I=. -I=$GOPATH/src -I=$GOPATH/src/github.com/gogo/protobuf/protobuf --{binary}_out=. myproto.proto
To use proto files from “google/protobuf” you need to add additional args to protoc.
protoc -I=. -I=$GOPATH/src -I=$GOPATH/src/github.com/gogo/protobuf/protobuf --{binary}_out=\
Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/duration.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/struct.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/wrappers.proto=github.com/gogo/protobuf/types:. \
myproto.proto
Note that in the protoc command, {binary} does not contain the initial prefix of “protoc-gen”.
Customizing the fields of the messages to be the fields that you actually want to use removes the need to copy between the structs you use and structs you use to serialize. gogoprotobuf also offers more serialization formats and generation of tests and even more methods.
Please visit the extensions page for more documentation.
Install protoc-gen-gogo:
go get github.com/gogo/protobuf/proto go get github.com/gogo/protobuf/jsonpb go get github.com/gogo/protobuf/protoc-gen-gogo go get github.com/gogo/protobuf/gogoproto
It works the same as golang/protobuf, simply specify the plugin. Here is an example using gofast:
protoc --gofast_out=plugins=grpc:. my.proto
See https://github.com/gogo/grpc-example for an example of using gRPC with gogoprotobuf and the wider grpc-ecosystem.