Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mkeeler/protoc-gen-go-binary
Auto generate Binary Marshaler inteface
https://github.com/mkeeler/protoc-gen-go-binary
Last synced: 19 days ago
JSON representation
Auto generate Binary Marshaler inteface
- Host: GitHub
- URL: https://github.com/mkeeler/protoc-gen-go-binary
- Owner: mkeeler
- License: mit
- Created: 2019-09-27T19:38:26.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2019-09-27T19:41:46.000Z (about 5 years ago)
- Last Synced: 2023-03-30T16:02:06.631Z (over 1 year ago)
- Language: Go
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# protoc-gen-go-binary
This is a plugin for the Google Protocol Buffers compiler
[`protoc`](https://github.com/protocolbuffers/protobuf) that generates
code to implement [`encoding.BinaryMarshaler`](https://golang.org/pkg/encoding/#BinaryMarshaler)
and [`encoding.BinaryUnmarshaler`](https://golang.org/pkg/encoding/#BinaryUnmarshaler)
by just calling the `Marshal` and `Unmarshal` functions already generated for the types.This enables Go-generated protobuf messages to be used in situations where the code
already supports using the binary marshaling interfaces.Most of the code herein was copied from github.com/mitchellh/protoc-gen-go-json with enough modifications
to implement the binary marshaling interfaces instead of the JSON ones.## Install
```
go get github.com/mkeeler/protoc-gen-go-binary
```Also required:
- [protoc](https://github.com/google/protobuf)
- [protoc-gen-gofast](https://github.com/gogo/protobuf)## Usage
Define your messages like normal:
```proto
syntax = "proto3";message Request {
oneof kind {
string name = 1;
int32 code = 2;
}
}
```The example message purposely uses a `oneof` since this won't work by
default with `encoding/json`. Next, generate the code:```
protoc --gofast_out=. --go-binary_out=. request.proto
```Your output should contain a file `request.pb.binary.go` which contains
the implementation of `encoding.BinaryMarshal/BinaryUnmarshal` for all your message types.
You can then encode binary encode your message as protobufs.```go
import (
"bytes"
"encoding/gob"
)var buf bytes.Buffer
encoder := gob.NewEncoder(&buf)// Marshal
err := encoder.Encode(&Request{
Kind: &Kind_Name{
Name: "alice",
},
}// Unmarshal
var result Request
decoder := gob.NewDecoder(&buf)
err := decoder.Decode(&result)
```