-
Notifications
You must be signed in to change notification settings - Fork 5
/
load.go
217 lines (207 loc) · 7.13 KB
/
load.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package protobq
import (
"fmt"
"cloud.google.com/go/bigquery"
"go.einride.tech/protobuf-bigquery/internal/wkt"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
// Load the bigquery.Value list into the given proto.Message using the given bigquery.Schema.
// It will clear the message first before setting the fields. If it returns an error,
// the given message may be partially set.
func Load(bqMessage []bigquery.Value, bqSchema bigquery.Schema, message proto.Message) error {
return UnmarshalOptions{}.Load(bqMessage, bqSchema, message)
}
// Load the bigquery.Value list into the given proto.Message using the given bigquery.Schema
// using options in UnmarshalOptions object.
// It will clear the message first before setting the fields. If it returns an error,
// the given message may be partially set.
func (o UnmarshalOptions) Load(bqMessage []bigquery.Value, bqSchema bigquery.Schema, message proto.Message) error {
proto.Reset(message)
if err := o.loadMessage(bqMessage, bqSchema, message.ProtoReflect()); err != nil {
return err
}
if o.AllowPartial {
return nil
}
return proto.CheckInitialized(message)
}
func (o UnmarshalOptions) loadMessage(
bqMessage []bigquery.Value,
bqSchema bigquery.Schema,
message protoreflect.Message,
) error {
if len(bqMessage) != len(bqSchema) {
return fmt.Errorf("message has %d fields but schema has %d fields", len(bqMessage), len(bqSchema))
}
for i, bqFieldSchema := range bqSchema {
bqField := bqMessage[i]
fieldName := protoreflect.Name(bqFieldSchema.Name)
field := message.Descriptor().Fields().ByName(fieldName)
if field == nil {
if !o.DiscardUnknown && !message.Descriptor().ReservedNames().Has(fieldName) {
return fmt.Errorf("unknown field: %s", fieldName)
}
continue
}
switch {
case field.IsList():
if err := o.loadListField(bqField, bqFieldSchema, field, message); err != nil {
return err
}
case field.IsMap():
if err := o.loadMapField(bqField, bqFieldSchema, field, message); err != nil {
return err
}
default:
value, err := o.loadSingularField(bqField, bqFieldSchema, field, message)
if err != nil {
return err
}
if value.IsValid() {
message.Set(field, value)
}
}
}
return nil
}
func (o UnmarshalOptions) loadListField(
bqField bigquery.Value,
bqFieldSchema *bigquery.FieldSchema,
field protoreflect.FieldDescriptor,
message protoreflect.Message,
) error {
if !bqFieldSchema.Repeated {
return fmt.Errorf("%s: unsupported field schema for list field: not repeated", field.Name())
}
bqList, ok := bqField.([]bigquery.Value)
if !ok {
return fmt.Errorf("%s: unsupported BigQuery value for message: %v", field.Name(), bqField)
}
isMessage := field.Kind() == protoreflect.MessageKind || field.Kind() == protoreflect.GroupKind
switch {
case isMessage && wkt.IsWellKnownType(string(field.Message().FullName())):
return o.unmarshalWellKnownTypeListField(bqList, field, message)
case isMessage:
return o.loadMessageListField(bqList, bqFieldSchema, field, message)
default:
return o.unmarshalScalarListField(bqList, field, message)
}
}
func (o UnmarshalOptions) loadMessageListField(
bqListValue []bigquery.Value,
bqFieldSchema *bigquery.FieldSchema,
field protoreflect.FieldDescriptor,
message protoreflect.Message,
) error {
list := message.Mutable(field).List()
for _, bqElement := range bqListValue {
if bqFieldSchema.Type != bigquery.RecordFieldType {
return fmt.Errorf(
"%s: field schema has type %s but expected %s",
field.Name(),
bqFieldSchema.Type,
bigquery.RecordFieldType,
)
}
bqMessageElement, ok := bqElement.([]bigquery.Value)
if !ok {
return fmt.Errorf(
"%s: unsupported BigQuery value for message: %v", field.Name(), bqMessageElement,
)
}
listElementValue := list.NewElement()
if err := o.loadMessage(bqMessageElement, bqFieldSchema.Schema, listElementValue.Message()); err != nil {
return err
}
list.Append(listElementValue)
}
return nil
}
func (o UnmarshalOptions) loadMapField(
bqField bigquery.Value,
bqFieldSchema *bigquery.FieldSchema,
field protoreflect.FieldDescriptor,
message protoreflect.Message,
) error {
bqMapField, ok := bqField.([]bigquery.Value)
if !ok {
return fmt.Errorf("%s: unsupported BigQuery value for message: %v", field.Name(), bqField)
}
mapValue := field.MapValue()
isMessage := mapValue.Kind() == protoreflect.MessageKind || mapValue.Kind() == protoreflect.GroupKind
switch {
case isMessage && wkt.IsWellKnownType(string(mapValue.Message().FullName())):
return o.unmarshalWellKnownTypeValueMapField(bqMapField, field, message)
case isMessage:
return o.loadMessageValueMapField(bqMapField, bqFieldSchema, field, message)
default:
return o.unmarshalScalarValueMapField(bqMapField, field, message)
}
}
func (o UnmarshalOptions) loadMessageValueMapField(
bqMapField []bigquery.Value,
bqFieldSchema *bigquery.FieldSchema,
field protoreflect.FieldDescriptor,
message protoreflect.Message,
) error {
mapField := message.Mutable(field).Map()
for _, bqMapEntry := range bqMapField {
bqMapEntry, ok := bqMapEntry.(map[string]bigquery.Value)
if !ok {
return fmt.Errorf("%s: unsupported BigQuery value for map entry: %v", field.Name(), bqMapEntry)
}
mapEntryKey, err := o.unmarshalMapEntryKey(bqMapEntry)
if err != nil {
return err
}
bqMapEntryValue, ok := bqMapEntry["value"]
if !ok {
return fmt.Errorf("%s: map entry is missing value field", field.Name())
}
bqMapEntryMessageValue, ok := bqMapEntryValue.([]bigquery.Value)
if !ok {
return fmt.Errorf("%s: unsupported BigQuery value for message: %v", field.Name(), bqMapEntryValue)
}
if len(bqFieldSchema.Schema) != 2 || bqFieldSchema.Schema[1].Name != "value" {
return fmt.Errorf("%s: unsupported BigQuery schema for map entry", field.Name())
}
bqMapEntryValueSchema := bqFieldSchema.Schema[1].Schema
mapEntryValue := mapField.NewValue()
if err := o.loadMessage(bqMapEntryMessageValue, bqMapEntryValueSchema, mapEntryValue.Message()); err != nil {
return err
}
mapField.Set(mapEntryKey, mapEntryValue)
}
return nil
}
func (o UnmarshalOptions) loadSingularField(
bqField bigquery.Value,
bqFieldSchema *bigquery.FieldSchema,
field protoreflect.FieldDescriptor,
message protoreflect.Message,
) (protoreflect.Value, error) {
if bqField == nil {
return protoreflect.ValueOf(nil), nil
}
if field.Kind() == protoreflect.MessageKind || field.Kind() == protoreflect.GroupKind {
if wkt.IsWellKnownType(string(field.Message().FullName())) {
return o.unmarshalWellKnownTypeField(bqField, field)
}
if bqFieldSchema.Type != bigquery.RecordFieldType {
return protoreflect.ValueOf(nil), fmt.Errorf(
"%s: unsupported BigQuery type for message: %v", field.Name(), bqFieldSchema.Type,
)
}
bqMessage, ok := bqField.([]bigquery.Value)
if !ok {
return protoreflect.ValueOf(nil), fmt.Errorf("unsupported BigQuery value for message: %v", bqMessage)
}
fieldValue := message.NewField(field)
if err := o.loadMessage(bqMessage, bqFieldSchema.Schema, fieldValue.Message()); err != nil {
return protoreflect.ValueOf(nil), fmt.Errorf("%s: %w", field.Name(), err)
}
return fieldValue, nil
}
return o.unmarshalScalar(bqField, field)
}