-
Notifications
You must be signed in to change notification settings - Fork 3
/
value.go
1320 lines (1245 loc) · 33.2 KB
/
value.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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 Zhizhesihai (Beijing) Technology Limited.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package zetta
import (
"fmt"
"math"
"reflect"
"strings"
"time"
"cloud.google.com/go/civil"
"github.com/golang/protobuf/proto"
"github.com/zhihu/zetta-client-go/internal/fields"
tspb "github.com/zhihu/zetta-proto/pkg/tablestore"
"google.golang.org/grpc/codes"
)
//
// 新增的多种 NULL 类型
//
// NullInt64 represents a Cloud Spanner INT64 that may be NULL.
type NullInt64 struct {
Int64 int64
Valid bool // Valid is true if Int64 is not NULL.
}
// String implements Stringer.String for NullInt64
func (n NullInt64) String() string {
if !n.Valid {
return fmt.Sprintf("%v", "<null>")
}
return fmt.Sprintf("%v", n.Int64)
}
// NullString represents a Cloud Spanner STRING that may be NULL.
type NullString struct {
StringVal string
Valid bool // Valid is true if StringVal is not NULL.
}
// String implements Stringer.String for NullString
func (n NullString) String() string {
if !n.Valid {
return fmt.Sprintf("%v", "<null>")
}
return fmt.Sprintf("%q", n.StringVal)
}
// NullFloat64 represents a Cloud Spanner FLOAT64 that may be NULL.
type NullFloat64 struct {
Float64 float64
Valid bool // Valid is true if Float64 is not NULL.
}
// String implements Stringer.String for NullFloat64
func (n NullFloat64) String() string {
if !n.Valid {
return fmt.Sprintf("%v", "<null>")
}
return fmt.Sprintf("%v", n.Float64)
}
// NullBool represents a Cloud Spanner BOOL that may be NULL.
type NullBool struct {
Bool bool
Valid bool // Valid is true if Bool is not NULL.
}
// String implements Stringer.String for NullBool
func (n NullBool) String() string {
if !n.Valid {
return fmt.Sprintf("%v", "<null>")
}
return fmt.Sprintf("%v", n.Bool)
}
// NullTime represents a Cloud Spanner TIMESTAMP that may be null.
type NullTime struct {
Time time.Time
Valid bool // Valid is true if Time is not NULL.
}
// String implements Stringer.String for NullTime
func (n NullTime) String() string {
if !n.Valid {
return fmt.Sprintf("%s", "<null>")
}
return fmt.Sprintf("%q", n.Time.Format(time.RFC3339Nano))
}
// NullDate represents a Cloud Spanner DATE that may be null.
type NullDate struct {
Date civil.Date
Valid bool // Valid is true if Date is not NULL.
}
// String implements Stringer.String for NullDate
func (n NullDate) String() string {
if !n.Valid {
return fmt.Sprintf("%s", "<null>")
}
return fmt.Sprintf("%q", n.Date)
}
// NullRow represents a Cloud Spanner STRUCT that may be NULL.
// See also the document for Row.
// Note that NullRow is not a valid Cloud Spanner column Type.
type NullRow struct {
Row Row
Valid bool // Valid is true if Row is not NULL.
}
// 通用的字段类型和字段值,用于查询结果类型不可知的查询
// GenericColumnValue represents the generic encoded value and type of the
// column. See google.spanner.v1.ResultSet proto for details. This can be
// useful for proxying query results when the result types are not known in
// advance.
type GenericColumnValue struct {
Type *tspb.Type
Value *tspb.Value
}
// 从通用 cv 中解码到 ptr 中
// Decode decodes a GenericColumnValue. The ptr argument should be a pointer
// to a Go value that can accept v.
func (v GenericColumnValue) Decode(ptr interface{}) error {
return decodeValue(v.Value, v.Type, ptr)
}
// NewGenericColumnValue creates a GenericColumnValue from Go value that is
// valid for Cloud Spanner.
func NewGenericColumnValue(v interface{}) (*GenericColumnValue, error) {
value, typ, err := encodeValue(v)
if err != nil {
return nil, err
}
return &GenericColumnValue{Value: value, Type: typ}, nil
}
// errTypeMismatch returns error for destination not having a compatible type
// with source Cloud Spanner type.
func errTypeMismatch(srcType tspb.TypeCode, isArray bool, dst interface{}) error {
usage := srcType.String()
if isArray {
usage = fmt.Sprintf("%v[%v]", tspb.TypeCode_ARRAY, srcType)
}
return wrapError(codes.InvalidArgument, "type %T cannot be used for decoding %v", dst, usage)
}
// errNilSpannerType returns error for nil Cloud Spanner type in decoding.
func errNilSpannerType() error {
return wrapError(codes.FailedPrecondition, "unexpected nil Cloud Spanner data type in decoding")
}
// errNilSrc returns error for decoding from nil proto value.
func errNilSrc() error {
return wrapError(codes.FailedPrecondition, "unexpected nil Cloud Spanner value in decoding")
}
// errNilDst returns error for decoding into nil interface{}.
func errNilDst(dst interface{}) error {
return wrapError(codes.InvalidArgument, "cannot decode into nil type %T", dst)
}
// errNilArrElemType returns error for input Cloud Spanner data type being a array but without a
// non-nil array element type.
func errNilArrElemType(t *tspb.Type) error {
return wrapError(codes.FailedPrecondition, "array type %v is with nil array element type", t)
}
// errDstNotForNull returns error for decoding a SQL NULL value into a destination which doesn't
// support NULL values.
func errDstNotForNull(dst interface{}) error {
return wrapError(codes.InvalidArgument, "destination %T cannot support NULL SQL values", dst)
}
// errBadEncoding returns error for decoding wrongly encoded BYTES/INT64.
func errBadEncoding(v *tspb.Value, err error) error {
return wrapError(codes.FailedPrecondition, "%v wasn't correctly encoded: <%v>", v, err)
}
func parseNullTime(v *tspb.Value, p *NullTime, code tspb.TypeCode, isNull bool) error {
if p == nil {
return errNilDst(p)
}
if code != tspb.TypeCode_TIMESTAMP {
return errTypeMismatch(code, false, p)
}
if isNull {
*p = NullTime{}
return nil
}
x, err := getStringValue(v)
if err != nil {
return err
}
y, err := time.Parse(time.RFC3339Nano, x)
if err != nil {
return errBadEncoding(v, err)
}
p.Valid = true
p.Time = y
return nil
}
//
// 将 protobuf 值编码到 Go tspb.Type 类型的指针中
//
// decodeValue decodes a protobuf Value into a pointer to a Go value, as specified by tspb.Type.
func decodeValue(v *tspb.Value, t *tspb.Type, ptr interface{}) error {
if v == nil {
return errNilSrc()
}
if t == nil {
return errNilSpannerType()
}
code := t.Code
if t.Code == tspb.TypeCode_TYPE_CODE_UNSPECIFIED {
return decodeSparseValue(v, t, ptr)
}
acode := tspb.TypeCode_TYPE_CODE_UNSPECIFIED
if code == tspb.TypeCode_ARRAY {
if t.ArrayElementType == nil {
return errNilArrElemType(t)
}
acode = t.ArrayElementType.Code
}
typeErr := errTypeMismatch(code, false, ptr)
if code == tspb.TypeCode_ARRAY {
typeErr = errTypeMismatch(acode, true, ptr)
}
nullErr := errDstNotForNull(ptr)
_, isNull := v.Kind.(*tspb.Value_NullValue)
// Do the decoding based on the type of ptr.
switch p := ptr.(type) {
case nil:
return errNilDst(nil)
case *string:
if p == nil {
return errNilDst(p)
}
if code != tspb.TypeCode_STRING {
return typeErr
}
if isNull {
return nullErr
}
x, err := getStringValue(v)
if err != nil {
return err
}
*p = x
case *NullString:
if p == nil {
return errNilDst(p)
}
if code != tspb.TypeCode_STRING {
return typeErr
}
if isNull {
*p = NullString{}
break
}
x, err := getStringValue(v)
if err != nil {
return err
}
p.Valid = true
p.StringVal = x
case *[]NullString:
if p == nil {
return errNilDst(p)
}
if acode != tspb.TypeCode_STRING {
return typeErr
}
if isNull {
*p = nil
break
}
x, err := getListValue(v)
if err != nil {
return err
}
y, err := decodeStringArray(x)
if err != nil {
return err
}
*p = y
case *[]byte:
if p == nil {
return errNilDst(p)
}
if code != tspb.TypeCode_BYTES {
return typeErr
}
if isNull {
*p = nil
break
}
x, err := getBytesValue(v)
if err != nil {
return err
}
*p = x
case *[][]byte:
if p == nil {
return errNilDst(p)
}
if acode != tspb.TypeCode_BYTES {
return typeErr
}
if isNull {
*p = nil
break
}
x, err := getListValue(v)
if err != nil {
return err
}
y, err := decodeByteArray(x)
if err != nil {
return err
}
*p = y
case *int64:
if p == nil {
return errNilDst(p)
}
if code != tspb.TypeCode_INT64 {
return typeErr
}
if isNull {
return nullErr
}
x, err := getInteger64Value(v)
if err != nil {
return err
}
*p = x
case *NullInt64:
if p == nil {
return errNilDst(p)
}
if code != tspb.TypeCode_INT64 {
return typeErr
}
if isNull {
*p = NullInt64{}
break
}
x, err := getInteger64Value(v)
if err != nil {
return err
}
p.Valid = true
p.Int64 = x
case *[]NullInt64:
if p == nil {
return errNilDst(p)
}
if acode != tspb.TypeCode_INT64 {
return typeErr
}
if isNull {
*p = nil
break
}
x, err := getListValue(v)
if err != nil {
return err
}
y, err := decodeIntArray(x)
if err != nil {
return err
}
*p = y
case *bool:
if p == nil {
return errNilDst(p)
}
if code != tspb.TypeCode_BOOL {
return typeErr
}
if isNull {
return nullErr
}
x, err := getBoolValue(v)
if err != nil {
return err
}
*p = x
case *NullBool:
if p == nil {
return errNilDst(p)
}
if code != tspb.TypeCode_BOOL {
return typeErr
}
if isNull {
*p = NullBool{}
break
}
x, err := getBoolValue(v)
if err != nil {
return err
}
p.Valid = true
p.Bool = x
case *[]NullBool:
if p == nil {
return errNilDst(p)
}
if acode != tspb.TypeCode_BOOL {
return typeErr
}
if isNull {
*p = nil
break
}
x, err := getListValue(v)
if err != nil {
return err
}
y, err := decodeBoolArray(x)
if err != nil {
return err
}
*p = y
case *float64:
if p == nil {
return errNilDst(p)
}
if code != tspb.TypeCode_FLOAT64 {
return typeErr
}
if isNull {
return nullErr
}
x, err := getFloat64Value(v)
if err != nil {
return err
}
*p = x
case *NullFloat64:
if p == nil {
return errNilDst(p)
}
if code != tspb.TypeCode_FLOAT64 {
return typeErr
}
if isNull {
*p = NullFloat64{}
break
}
x, err := getFloat64Value(v)
if err != nil {
return err
}
p.Valid = true
p.Float64 = x
case *[]NullFloat64:
if p == nil {
return errNilDst(p)
}
if acode != tspb.TypeCode_FLOAT64 {
return typeErr
}
if isNull {
*p = nil
break
}
x, err := getListValue(v)
if err != nil {
return err
}
y, err := decodeFloat64Array(x)
if err != nil {
return err
}
*p = y
case *time.Time:
var nt NullTime
if isNull {
return nullErr
}
err := parseNullTime(v, &nt, code, isNull)
if err != nil {
return nil
}
*p = nt.Time
case *NullTime:
err := parseNullTime(v, p, code, isNull)
if err != nil {
return err
}
case *[]NullTime:
if p == nil {
return errNilDst(p)
}
if acode != tspb.TypeCode_TIMESTAMP {
return typeErr
}
if isNull {
*p = nil
break
}
x, err := getListValue(v)
if err != nil {
return err
}
y, err := decodeTimeArray(x)
if err != nil {
return err
}
*p = y
case *civil.Date:
if p == nil {
return errNilDst(p)
}
if code != tspb.TypeCode_DATE {
return typeErr
}
if isNull {
return nullErr
}
x, err := getStringValue(v)
if err != nil {
return err
}
y, err := civil.ParseDate(x)
if err != nil {
return errBadEncoding(v, err)
}
*p = y
case *NullDate:
if p == nil {
return errNilDst(p)
}
if code != tspb.TypeCode_DATE {
return typeErr
}
if isNull {
*p = NullDate{}
break
}
x, err := getStringValue(v)
if err != nil {
return err
}
y, err := civil.ParseDate(x)
if err != nil {
return errBadEncoding(v, err)
}
p.Valid = true
p.Date = y
case *[]NullDate:
if p == nil {
return errNilDst(p)
}
if acode != tspb.TypeCode_DATE {
return typeErr
}
if isNull {
*p = nil
break
}
x, err := getListValue(v)
if err != nil {
return err
}
y, err := decodeDateArray(x)
if err != nil {
return err
}
*p = y
case *[]NullRow:
if p == nil {
return errNilDst(p)
}
if acode != tspb.TypeCode_STRUCT {
return typeErr
}
if isNull {
*p = nil
break
}
x, err := getListValue(v)
if err != nil {
return err
}
y, err := decodeRowArray(t.ArrayElementType.StructType, x)
if err != nil {
return err
}
*p = y
case *GenericColumnValue:
*p = GenericColumnValue{
// Deep clone to ensure subsequent changes to t or v
// don't affect our decoded value.
Type: proto.Clone(t).(*tspb.Type),
Value: proto.Clone(v).(*tspb.Value),
}
default:
// Check if the proto encoding is for an array of structs.
if !(code == tspb.TypeCode_ARRAY && acode == tspb.TypeCode_STRUCT) {
return typeErr
}
vp := reflect.ValueOf(p)
if !vp.IsValid() {
return errNilDst(p)
}
if !isPtrStructPtrSlice(vp.Type()) {
// The container is not a pointer to a struct pointer slice.
return typeErr
}
// Only use reflection for nil detection on slow path.
// Also, IsNil panics on many types, so check it after the type check.
if vp.IsNil() {
return errNilDst(p)
}
if isNull {
// The proto Value is encoding NULL, set the pointer to struct
// slice to nil as well.
vp.Elem().Set(reflect.Zero(vp.Elem().Type()))
break
}
x, err := getListValue(v)
if err != nil {
return err
}
if err = decodeStructArray(t.ArrayElementType.StructType, x, p); err != nil {
return err
}
}
return nil
}
// errSrvVal returns an error for getting a wrong source protobuf value in decoding.
func errSrcVal(v *tspb.Value, want string) error {
return wrapError(codes.FailedPrecondition, "cannot use %v(Kind: %T) as Value_%sValue in decoding",
v, v.GetKind(), want)
}
// getStringValue returns the string value encoded in tspb.Value v whose
// kind is tspb.Value_StringValue.
func getStringValue(v *tspb.Value) (string, error) {
if x, ok := v.GetKind().(*tspb.Value_StringValue); ok && x != nil {
return x.StringValue, nil
}
return "", errSrcVal(v, "String")
}
// getBoolValue returns the bool value encoded in tspb.Value v whose
// kind is tspb.Value_BoolValue.
func getBoolValue(v *tspb.Value) (bool, error) {
if x, ok := v.GetKind().(*tspb.Value_BoolValue); ok && x != nil {
return x.BoolValue, nil
}
return false, errSrcVal(v, "Bool")
}
// getListValue returns the tspb.ListValue contained in tspb.Value v whose
// kind is tspb.Value_ListValue.
func getListValue(v *tspb.Value) (*tspb.ListValue, error) {
if x, ok := v.GetKind().(*tspb.Value_ListValue); ok && x != nil {
return x.ListValue, nil
}
return nil, errSrcVal(v, "List")
}
// errUnexpectedNumStr returns error for decoder getting a unexpected string for
// representing special float values.
func errUnexpectedNumStr(s string) error {
return wrapError(codes.FailedPrecondition, "unexpected string value %q for number", s)
}
// getFloat64Value returns the float64 value encoded in tspb.Value v whose
// kind is tspb.Value_NumberValue / tspb.Value_StringValue.
// Cloud Spanner uses string to encode NaN, Infinity and -Infinity.
func getFloat64Value(v *tspb.Value) (float64, error) {
switch x := v.GetKind().(type) {
case *tspb.Value_NumberValue:
if x == nil {
break
}
return x.NumberValue, nil
case *tspb.Value_StringValue:
if x == nil {
break
}
switch x.StringValue {
case "NaN":
return math.NaN(), nil
case "Infinity":
return math.Inf(1), nil
case "-Infinity":
return math.Inf(-1), nil
default:
return 0, errUnexpectedNumStr(x.StringValue)
}
}
return 0, errSrcVal(v, "Number")
}
// getInteger64Value returns the int64 value encoded in tspb.Value v whose
// kind is tspb.Value_IntegerValue
func getInteger64Value(v *tspb.Value) (int64, error) {
if x, ok := v.GetKind().(*tspb.Value_IntegerValue); ok && x != nil {
return x.IntegerValue, nil
}
return 0, errSrcVal(v, "Integer")
}
// getTimestampValue returns the timestamp value encoded in tspb.Value v whose
// kind is tspb.Value_TimestampValue
func getTimestampValue(v *tspb.Value) (time.Time, error) {
if x, ok := v.GetKind().(*tspb.Value_TimestampValue); ok && x != nil {
tsv := x.TimestampValue
return time.Unix(tsv.Seconds, int64(tsv.Nanos)), nil
}
return time.Time{}, errSrcVal(v, "Timestamp")
}
// getDateValue returns the date value encoded in tspb.Value v whose
// kind is tspb.Value_TimestampValue
func getDateValue(v *tspb.Value) (civil.Date, error) {
if x, ok := v.GetKind().(*tspb.Value_TimestampValue); ok && x != nil {
tsv := x.TimestampValue
date := civil.DateOf(time.Unix(tsv.Seconds, int64(tsv.Nanos)))
return date, nil
}
return civil.Date{}, errSrcVal(v, "Date")
}
// getStringValue returns the string value encoded in tspb.Value v whose
// kind is tspb.Value_StringValue.
func getBytesValue(v *tspb.Value) ([]byte, error) {
if x, ok := v.GetKind().(*tspb.Value_BytesValue); ok && x != nil {
return x.BytesValue, nil
}
return nil, errSrcVal(v, "Bytes")
}
// errNilListValue returns error for unexpected nil ListValue in decoding Cloud Spanner ARRAYs.
func errNilListValue(sqlType string) error {
return wrapError(codes.FailedPrecondition, "unexpected nil ListValue in decoding %v array", sqlType)
}
// errDecodeArrayElement returns error for failure in decoding single array element.
func errDecodeArrayElement(i int, v proto.Message, sqlType string, err error) error {
se, ok := err.(*Error)
if !ok {
return wrapError(codes.Unknown,
"cannot decode %v(array element %v) as %v, error = <%v>", v, i, sqlType, err)
}
se.decorate(fmt.Sprintf("cannot decode %v(array element %v) as %v", v, i, sqlType))
return se
}
// decodeStringArray decodes tspb.ListValue pb into a NullString slice.
func decodeStringArray(pb *tspb.ListValue) ([]NullString, error) {
if pb == nil {
return nil, errNilListValue("STRING")
}
a := make([]NullString, len(pb.Values))
for i, v := range pb.Values {
if err := decodeValue(v, stringType(), &a[i]); err != nil {
return nil, errDecodeArrayElement(i, v, "STRING", err)
}
}
return a, nil
}
// decodeIntArray decodes tspb.ListValue pb into a NullInt64 slice.
func decodeIntArray(pb *tspb.ListValue) ([]NullInt64, error) {
if pb == nil {
return nil, errNilListValue("INT64")
}
a := make([]NullInt64, len(pb.Values))
for i, v := range pb.Values {
if err := decodeValue(v, intType(), &a[i]); err != nil {
return nil, errDecodeArrayElement(i, v, "INT64", err)
}
}
return a, nil
}
// decodeBoolArray decodes tspb.ListValue pb into a NullBool slice.
func decodeBoolArray(pb *tspb.ListValue) ([]NullBool, error) {
if pb == nil {
return nil, errNilListValue("BOOL")
}
a := make([]NullBool, len(pb.Values))
for i, v := range pb.Values {
if err := decodeValue(v, boolType(), &a[i]); err != nil {
return nil, errDecodeArrayElement(i, v, "BOOL", err)
}
}
return a, nil
}
// decodeFloat64Array decodes tspb.ListValue pb into a NullFloat64 slice.
func decodeFloat64Array(pb *tspb.ListValue) ([]NullFloat64, error) {
if pb == nil {
return nil, errNilListValue("FLOAT64")
}
a := make([]NullFloat64, len(pb.Values))
for i, v := range pb.Values {
if err := decodeValue(v, floatType(), &a[i]); err != nil {
return nil, errDecodeArrayElement(i, v, "FLOAT64", err)
}
}
return a, nil
}
// decodeByteArray decodes tspb.ListValue pb into a slice of byte slice.
func decodeByteArray(pb *tspb.ListValue) ([][]byte, error) {
if pb == nil {
return nil, errNilListValue("BYTES")
}
a := make([][]byte, len(pb.Values))
for i, v := range pb.Values {
if err := decodeValue(v, bytesType(), &a[i]); err != nil {
return nil, errDecodeArrayElement(i, v, "BYTES", err)
}
}
return a, nil
}
// decodeTimeArray decodes tspb.ListValue pb into a NullTime slice.
func decodeTimeArray(pb *tspb.ListValue) ([]NullTime, error) {
if pb == nil {
return nil, errNilListValue("TIMESTAMP")
}
a := make([]NullTime, len(pb.Values))
for i, v := range pb.Values {
if err := decodeValue(v, timeType(), &a[i]); err != nil {
return nil, errDecodeArrayElement(i, v, "TIMESTAMP", err)
}
}
return a, nil
}
// decodeDateArray decodes tspb.ListValue pb into a NullDate slice.
func decodeDateArray(pb *tspb.ListValue) ([]NullDate, error) {
if pb == nil {
return nil, errNilListValue("DATE")
}
a := make([]NullDate, len(pb.Values))
for i, v := range pb.Values {
if err := decodeValue(v, dateType(), &a[i]); err != nil {
return nil, errDecodeArrayElement(i, v, "DATE", err)
}
}
return a, nil
}
func errNotStructElement(i int, v *tspb.Value) error {
return errDecodeArrayElement(i, v, "STRUCT",
wrapError(codes.FailedPrecondition, "%v(type: %T) doesn't encode Cloud Spanner STRUCT", v, v))
}
// decodeRowArray decodes tspb.ListValue pb into a NullRow slice according to
// the structual information given in tspb.StructType ty.
func decodeRowArray(ty *tspb.StructType, pb *tspb.ListValue) ([]NullRow, error) {
if pb == nil {
return nil, errNilListValue("STRUCT")
}
a := make([]NullRow, len(pb.Values))
for i := range pb.Values {
switch v := pb.Values[i].GetKind().(type) {
case *tspb.Value_ListValue:
a[i] = NullRow{
Row: Row{
fields: ty.Fields,
vals: v.ListValue.Values,
},
Valid: true,
}
// Null elements not currently supported by the server, see
// https://cloud.google.com/spanner/docs/query-syntax#using-structs-with-select
case *tspb.Value_NullValue:
// no-op, a[i] is NullRow{} already
default:
return nil, errNotStructElement(i, pb.Values[i])
}
}
return a, nil
}
// structFieldColumn returns the name of i-th field of struct type typ if the field
// is untagged; otherwise, it returns the tagged name of the field.
func structFieldColumn(typ reflect.Type, i int) (col string, ok bool) {
desc := typ.Field(i)
if desc.PkgPath != "" || desc.Anonymous {
// Skip unexported or anonymous fields.
return "", false
}
col = desc.Name
if tag := desc.Tag.Get("spanner"); tag != "" {
if tag == "-" {
// Skip fields tagged "-" to match encoding/json and others.
return "", false
}
col = tag
if idx := strings.Index(tag, ","); idx != -1 {
col = tag[:idx]
}
}
return col, true
}
// errNilSpannerStructType returns error for unexpected nil Cloud Spanner STRUCT schema type in decoding.
func errNilSpannerStructType() error {
return wrapError(codes.FailedPrecondition, "unexpected nil StructType in decoding Cloud Spanner STRUCT")
}
// errUnnamedField returns error for decoding a Cloud Spanner STRUCT with unnamed field into a Go struct.
func errUnnamedField(ty *tspb.StructType, i int) error {
return wrapError(codes.InvalidArgument, "unnamed field %v in Cloud Spanner STRUCT %+v", i, ty)
}
func errUnnamedCellField(cell *tspb.Cell, i int) error {
return wrapError(codes.InvalidArgument, "unnamed field %v in Zetta Cell %+v", i, cell)
}
// errNoOrDupGoField returns error for decoding a Cloud Spanner
// STRUCT into a Go struct which is either missing a field, or has duplicate fields.
func errNoOrDupGoField(s interface{}, f string) error {
return wrapError(codes.InvalidArgument, "Go struct %+v(type %T) has no or duplicate fields for Cloud Spanner STRUCT field %v", s, s, f)
}
// errDupColNames returns error for duplicated Cloud Spanner STRUCT field names found in decoding a Cloud Spanner STRUCT into a Go struct.
func errDupSpannerField(f string, ty *tspb.StructType) error {
return wrapError(codes.InvalidArgument, "duplicated field name %q in Cloud Spanner STRUCT %+v", f, ty)
}
func errDupCellField(f string, ty *tspb.Cell) error {
return wrapError(codes.InvalidArgument, "duplicated field name %q in Zetta Cell %+v", f, ty)
}
// errDecodeStructField returns error for failure in decoding a single field of a Cloud Spanner STRUCT.
func errDecodeStructField(ty *tspb.StructType, f string, err error) error {
se, ok := err.(*Error)
if !ok {
return wrapError(codes.Unknown,
"cannot decode field %v of Cloud Spanner STRUCT %+v, error = <%v>", f, ty, err)
}
se.decorate(fmt.Sprintf("cannot decode field %v of Cloud Spanner STRUCT %+v", f, ty))
return se
}
func errDecodeCellField(ty *tspb.Cell, f string, err error) error {
se, ok := err.(*Error)
if !ok {
return wrapError(codes.Unknown,
"cannot decode field %v of Zetta Cell %+v, error = <%v>", f, ty, err)
}
se.decorate(fmt.Sprintf("cannot decode field %v of Zetta Cell %+v", f, ty))
return se
}
// decodeStruct decodes tspb.ListValue pb into struct referenced by pointer ptr, according to
// the structual information given in tspb.StructType ty.
func decodeStruct(ty *tspb.StructType, pb *tspb.ListValue, ptr interface{}) error {
if reflect.ValueOf(ptr).IsNil() {
return errNilDst(ptr)
}
if ty == nil {
return errNilSpannerStructType()
}
// t holds the structual information of ptr.
t := reflect.TypeOf(ptr).Elem()
// v is the actual value that ptr points to.
v := reflect.ValueOf(ptr).Elem()
fields, err := fieldCache.Fields(t)
if err != nil {
return err
}
seen := map[string]bool{}
for i, f := range ty.Fields {