-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
140 lines (127 loc) · 5.11 KB
/
helpers_test.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package protoparts
import (
"fmt"
"testing"
"github.com/obeattie/protoparts/internal/testproto"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/dynamicpb"
)
func p[T any](v T) *T {
return &v
}
func marshalProto(t testing.TB, msg proto.Message) []byte {
b, err := proto.MarshalOptions{Deterministic: true}.Marshal(msg)
require.NoError(t, err)
return b
}
func testMsg(t testing.TB, name, streetAddress, city *string, tags []string, boop [][]byte, kv map[string]string) *dynamicpb.Message {
personMd := (&testproto.Person{}).ProtoReflect().Descriptor()
person := dynamicpb.NewMessage(personMd)
if name != nil {
person.Set(personMd.Fields().ByName("name"), protoreflect.ValueOfString(*name))
}
if streetAddress != nil || city != nil {
addressMd := personMd.Fields().ByName("address").Message()
address := dynamicpb.NewMessage(addressMd)
if streetAddress != nil {
address.Set(addressMd.Fields().ByName("street_address"), protoreflect.ValueOfString(*streetAddress))
}
if city != nil {
address.Set(addressMd.Fields().ByName("city"), protoreflect.ValueOfString(*city))
}
person.Set(personMd.Fields().ByName("address"), protoreflect.ValueOfMessage(address))
}
if tags != nil {
tagsL := person.NewField(personMd.Fields().ByName("tags")).List()
for _, t := range tags {
tagsL.Append(protoreflect.ValueOfString(t))
}
person.Set(personMd.Fields().ByName("tags"), protoreflect.ValueOfList(tagsL))
}
if boop != nil {
boopL := person.NewField(personMd.Fields().ByName("boop")).List()
for _, b := range boop {
boopL.Append(protoreflect.ValueOfBytes(b))
}
person.Set(personMd.Fields().ByName("boop"), protoreflect.ValueOfList(boopL))
}
if kv != nil {
kvM := person.NewField(personMd.Fields().ByName("map_string_string")).Map()
for k, v := range kv {
kvM.Set(protoreflect.ValueOfString(k).MapKey(), protoreflect.ValueOfString(v))
}
person.Set(personMd.Fields().ByName("map_string_string"), protoreflect.ValueOfMap(kvM))
}
return person
}
func quickTestMsg(t testing.TB) *dynamicpb.Message {
person := testMsg(t,
p("Ryan Gosling"),
p("3532 Hayden Ave"),
p("Culver City"),
[]string{
"The Driver",
"Sebastian Wilder",
"", // deliberately diabolical
},
[][]byte{
[]byte("🕺"),
[]byte("🏍"),
},
map[string]string{
"k1": "v1",
"k2": "v2",
"k3": "v3",
"k4": "v4",
"": "", // deliberately diabolical
})
personMd := person.Type().Descriptor()
address := person.Get(personMd.Fields().ByName("address")).Message()
addressMd := address.Type().Descriptor()
latLngMd := addressMd.Fields().ByName("lat_lng").Message()
latLng := dynamicpb.NewMessage(latLngMd)
latLng.Set(latLngMd.Fields().ByName("latitude"), protoreflect.ValueOfFloat64(34.0257178))
latLng.Set(latLngMd.Fields().ByName("longitude"), protoreflect.ValueOfFloat64(-118.3802275))
address.Set(addressMd.Fields().ByName("lat_lng"), protoreflect.ValueOf(latLng))
person.Set(personMd.Fields().ByName("address"), protoreflect.ValueOf(address))
moarAddresses := person.NewField(personMd.Fields().ByName("moar_addresses")).List()
moarAddresses.Append(protoreflect.ValueOf(address))
moarAddresses.Append(protoreflect.ValueOf(address))
person.Set(personMd.Fields().ByName("moar_addresses"), protoreflect.ValueOf(moarAddresses))
single := personMd.Fields().ByName("marital_status").Enum().Values().ByName("SINGLE")
person.Set(personMd.Fields().ByName("marital_status"), protoreflect.ValueOf(single.Number()))
person.Set(personMd.Fields().ByName("maybe_latlng"), protoreflect.ValueOf(latLng))
mapStringLatLng := person.NewField(personMd.Fields().ByName("map_string_latlng")).Map()
for i := 0; i < 100; i++ { // 100 map elements
mapStringLatLng.Set(protoreflect.ValueOf(fmt.Sprintf("%da", i)).MapKey(), protoreflect.ValueOf(latLng))
mapStringLatLng.Set(protoreflect.ValueOf(fmt.Sprintf("%db", i)).MapKey(), protoreflect.ValueOf(latLng))
mapStringLatLng.Set(protoreflect.ValueOf(fmt.Sprintf("%dc", i)).MapKey(), protoreflect.ValueOf(latLng))
mapStringLatLng.Set(protoreflect.ValueOf(fmt.Sprintf("%dd", i)).MapKey(), protoreflect.ValueOf(latLng))
}
// Also set an empty-valued key – this is an important edge case, serialising as a map element with a 0-length value
emptyLatLng := dynamicpb.NewMessage(latLngMd)
mapStringLatLng.Set(protoreflect.ValueOf("empty").MapKey(), protoreflect.ValueOf(emptyLatLng))
person.Set(personMd.Fields().ByName("map_string_latlng"), protoreflect.ValueOf(mapStringLatLng))
return person
}
// benchmarkMsg returns a message for use in all benchmarks (so benchmarks involving serialisation/deserialisation code
// are using a common baseline)
func benchmarkMsg(t testing.TB) *dynamicpb.Message {
return testMsg(t,
p("Sirius Black"),
p("12 Grimmauld Place"),
p("London"),
[]string{"confringo"},
nil,
map[string]string{
"wizard": "yes"})
}
// split is a shortcut which splits the dynamic message into Parts
func split(t testing.TB, msg protoreflect.Message) Parts {
pb := marshalProto(t, msg.Interface())
parts, err := Split(pb, msg.Descriptor())
require.NoError(t, err)
return parts
}