-
Notifications
You must be signed in to change notification settings - Fork 807
/
t.go
73 lines (60 loc) · 1.1 KB
/
t.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package test
import (
"encoding/json"
"strings"
"github.com/gogo/protobuf/proto"
)
type T struct {
Data string
}
func (gt *T) protoType() *ProtoType {
return &ProtoType{
Field2: >.Data,
}
}
func (gt T) Equal(other T) bool {
return gt.protoType().Equal(other.protoType())
}
func (gt *T) Size() int {
proto := &ProtoType{
Field2: >.Data,
}
return proto.Size()
}
func NewPopulatedT(r randyThetest) *T {
data := NewPopulatedProtoType(r, false).Field2
gt := &T{}
if data != nil {
gt.Data = *data
}
return gt
}
func (r T) Marshal() ([]byte, error) {
return proto.Marshal(r.protoType())
}
func (r *T) Unmarshal(data []byte) error {
pr := &ProtoType{}
err := proto.Unmarshal(data, pr)
if err != nil {
return err
}
if pr.Field2 != nil {
r.Data = *pr.Field2
}
return nil
}
func (gt T) MarshalJSON() ([]byte, error) {
return json.Marshal(gt.Data)
}
func (gt *T) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
*gt = T{Data: s}
return nil
}
func (gt T) Compare(other T) int {
return strings.Compare(gt.Data, other.Data)
}