Deprecated Experimental encoder in Go for Protocol Buffers using gogoprotobuf.
This package takes a Parser interface for any parser and encodes it. The encoder for JSON, XML and Reflect is also experimentally supported.
Here is an example of encoding into a protocol buffer (marshaling/dynamic transcoding).
import "github.com/katydid/parser-go/parser"
import "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
import "github.com/katydid/encode-gogo-proto/proto"
func encodeToProto(p parser.Interface, fileDescriptorSet *descriptor.FileDescriptorSet, packageName, messageName string) ([]byte, error){
enc, err := proto.NewEncoder(fileDescriptorSet, packageName, messageName)
if err != nil {
return nil, err
}
return enc.Encode(make([]byte, 1024*1024), p)
}
Dynamic encoding means that the protocol buffer does not necessarily need to be compiled in the program.
Parsing the .proto
files with protoc
can give you a fileDescriptorSet which specifies the protocol buffer message.
The packageName and messageName are used to locate the message in the fileDescriptorSet.
The fileDescriptorSet can also be generated by gogoprotobuf as a method on the message.
The encoder can be reused and providing a large enough buffer to encode means that the buffer can also be reused.