mirror of
https://github.com/status-im/consul.git
synced 2025-01-11 06:16:08 +00:00
76cf54068b
In a previous PR I made it so that we had interfaces that would work enough to allow blockingQueries to work. However to complete this we need all fields to be settable and gettable. Notes: • If Go ever gets contracts/generics then we could get rid of all the Getters/Setters • protoc / protoc-gen-gogo are going to generate all the getters for us. • I copied all the getters/setters from the protobuf funcs into agent/structs/protobuf_compat.go • Also added JSON marshaling funcs that use jsonpb for protobuf types.
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package agentpb
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/gogo/protobuf/jsonpb"
|
|
"github.com/gogo/protobuf/proto"
|
|
"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)
|
|
}
|
|
|
|
func MarshalJSON(msg proto.Message) ([]byte, error) {
|
|
m := jsonpb.Marshaler{
|
|
EmitDefaults: false,
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
if err := m.Marshal(&buf, msg); err != nil {
|
|
return nil, err
|
|
}
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
func UnmarshalJSON(r io.Reader, msg proto.Message) error {
|
|
u := jsonpb.Unmarshaler{
|
|
AllowUnknownFields: true,
|
|
}
|
|
|
|
return u.Unmarshal(r, msg)
|
|
}
|
|
|
|
func UnmarshalJSONBytes(b []byte, msg proto.Message) error {
|
|
return UnmarshalJSON(bytes.NewReader(b), msg)
|
|
}
|