/
prototext.go
35 lines (29 loc) · 991 Bytes
/
prototext.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package encoders
import (
"errors"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
)
var _ Marshaller = (*protoTextEncoder)(nil)
// protoTextEncoder implements Marshaller for the ProtoText format.
type protoTextEncoder struct{}
// Marshal returns the ProtoText encoding of v.
func (protoTextEncoder) Marshal(v interface{}) ([]byte, error) {
m, ok := v.(proto.Message)
if !ok {
return nil, errors.New("invalid proto message")
}
return prototext.Marshal(m)
}
// Unmarshal parses the ProtoText-encoded data and stores the result
// in the value pointed to by v. If v is nil or not a pointer,
// Unmarshal returns an InvalidUnmarshalError.
func (protoTextEncoder) Unmarshal(data []byte, v interface{}) error {
m, ok := v.(proto.Message)
if !ok {
return errors.New("invalid proto message")
}
return prototext.Unmarshal(data, m)
}
// ProtoText holds a proto text encoder instance implementing Marshaller.
var ProtoText = &protoTextEncoder{}