github.com/gogo/protobuf@v1.3.2/test/combos/unmarshaler/t.go (about) 1 package test 2 3 import ( 4 "encoding/json" 5 "strings" 6 7 "github.com/gogo/protobuf/proto" 8 ) 9 10 type T struct { 11 Data string 12 } 13 14 func (gt *T) protoType() *ProtoType { 15 return &ProtoType{ 16 Field2: >.Data, 17 } 18 } 19 20 func (gt T) Equal(other T) bool { 21 return gt.protoType().Equal(other.protoType()) 22 } 23 24 func (gt *T) Size() int { 25 proto := &ProtoType{ 26 Field2: >.Data, 27 } 28 return proto.Size() 29 } 30 31 func NewPopulatedT(r randyThetest) *T { 32 data := NewPopulatedProtoType(r, false).Field2 33 gt := &T{} 34 if data != nil { 35 gt.Data = *data 36 } 37 return gt 38 } 39 40 func (r T) Marshal() ([]byte, error) { 41 return proto.Marshal(r.protoType()) 42 } 43 44 func (r *T) Unmarshal(data []byte) error { 45 pr := &ProtoType{} 46 err := proto.Unmarshal(data, pr) 47 if err != nil { 48 return err 49 } 50 51 if pr.Field2 != nil { 52 r.Data = *pr.Field2 53 } 54 return nil 55 } 56 57 func (gt T) MarshalJSON() ([]byte, error) { 58 return json.Marshal(gt.Data) 59 } 60 61 func (gt *T) UnmarshalJSON(data []byte) error { 62 var s string 63 err := json.Unmarshal(data, &s) 64 if err != nil { 65 return err 66 } 67 *gt = T{Data: s} 68 return nil 69 } 70 71 func (gt T) Compare(other T) int { 72 return strings.Compare(gt.Data, other.Data) 73 }