github.com/gogo/protobuf@v1.3.2/test/combos/both/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) MarshalTo(data []byte) (n int, err error) { 45 return r.protoType().MarshalTo(data) 46 } 47 48 func (r *T) Unmarshal(data []byte) error { 49 pr := &ProtoType{} 50 err := proto.Unmarshal(data, pr) 51 if err != nil { 52 return err 53 } 54 55 if pr.Field2 != nil { 56 r.Data = *pr.Field2 57 } 58 return nil 59 } 60 61 func (gt T) MarshalJSON() ([]byte, error) { 62 return json.Marshal(gt.Data) 63 } 64 65 func (gt *T) UnmarshalJSON(data []byte) error { 66 var s string 67 err := json.Unmarshal(data, &s) 68 if err != nil { 69 return err 70 } 71 *gt = T{Data: s} 72 return nil 73 } 74 75 func (gt T) Compare(other T) int { 76 return strings.Compare(gt.Data, other.Data) 77 }