-
Notifications
You must be signed in to change notification settings - Fork 5
/
unmarshal.go
659 lines (611 loc) · 20.6 KB
/
unmarshal.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
package protobq
import (
"fmt"
"time"
"cloud.google.com/go/bigquery"
"cloud.google.com/go/civil"
"go.einride.tech/protobuf-bigquery/internal/wkt"
"google.golang.org/genproto/googleapis/type/date"
"google.golang.org/genproto/googleapis/type/datetime"
"google.golang.org/genproto/googleapis/type/latlng"
"google.golang.org/genproto/googleapis/type/timeofday"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
// Unmarshal the bigquery.Value map into the given proto.Message.
// It will clear the message first before setting the fields. If it returns an error,
// the given message may be partially set.
func Unmarshal(bqMessage map[string]bigquery.Value, message proto.Message) error {
return UnmarshalOptions{}.Unmarshal(bqMessage, message)
}
// UnmarshalOptions is a configurable BigQuery format parser.
type UnmarshalOptions struct {
// Schema contains the schema options.
Schema SchemaOptions
// If AllowPartial is set, input for messages that will result in missing
// required fields will not return an error.
AllowPartial bool
// If DiscardUnknown is set, unknown fields are ignored.
DiscardUnknown bool
}
// Unmarshal reads the given BigQuery row and populates the given proto.Message 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) Unmarshal(bqMessage map[string]bigquery.Value, message proto.Message) error {
proto.Reset(message)
if err := o.unmarshalMessage(bqMessage, message.ProtoReflect()); err != nil {
return err
}
if o.AllowPartial {
return nil
}
return proto.CheckInitialized(message)
}
func (o UnmarshalOptions) unmarshalMessage(
bqMessage map[string]bigquery.Value,
message protoreflect.Message,
) error {
oneofFields := make(map[protoreflect.Name]string)
for bqFieldName, bqField := range bqMessage {
fieldName := protoreflect.Name(bqFieldName)
field := message.Descriptor().Fields().ByName(fieldName)
if field == nil {
if o.Schema.UseOneofFields && o.isOneOfField(fieldName, message) {
stringVal, ok := bqField.(string)
if !ok {
return fmt.Errorf(
"unexpected type %T for oneof field %s, expected string",
bqField,
bqFieldName,
)
}
oneofFields[fieldName] = stringVal
continue
}
if !o.DiscardUnknown && !message.Descriptor().ReservedNames().Has(fieldName) {
return fmt.Errorf("unknown field: %s", fieldName)
}
continue
}
switch {
case field.IsList():
if err := o.unmarshalListField(bqField, field, message); err != nil {
return err
}
case field.IsMap():
if err := o.unmarshalMapField(bqField, field, message); err != nil {
return err
}
default:
value, err := o.unmarshalSingularField(bqField, field, message)
if err != nil {
return err
}
if value.IsValid() {
message.Set(field, value)
}
}
}
if o.Schema.UseOneofFields {
o.ensureOneofsAreSet(oneofFields, message)
}
return nil
}
func (o UnmarshalOptions) isOneOfField(fieldName protoreflect.Name, message protoreflect.Message) bool {
descriptor := message.Descriptor().Oneofs().ByName(fieldName)
return descriptor != nil
}
func (o UnmarshalOptions) ensureOneofsAreSet(
oneofFields map[protoreflect.Name]string,
message protoreflect.Message,
) {
for name, value := range oneofFields {
oneofDesc := message.Descriptor().Oneofs().ByName(name)
field := message.WhichOneof(oneofDesc)
if field != nil {
continue
}
field = message.Descriptor().Fields().ByName(protoreflect.Name(value))
fieldValue := message.NewField(field)
message.Set(field, fieldValue)
}
}
func (o UnmarshalOptions) unmarshalListField(
bqValue bigquery.Value,
field protoreflect.FieldDescriptor,
message protoreflect.Message,
) error {
bqListValue, ok := bqValue.([]bigquery.Value)
if !ok {
return fmt.Errorf("%s: unsupported BigQuery value for message: %v", field.Name(), bqValue)
}
isMessage := field.Kind() == protoreflect.MessageKind || field.Kind() == protoreflect.GroupKind
switch {
case isMessage && wkt.IsWellKnownType(string(field.Message().FullName())):
return o.unmarshalWellKnownTypeListField(bqListValue, field, message)
case isMessage:
return o.unmarshalMessageListField(bqListValue, field, message)
default:
return o.unmarshalScalarListField(bqListValue, field, message)
}
}
func (o UnmarshalOptions) unmarshalWellKnownTypeListField(
bqListValue []bigquery.Value,
field protoreflect.FieldDescriptor,
message protoreflect.Message,
) error {
list := message.Mutable(field).List()
for _, bqListElementValue := range bqListValue {
value, err := o.unmarshalWellKnownTypeField(bqListElementValue, field)
if err != nil {
return err
}
list.Append(value)
}
return nil
}
func (o UnmarshalOptions) unmarshalMessageListField(
bqListValue []bigquery.Value,
field protoreflect.FieldDescriptor,
message protoreflect.Message,
) error {
list := message.Mutable(field).List()
for _, bqListElementValue := range bqListValue {
bqListElementMessageValue, ok := bqListElementValue.(map[string]bigquery.Value)
if !ok {
return fmt.Errorf(
"%s: unsupported BigQuery value for message: %v", field.Name(), bqListElementMessageValue,
)
}
listElementValue := list.NewElement()
if err := o.unmarshalMessage(bqListElementMessageValue, listElementValue.Message()); err != nil {
return err
}
list.Append(listElementValue)
}
return nil
}
func (o UnmarshalOptions) unmarshalScalarListField(
bqListValue []bigquery.Value,
field protoreflect.FieldDescriptor,
message protoreflect.Message,
) error {
list := message.Mutable(field).List()
for _, bqListElementValue := range bqListValue {
value, err := o.unmarshalScalar(bqListElementValue, field)
if err != nil {
return err
}
list.Append(value)
}
return nil
}
func (o UnmarshalOptions) unmarshalMapField(
bqField bigquery.Value,
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.unmarshalMessageValueMapField(bqMapField, field, message)
default:
return o.unmarshalScalarValueMapField(bqMapField, field, message)
}
}
func (o UnmarshalOptions) unmarshalScalarValueMapField(
bqMapField []bigquery.Value,
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())
}
mapEntryValue, err := o.unmarshalScalar(bqMapEntryValue, field.MapValue())
if err != nil {
return err
}
mapField.Set(mapEntryKey, mapEntryValue)
}
return nil
}
func (o UnmarshalOptions) unmarshalWellKnownTypeValueMapField(
bqMapField []bigquery.Value,
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())
}
mapEntryValue, err := o.unmarshalWellKnownTypeField(bqMapEntryValue, field.MapValue())
if err != nil {
return err
}
mapField.Set(mapEntryKey, mapEntryValue)
}
return nil
}
func (o UnmarshalOptions) unmarshalMessageValueMapField(
bqMapField []bigquery.Value,
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.(map[string]bigquery.Value)
if !ok {
return fmt.Errorf("%s: unsupported BigQuery value for message: %v", field.Name(), bqMapEntryValue)
}
mapEntryValue := mapField.NewValue()
if err := o.unmarshalMessage(bqMapEntryMessageValue, mapEntryValue.Message()); err != nil {
return err
}
mapField.Set(mapEntryKey, mapEntryValue)
}
return nil
}
func (o UnmarshalOptions) unmarshalMapEntryKey(
bqMapEntry map[string]bigquery.Value,
) (protoreflect.MapKey, error) {
bqMapEntryKey, ok := bqMapEntry["key"]
if !ok {
return protoreflect.MapKey{}, fmt.Errorf("map entry is missing key field")
}
return protoreflect.ValueOf(bqMapEntryKey).MapKey(), nil
}
func (o UnmarshalOptions) unmarshalSingularField(
bqValue bigquery.Value,
field protoreflect.FieldDescriptor,
message protoreflect.Message,
) (protoreflect.Value, error) {
if bqValue == 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(bqValue, field)
}
bqMessage, ok := bqValue.(map[string]bigquery.Value)
if !ok {
return protoreflect.ValueOf(nil), fmt.Errorf(
"%s: unsupported BigQuery value for message: %v", field.Name(), bqMessage,
)
}
fieldValue := message.NewField(field)
if err := o.unmarshalMessage(bqMessage, fieldValue.Message()); err != nil {
return protoreflect.ValueOf(nil), fmt.Errorf("%s: %w", field.Name(), err)
}
return fieldValue, nil
}
return o.unmarshalScalar(bqValue, field)
}
func (o UnmarshalOptions) unmarshalWellKnownTypeField(
bqValue bigquery.Value,
field protoreflect.FieldDescriptor,
) (protoreflect.Value, error) {
var result proto.Message
var err error
switch field.Message().FullName() {
case wkt.Timestamp:
result, err = o.unmarshalTimestamp(bqValue)
case wkt.Duration:
result, err = o.unmarshalDuration(bqValue)
case wkt.TimeOfDay:
result, err = o.unmarshalTimeOfDay(bqValue)
case wkt.Date:
result, err = o.unmarshalDate(bqValue)
case wkt.DateTime:
result, err = o.unmarshalDateTime(bqValue)
case wkt.LatLng:
result, err = o.unmarshalLatLng(bqValue)
case wkt.Struct:
result, err = o.unmarshalStruct(bqValue)
case wkt.DoubleValue:
result, err = o.unmarshalDoubleValue(bqValue)
case wkt.FloatValue:
result, err = o.unmarshalFloatValue(bqValue)
case wkt.Int32Value:
result, err = o.unmarshalInt32Value(bqValue)
case wkt.Int64Value:
result, err = o.unmarshalInt64Value(bqValue)
case wkt.UInt32Value:
result, err = o.unmarshalUInt32Value(bqValue)
case wkt.UInt64Value:
result, err = o.unmarshalUInt64Value(bqValue)
case wkt.BoolValue:
result, err = o.unmarshalBoolValue(bqValue)
case wkt.StringValue:
result, err = o.unmarshalStringValue(bqValue)
case wkt.BytesValue:
result, err = o.unmarshalBytesValue(bqValue)
default:
result, err = nil, fmt.Errorf("unsupported well-known-type: %s", field.Message().FullName())
}
if err != nil {
return protoreflect.ValueOf(nil), err
}
return protoreflect.ValueOf(result.ProtoReflect()), nil
}
func (o UnmarshalOptions) unmarshalTimestamp(bqValue bigquery.Value) (*timestamppb.Timestamp, error) {
t, ok := bqValue.(time.Time)
if !ok {
return nil, fmt.Errorf("unsupported BigQuery value for %s: %v", wkt.Timestamp, bqValue)
}
return timestamppb.New(t), nil
}
func (o UnmarshalOptions) unmarshalDuration(bqValue bigquery.Value) (*durationpb.Duration, error) {
var duration time.Duration
switch bigqueryValue := bqValue.(type) {
case int64:
duration = time.Duration(bigqueryValue) * time.Second
case float64:
duration = time.Duration(bigqueryValue * float64(time.Second))
default:
return nil, fmt.Errorf("unsupported BigQuery value for %s: %#v", wkt.Duration, bigqueryValue)
}
return durationpb.New(duration), nil
}
func (o UnmarshalOptions) unmarshalTimeOfDay(bqValue bigquery.Value) (*timeofday.TimeOfDay, error) {
t, ok := bqValue.(civil.Time)
if !ok {
return nil, fmt.Errorf("unsupported BigQuery value for %s: %#v", wkt.TimeOfDay, bqValue)
}
return &timeofday.TimeOfDay{
Hours: int32(t.Hour),
Minutes: int32(t.Minute),
Seconds: int32(t.Second),
Nanos: int32(t.Nanosecond),
}, nil
}
func (o UnmarshalOptions) unmarshalDate(bqValue bigquery.Value) (*date.Date, error) {
d, ok := bqValue.(civil.Date)
if !ok {
return nil, fmt.Errorf("unsupported BigQuery value for %s: %#v", wkt.Date, bqValue)
}
return &date.Date{
Year: int32(d.Year),
Month: int32(d.Month),
Day: int32(d.Day),
}, nil
}
func (o UnmarshalOptions) unmarshalDateTime(bqValue bigquery.Value) (*datetime.DateTime, error) {
if o.Schema.UseDateTimeWithoutOffset {
d, ok := bqValue.(civil.DateTime)
if !ok {
return nil, fmt.Errorf("unsupported BigQuery value for %s (without offset): %#v", wkt.DateTime, bqValue)
}
return &datetime.DateTime{
Year: int32(d.Date.Year),
Month: int32(d.Date.Month),
Day: int32(d.Date.Day),
Hours: int32(d.Time.Hour),
Minutes: int32(d.Time.Minute),
Seconds: int32(d.Time.Second),
Nanos: int32(d.Time.Nanosecond),
}, nil
}
return nil, fmt.Errorf("TODO: implement support for %s with offset", wkt.DateTime)
}
func (o UnmarshalOptions) unmarshalLatLng(bqValue bigquery.Value) (*latlng.LatLng, error) {
s, ok := bqValue.(string)
if !ok {
return nil, fmt.Errorf("unsupported BigQuery value for %s: %#v", wkt.LatLng, bqValue)
}
latLng := &latlng.LatLng{}
if _, err := fmt.Sscanf(s, "POINT(%f %f)", &latLng.Longitude, &latLng.Latitude); err != nil {
return nil, fmt.Errorf("invalid GEOGRAPHY value for %s: %#v: %w", wkt.LatLng, bqValue, err)
}
return latLng, nil
}
func (o UnmarshalOptions) unmarshalStruct(bqValue bigquery.Value) (*structpb.Struct, error) {
s, ok := bqValue.(string)
if !ok {
return nil, fmt.Errorf("unsupported BigQuery value for %s: %#v", wkt.Struct, bqValue)
}
var structValue structpb.Struct
// Linter issue https://github.com/ghostiam/protogetter/issues/8
//nolint:protogetter
if err := structValue.UnmarshalJSON([]byte(s)); err != nil {
return nil, fmt.Errorf("invalid BigQuery value for %s: %#v: %w", wkt.Struct, bqValue, err)
}
return &structValue, nil
}
func (o UnmarshalOptions) unmarshalDoubleValue(bqValue bigquery.Value) (*wrapperspb.DoubleValue, error) {
switch bqValue := bqValue.(type) {
case float32:
return wrapperspb.Double(float64(bqValue)), nil
case float64:
return wrapperspb.Double(bqValue), nil
default:
return nil, fmt.Errorf("invalid BigQuery value for %s: %#v", wkt.DoubleValue, bqValue)
}
}
func (o UnmarshalOptions) unmarshalFloatValue(bqValue bigquery.Value) (*wrapperspb.FloatValue, error) {
switch bqValue := bqValue.(type) {
case float32:
return wrapperspb.Float(bqValue), nil
case float64:
return wrapperspb.Float(float32(bqValue)), nil
default:
return nil, fmt.Errorf("invalid BigQuery value for %s: %#v", wkt.FloatValue, bqValue)
}
}
func (o UnmarshalOptions) unmarshalInt32Value(bqValue bigquery.Value) (*wrapperspb.Int32Value, error) {
switch bqValue := bqValue.(type) {
case int32:
return wrapperspb.Int32(bqValue), nil
case int64:
return wrapperspb.Int32(int32(bqValue)), nil
default:
return nil, fmt.Errorf("invalid BigQuery value for %s: %#v", wkt.Int32Value, bqValue)
}
}
func (o UnmarshalOptions) unmarshalInt64Value(bqValue bigquery.Value) (*wrapperspb.Int64Value, error) {
switch bqValue := bqValue.(type) {
case int32:
return wrapperspb.Int64(int64(bqValue)), nil
case int64:
return wrapperspb.Int64(bqValue), nil
default:
return nil, fmt.Errorf("invalid BigQuery value for %s: %#v", wkt.Int64Value, bqValue)
}
}
func (o UnmarshalOptions) unmarshalUInt32Value(bqValue bigquery.Value) (*wrapperspb.UInt32Value, error) {
switch bqValue := bqValue.(type) {
case uint32:
return wrapperspb.UInt32(bqValue), nil
case uint64:
return wrapperspb.UInt32(uint32(bqValue)), nil
default:
return nil, fmt.Errorf("invalid BigQuery value for %s: %#v", wkt.UInt32Value, bqValue)
}
}
func (o UnmarshalOptions) unmarshalUInt64Value(bqValue bigquery.Value) (*wrapperspb.UInt64Value, error) {
switch bqValue := bqValue.(type) {
case uint32:
return wrapperspb.UInt64(uint64(bqValue)), nil
case uint64:
return wrapperspb.UInt64(bqValue), nil
default:
return nil, fmt.Errorf("invalid BigQuery value for %s: %#v", wkt.UInt64Value, bqValue)
}
}
func (o UnmarshalOptions) unmarshalBoolValue(bqValue bigquery.Value) (*wrapperspb.BoolValue, error) {
if bqValue, ok := bqValue.(bool); ok {
return wrapperspb.Bool(bqValue), nil
}
return nil, fmt.Errorf("invalid BigQuery value for %s: %#v", wkt.BoolValue, bqValue)
}
func (o UnmarshalOptions) unmarshalStringValue(bqValue bigquery.Value) (*wrapperspb.StringValue, error) {
if bqValue, ok := bqValue.(string); ok {
return wrapperspb.String(bqValue), nil
}
return nil, fmt.Errorf("invalid BigQuery value for %s: %#v", wkt.StringValue, bqValue)
}
func (o UnmarshalOptions) unmarshalBytesValue(bqValue bigquery.Value) (*wrapperspb.BytesValue, error) {
if bqValue, ok := bqValue.([]byte); ok {
return wrapperspb.Bytes(bqValue), nil
}
return nil, fmt.Errorf("invalid BigQuery value for %s: %#v", wkt.BytesValue, bqValue)
}
func (o UnmarshalOptions) unmarshalScalar(
bqValue bigquery.Value,
field protoreflect.FieldDescriptor,
) (protoreflect.Value, error) {
switch field.Kind() {
case protoreflect.BoolKind:
if b, ok := bqValue.(bool); ok {
return protoreflect.ValueOfBool(b), nil
}
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
if n, ok := bqValue.(int64); ok {
return protoreflect.ValueOfInt32(int32(n)), nil
}
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
if n, ok := bqValue.(int64); ok {
return protoreflect.ValueOfInt64(n), nil
}
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
if n, ok := bqValue.(int64); ok {
return protoreflect.ValueOfUint32(uint32(n)), nil
}
case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
if n, ok := bqValue.(int64); ok {
return protoreflect.ValueOfUint64(uint64(n)), nil
}
case protoreflect.FloatKind:
if n, ok := bqValue.(float64); ok {
return protoreflect.ValueOfFloat32(float32(n)), nil
}
case protoreflect.DoubleKind:
if n, ok := bqValue.(float64); ok {
return protoreflect.ValueOfFloat64(n), nil
}
case protoreflect.StringKind:
if s, ok := bqValue.(string); ok {
return protoreflect.ValueOfString(s), nil
}
case protoreflect.BytesKind:
if b, ok := bqValue.([]byte); ok {
return protoreflect.ValueOfBytes(b), nil
}
case protoreflect.EnumKind:
return o.unmarshalEnumScalar(bqValue, field)
case protoreflect.MessageKind, protoreflect.GroupKind:
// Fall through to return error, these should have been handled by the caller.
}
return protoreflect.Value{}, fmt.Errorf("invalid BigQuery value %#v for kind %v", bqValue, field.Kind())
}
func (o UnmarshalOptions) unmarshalEnumScalar(
bqValue bigquery.Value,
field protoreflect.FieldDescriptor,
) (protoreflect.Value, error) {
if o.Schema.UseEnumNumbers {
v, ok := bqValue.(int64)
if !ok {
return protoreflect.Value{}, fmt.Errorf(
"invalid BigQuery value %#v for enum %s number", bqValue, field.Enum().FullName(),
)
}
return protoreflect.ValueOfEnum(protoreflect.EnumNumber(int32(v))), nil
}
v, ok := bqValue.(string)
if !ok {
return protoreflect.Value{}, fmt.Errorf(
"invalid BigQuery value %#v for enum %s", bqValue, field.Enum().FullName(),
)
}
enumVal := field.Enum().Values().ByName(protoreflect.Name(v))
if enumVal == nil {
return protoreflect.Value{}, fmt.Errorf(
"unknown enum value %#v for enum %s", bqValue, field.Enum().FullName(),
)
}
return protoreflect.ValueOfEnum(enumVal.Number()), nil
}