mirror of
https://github.com/status-im/consul.git
synced 2025-01-14 07:44:50 +00:00
abed91d069
* Add JSON and Binary Marshaler Generators for Protobuf Types * Generate files with the correct version of gogo/protobuf I have pinned the version in the makefile so when you run make tools you get the right version. This pulls the version out of go.mod so it should remain up to date. The version at the time of this commit we are using is v1.2.1 * Fixup some shell output * Update how we determine the version of gogo This just greps the go.mod file instead of expecting the go mod cache to already be present * Fixup vendoring and remove no longer needed json encoder functions
36 lines
886 B
Go
36 lines
886 B
Go
package agentpb
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
)
|
|
|
|
type ProtoMarshaller interface {
|
|
Size() int
|
|
MarshalTo([]byte) (int, error)
|
|
Unmarshal([]byte) error
|
|
ProtoMessage()
|
|
}
|
|
|
|
func EncodeInterface(t structs.MessageType, message interface{}) ([]byte, error) {
|
|
if marshaller, ok := message.(ProtoMarshaller); ok {
|
|
return Encode(t, marshaller)
|
|
}
|
|
return nil, fmt.Errorf("message does not implement the ProtoMarshaller interface: %T", message)
|
|
}
|
|
|
|
func Encode(t structs.MessageType, message ProtoMarshaller) ([]byte, error) {
|
|
data := make([]byte, message.Size()+1)
|
|
data[0] = uint8(t)
|
|
if _, err := message.MarshalTo(data[1:]); err != nil {
|
|
return nil, err
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
func Decode(buf []byte, out ProtoMarshaller) error {
|
|
// Note that this assumes the leading byte indicating the type has already been stripped off
|
|
return out.Unmarshal(buf)
|
|
}
|