-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (99 loc) · 3.03 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"errors"
"github.com/brianvoe/gofakeit"
"github.com/ymz-ncnk/assert"
"google.golang.org/protobuf/proto"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
func init() {
assert.On = true
}
// In this example, Protobuf encoding is implemented using mus-go. There are two
// structures DataV1, DataV2. The last one is the same as DataV1, but with two
// deleted fields: Bool and Slice.
func main() {
var (
dataV1 = DataV1{
Str: gofakeit.UUID(),
Bool: gofakeit.Bool(),
Int32: gofakeit.Int32(),
Float64: gofakeit.Float64(),
Slice: []int32{gofakeit.Int32(), gofakeit.Int32()},
Time: timestamppb.New(gofakeit.Date()),
}
dataV2 = DataV2{
Str: gofakeit.UUID(),
Int32: gofakeit.Int32(),
Float64: gofakeit.Float64(),
Time: timestamppb.New(gofakeit.Date()),
}
)
// Marshal using protobuf and unmarshal using mus-go implementation (at
// the end the unmarshalled data is compared with the original).
MarshalProtobuf_UnmarshalMusGo(&dataV1)
// Marshal using mus-go - unmarshal using protobuf.
MarshalMusGo_UnmarshalProtobuf(&dataV1)
// Marshal first version and unmarshal second, both using mus-go.
MarshalDataV1_UnmarshalDataV2(&dataV1)
// Marshal second version and unmarshal first one again using mus-go.
MarshalDataV2_UnmarshalDataV1(&dataV2)
// As you can see, everything works as expected.
}
func MarshalProtobuf_UnmarshalMusGo(data *DataV1) {
bs, err := proto.Marshal(data)
assert.EqualError(err, nil)
adata, _, err := UnmarshalDataV1Protobuf(bs)
assert.EqualError(err, nil)
assert.Equal(data.String(), adata.String())
}
func MarshalMusGo_UnmarshalProtobuf(data *DataV1) {
bs := make([]byte, SizeDataV1Protobuf(data))
MarshalDataV1Protobuf(data, bs)
adata := DataV1{}
err := proto.Unmarshal(bs, &adata)
assert.EqualError(err, nil)
assert.Equal(data.String(), adata.String())
}
func MarshalDataV1_UnmarshalDataV2(dataV1 *DataV1) {
bs := make([]byte, SizeDataV1Protobuf(dataV1))
MarshalDataV1Protobuf(dataV1, bs)
dataV2, _, err := UnmarshalDataV2Protobuf(bs)
assert.EqualError(err, nil)
if err := same(dataV1, dataV2); err != nil {
panic(err)
}
}
func MarshalDataV2_UnmarshalDataV1(dataV2 *DataV2) {
bs := make([]byte, SizeDataV2Protobuf(dataV2))
MarshalDataV2Protobuf(dataV2, bs)
dataV1, _, err := UnmarshalDataV1Protobuf(bs)
assert.EqualError(err, nil)
if err := same(dataV1, dataV2); err != nil {
panic(err)
}
}
func same(dataV1 *DataV1, dataV2 *DataV2) (err error) {
if dataV1.Str != dataV2.Str {
return errors.New("Str")
}
if dataV1.Int32 != dataV2.Int32 {
return errors.New("Int32")
}
if dataV1.Float64 != dataV2.Float64 {
return errors.New("Float64")
}
if dataV1.Time != nil && dataV2.Time != nil {
if dataV1.Time.Seconds != dataV2.Time.Seconds {
return errors.New("Seconds")
}
if dataV1.Time.Nanos != dataV2.Time.Nanos {
return errors.New("Nanos")
}
}
if (dataV1.Time != nil && dataV2.Time == nil) ||
(dataV1.Time == nil && dataV2.Time != nil) {
return errors.New("one time is nil, another is not")
}
return
}