1/*
2Copyright 2017 Google LLC
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package spanner
18
19import (
20	"bytes"
21	"encoding/base64"
22	"fmt"
23	"math"
24	"math/big"
25	"reflect"
26	"strconv"
27	"time"
28
29	"cloud.google.com/go/civil"
30	"cloud.google.com/go/internal/fields"
31	"github.com/golang/protobuf/proto"
32	proto3 "github.com/golang/protobuf/ptypes/struct"
33	sppb "google.golang.org/genproto/googleapis/spanner/v1"
34	"google.golang.org/grpc/codes"
35)
36
37const (
38	// nullString is returned by the String methods of NullableValues when the
39	// underlying database value is null.
40	nullString                       = "<null>"
41	commitTimestampPlaceholderString = "spanner.commit_timestamp()"
42
43	// NumericPrecisionDigits is the maximum number of digits in a NUMERIC
44	// value.
45	NumericPrecisionDigits = 38
46
47	// NumericScaleDigits is the maximum number of digits after the decimal
48	// point in a NUMERIC value.
49	NumericScaleDigits = 9
50)
51
52// NumericString returns a string representing a *big.Rat in a format compatible
53// with Spanner SQL. It returns a floating-point literal with 9 digits after the
54// decimal point.
55func NumericString(r *big.Rat) string {
56	return r.FloatString(NumericScaleDigits)
57}
58
59var (
60	// CommitTimestamp is a special value used to tell Cloud Spanner to insert
61	// the commit timestamp of the transaction into a column. It can be used in
62	// a Mutation, or directly used in InsertStruct or InsertMap. See
63	// ExampleCommitTimestamp. This is just a placeholder and the actual value
64	// stored in this variable has no meaning.
65	CommitTimestamp = commitTimestamp
66	commitTimestamp = time.Unix(0, 0).In(time.FixedZone("CommitTimestamp placeholder", 0xDB))
67
68	jsonNullBytes = []byte("null")
69)
70
71// Encoder is the interface implemented by a custom type that can be encoded to
72// a supported type by Spanner. A code example:
73//
74// type customField struct {
75//     Prefix string
76//     Suffix string
77// }
78//
79// // Convert a customField value to a string
80// func (cf customField) EncodeSpanner() (interface{}, error) {
81//     var b bytes.Buffer
82//     b.WriteString(cf.Prefix)
83//     b.WriteString("-")
84//     b.WriteString(cf.Suffix)
85//     return b.String(), nil
86// }
87type Encoder interface {
88	EncodeSpanner() (interface{}, error)
89}
90
91// Decoder is the interface implemented by a custom type that can be decoded
92// from a supported type by Spanner. A code example:
93//
94// type customField struct {
95//     Prefix string
96//     Suffix string
97// }
98//
99// // Convert a string to a customField value
100// func (cf *customField) DecodeSpanner(val interface{}) (err error) {
101//     strVal, ok := val.(string)
102//     if !ok {
103//         return fmt.Errorf("failed to decode customField: %v", val)
104//     }
105//     s := strings.Split(strVal, "-")
106//     if len(s) > 1 {
107//         cf.Prefix = s[0]
108//         cf.Suffix = s[1]
109//     }
110//     return nil
111// }
112type Decoder interface {
113	DecodeSpanner(input interface{}) error
114}
115
116// NullableValue is the interface implemented by all null value wrapper types.
117type NullableValue interface {
118	// IsNull returns true if the underlying database value is null.
119	IsNull() bool
120}
121
122// NullInt64 represents a Cloud Spanner INT64 that may be NULL.
123type NullInt64 struct {
124	Int64 int64 // Int64 contains the value when it is non-NULL, and zero when NULL.
125	Valid bool  // Valid is true if Int64 is not NULL.
126}
127
128// IsNull implements NullableValue.IsNull for NullInt64.
129func (n NullInt64) IsNull() bool {
130	return !n.Valid
131}
132
133// String implements Stringer.String for NullInt64
134func (n NullInt64) String() string {
135	if !n.Valid {
136		return nullString
137	}
138	return fmt.Sprintf("%v", n.Int64)
139}
140
141// MarshalJSON implements json.Marshaler.MarshalJSON for NullInt64.
142func (n NullInt64) MarshalJSON() ([]byte, error) {
143	if n.Valid {
144		return []byte(fmt.Sprintf("%v", n.Int64)), nil
145	}
146	return jsonNullBytes, nil
147}
148
149// UnmarshalJSON implements json.Unmarshaler.UnmarshalJSON for NullInt64.
150func (n *NullInt64) UnmarshalJSON(payload []byte) error {
151	if payload == nil {
152		return fmt.Errorf("payload should not be nil")
153	}
154	if bytes.Equal(payload, jsonNullBytes) {
155		n.Int64 = int64(0)
156		n.Valid = false
157		return nil
158	}
159	num, err := strconv.ParseInt(string(payload), 10, 64)
160	if err != nil {
161		return fmt.Errorf("payload cannot be converted to int64: got %v", string(payload))
162	}
163	n.Int64 = num
164	n.Valid = true
165	return nil
166}
167
168// NullString represents a Cloud Spanner STRING that may be NULL.
169type NullString struct {
170	StringVal string // StringVal contains the value when it is non-NULL, and an empty string when NULL.
171	Valid     bool   // Valid is true if StringVal is not NULL.
172}
173
174// IsNull implements NullableValue.IsNull for NullString.
175func (n NullString) IsNull() bool {
176	return !n.Valid
177}
178
179// String implements Stringer.String for NullString
180func (n NullString) String() string {
181	if !n.Valid {
182		return nullString
183	}
184	return n.StringVal
185}
186
187// MarshalJSON implements json.Marshaler.MarshalJSON for NullString.
188func (n NullString) MarshalJSON() ([]byte, error) {
189	if n.Valid {
190		return []byte(fmt.Sprintf("%q", n.StringVal)), nil
191	}
192	return jsonNullBytes, nil
193}
194
195// UnmarshalJSON implements json.Unmarshaler.UnmarshalJSON for NullString.
196func (n *NullString) UnmarshalJSON(payload []byte) error {
197	if payload == nil {
198		return fmt.Errorf("payload should not be nil")
199	}
200	if bytes.Equal(payload, jsonNullBytes) {
201		n.StringVal = ""
202		n.Valid = false
203		return nil
204	}
205	payload, err := trimDoubleQuotes(payload)
206	if err != nil {
207		return err
208	}
209	n.StringVal = string(payload)
210	n.Valid = true
211	return nil
212}
213
214// NullFloat64 represents a Cloud Spanner FLOAT64 that may be NULL.
215type NullFloat64 struct {
216	Float64 float64 // Float64 contains the value when it is non-NULL, and zero when NULL.
217	Valid   bool    // Valid is true if Float64 is not NULL.
218}
219
220// IsNull implements NullableValue.IsNull for NullFloat64.
221func (n NullFloat64) IsNull() bool {
222	return !n.Valid
223}
224
225// String implements Stringer.String for NullFloat64
226func (n NullFloat64) String() string {
227	if !n.Valid {
228		return nullString
229	}
230	return fmt.Sprintf("%v", n.Float64)
231}
232
233// MarshalJSON implements json.Marshaler.MarshalJSON for NullFloat64.
234func (n NullFloat64) MarshalJSON() ([]byte, error) {
235	if n.Valid {
236		return []byte(fmt.Sprintf("%v", n.Float64)), nil
237	}
238	return jsonNullBytes, nil
239}
240
241// UnmarshalJSON implements json.Unmarshaler.UnmarshalJSON for NullFloat64.
242func (n *NullFloat64) UnmarshalJSON(payload []byte) error {
243	if payload == nil {
244		return fmt.Errorf("payload should not be nil")
245	}
246	if bytes.Equal(payload, jsonNullBytes) {
247		n.Float64 = float64(0)
248		n.Valid = false
249		return nil
250	}
251	num, err := strconv.ParseFloat(string(payload), 64)
252	if err != nil {
253		return fmt.Errorf("payload cannot be converted to float64: got %v", string(payload))
254	}
255	n.Float64 = num
256	n.Valid = true
257	return nil
258}
259
260// NullBool represents a Cloud Spanner BOOL that may be NULL.
261type NullBool struct {
262	Bool  bool // Bool contains the value when it is non-NULL, and false when NULL.
263	Valid bool // Valid is true if Bool is not NULL.
264}
265
266// IsNull implements NullableValue.IsNull for NullBool.
267func (n NullBool) IsNull() bool {
268	return !n.Valid
269}
270
271// String implements Stringer.String for NullBool
272func (n NullBool) String() string {
273	if !n.Valid {
274		return nullString
275	}
276	return fmt.Sprintf("%v", n.Bool)
277}
278
279// MarshalJSON implements json.Marshaler.MarshalJSON for NullBool.
280func (n NullBool) MarshalJSON() ([]byte, error) {
281	if n.Valid {
282		return []byte(fmt.Sprintf("%v", n.Bool)), nil
283	}
284	return jsonNullBytes, nil
285}
286
287// UnmarshalJSON implements json.Unmarshaler.UnmarshalJSON for NullBool.
288func (n *NullBool) UnmarshalJSON(payload []byte) error {
289	if payload == nil {
290		return fmt.Errorf("payload should not be nil")
291	}
292	if bytes.Equal(payload, jsonNullBytes) {
293		n.Bool = false
294		n.Valid = false
295		return nil
296	}
297	b, err := strconv.ParseBool(string(payload))
298	if err != nil {
299		return fmt.Errorf("payload cannot be converted to bool: got %v", string(payload))
300	}
301	n.Bool = b
302	n.Valid = true
303	return nil
304}
305
306// NullTime represents a Cloud Spanner TIMESTAMP that may be null.
307type NullTime struct {
308	Time  time.Time // Time contains the value when it is non-NULL, and a zero time.Time when NULL.
309	Valid bool      // Valid is true if Time is not NULL.
310}
311
312// IsNull implements NullableValue.IsNull for NullTime.
313func (n NullTime) IsNull() bool {
314	return !n.Valid
315}
316
317// String implements Stringer.String for NullTime
318func (n NullTime) String() string {
319	if !n.Valid {
320		return nullString
321	}
322	return n.Time.Format(time.RFC3339Nano)
323}
324
325// MarshalJSON implements json.Marshaler.MarshalJSON for NullTime.
326func (n NullTime) MarshalJSON() ([]byte, error) {
327	if n.Valid {
328		return []byte(fmt.Sprintf("%q", n.String())), nil
329	}
330	return jsonNullBytes, nil
331}
332
333// UnmarshalJSON implements json.Unmarshaler.UnmarshalJSON for NullTime.
334func (n *NullTime) UnmarshalJSON(payload []byte) error {
335	if payload == nil {
336		return fmt.Errorf("payload should not be nil")
337	}
338	if bytes.Equal(payload, jsonNullBytes) {
339		n.Time = time.Time{}
340		n.Valid = false
341		return nil
342	}
343	payload, err := trimDoubleQuotes(payload)
344	if err != nil {
345		return err
346	}
347	s := string(payload)
348	t, err := time.Parse(time.RFC3339Nano, s)
349	if err != nil {
350		return fmt.Errorf("payload cannot be converted to time.Time: got %v", string(payload))
351	}
352	n.Time = t
353	n.Valid = true
354	return nil
355}
356
357// NullDate represents a Cloud Spanner DATE that may be null.
358type NullDate struct {
359	Date  civil.Date // Date contains the value when it is non-NULL, and a zero civil.Date when NULL.
360	Valid bool       // Valid is true if Date is not NULL.
361}
362
363// IsNull implements NullableValue.IsNull for NullDate.
364func (n NullDate) IsNull() bool {
365	return !n.Valid
366}
367
368// String implements Stringer.String for NullDate
369func (n NullDate) String() string {
370	if !n.Valid {
371		return nullString
372	}
373	return n.Date.String()
374}
375
376// MarshalJSON implements json.Marshaler.MarshalJSON for NullDate.
377func (n NullDate) MarshalJSON() ([]byte, error) {
378	if n.Valid {
379		return []byte(fmt.Sprintf("%q", n.String())), nil
380	}
381	return jsonNullBytes, nil
382}
383
384// UnmarshalJSON implements json.Unmarshaler.UnmarshalJSON for NullDate.
385func (n *NullDate) UnmarshalJSON(payload []byte) error {
386	if payload == nil {
387		return fmt.Errorf("payload should not be nil")
388	}
389	if bytes.Equal(payload, jsonNullBytes) {
390		n.Date = civil.Date{}
391		n.Valid = false
392		return nil
393	}
394	payload, err := trimDoubleQuotes(payload)
395	if err != nil {
396		return err
397	}
398	s := string(payload)
399	t, err := civil.ParseDate(s)
400	if err != nil {
401		return fmt.Errorf("payload cannot be converted to civil.Date: got %v", string(payload))
402	}
403	n.Date = t
404	n.Valid = true
405	return nil
406}
407
408// NullNumeric represents a Cloud Spanner Numeric that may be NULL.
409type NullNumeric struct {
410	Numeric big.Rat // Numeric contains the value when it is non-NULL, and a zero big.Rat when NULL.
411	Valid   bool    // Valid is true if Numeric is not NULL.
412}
413
414// IsNull implements NullableValue.IsNull for NullNumeric.
415func (n NullNumeric) IsNull() bool {
416	return !n.Valid
417}
418
419// String implements Stringer.String for NullNumeric
420func (n NullNumeric) String() string {
421	if !n.Valid {
422		return nullString
423	}
424	return fmt.Sprintf("%v", NumericString(&n.Numeric))
425}
426
427// MarshalJSON implements json.Marshaler.MarshalJSON for NullNumeric.
428func (n NullNumeric) MarshalJSON() ([]byte, error) {
429	if n.Valid {
430		return []byte(fmt.Sprintf("%q", NumericString(&n.Numeric))), nil
431	}
432	return jsonNullBytes, nil
433}
434
435// UnmarshalJSON implements json.Unmarshaler.UnmarshalJSON for NullNumeric.
436func (n *NullNumeric) UnmarshalJSON(payload []byte) error {
437	if payload == nil {
438		return fmt.Errorf("payload should not be nil")
439	}
440	if bytes.Equal(payload, jsonNullBytes) {
441		n.Numeric = big.Rat{}
442		n.Valid = false
443		return nil
444	}
445	payload, err := trimDoubleQuotes(payload)
446	if err != nil {
447		return err
448	}
449	s := string(payload)
450	val, ok := (&big.Rat{}).SetString(s)
451	if !ok {
452		return fmt.Errorf("payload cannot be converted to big.Rat: got %v", string(payload))
453	}
454	n.Numeric = *val
455	n.Valid = true
456	return nil
457}
458
459// NullRow represents a Cloud Spanner STRUCT that may be NULL.
460// See also the document for Row.
461// Note that NullRow is not a valid Cloud Spanner column Type.
462type NullRow struct {
463	Row   Row  // Row contains the value when it is non-NULL, and a zero Row when NULL.
464	Valid bool // Valid is true if Row is not NULL.
465}
466
467// GenericColumnValue represents the generic encoded value and type of the
468// column.  See google.spanner.v1.ResultSet proto for details.  This can be
469// useful for proxying query results when the result types are not known in
470// advance.
471//
472// If you populate a GenericColumnValue from a row using Row.Column or related
473// methods, do not modify the contents of Type and Value.
474type GenericColumnValue struct {
475	Type  *sppb.Type
476	Value *proto3.Value
477}
478
479// Decode decodes a GenericColumnValue. The ptr argument should be a pointer
480// to a Go value that can accept v.
481func (v GenericColumnValue) Decode(ptr interface{}) error {
482	return decodeValue(v.Value, v.Type, ptr)
483}
484
485// NewGenericColumnValue creates a GenericColumnValue from Go value that is
486// valid for Cloud Spanner.
487func newGenericColumnValue(v interface{}) (*GenericColumnValue, error) {
488	value, typ, err := encodeValue(v)
489	if err != nil {
490		return nil, err
491	}
492	return &GenericColumnValue{Value: value, Type: typ}, nil
493}
494
495// errTypeMismatch returns error for destination not having a compatible type
496// with source Cloud Spanner type.
497func errTypeMismatch(srcCode, elCode sppb.TypeCode, dst interface{}) error {
498	s := srcCode.String()
499	if srcCode == sppb.TypeCode_ARRAY {
500		s = fmt.Sprintf("%v[%v]", srcCode, elCode)
501	}
502	return spannerErrorf(codes.InvalidArgument, "type %T cannot be used for decoding %s", dst, s)
503}
504
505// errNilSpannerType returns error for nil Cloud Spanner type in decoding.
506func errNilSpannerType() error {
507	return spannerErrorf(codes.FailedPrecondition, "unexpected nil Cloud Spanner data type in decoding")
508}
509
510// errNilSrc returns error for decoding from nil proto value.
511func errNilSrc() error {
512	return spannerErrorf(codes.FailedPrecondition, "unexpected nil Cloud Spanner value in decoding")
513}
514
515// errNilDst returns error for decoding into nil interface{}.
516func errNilDst(dst interface{}) error {
517	return spannerErrorf(codes.InvalidArgument, "cannot decode into nil type %T", dst)
518}
519
520// errNilArrElemType returns error for input Cloud Spanner data type being a array but without a
521// non-nil array element type.
522func errNilArrElemType(t *sppb.Type) error {
523	return spannerErrorf(codes.FailedPrecondition, "array type %v is with nil array element type", t)
524}
525
526func errUnsupportedEmbeddedStructFields(fname string) error {
527	return spannerErrorf(codes.InvalidArgument, "Embedded field: %s. Embedded and anonymous fields are not allowed "+
528		"when converting Go structs to Cloud Spanner STRUCT values. To create a STRUCT value with an "+
529		"unnamed field, use a `spanner:\"\"` field tag.", fname)
530}
531
532// errDstNotForNull returns error for decoding a SQL NULL value into a destination which doesn't
533// support NULL values.
534func errDstNotForNull(dst interface{}) error {
535	return spannerErrorf(codes.InvalidArgument, "destination %T cannot support NULL SQL values", dst)
536}
537
538// errBadEncoding returns error for decoding wrongly encoded types.
539func errBadEncoding(v *proto3.Value, err error) error {
540	return spannerErrorf(codes.FailedPrecondition, "%v wasn't correctly encoded: <%v>", v, err)
541}
542
543func parseNullTime(v *proto3.Value, p *NullTime, code sppb.TypeCode, isNull bool) error {
544	if p == nil {
545		return errNilDst(p)
546	}
547	if code != sppb.TypeCode_TIMESTAMP {
548		return errTypeMismatch(code, sppb.TypeCode_TYPE_CODE_UNSPECIFIED, p)
549	}
550	if isNull {
551		*p = NullTime{}
552		return nil
553	}
554	x, err := getStringValue(v)
555	if err != nil {
556		return err
557	}
558	y, err := time.Parse(time.RFC3339Nano, x)
559	if err != nil {
560		return errBadEncoding(v, err)
561	}
562	p.Valid = true
563	p.Time = y
564	return nil
565}
566
567// decodeValue decodes a protobuf Value into a pointer to a Go value, as
568// specified by sppb.Type.
569func decodeValue(v *proto3.Value, t *sppb.Type, ptr interface{}) error {
570	if v == nil {
571		return errNilSrc()
572	}
573	if t == nil {
574		return errNilSpannerType()
575	}
576	code := t.Code
577	acode := sppb.TypeCode_TYPE_CODE_UNSPECIFIED
578	if code == sppb.TypeCode_ARRAY {
579		if t.ArrayElementType == nil {
580			return errNilArrElemType(t)
581		}
582		acode = t.ArrayElementType.Code
583	}
584	_, isNull := v.Kind.(*proto3.Value_NullValue)
585
586	// Do the decoding based on the type of ptr.
587	switch p := ptr.(type) {
588	case nil:
589		return errNilDst(nil)
590	case *string:
591		if p == nil {
592			return errNilDst(p)
593		}
594		if code != sppb.TypeCode_STRING {
595			return errTypeMismatch(code, acode, ptr)
596		}
597		if isNull {
598			return errDstNotForNull(ptr)
599		}
600		x, err := getStringValue(v)
601		if err != nil {
602			return err
603		}
604		*p = x
605	case *NullString, **string:
606		if p == nil {
607			return errNilDst(p)
608		}
609		if code != sppb.TypeCode_STRING {
610			return errTypeMismatch(code, acode, ptr)
611		}
612		if isNull {
613			switch sp := ptr.(type) {
614			case *NullString:
615				*sp = NullString{}
616			case **string:
617				*sp = nil
618			}
619			break
620		}
621		x, err := getStringValue(v)
622		if err != nil {
623			return err
624		}
625		switch sp := ptr.(type) {
626		case *NullString:
627			sp.Valid = true
628			sp.StringVal = x
629		case **string:
630			*sp = &x
631		}
632	case *[]NullString, *[]*string:
633		if p == nil {
634			return errNilDst(p)
635		}
636		if acode != sppb.TypeCode_STRING {
637			return errTypeMismatch(code, acode, ptr)
638		}
639		if isNull {
640			switch sp := ptr.(type) {
641			case *[]NullString:
642				*sp = nil
643			case *[]*string:
644				*sp = nil
645			}
646			break
647		}
648		x, err := getListValue(v)
649		if err != nil {
650			return err
651		}
652		switch sp := ptr.(type) {
653		case *[]NullString:
654			y, err := decodeNullStringArray(x)
655			if err != nil {
656				return err
657			}
658			*sp = y
659		case *[]*string:
660			y, err := decodeStringPointerArray(x)
661			if err != nil {
662				return err
663			}
664			*sp = y
665		}
666	case *[]string:
667		if p == nil {
668			return errNilDst(p)
669		}
670		if acode != sppb.TypeCode_STRING {
671			return errTypeMismatch(code, acode, ptr)
672		}
673		if isNull {
674			*p = nil
675			break
676		}
677		x, err := getListValue(v)
678		if err != nil {
679			return err
680		}
681		y, err := decodeStringArray(x)
682		if err != nil {
683			return err
684		}
685		*p = y
686	case *[]byte:
687		if p == nil {
688			return errNilDst(p)
689		}
690		if code != sppb.TypeCode_BYTES {
691			return errTypeMismatch(code, acode, ptr)
692		}
693		if isNull {
694			*p = nil
695			break
696		}
697		x, err := getStringValue(v)
698		if err != nil {
699			return err
700		}
701		y, err := base64.StdEncoding.DecodeString(x)
702		if err != nil {
703			return errBadEncoding(v, err)
704		}
705		*p = y
706	case *[][]byte:
707		if p == nil {
708			return errNilDst(p)
709		}
710		if acode != sppb.TypeCode_BYTES {
711			return errTypeMismatch(code, acode, ptr)
712		}
713		if isNull {
714			*p = nil
715			break
716		}
717		x, err := getListValue(v)
718		if err != nil {
719			return err
720		}
721		y, err := decodeByteArray(x)
722		if err != nil {
723			return err
724		}
725		*p = y
726	case *int64:
727		if p == nil {
728			return errNilDst(p)
729		}
730		if code != sppb.TypeCode_INT64 {
731			return errTypeMismatch(code, acode, ptr)
732		}
733		if isNull {
734			return errDstNotForNull(ptr)
735		}
736		x, err := getStringValue(v)
737		if err != nil {
738			return err
739		}
740		y, err := strconv.ParseInt(x, 10, 64)
741		if err != nil {
742			return errBadEncoding(v, err)
743		}
744		*p = y
745	case *NullInt64, **int64:
746		if p == nil {
747			return errNilDst(p)
748		}
749		if code != sppb.TypeCode_INT64 {
750			return errTypeMismatch(code, acode, ptr)
751		}
752		if isNull {
753			switch sp := ptr.(type) {
754			case *NullInt64:
755				*sp = NullInt64{}
756			case **int64:
757				*sp = nil
758			}
759			break
760		}
761		x, err := getStringValue(v)
762		if err != nil {
763			return err
764		}
765		y, err := strconv.ParseInt(x, 10, 64)
766		if err != nil {
767			return errBadEncoding(v, err)
768		}
769		switch sp := ptr.(type) {
770		case *NullInt64:
771			sp.Valid = true
772			sp.Int64 = y
773		case **int64:
774			*sp = &y
775		}
776	case *[]NullInt64, *[]*int64:
777		if p == nil {
778			return errNilDst(p)
779		}
780		if acode != sppb.TypeCode_INT64 {
781			return errTypeMismatch(code, acode, ptr)
782		}
783		if isNull {
784			switch sp := ptr.(type) {
785			case *[]NullInt64:
786				*sp = nil
787			case *[]*int64:
788				*sp = nil
789			}
790			break
791		}
792		x, err := getListValue(v)
793		if err != nil {
794			return err
795		}
796		switch sp := ptr.(type) {
797		case *[]NullInt64:
798			y, err := decodeNullInt64Array(x)
799			if err != nil {
800				return err
801			}
802			*sp = y
803		case *[]*int64:
804			y, err := decodeInt64PointerArray(x)
805			if err != nil {
806				return err
807			}
808			*sp = y
809		}
810	case *[]int64:
811		if p == nil {
812			return errNilDst(p)
813		}
814		if acode != sppb.TypeCode_INT64 {
815			return errTypeMismatch(code, acode, ptr)
816		}
817		if isNull {
818			*p = nil
819			break
820		}
821		x, err := getListValue(v)
822		if err != nil {
823			return err
824		}
825		y, err := decodeInt64Array(x)
826		if err != nil {
827			return err
828		}
829		*p = y
830	case *bool:
831		if p == nil {
832			return errNilDst(p)
833		}
834		if code != sppb.TypeCode_BOOL {
835			return errTypeMismatch(code, acode, ptr)
836		}
837		if isNull {
838			return errDstNotForNull(ptr)
839		}
840		x, err := getBoolValue(v)
841		if err != nil {
842			return err
843		}
844		*p = x
845	case *NullBool, **bool:
846		if p == nil {
847			return errNilDst(p)
848		}
849		if code != sppb.TypeCode_BOOL {
850			return errTypeMismatch(code, acode, ptr)
851		}
852		if isNull {
853			switch sp := ptr.(type) {
854			case *NullBool:
855				*sp = NullBool{}
856			case **bool:
857				*sp = nil
858			}
859			break
860		}
861		x, err := getBoolValue(v)
862		if err != nil {
863			return err
864		}
865		switch sp := ptr.(type) {
866		case *NullBool:
867			sp.Valid = true
868			sp.Bool = x
869		case **bool:
870			*sp = &x
871		}
872	case *[]NullBool, *[]*bool:
873		if p == nil {
874			return errNilDst(p)
875		}
876		if acode != sppb.TypeCode_BOOL {
877			return errTypeMismatch(code, acode, ptr)
878		}
879		if isNull {
880			switch sp := ptr.(type) {
881			case *[]NullBool:
882				*sp = nil
883			case *[]*bool:
884				*sp = nil
885			}
886			break
887		}
888		x, err := getListValue(v)
889		if err != nil {
890			return err
891		}
892		switch sp := ptr.(type) {
893		case *[]NullBool:
894			y, err := decodeNullBoolArray(x)
895			if err != nil {
896				return err
897			}
898			*sp = y
899		case *[]*bool:
900			y, err := decodeBoolPointerArray(x)
901			if err != nil {
902				return err
903			}
904			*sp = y
905		}
906	case *[]bool:
907		if p == nil {
908			return errNilDst(p)
909		}
910		if acode != sppb.TypeCode_BOOL {
911			return errTypeMismatch(code, acode, ptr)
912		}
913		if isNull {
914			*p = nil
915			break
916		}
917		x, err := getListValue(v)
918		if err != nil {
919			return err
920		}
921		y, err := decodeBoolArray(x)
922		if err != nil {
923			return err
924		}
925		*p = y
926	case *float64:
927		if p == nil {
928			return errNilDst(p)
929		}
930		if code != sppb.TypeCode_FLOAT64 {
931			return errTypeMismatch(code, acode, ptr)
932		}
933		if isNull {
934			return errDstNotForNull(ptr)
935		}
936		x, err := getFloat64Value(v)
937		if err != nil {
938			return err
939		}
940		*p = x
941	case *NullFloat64, **float64:
942		if p == nil {
943			return errNilDst(p)
944		}
945		if code != sppb.TypeCode_FLOAT64 {
946			return errTypeMismatch(code, acode, ptr)
947		}
948		if isNull {
949			switch sp := ptr.(type) {
950			case *NullFloat64:
951				*sp = NullFloat64{}
952			case **float64:
953				*sp = nil
954			}
955			break
956		}
957		x, err := getFloat64Value(v)
958		if err != nil {
959			return err
960		}
961		switch sp := ptr.(type) {
962		case *NullFloat64:
963			sp.Valid = true
964			sp.Float64 = x
965		case **float64:
966			*sp = &x
967		}
968	case *[]NullFloat64, *[]*float64:
969		if p == nil {
970			return errNilDst(p)
971		}
972		if acode != sppb.TypeCode_FLOAT64 {
973			return errTypeMismatch(code, acode, ptr)
974		}
975		if isNull {
976			switch sp := ptr.(type) {
977			case *[]NullFloat64:
978				*sp = nil
979			case *[]*float64:
980				*sp = nil
981			}
982			break
983		}
984		x, err := getListValue(v)
985		if err != nil {
986			return err
987		}
988		switch sp := ptr.(type) {
989		case *[]NullFloat64:
990			y, err := decodeNullFloat64Array(x)
991			if err != nil {
992				return err
993			}
994			*sp = y
995		case *[]*float64:
996			y, err := decodeFloat64PointerArray(x)
997			if err != nil {
998				return err
999			}
1000			*sp = y
1001		}
1002	case *[]float64:
1003		if p == nil {
1004			return errNilDst(p)
1005		}
1006		if acode != sppb.TypeCode_FLOAT64 {
1007			return errTypeMismatch(code, acode, ptr)
1008		}
1009		if isNull {
1010			*p = nil
1011			break
1012		}
1013		x, err := getListValue(v)
1014		if err != nil {
1015			return err
1016		}
1017		y, err := decodeFloat64Array(x)
1018		if err != nil {
1019			return err
1020		}
1021		*p = y
1022	case *big.Rat:
1023		if code != sppb.TypeCode_NUMERIC {
1024			return errTypeMismatch(code, acode, ptr)
1025		}
1026		if isNull {
1027			return errDstNotForNull(ptr)
1028		}
1029		x := v.GetStringValue()
1030		y, ok := (&big.Rat{}).SetString(x)
1031		if !ok {
1032			return errUnexpectedNumericStr(x)
1033		}
1034		*p = *y
1035	case *NullNumeric:
1036		if p == nil {
1037			return errNilDst(p)
1038		}
1039		if code != sppb.TypeCode_NUMERIC {
1040			return errTypeMismatch(code, acode, ptr)
1041		}
1042		if isNull {
1043			*p = NullNumeric{}
1044			break
1045		}
1046		x := v.GetStringValue()
1047		y, ok := (&big.Rat{}).SetString(x)
1048		if !ok {
1049			return errUnexpectedNumericStr(x)
1050		}
1051		*p = NullNumeric{*y, true}
1052	case **big.Rat:
1053		if p == nil {
1054			return errNilDst(p)
1055		}
1056		if code != sppb.TypeCode_NUMERIC {
1057			return errTypeMismatch(code, acode, ptr)
1058		}
1059		if isNull {
1060			*p = nil
1061			break
1062		}
1063		x := v.GetStringValue()
1064		y, ok := (&big.Rat{}).SetString(x)
1065		if !ok {
1066			return errUnexpectedNumericStr(x)
1067		}
1068		*p = y
1069	case *[]NullNumeric, *[]*big.Rat:
1070		if p == nil {
1071			return errNilDst(p)
1072		}
1073		if acode != sppb.TypeCode_NUMERIC {
1074			return errTypeMismatch(code, acode, ptr)
1075		}
1076		if isNull {
1077			switch sp := ptr.(type) {
1078			case *[]NullNumeric:
1079				*sp = nil
1080			case *[]*big.Rat:
1081				*sp = nil
1082			}
1083			break
1084		}
1085		x, err := getListValue(v)
1086		if err != nil {
1087			return err
1088		}
1089		switch sp := ptr.(type) {
1090		case *[]NullNumeric:
1091			y, err := decodeNullNumericArray(x)
1092			if err != nil {
1093				return err
1094			}
1095			*sp = y
1096		case *[]*big.Rat:
1097			y, err := decodeNumericPointerArray(x)
1098			if err != nil {
1099				return err
1100			}
1101			*sp = y
1102		}
1103	case *[]big.Rat:
1104		if p == nil {
1105			return errNilDst(p)
1106		}
1107		if acode != sppb.TypeCode_NUMERIC {
1108			return errTypeMismatch(code, acode, ptr)
1109		}
1110		if isNull {
1111			*p = nil
1112			break
1113		}
1114		x, err := getListValue(v)
1115		if err != nil {
1116			return err
1117		}
1118		y, err := decodeNumericArray(x)
1119		if err != nil {
1120			return err
1121		}
1122		*p = y
1123	case *time.Time:
1124		var nt NullTime
1125		if isNull {
1126			return errDstNotForNull(ptr)
1127		}
1128		err := parseNullTime(v, &nt, code, isNull)
1129		if err != nil {
1130			return err
1131		}
1132		*p = nt.Time
1133	case *NullTime:
1134		err := parseNullTime(v, p, code, isNull)
1135		if err != nil {
1136			return err
1137		}
1138	case **time.Time:
1139		var nt NullTime
1140		if isNull {
1141			*p = nil
1142			break
1143		}
1144		err := parseNullTime(v, &nt, code, isNull)
1145		if err != nil {
1146			return err
1147		}
1148		*p = &nt.Time
1149	case *[]NullTime, *[]*time.Time:
1150		if p == nil {
1151			return errNilDst(p)
1152		}
1153		if acode != sppb.TypeCode_TIMESTAMP {
1154			return errTypeMismatch(code, acode, ptr)
1155		}
1156		if isNull {
1157			switch sp := ptr.(type) {
1158			case *[]NullTime:
1159				*sp = nil
1160			case *[]*time.Time:
1161				*sp = nil
1162			}
1163			break
1164		}
1165		x, err := getListValue(v)
1166		if err != nil {
1167			return err
1168		}
1169		switch sp := ptr.(type) {
1170		case *[]NullTime:
1171			y, err := decodeNullTimeArray(x)
1172			if err != nil {
1173				return err
1174			}
1175			*sp = y
1176		case *[]*time.Time:
1177			y, err := decodeTimePointerArray(x)
1178			if err != nil {
1179				return err
1180			}
1181			*sp = y
1182		}
1183	case *[]time.Time:
1184		if p == nil {
1185			return errNilDst(p)
1186		}
1187		if acode != sppb.TypeCode_TIMESTAMP {
1188			return errTypeMismatch(code, acode, ptr)
1189		}
1190		if isNull {
1191			*p = nil
1192			break
1193		}
1194		x, err := getListValue(v)
1195		if err != nil {
1196			return err
1197		}
1198		y, err := decodeTimeArray(x)
1199		if err != nil {
1200			return err
1201		}
1202		*p = y
1203	case *civil.Date:
1204		if p == nil {
1205			return errNilDst(p)
1206		}
1207		if code != sppb.TypeCode_DATE {
1208			return errTypeMismatch(code, acode, ptr)
1209		}
1210		if isNull {
1211			return errDstNotForNull(ptr)
1212		}
1213		x, err := getStringValue(v)
1214		if err != nil {
1215			return err
1216		}
1217		y, err := civil.ParseDate(x)
1218		if err != nil {
1219			return errBadEncoding(v, err)
1220		}
1221		*p = y
1222	case *NullDate, **civil.Date:
1223		if p == nil {
1224			return errNilDst(p)
1225		}
1226		if code != sppb.TypeCode_DATE {
1227			return errTypeMismatch(code, acode, ptr)
1228		}
1229		if isNull {
1230			switch sp := ptr.(type) {
1231			case *NullDate:
1232				*sp = NullDate{}
1233			case **civil.Date:
1234				*sp = nil
1235			}
1236			break
1237		}
1238		x, err := getStringValue(v)
1239		if err != nil {
1240			return err
1241		}
1242		y, err := civil.ParseDate(x)
1243		if err != nil {
1244			return errBadEncoding(v, err)
1245		}
1246		switch sp := ptr.(type) {
1247		case *NullDate:
1248			sp.Valid = true
1249			sp.Date = y
1250		case **civil.Date:
1251			*sp = &y
1252		}
1253	case *[]NullDate, *[]*civil.Date:
1254		if p == nil {
1255			return errNilDst(p)
1256		}
1257		if acode != sppb.TypeCode_DATE {
1258			return errTypeMismatch(code, acode, ptr)
1259		}
1260		if isNull {
1261			switch sp := ptr.(type) {
1262			case *[]NullDate:
1263				*sp = nil
1264			case *[]*civil.Date:
1265				*sp = nil
1266			}
1267			break
1268		}
1269		x, err := getListValue(v)
1270		if err != nil {
1271			return err
1272		}
1273		switch sp := ptr.(type) {
1274		case *[]NullDate:
1275			y, err := decodeNullDateArray(x)
1276			if err != nil {
1277				return err
1278			}
1279			*sp = y
1280		case *[]*civil.Date:
1281			y, err := decodeDatePointerArray(x)
1282			if err != nil {
1283				return err
1284			}
1285			*sp = y
1286		}
1287	case *[]civil.Date:
1288		if p == nil {
1289			return errNilDst(p)
1290		}
1291		if acode != sppb.TypeCode_DATE {
1292			return errTypeMismatch(code, acode, ptr)
1293		}
1294		if isNull {
1295			*p = nil
1296			break
1297		}
1298		x, err := getListValue(v)
1299		if err != nil {
1300			return err
1301		}
1302		y, err := decodeDateArray(x)
1303		if err != nil {
1304			return err
1305		}
1306		*p = y
1307	case *[]NullRow:
1308		if p == nil {
1309			return errNilDst(p)
1310		}
1311		if acode != sppb.TypeCode_STRUCT {
1312			return errTypeMismatch(code, acode, ptr)
1313		}
1314		if isNull {
1315			*p = nil
1316			break
1317		}
1318		x, err := getListValue(v)
1319		if err != nil {
1320			return err
1321		}
1322		y, err := decodeRowArray(t.ArrayElementType.StructType, x)
1323		if err != nil {
1324			return err
1325		}
1326		*p = y
1327	case *GenericColumnValue:
1328		*p = GenericColumnValue{Type: t, Value: v}
1329	default:
1330		// Check if the pointer is a custom type that implements spanner.Decoder
1331		// interface.
1332		if decodedVal, ok := ptr.(Decoder); ok {
1333			x, err := getGenericValue(v)
1334			if err != nil {
1335				return err
1336			}
1337			return decodedVal.DecodeSpanner(x)
1338		}
1339
1340		// Check if the pointer is a variant of a base type.
1341		decodableType := getDecodableSpannerType(ptr, true)
1342		if decodableType != spannerTypeUnknown {
1343			if isNull && !decodableType.supportsNull() {
1344				return errDstNotForNull(ptr)
1345			}
1346			return decodableType.decodeValueToCustomType(v, t, acode, ptr)
1347		}
1348
1349		// Check if the proto encoding is for an array of structs.
1350		if !(code == sppb.TypeCode_ARRAY && acode == sppb.TypeCode_STRUCT) {
1351			return errTypeMismatch(code, acode, ptr)
1352		}
1353		vp := reflect.ValueOf(p)
1354		if !vp.IsValid() {
1355			return errNilDst(p)
1356		}
1357		if !isPtrStructPtrSlice(vp.Type()) {
1358			// The container is not a pointer to a struct pointer slice.
1359			return errTypeMismatch(code, acode, ptr)
1360		}
1361		// Only use reflection for nil detection on slow path.
1362		// Also, IsNil panics on many types, so check it after the type check.
1363		if vp.IsNil() {
1364			return errNilDst(p)
1365		}
1366		if isNull {
1367			// The proto Value is encoding NULL, set the pointer to struct
1368			// slice to nil as well.
1369			vp.Elem().Set(reflect.Zero(vp.Elem().Type()))
1370			break
1371		}
1372		x, err := getListValue(v)
1373		if err != nil {
1374			return err
1375		}
1376		if err = decodeStructArray(t.ArrayElementType.StructType, x, p); err != nil {
1377			return err
1378		}
1379	}
1380	return nil
1381}
1382
1383// decodableSpannerType represents the Go types that a value from a Spanner
1384// database can be converted to.
1385type decodableSpannerType uint
1386
1387const (
1388	spannerTypeUnknown decodableSpannerType = iota
1389	spannerTypeInvalid
1390	spannerTypeNonNullString
1391	spannerTypeByteArray
1392	spannerTypeNonNullInt64
1393	spannerTypeNonNullBool
1394	spannerTypeNonNullFloat64
1395	spannerTypeNonNullNumeric
1396	spannerTypeNonNullTime
1397	spannerTypeNonNullDate
1398	spannerTypeNullString
1399	spannerTypeNullInt64
1400	spannerTypeNullBool
1401	spannerTypeNullFloat64
1402	spannerTypeNullTime
1403	spannerTypeNullDate
1404	spannerTypeNullNumeric
1405	spannerTypeArrayOfNonNullString
1406	spannerTypeArrayOfByteArray
1407	spannerTypeArrayOfNonNullInt64
1408	spannerTypeArrayOfNonNullBool
1409	spannerTypeArrayOfNonNullFloat64
1410	spannerTypeArrayOfNonNullNumeric
1411	spannerTypeArrayOfNonNullTime
1412	spannerTypeArrayOfNonNullDate
1413	spannerTypeArrayOfNullString
1414	spannerTypeArrayOfNullInt64
1415	spannerTypeArrayOfNullBool
1416	spannerTypeArrayOfNullFloat64
1417	spannerTypeArrayOfNullNumeric
1418	spannerTypeArrayOfNullTime
1419	spannerTypeArrayOfNullDate
1420)
1421
1422// supportsNull returns true for the Go types that can hold a null value from
1423// Spanner.
1424func (d decodableSpannerType) supportsNull() bool {
1425	switch d {
1426	case spannerTypeNonNullString, spannerTypeNonNullInt64, spannerTypeNonNullBool, spannerTypeNonNullFloat64, spannerTypeNonNullTime, spannerTypeNonNullDate, spannerTypeNonNullNumeric:
1427		return false
1428	default:
1429		return true
1430	}
1431}
1432
1433// The following list of types represent the struct types that represent a
1434// specific Spanner data type in Go. If a pointer to one of these types is
1435// passed to decodeValue, the client library will decode one column value into
1436// the struct. For pointers to all other struct types, the client library will
1437// treat it as a generic struct that should contain a field for each column in
1438// the result set that is being decoded.
1439
1440var typeOfNonNullTime = reflect.TypeOf(time.Time{})
1441var typeOfNonNullDate = reflect.TypeOf(civil.Date{})
1442var typeOfNonNullNumeric = reflect.TypeOf(big.Rat{})
1443var typeOfNullString = reflect.TypeOf(NullString{})
1444var typeOfNullInt64 = reflect.TypeOf(NullInt64{})
1445var typeOfNullBool = reflect.TypeOf(NullBool{})
1446var typeOfNullFloat64 = reflect.TypeOf(NullFloat64{})
1447var typeOfNullTime = reflect.TypeOf(NullTime{})
1448var typeOfNullDate = reflect.TypeOf(NullDate{})
1449var typeOfNullNumeric = reflect.TypeOf(NullNumeric{})
1450
1451// getDecodableSpannerType returns the corresponding decodableSpannerType of
1452// the given pointer.
1453func getDecodableSpannerType(ptr interface{}, isPtr bool) decodableSpannerType {
1454	var val reflect.Value
1455	var kind reflect.Kind
1456	if isPtr {
1457		val = reflect.Indirect(reflect.ValueOf(ptr))
1458	} else {
1459		val = reflect.ValueOf(ptr)
1460	}
1461	kind = val.Kind()
1462	if kind == reflect.Invalid {
1463		return spannerTypeInvalid
1464	}
1465	switch kind {
1466	case reflect.Invalid:
1467		return spannerTypeInvalid
1468	case reflect.String:
1469		return spannerTypeNonNullString
1470	case reflect.Int64:
1471		return spannerTypeNonNullInt64
1472	case reflect.Bool:
1473		return spannerTypeNonNullBool
1474	case reflect.Float64:
1475		return spannerTypeNonNullFloat64
1476	case reflect.Ptr:
1477		t := val.Type()
1478		if t.ConvertibleTo(typeOfNullNumeric) {
1479			return spannerTypeNullNumeric
1480		}
1481	case reflect.Struct:
1482		t := val.Type()
1483		if t.ConvertibleTo(typeOfNonNullNumeric) {
1484			return spannerTypeNonNullNumeric
1485		}
1486		if t.ConvertibleTo(typeOfNonNullTime) {
1487			return spannerTypeNonNullTime
1488		}
1489		if t.ConvertibleTo(typeOfNonNullDate) {
1490			return spannerTypeNonNullDate
1491		}
1492		if t.ConvertibleTo(typeOfNullString) {
1493			return spannerTypeNullString
1494		}
1495		if t.ConvertibleTo(typeOfNullInt64) {
1496			return spannerTypeNullInt64
1497		}
1498		if t.ConvertibleTo(typeOfNullBool) {
1499			return spannerTypeNullBool
1500		}
1501		if t.ConvertibleTo(typeOfNullFloat64) {
1502			return spannerTypeNullFloat64
1503		}
1504		if t.ConvertibleTo(typeOfNullTime) {
1505			return spannerTypeNullTime
1506		}
1507		if t.ConvertibleTo(typeOfNullDate) {
1508			return spannerTypeNullDate
1509		}
1510		if t.ConvertibleTo(typeOfNullNumeric) {
1511			return spannerTypeNullNumeric
1512		}
1513	case reflect.Slice:
1514		kind := val.Type().Elem().Kind()
1515		switch kind {
1516		case reflect.Invalid:
1517			return spannerTypeUnknown
1518		case reflect.String:
1519			return spannerTypeArrayOfNonNullString
1520		case reflect.Uint8:
1521			return spannerTypeByteArray
1522		case reflect.Int64:
1523			return spannerTypeArrayOfNonNullInt64
1524		case reflect.Bool:
1525			return spannerTypeArrayOfNonNullBool
1526		case reflect.Float64:
1527			return spannerTypeArrayOfNonNullFloat64
1528		case reflect.Ptr:
1529			t := val.Type().Elem()
1530			if t.ConvertibleTo(typeOfNullNumeric) {
1531				return spannerTypeArrayOfNullNumeric
1532			}
1533		case reflect.Struct:
1534			t := val.Type().Elem()
1535			if t.ConvertibleTo(typeOfNonNullNumeric) {
1536				return spannerTypeArrayOfNonNullNumeric
1537			}
1538			if t.ConvertibleTo(typeOfNonNullTime) {
1539				return spannerTypeArrayOfNonNullTime
1540			}
1541			if t.ConvertibleTo(typeOfNonNullDate) {
1542				return spannerTypeArrayOfNonNullDate
1543			}
1544			if t.ConvertibleTo(typeOfNullString) {
1545				return spannerTypeArrayOfNullString
1546			}
1547			if t.ConvertibleTo(typeOfNullInt64) {
1548				return spannerTypeArrayOfNullInt64
1549			}
1550			if t.ConvertibleTo(typeOfNullBool) {
1551				return spannerTypeArrayOfNullBool
1552			}
1553			if t.ConvertibleTo(typeOfNullFloat64) {
1554				return spannerTypeArrayOfNullFloat64
1555			}
1556			if t.ConvertibleTo(typeOfNullTime) {
1557				return spannerTypeArrayOfNullTime
1558			}
1559			if t.ConvertibleTo(typeOfNullDate) {
1560				return spannerTypeArrayOfNullDate
1561			}
1562			if t.ConvertibleTo(typeOfNullNumeric) {
1563				return spannerTypeArrayOfNullNumeric
1564			}
1565		case reflect.Slice:
1566			// The only array-of-array type that is supported is [][]byte.
1567			kind := val.Type().Elem().Elem().Kind()
1568			switch kind {
1569			case reflect.Uint8:
1570				return spannerTypeArrayOfByteArray
1571			}
1572		}
1573	}
1574	// Not convertible to a known base type.
1575	return spannerTypeUnknown
1576}
1577
1578// decodeValueToCustomType decodes a protobuf Value into a pointer to a Go
1579// value. It must be possible to convert the value to the type pointed to by
1580// the pointer.
1581func (dsc decodableSpannerType) decodeValueToCustomType(v *proto3.Value, t *sppb.Type, acode sppb.TypeCode, ptr interface{}) error {
1582	code := t.Code
1583	_, isNull := v.Kind.(*proto3.Value_NullValue)
1584	if dsc == spannerTypeInvalid {
1585		return errNilDst(ptr)
1586	}
1587	if isNull && !dsc.supportsNull() {
1588		return errDstNotForNull(ptr)
1589	}
1590
1591	var result interface{}
1592	switch dsc {
1593	case spannerTypeNonNullString, spannerTypeNullString:
1594		if code != sppb.TypeCode_STRING {
1595			return errTypeMismatch(code, acode, ptr)
1596		}
1597		if isNull {
1598			result = &NullString{}
1599			break
1600		}
1601		x, err := getStringValue(v)
1602		if err != nil {
1603			return err
1604		}
1605		if dsc == spannerTypeNonNullString {
1606			result = &x
1607		} else {
1608			result = &NullString{x, !isNull}
1609		}
1610	case spannerTypeByteArray:
1611		if code != sppb.TypeCode_BYTES {
1612			return errTypeMismatch(code, acode, ptr)
1613		}
1614		if isNull {
1615			result = []byte(nil)
1616			break
1617		}
1618		x, err := getStringValue(v)
1619		if err != nil {
1620			return err
1621		}
1622		y, err := base64.StdEncoding.DecodeString(x)
1623		if err != nil {
1624			return errBadEncoding(v, err)
1625		}
1626		result = y
1627	case spannerTypeNonNullInt64, spannerTypeNullInt64:
1628		if code != sppb.TypeCode_INT64 {
1629			return errTypeMismatch(code, acode, ptr)
1630		}
1631		if isNull {
1632			result = &NullInt64{}
1633			break
1634		}
1635		x, err := getStringValue(v)
1636		if err != nil {
1637			return err
1638		}
1639		y, err := strconv.ParseInt(x, 10, 64)
1640		if err != nil {
1641			return errBadEncoding(v, err)
1642		}
1643		if dsc == spannerTypeNonNullInt64 {
1644			result = &y
1645		} else {
1646			result = &NullInt64{y, !isNull}
1647		}
1648	case spannerTypeNonNullBool, spannerTypeNullBool:
1649		if code != sppb.TypeCode_BOOL {
1650			return errTypeMismatch(code, acode, ptr)
1651		}
1652		if isNull {
1653			result = &NullBool{}
1654			break
1655		}
1656		x, err := getBoolValue(v)
1657		if err != nil {
1658			return err
1659		}
1660		if dsc == spannerTypeNonNullBool {
1661			result = &x
1662		} else {
1663			result = &NullBool{x, !isNull}
1664		}
1665	case spannerTypeNonNullFloat64, spannerTypeNullFloat64:
1666		if code != sppb.TypeCode_FLOAT64 {
1667			return errTypeMismatch(code, acode, ptr)
1668		}
1669		if isNull {
1670			result = &NullFloat64{}
1671			break
1672		}
1673		x, err := getFloat64Value(v)
1674		if err != nil {
1675			return err
1676		}
1677		if dsc == spannerTypeNonNullFloat64 {
1678			result = &x
1679		} else {
1680			result = &NullFloat64{x, !isNull}
1681		}
1682	case spannerTypeNonNullNumeric, spannerTypeNullNumeric:
1683		if code != sppb.TypeCode_NUMERIC {
1684			return errTypeMismatch(code, acode, ptr)
1685		}
1686		if isNull {
1687			result = &NullNumeric{}
1688			break
1689		}
1690		x := v.GetStringValue()
1691		y, ok := (&big.Rat{}).SetString(x)
1692		if !ok {
1693			return errUnexpectedNumericStr(x)
1694		}
1695		if dsc == spannerTypeNonNullNumeric {
1696			result = y
1697		} else {
1698			result = &NullNumeric{*y, true}
1699		}
1700	case spannerTypeNonNullTime, spannerTypeNullTime:
1701		var nt NullTime
1702		err := parseNullTime(v, &nt, code, isNull)
1703		if err != nil {
1704			return err
1705		}
1706		if dsc == spannerTypeNonNullTime {
1707			result = &nt.Time
1708		} else {
1709			result = &nt
1710		}
1711	case spannerTypeNonNullDate, spannerTypeNullDate:
1712		if code != sppb.TypeCode_DATE {
1713			return errTypeMismatch(code, acode, ptr)
1714		}
1715		if isNull {
1716			result = &NullDate{}
1717			break
1718		}
1719		x, err := getStringValue(v)
1720		if err != nil {
1721			return err
1722		}
1723		y, err := civil.ParseDate(x)
1724		if err != nil {
1725			return errBadEncoding(v, err)
1726		}
1727		if dsc == spannerTypeNonNullDate {
1728			result = &y
1729		} else {
1730			result = &NullDate{y, !isNull}
1731		}
1732	case spannerTypeArrayOfNonNullString, spannerTypeArrayOfNullString:
1733		if acode != sppb.TypeCode_STRING {
1734			return errTypeMismatch(code, acode, ptr)
1735		}
1736		if isNull {
1737			ptr = nil
1738			return nil
1739		}
1740		x, err := getListValue(v)
1741		if err != nil {
1742			return err
1743		}
1744		y, err := decodeGenericArray(reflect.TypeOf(ptr).Elem(), x, stringType(), "STRING")
1745		if err != nil {
1746			return err
1747		}
1748		result = y
1749	case spannerTypeArrayOfByteArray:
1750		if acode != sppb.TypeCode_BYTES {
1751			return errTypeMismatch(code, acode, ptr)
1752		}
1753		if isNull {
1754			ptr = nil
1755			return nil
1756		}
1757		x, err := getListValue(v)
1758		if err != nil {
1759			return err
1760		}
1761		y, err := decodeGenericArray(reflect.TypeOf(ptr).Elem(), x, bytesType(), "BYTES")
1762		if err != nil {
1763			return err
1764		}
1765		result = y
1766	case spannerTypeArrayOfNonNullInt64, spannerTypeArrayOfNullInt64:
1767		if acode != sppb.TypeCode_INT64 {
1768			return errTypeMismatch(code, acode, ptr)
1769		}
1770		if isNull {
1771			ptr = nil
1772			return nil
1773		}
1774		x, err := getListValue(v)
1775		if err != nil {
1776			return err
1777		}
1778		y, err := decodeGenericArray(reflect.TypeOf(ptr).Elem(), x, intType(), "INT64")
1779		if err != nil {
1780			return err
1781		}
1782		result = y
1783	case spannerTypeArrayOfNonNullBool, spannerTypeArrayOfNullBool:
1784		if acode != sppb.TypeCode_BOOL {
1785			return errTypeMismatch(code, acode, ptr)
1786		}
1787		if isNull {
1788			ptr = nil
1789			return nil
1790		}
1791		x, err := getListValue(v)
1792		if err != nil {
1793			return err
1794		}
1795		y, err := decodeGenericArray(reflect.TypeOf(ptr).Elem(), x, boolType(), "BOOL")
1796		if err != nil {
1797			return err
1798		}
1799		result = y
1800	case spannerTypeArrayOfNonNullFloat64, spannerTypeArrayOfNullFloat64:
1801		if acode != sppb.TypeCode_FLOAT64 {
1802			return errTypeMismatch(code, acode, ptr)
1803		}
1804		if isNull {
1805			ptr = nil
1806			return nil
1807		}
1808		x, err := getListValue(v)
1809		if err != nil {
1810			return err
1811		}
1812		y, err := decodeGenericArray(reflect.TypeOf(ptr).Elem(), x, floatType(), "FLOAT64")
1813		if err != nil {
1814			return err
1815		}
1816		result = y
1817	case spannerTypeArrayOfNonNullNumeric, spannerTypeArrayOfNullNumeric:
1818		if acode != sppb.TypeCode_NUMERIC {
1819			return errTypeMismatch(code, acode, ptr)
1820		}
1821		if isNull {
1822			ptr = nil
1823			return nil
1824		}
1825		x, err := getListValue(v)
1826		if err != nil {
1827			return err
1828		}
1829		y, err := decodeGenericArray(reflect.TypeOf(ptr).Elem(), x, numericType(), "NUMERIC")
1830		if err != nil {
1831			return err
1832		}
1833		result = y
1834	case spannerTypeArrayOfNonNullTime, spannerTypeArrayOfNullTime:
1835		if acode != sppb.TypeCode_TIMESTAMP {
1836			return errTypeMismatch(code, acode, ptr)
1837		}
1838		if isNull {
1839			ptr = nil
1840			return nil
1841		}
1842		x, err := getListValue(v)
1843		if err != nil {
1844			return err
1845		}
1846		y, err := decodeGenericArray(reflect.TypeOf(ptr).Elem(), x, timeType(), "TIMESTAMP")
1847		if err != nil {
1848			return err
1849		}
1850		result = y
1851	case spannerTypeArrayOfNonNullDate, spannerTypeArrayOfNullDate:
1852		if acode != sppb.TypeCode_DATE {
1853			return errTypeMismatch(code, acode, ptr)
1854		}
1855		if isNull {
1856			ptr = nil
1857			return nil
1858		}
1859		x, err := getListValue(v)
1860		if err != nil {
1861			return err
1862		}
1863		y, err := decodeGenericArray(reflect.TypeOf(ptr).Elem(), x, dateType(), "DATE")
1864		if err != nil {
1865			return err
1866		}
1867		result = y
1868	default:
1869		// This should not be possible.
1870		return fmt.Errorf("unknown decodable type found: %v", dsc)
1871	}
1872	source := reflect.Indirect(reflect.ValueOf(result))
1873	destination := reflect.Indirect(reflect.ValueOf(ptr))
1874	destination.Set(source.Convert(destination.Type()))
1875	return nil
1876}
1877
1878// errSrvVal returns an error for getting a wrong source protobuf value in decoding.
1879func errSrcVal(v *proto3.Value, want string) error {
1880	return spannerErrorf(codes.FailedPrecondition, "cannot use %v(Kind: %T) as %s Value",
1881		v, v.GetKind(), want)
1882}
1883
1884// getStringValue returns the string value encoded in proto3.Value v whose
1885// kind is proto3.Value_StringValue.
1886func getStringValue(v *proto3.Value) (string, error) {
1887	if x, ok := v.GetKind().(*proto3.Value_StringValue); ok && x != nil {
1888		return x.StringValue, nil
1889	}
1890	return "", errSrcVal(v, "String")
1891}
1892
1893// getBoolValue returns the bool value encoded in proto3.Value v whose
1894// kind is proto3.Value_BoolValue.
1895func getBoolValue(v *proto3.Value) (bool, error) {
1896	if x, ok := v.GetKind().(*proto3.Value_BoolValue); ok && x != nil {
1897		return x.BoolValue, nil
1898	}
1899	return false, errSrcVal(v, "Bool")
1900}
1901
1902// getListValue returns the proto3.ListValue contained in proto3.Value v whose
1903// kind is proto3.Value_ListValue.
1904func getListValue(v *proto3.Value) (*proto3.ListValue, error) {
1905	if x, ok := v.GetKind().(*proto3.Value_ListValue); ok && x != nil {
1906		return x.ListValue, nil
1907	}
1908	return nil, errSrcVal(v, "List")
1909}
1910
1911// getGenericValue returns the interface{} value encoded in proto3.Value.
1912func getGenericValue(v *proto3.Value) (interface{}, error) {
1913	switch x := v.GetKind().(type) {
1914	case *proto3.Value_NumberValue:
1915		return x.NumberValue, nil
1916	case *proto3.Value_BoolValue:
1917		return x.BoolValue, nil
1918	case *proto3.Value_StringValue:
1919		return x.StringValue, nil
1920	default:
1921		return 0, errSrcVal(v, "Number, Bool, String")
1922	}
1923}
1924
1925// errUnexpectedNumericStr returns error for decoder getting an unexpected
1926// string for representing special numeric values.
1927func errUnexpectedNumericStr(s string) error {
1928	return spannerErrorf(codes.FailedPrecondition, "unexpected string value %q for numeric number", s)
1929}
1930
1931// errUnexpectedFloat64Str returns error for decoder getting an unexpected
1932// string for representing special float values.
1933func errUnexpectedFloat64Str(s string) error {
1934	return spannerErrorf(codes.FailedPrecondition, "unexpected string value %q for float64 number", s)
1935}
1936
1937// getFloat64Value returns the float64 value encoded in proto3.Value v whose
1938// kind is proto3.Value_NumberValue / proto3.Value_StringValue.
1939// Cloud Spanner uses string to encode NaN, Infinity and -Infinity.
1940func getFloat64Value(v *proto3.Value) (float64, error) {
1941	switch x := v.GetKind().(type) {
1942	case *proto3.Value_NumberValue:
1943		if x == nil {
1944			break
1945		}
1946		return x.NumberValue, nil
1947	case *proto3.Value_StringValue:
1948		if x == nil {
1949			break
1950		}
1951		switch x.StringValue {
1952		case "NaN":
1953			return math.NaN(), nil
1954		case "Infinity":
1955			return math.Inf(1), nil
1956		case "-Infinity":
1957			return math.Inf(-1), nil
1958		default:
1959			return 0, errUnexpectedFloat64Str(x.StringValue)
1960		}
1961	}
1962	return 0, errSrcVal(v, "Number")
1963}
1964
1965// errNilListValue returns error for unexpected nil ListValue in decoding Cloud Spanner ARRAYs.
1966func errNilListValue(sqlType string) error {
1967	return spannerErrorf(codes.FailedPrecondition, "unexpected nil ListValue in decoding %v array", sqlType)
1968}
1969
1970// errDecodeArrayElement returns error for failure in decoding single array element.
1971func errDecodeArrayElement(i int, v proto.Message, sqlType string, err error) error {
1972	var se *Error
1973	if !errorAs(err, &se) {
1974		return spannerErrorf(codes.Unknown,
1975			"cannot decode %v(array element %v) as %v, error = <%v>", v, i, sqlType, err)
1976	}
1977	se.decorate(fmt.Sprintf("cannot decode %v(array element %v) as %v", v, i, sqlType))
1978	return se
1979}
1980
1981// decodeGenericArray decodes proto3.ListValue pb into a slice which type is
1982// determined through reflection.
1983func decodeGenericArray(tp reflect.Type, pb *proto3.ListValue, t *sppb.Type, sqlType string) (interface{}, error) {
1984	if pb == nil {
1985		return nil, errNilListValue(sqlType)
1986	}
1987	a := reflect.MakeSlice(tp, len(pb.Values), len(pb.Values))
1988	for i, v := range pb.Values {
1989		if err := decodeValue(v, t, a.Index(i).Addr().Interface()); err != nil {
1990			return nil, errDecodeArrayElement(i, v, "STRING", err)
1991		}
1992	}
1993	return a.Interface(), nil
1994}
1995
1996// decodeNullStringArray decodes proto3.ListValue pb into a NullString slice.
1997func decodeNullStringArray(pb *proto3.ListValue) ([]NullString, error) {
1998	if pb == nil {
1999		return nil, errNilListValue("STRING")
2000	}
2001	a := make([]NullString, len(pb.Values))
2002	for i, v := range pb.Values {
2003		if err := decodeValue(v, stringType(), &a[i]); err != nil {
2004			return nil, errDecodeArrayElement(i, v, "STRING", err)
2005		}
2006	}
2007	return a, nil
2008}
2009
2010// decodeStringPointerArray decodes proto3.ListValue pb into a *string slice.
2011func decodeStringPointerArray(pb *proto3.ListValue) ([]*string, error) {
2012	if pb == nil {
2013		return nil, errNilListValue("STRING")
2014	}
2015	a := make([]*string, len(pb.Values))
2016	for i, v := range pb.Values {
2017		if err := decodeValue(v, stringType(), &a[i]); err != nil {
2018			return nil, errDecodeArrayElement(i, v, "STRING", err)
2019		}
2020	}
2021	return a, nil
2022}
2023
2024// decodeStringArray decodes proto3.ListValue pb into a string slice.
2025func decodeStringArray(pb *proto3.ListValue) ([]string, error) {
2026	if pb == nil {
2027		return nil, errNilListValue("STRING")
2028	}
2029	a := make([]string, len(pb.Values))
2030	st := stringType()
2031	for i, v := range pb.Values {
2032		if err := decodeValue(v, st, &a[i]); err != nil {
2033			return nil, errDecodeArrayElement(i, v, "STRING", err)
2034		}
2035	}
2036	return a, nil
2037}
2038
2039// decodeNullInt64Array decodes proto3.ListValue pb into a NullInt64 slice.
2040func decodeNullInt64Array(pb *proto3.ListValue) ([]NullInt64, error) {
2041	if pb == nil {
2042		return nil, errNilListValue("INT64")
2043	}
2044	a := make([]NullInt64, len(pb.Values))
2045	for i, v := range pb.Values {
2046		if err := decodeValue(v, intType(), &a[i]); err != nil {
2047			return nil, errDecodeArrayElement(i, v, "INT64", err)
2048		}
2049	}
2050	return a, nil
2051}
2052
2053// decodeInt64PointerArray decodes proto3.ListValue pb into a *int64 slice.
2054func decodeInt64PointerArray(pb *proto3.ListValue) ([]*int64, error) {
2055	if pb == nil {
2056		return nil, errNilListValue("INT64")
2057	}
2058	a := make([]*int64, len(pb.Values))
2059	for i, v := range pb.Values {
2060		if err := decodeValue(v, intType(), &a[i]); err != nil {
2061			return nil, errDecodeArrayElement(i, v, "INT64", err)
2062		}
2063	}
2064	return a, nil
2065}
2066
2067// decodeInt64Array decodes proto3.ListValue pb into a int64 slice.
2068func decodeInt64Array(pb *proto3.ListValue) ([]int64, error) {
2069	if pb == nil {
2070		return nil, errNilListValue("INT64")
2071	}
2072	a := make([]int64, len(pb.Values))
2073	for i, v := range pb.Values {
2074		if err := decodeValue(v, intType(), &a[i]); err != nil {
2075			return nil, errDecodeArrayElement(i, v, "INT64", err)
2076		}
2077	}
2078	return a, nil
2079}
2080
2081// decodeNullBoolArray decodes proto3.ListValue pb into a NullBool slice.
2082func decodeNullBoolArray(pb *proto3.ListValue) ([]NullBool, error) {
2083	if pb == nil {
2084		return nil, errNilListValue("BOOL")
2085	}
2086	a := make([]NullBool, len(pb.Values))
2087	for i, v := range pb.Values {
2088		if err := decodeValue(v, boolType(), &a[i]); err != nil {
2089			return nil, errDecodeArrayElement(i, v, "BOOL", err)
2090		}
2091	}
2092	return a, nil
2093}
2094
2095// decodeBoolPointerArray decodes proto3.ListValue pb into a *bool slice.
2096func decodeBoolPointerArray(pb *proto3.ListValue) ([]*bool, error) {
2097	if pb == nil {
2098		return nil, errNilListValue("BOOL")
2099	}
2100	a := make([]*bool, len(pb.Values))
2101	for i, v := range pb.Values {
2102		if err := decodeValue(v, boolType(), &a[i]); err != nil {
2103			return nil, errDecodeArrayElement(i, v, "BOOL", err)
2104		}
2105	}
2106	return a, nil
2107}
2108
2109// decodeBoolArray decodes proto3.ListValue pb into a bool slice.
2110func decodeBoolArray(pb *proto3.ListValue) ([]bool, error) {
2111	if pb == nil {
2112		return nil, errNilListValue("BOOL")
2113	}
2114	a := make([]bool, len(pb.Values))
2115	for i, v := range pb.Values {
2116		if err := decodeValue(v, boolType(), &a[i]); err != nil {
2117			return nil, errDecodeArrayElement(i, v, "BOOL", err)
2118		}
2119	}
2120	return a, nil
2121}
2122
2123// decodeNullFloat64Array decodes proto3.ListValue pb into a NullFloat64 slice.
2124func decodeNullFloat64Array(pb *proto3.ListValue) ([]NullFloat64, error) {
2125	if pb == nil {
2126		return nil, errNilListValue("FLOAT64")
2127	}
2128	a := make([]NullFloat64, len(pb.Values))
2129	for i, v := range pb.Values {
2130		if err := decodeValue(v, floatType(), &a[i]); err != nil {
2131			return nil, errDecodeArrayElement(i, v, "FLOAT64", err)
2132		}
2133	}
2134	return a, nil
2135}
2136
2137// decodeFloat64PointerArray decodes proto3.ListValue pb into a *float slice.
2138func decodeFloat64PointerArray(pb *proto3.ListValue) ([]*float64, error) {
2139	if pb == nil {
2140		return nil, errNilListValue("FLOAT64")
2141	}
2142	a := make([]*float64, len(pb.Values))
2143	for i, v := range pb.Values {
2144		if err := decodeValue(v, floatType(), &a[i]); err != nil {
2145			return nil, errDecodeArrayElement(i, v, "FLOAT64", err)
2146		}
2147	}
2148	return a, nil
2149}
2150
2151// decodeFloat64Array decodes proto3.ListValue pb into a float64 slice.
2152func decodeFloat64Array(pb *proto3.ListValue) ([]float64, error) {
2153	if pb == nil {
2154		return nil, errNilListValue("FLOAT64")
2155	}
2156	a := make([]float64, len(pb.Values))
2157	for i, v := range pb.Values {
2158		if err := decodeValue(v, floatType(), &a[i]); err != nil {
2159			return nil, errDecodeArrayElement(i, v, "FLOAT64", err)
2160		}
2161	}
2162	return a, nil
2163}
2164
2165// decodeNullNumericArray decodes proto3.ListValue pb into a NullNumeric slice.
2166func decodeNullNumericArray(pb *proto3.ListValue) ([]NullNumeric, error) {
2167	if pb == nil {
2168		return nil, errNilListValue("NUMERIC")
2169	}
2170	a := make([]NullNumeric, len(pb.Values))
2171	for i, v := range pb.Values {
2172		if err := decodeValue(v, numericType(), &a[i]); err != nil {
2173			return nil, errDecodeArrayElement(i, v, "NUMERIC", err)
2174		}
2175	}
2176	return a, nil
2177}
2178
2179// decodeNumericPointerArray decodes proto3.ListValue pb into a *big.Rat slice.
2180func decodeNumericPointerArray(pb *proto3.ListValue) ([]*big.Rat, error) {
2181	if pb == nil {
2182		return nil, errNilListValue("NUMERIC")
2183	}
2184	a := make([]*big.Rat, len(pb.Values))
2185	for i, v := range pb.Values {
2186		if err := decodeValue(v, numericType(), &a[i]); err != nil {
2187			return nil, errDecodeArrayElement(i, v, "NUMERIC", err)
2188		}
2189	}
2190	return a, nil
2191}
2192
2193// decodeNumericArray decodes proto3.ListValue pb into a big.Rat slice.
2194func decodeNumericArray(pb *proto3.ListValue) ([]big.Rat, error) {
2195	if pb == nil {
2196		return nil, errNilListValue("NUMERIC")
2197	}
2198	a := make([]big.Rat, len(pb.Values))
2199	for i, v := range pb.Values {
2200		if err := decodeValue(v, numericType(), &a[i]); err != nil {
2201			return nil, errDecodeArrayElement(i, v, "NUMERIC", err)
2202		}
2203	}
2204	return a, nil
2205}
2206
2207// decodeByteArray decodes proto3.ListValue pb into a slice of byte slice.
2208func decodeByteArray(pb *proto3.ListValue) ([][]byte, error) {
2209	if pb == nil {
2210		return nil, errNilListValue("BYTES")
2211	}
2212	a := make([][]byte, len(pb.Values))
2213	for i, v := range pb.Values {
2214		if err := decodeValue(v, bytesType(), &a[i]); err != nil {
2215			return nil, errDecodeArrayElement(i, v, "BYTES", err)
2216		}
2217	}
2218	return a, nil
2219}
2220
2221// decodeNullTimeArray decodes proto3.ListValue pb into a NullTime slice.
2222func decodeNullTimeArray(pb *proto3.ListValue) ([]NullTime, error) {
2223	if pb == nil {
2224		return nil, errNilListValue("TIMESTAMP")
2225	}
2226	a := make([]NullTime, len(pb.Values))
2227	for i, v := range pb.Values {
2228		if err := decodeValue(v, timeType(), &a[i]); err != nil {
2229			return nil, errDecodeArrayElement(i, v, "TIMESTAMP", err)
2230		}
2231	}
2232	return a, nil
2233}
2234
2235// decodeTimePointerArray decodes proto3.ListValue pb into a NullTime slice.
2236func decodeTimePointerArray(pb *proto3.ListValue) ([]*time.Time, error) {
2237	if pb == nil {
2238		return nil, errNilListValue("TIMESTAMP")
2239	}
2240	a := make([]*time.Time, len(pb.Values))
2241	for i, v := range pb.Values {
2242		if err := decodeValue(v, timeType(), &a[i]); err != nil {
2243			return nil, errDecodeArrayElement(i, v, "TIMESTAMP", err)
2244		}
2245	}
2246	return a, nil
2247}
2248
2249// decodeTimeArray decodes proto3.ListValue pb into a time.Time slice.
2250func decodeTimeArray(pb *proto3.ListValue) ([]time.Time, error) {
2251	if pb == nil {
2252		return nil, errNilListValue("TIMESTAMP")
2253	}
2254	a := make([]time.Time, len(pb.Values))
2255	for i, v := range pb.Values {
2256		if err := decodeValue(v, timeType(), &a[i]); err != nil {
2257			return nil, errDecodeArrayElement(i, v, "TIMESTAMP", err)
2258		}
2259	}
2260	return a, nil
2261}
2262
2263// decodeNullDateArray decodes proto3.ListValue pb into a NullDate slice.
2264func decodeNullDateArray(pb *proto3.ListValue) ([]NullDate, error) {
2265	if pb == nil {
2266		return nil, errNilListValue("DATE")
2267	}
2268	a := make([]NullDate, len(pb.Values))
2269	for i, v := range pb.Values {
2270		if err := decodeValue(v, dateType(), &a[i]); err != nil {
2271			return nil, errDecodeArrayElement(i, v, "DATE", err)
2272		}
2273	}
2274	return a, nil
2275}
2276
2277// decodeDatePointerArray decodes proto3.ListValue pb into a *civil.Date slice.
2278func decodeDatePointerArray(pb *proto3.ListValue) ([]*civil.Date, error) {
2279	if pb == nil {
2280		return nil, errNilListValue("DATE")
2281	}
2282	a := make([]*civil.Date, len(pb.Values))
2283	for i, v := range pb.Values {
2284		if err := decodeValue(v, dateType(), &a[i]); err != nil {
2285			return nil, errDecodeArrayElement(i, v, "DATE", err)
2286		}
2287	}
2288	return a, nil
2289}
2290
2291// decodeDateArray decodes proto3.ListValue pb into a civil.Date slice.
2292func decodeDateArray(pb *proto3.ListValue) ([]civil.Date, error) {
2293	if pb == nil {
2294		return nil, errNilListValue("DATE")
2295	}
2296	a := make([]civil.Date, len(pb.Values))
2297	for i, v := range pb.Values {
2298		if err := decodeValue(v, dateType(), &a[i]); err != nil {
2299			return nil, errDecodeArrayElement(i, v, "DATE", err)
2300		}
2301	}
2302	return a, nil
2303}
2304
2305func errNotStructElement(i int, v *proto3.Value) error {
2306	return errDecodeArrayElement(i, v, "STRUCT",
2307		spannerErrorf(codes.FailedPrecondition, "%v(type: %T) doesn't encode Cloud Spanner STRUCT", v, v))
2308}
2309
2310// decodeRowArray decodes proto3.ListValue pb into a NullRow slice according to
2311// the structural information given in sppb.StructType ty.
2312func decodeRowArray(ty *sppb.StructType, pb *proto3.ListValue) ([]NullRow, error) {
2313	if pb == nil {
2314		return nil, errNilListValue("STRUCT")
2315	}
2316	a := make([]NullRow, len(pb.Values))
2317	for i := range pb.Values {
2318		switch v := pb.Values[i].GetKind().(type) {
2319		case *proto3.Value_ListValue:
2320			a[i] = NullRow{
2321				Row: Row{
2322					fields: ty.Fields,
2323					vals:   v.ListValue.Values,
2324				},
2325				Valid: true,
2326			}
2327		// Null elements not currently supported by the server, see
2328		// https://cloud.google.com/spanner/docs/query-syntax#using-structs-with-select
2329		case *proto3.Value_NullValue:
2330			// no-op, a[i] is NullRow{} already
2331		default:
2332			return nil, errNotStructElement(i, pb.Values[i])
2333		}
2334	}
2335	return a, nil
2336}
2337
2338// errNilSpannerStructType returns error for unexpected nil Cloud Spanner STRUCT
2339// schema type in decoding.
2340func errNilSpannerStructType() error {
2341	return spannerErrorf(codes.FailedPrecondition, "unexpected nil StructType in decoding Cloud Spanner STRUCT")
2342}
2343
2344// errUnnamedField returns error for decoding a Cloud Spanner STRUCT with
2345// unnamed field into a Go struct.
2346func errUnnamedField(ty *sppb.StructType, i int) error {
2347	return spannerErrorf(codes.InvalidArgument, "unnamed field %v in Cloud Spanner STRUCT %+v", i, ty)
2348}
2349
2350// errNoOrDupGoField returns error for decoding a Cloud Spanner
2351// STRUCT into a Go struct which is either missing a field, or has duplicate
2352// fields.
2353func errNoOrDupGoField(s interface{}, f string) error {
2354	return spannerErrorf(codes.InvalidArgument, "Go struct %+v(type %T) has no or duplicate fields for Cloud Spanner STRUCT field %v", s, s, f)
2355}
2356
2357// errDupColNames returns error for duplicated Cloud Spanner STRUCT field names
2358// found in decoding a Cloud Spanner STRUCT into a Go struct.
2359func errDupSpannerField(f string, ty *sppb.StructType) error {
2360	return spannerErrorf(codes.InvalidArgument, "duplicated field name %q in Cloud Spanner STRUCT %+v", f, ty)
2361}
2362
2363// errDecodeStructField returns error for failure in decoding a single field of
2364// a Cloud Spanner STRUCT.
2365func errDecodeStructField(ty *sppb.StructType, f string, err error) error {
2366	var se *Error
2367	if !errorAs(err, &se) {
2368		return spannerErrorf(codes.Unknown,
2369			"cannot decode field %v of Cloud Spanner STRUCT %+v, error = <%v>", f, ty, err)
2370	}
2371	se.decorate(fmt.Sprintf("cannot decode field %v of Cloud Spanner STRUCT %+v", f, ty))
2372	return se
2373}
2374
2375// decodeStruct decodes proto3.ListValue pb into struct referenced by pointer
2376// ptr, according to
2377// the structural information given in sppb.StructType ty.
2378func decodeStruct(ty *sppb.StructType, pb *proto3.ListValue, ptr interface{}) error {
2379	if reflect.ValueOf(ptr).IsNil() {
2380		return errNilDst(ptr)
2381	}
2382	if ty == nil {
2383		return errNilSpannerStructType()
2384	}
2385	// t holds the structural information of ptr.
2386	t := reflect.TypeOf(ptr).Elem()
2387	// v is the actual value that ptr points to.
2388	v := reflect.ValueOf(ptr).Elem()
2389
2390	fields, err := fieldCache.Fields(t)
2391	if err != nil {
2392		return toSpannerError(err)
2393	}
2394	seen := map[string]bool{}
2395	for i, f := range ty.Fields {
2396		if f.Name == "" {
2397			return errUnnamedField(ty, i)
2398		}
2399		sf := fields.Match(f.Name)
2400		if sf == nil {
2401			return errNoOrDupGoField(ptr, f.Name)
2402		}
2403		if seen[f.Name] {
2404			// We don't allow duplicated field name.
2405			return errDupSpannerField(f.Name, ty)
2406		}
2407		// Try to decode a single field.
2408		if err := decodeValue(pb.Values[i], f.Type, v.FieldByIndex(sf.Index).Addr().Interface()); err != nil {
2409			return errDecodeStructField(ty, f.Name, err)
2410		}
2411		// Mark field f.Name as processed.
2412		seen[f.Name] = true
2413	}
2414	return nil
2415}
2416
2417// isPtrStructPtrSlice returns true if ptr is a pointer to a slice of struct pointers.
2418func isPtrStructPtrSlice(t reflect.Type) bool {
2419	if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Slice {
2420		// t is not a pointer to a slice.
2421		return false
2422	}
2423	if t = t.Elem(); t.Elem().Kind() != reflect.Ptr || t.Elem().Elem().Kind() != reflect.Struct {
2424		// the slice that t points to is not a slice of struct pointers.
2425		return false
2426	}
2427	return true
2428}
2429
2430// decodeStructArray decodes proto3.ListValue pb into struct slice referenced by
2431// pointer ptr, according to the
2432// structural information given in a sppb.StructType.
2433func decodeStructArray(ty *sppb.StructType, pb *proto3.ListValue, ptr interface{}) error {
2434	if pb == nil {
2435		return errNilListValue("STRUCT")
2436	}
2437	// Type of the struct pointers stored in the slice that ptr points to.
2438	ts := reflect.TypeOf(ptr).Elem().Elem()
2439	// The slice that ptr points to, might be nil at this point.
2440	v := reflect.ValueOf(ptr).Elem()
2441	// Allocate empty slice.
2442	v.Set(reflect.MakeSlice(v.Type(), 0, len(pb.Values)))
2443	// Decode every struct in pb.Values.
2444	for i, pv := range pb.Values {
2445		// Check if pv is a NULL value.
2446		if _, isNull := pv.Kind.(*proto3.Value_NullValue); isNull {
2447			// Append a nil pointer to the slice.
2448			v.Set(reflect.Append(v, reflect.New(ts).Elem()))
2449			continue
2450		}
2451		// Allocate empty struct.
2452		s := reflect.New(ts.Elem())
2453		// Get proto3.ListValue l from proto3.Value pv.
2454		l, err := getListValue(pv)
2455		if err != nil {
2456			return errDecodeArrayElement(i, pv, "STRUCT", err)
2457		}
2458		// Decode proto3.ListValue l into struct referenced by s.Interface().
2459		if err = decodeStruct(ty, l, s.Interface()); err != nil {
2460			return errDecodeArrayElement(i, pv, "STRUCT", err)
2461		}
2462		// Append the decoded struct back into the slice.
2463		v.Set(reflect.Append(v, s))
2464	}
2465	return nil
2466}
2467
2468// errEncoderUnsupportedType returns error for not being able to encode a value
2469// of certain type.
2470func errEncoderUnsupportedType(v interface{}) error {
2471	return spannerErrorf(codes.InvalidArgument, "client doesn't support type %T", v)
2472}
2473
2474// encodeValue encodes a Go native type into a proto3.Value.
2475func encodeValue(v interface{}) (*proto3.Value, *sppb.Type, error) {
2476	pb := &proto3.Value{
2477		Kind: &proto3.Value_NullValue{NullValue: proto3.NullValue_NULL_VALUE},
2478	}
2479	var pt *sppb.Type
2480	var err error
2481	switch v := v.(type) {
2482	case nil:
2483	case string:
2484		pb.Kind = stringKind(v)
2485		pt = stringType()
2486	case NullString:
2487		if v.Valid {
2488			return encodeValue(v.StringVal)
2489		}
2490		pt = stringType()
2491	case []string:
2492		if v != nil {
2493			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2494			if err != nil {
2495				return nil, nil, err
2496			}
2497		}
2498		pt = listType(stringType())
2499	case []NullString:
2500		if v != nil {
2501			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2502			if err != nil {
2503				return nil, nil, err
2504			}
2505		}
2506		pt = listType(stringType())
2507	case *string:
2508		if v != nil {
2509			return encodeValue(*v)
2510		}
2511		pt = stringType()
2512	case []*string:
2513		if v != nil {
2514			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2515			if err != nil {
2516				return nil, nil, err
2517			}
2518		}
2519		pt = listType(stringType())
2520	case []byte:
2521		if v != nil {
2522			pb.Kind = stringKind(base64.StdEncoding.EncodeToString(v))
2523		}
2524		pt = bytesType()
2525	case [][]byte:
2526		if v != nil {
2527			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2528			if err != nil {
2529				return nil, nil, err
2530			}
2531		}
2532		pt = listType(bytesType())
2533	case int:
2534		pb.Kind = stringKind(strconv.FormatInt(int64(v), 10))
2535		pt = intType()
2536	case []int:
2537		if v != nil {
2538			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2539			if err != nil {
2540				return nil, nil, err
2541			}
2542		}
2543		pt = listType(intType())
2544	case int64:
2545		pb.Kind = stringKind(strconv.FormatInt(v, 10))
2546		pt = intType()
2547	case []int64:
2548		if v != nil {
2549			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2550			if err != nil {
2551				return nil, nil, err
2552			}
2553		}
2554		pt = listType(intType())
2555	case NullInt64:
2556		if v.Valid {
2557			return encodeValue(v.Int64)
2558		}
2559		pt = intType()
2560	case []NullInt64:
2561		if v != nil {
2562			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2563			if err != nil {
2564				return nil, nil, err
2565			}
2566		}
2567		pt = listType(intType())
2568	case *int64:
2569		if v != nil {
2570			return encodeValue(*v)
2571		}
2572		pt = intType()
2573	case []*int64:
2574		if v != nil {
2575			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2576			if err != nil {
2577				return nil, nil, err
2578			}
2579		}
2580		pt = listType(intType())
2581	case bool:
2582		pb.Kind = &proto3.Value_BoolValue{BoolValue: v}
2583		pt = boolType()
2584	case []bool:
2585		if v != nil {
2586			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2587			if err != nil {
2588				return nil, nil, err
2589			}
2590		}
2591		pt = listType(boolType())
2592	case NullBool:
2593		if v.Valid {
2594			return encodeValue(v.Bool)
2595		}
2596		pt = boolType()
2597	case []NullBool:
2598		if v != nil {
2599			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2600			if err != nil {
2601				return nil, nil, err
2602			}
2603		}
2604		pt = listType(boolType())
2605	case *bool:
2606		if v != nil {
2607			return encodeValue(*v)
2608		}
2609		pt = boolType()
2610	case []*bool:
2611		if v != nil {
2612			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2613			if err != nil {
2614				return nil, nil, err
2615			}
2616		}
2617		pt = listType(boolType())
2618	case float64:
2619		pb.Kind = &proto3.Value_NumberValue{NumberValue: v}
2620		pt = floatType()
2621	case []float64:
2622		if v != nil {
2623			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2624			if err != nil {
2625				return nil, nil, err
2626			}
2627		}
2628		pt = listType(floatType())
2629	case NullFloat64:
2630		if v.Valid {
2631			return encodeValue(v.Float64)
2632		}
2633		pt = floatType()
2634	case []NullFloat64:
2635		if v != nil {
2636			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2637			if err != nil {
2638				return nil, nil, err
2639			}
2640		}
2641		pt = listType(floatType())
2642	case *float64:
2643		if v != nil {
2644			return encodeValue(*v)
2645		}
2646		pt = floatType()
2647	case []*float64:
2648		if v != nil {
2649			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2650			if err != nil {
2651				return nil, nil, err
2652			}
2653		}
2654		pt = listType(floatType())
2655	case big.Rat:
2656		pb.Kind = stringKind(NumericString(&v))
2657		pt = numericType()
2658	case []big.Rat:
2659		if v != nil {
2660			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2661			if err != nil {
2662				return nil, nil, err
2663			}
2664		}
2665		pt = listType(numericType())
2666	case NullNumeric:
2667		if v.Valid {
2668			return encodeValue(v.Numeric)
2669		}
2670		pt = numericType()
2671	case []NullNumeric:
2672		if v != nil {
2673			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2674			if err != nil {
2675				return nil, nil, err
2676			}
2677		}
2678		pt = listType(numericType())
2679	case *big.Rat:
2680		if v != nil {
2681			pb.Kind = stringKind(NumericString(v))
2682		}
2683		pt = numericType()
2684	case []*big.Rat:
2685		if v != nil {
2686			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2687			if err != nil {
2688				return nil, nil, err
2689			}
2690		}
2691		pt = listType(numericType())
2692	case time.Time:
2693		if v == commitTimestamp {
2694			pb.Kind = stringKind(commitTimestampPlaceholderString)
2695		} else {
2696			pb.Kind = stringKind(v.UTC().Format(time.RFC3339Nano))
2697		}
2698		pt = timeType()
2699	case []time.Time:
2700		if v != nil {
2701			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2702			if err != nil {
2703				return nil, nil, err
2704			}
2705		}
2706		pt = listType(timeType())
2707	case NullTime:
2708		if v.Valid {
2709			return encodeValue(v.Time)
2710		}
2711		pt = timeType()
2712	case []NullTime:
2713		if v != nil {
2714			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2715			if err != nil {
2716				return nil, nil, err
2717			}
2718		}
2719		pt = listType(timeType())
2720	case *time.Time:
2721		if v != nil {
2722			return encodeValue(*v)
2723		}
2724		pt = timeType()
2725	case []*time.Time:
2726		if v != nil {
2727			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2728			if err != nil {
2729				return nil, nil, err
2730			}
2731		}
2732		pt = listType(timeType())
2733	case civil.Date:
2734		pb.Kind = stringKind(v.String())
2735		pt = dateType()
2736	case []civil.Date:
2737		if v != nil {
2738			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2739			if err != nil {
2740				return nil, nil, err
2741			}
2742		}
2743		pt = listType(dateType())
2744	case NullDate:
2745		if v.Valid {
2746			return encodeValue(v.Date)
2747		}
2748		pt = dateType()
2749	case []NullDate:
2750		if v != nil {
2751			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2752			if err != nil {
2753				return nil, nil, err
2754			}
2755		}
2756		pt = listType(dateType())
2757	case *civil.Date:
2758		if v != nil {
2759			return encodeValue(*v)
2760		}
2761		pt = dateType()
2762	case []*civil.Date:
2763		if v != nil {
2764			pb, err = encodeArray(len(v), func(i int) interface{} { return v[i] })
2765			if err != nil {
2766				return nil, nil, err
2767			}
2768		}
2769		pt = listType(dateType())
2770	case GenericColumnValue:
2771		// Deep clone to ensure subsequent changes to v before
2772		// transmission don't affect our encoded value.
2773		pb = proto.Clone(v.Value).(*proto3.Value)
2774		pt = proto.Clone(v.Type).(*sppb.Type)
2775	case []GenericColumnValue:
2776		return nil, nil, errEncoderUnsupportedType(v)
2777	default:
2778		// Check if the value is a custom type that implements spanner.Encoder
2779		// interface.
2780		if encodedVal, ok := v.(Encoder); ok {
2781			nv, err := encodedVal.EncodeSpanner()
2782			if err != nil {
2783				return nil, nil, err
2784			}
2785			return encodeValue(nv)
2786		}
2787
2788		// Check if the value is a variant of a base type.
2789		decodableType := getDecodableSpannerType(v, false)
2790		if decodableType != spannerTypeUnknown && decodableType != spannerTypeInvalid {
2791			converted, err := convertCustomTypeValue(decodableType, v)
2792			if err != nil {
2793				return nil, nil, err
2794			}
2795			return encodeValue(converted)
2796		}
2797
2798		if !isStructOrArrayOfStructValue(v) {
2799			return nil, nil, errEncoderUnsupportedType(v)
2800		}
2801		typ := reflect.TypeOf(v)
2802
2803		// Value is a Go struct value/ptr.
2804		if (typ.Kind() == reflect.Struct) ||
2805			(typ.Kind() == reflect.Ptr && typ.Elem().Kind() == reflect.Struct) {
2806			return encodeStruct(v)
2807		}
2808
2809		// Value is a slice of Go struct values/ptrs.
2810		if typ.Kind() == reflect.Slice {
2811			return encodeStructArray(v)
2812		}
2813	}
2814	return pb, pt, nil
2815}
2816
2817func convertCustomTypeValue(sourceType decodableSpannerType, v interface{}) (interface{}, error) {
2818	// destination will be initialized to a base type. The input value will be
2819	// converted to this type and copied to destination.
2820	var destination reflect.Value
2821	switch sourceType {
2822	case spannerTypeInvalid:
2823		return nil, fmt.Errorf("cannot encode a value to type spannerTypeInvalid")
2824	case spannerTypeNonNullString:
2825		destination = reflect.Indirect(reflect.New(reflect.TypeOf("")))
2826	case spannerTypeNullString:
2827		destination = reflect.Indirect(reflect.New(reflect.TypeOf(NullString{})))
2828	case spannerTypeByteArray:
2829		// Return a nil array directly if the input value is nil instead of
2830		// creating an empty slice and returning that.
2831		if reflect.ValueOf(v).IsNil() {
2832			return []byte(nil), nil
2833		}
2834		destination = reflect.MakeSlice(reflect.TypeOf([]byte{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2835	case spannerTypeNonNullInt64:
2836		destination = reflect.Indirect(reflect.New(reflect.TypeOf(int64(0))))
2837	case spannerTypeNullInt64:
2838		destination = reflect.Indirect(reflect.New(reflect.TypeOf(NullInt64{})))
2839	case spannerTypeNonNullBool:
2840		destination = reflect.Indirect(reflect.New(reflect.TypeOf(false)))
2841	case spannerTypeNullBool:
2842		destination = reflect.Indirect(reflect.New(reflect.TypeOf(NullBool{})))
2843	case spannerTypeNonNullFloat64:
2844		destination = reflect.Indirect(reflect.New(reflect.TypeOf(float64(0.0))))
2845	case spannerTypeNullFloat64:
2846		destination = reflect.Indirect(reflect.New(reflect.TypeOf(NullFloat64{})))
2847	case spannerTypeNonNullTime:
2848		destination = reflect.Indirect(reflect.New(reflect.TypeOf(time.Time{})))
2849	case spannerTypeNullTime:
2850		destination = reflect.Indirect(reflect.New(reflect.TypeOf(NullTime{})))
2851	case spannerTypeNonNullDate:
2852		destination = reflect.Indirect(reflect.New(reflect.TypeOf(civil.Date{})))
2853	case spannerTypeNullDate:
2854		destination = reflect.Indirect(reflect.New(reflect.TypeOf(NullDate{})))
2855	case spannerTypeNonNullNumeric:
2856		destination = reflect.Indirect(reflect.New(reflect.TypeOf(big.Rat{})))
2857	case spannerTypeNullNumeric:
2858		destination = reflect.Indirect(reflect.New(reflect.TypeOf(NullNumeric{})))
2859	case spannerTypeArrayOfNonNullString:
2860		if reflect.ValueOf(v).IsNil() {
2861			return []string(nil), nil
2862		}
2863		destination = reflect.MakeSlice(reflect.TypeOf([]string{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2864	case spannerTypeArrayOfNullString:
2865		if reflect.ValueOf(v).IsNil() {
2866			return []NullString(nil), nil
2867		}
2868		destination = reflect.MakeSlice(reflect.TypeOf([]NullString{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2869	case spannerTypeArrayOfByteArray:
2870		if reflect.ValueOf(v).IsNil() {
2871			return [][]byte(nil), nil
2872		}
2873		destination = reflect.MakeSlice(reflect.TypeOf([][]byte{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2874	case spannerTypeArrayOfNonNullInt64:
2875		if reflect.ValueOf(v).IsNil() {
2876			return []int64(nil), nil
2877		}
2878		destination = reflect.MakeSlice(reflect.TypeOf([]int64{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2879	case spannerTypeArrayOfNullInt64:
2880		if reflect.ValueOf(v).IsNil() {
2881			return []NullInt64(nil), nil
2882		}
2883		destination = reflect.MakeSlice(reflect.TypeOf([]NullInt64{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2884	case spannerTypeArrayOfNonNullBool:
2885		if reflect.ValueOf(v).IsNil() {
2886			return []bool(nil), nil
2887		}
2888		destination = reflect.MakeSlice(reflect.TypeOf([]bool{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2889	case spannerTypeArrayOfNullBool:
2890		if reflect.ValueOf(v).IsNil() {
2891			return []NullBool(nil), nil
2892		}
2893		destination = reflect.MakeSlice(reflect.TypeOf([]NullBool{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2894	case spannerTypeArrayOfNonNullFloat64:
2895		if reflect.ValueOf(v).IsNil() {
2896			return []float64(nil), nil
2897		}
2898		destination = reflect.MakeSlice(reflect.TypeOf([]float64{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2899	case spannerTypeArrayOfNullFloat64:
2900		if reflect.ValueOf(v).IsNil() {
2901			return []NullFloat64(nil), nil
2902		}
2903		destination = reflect.MakeSlice(reflect.TypeOf([]NullFloat64{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2904	case spannerTypeArrayOfNonNullTime:
2905		if reflect.ValueOf(v).IsNil() {
2906			return []time.Time(nil), nil
2907		}
2908		destination = reflect.MakeSlice(reflect.TypeOf([]time.Time{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2909	case spannerTypeArrayOfNullTime:
2910		if reflect.ValueOf(v).IsNil() {
2911			return []NullTime(nil), nil
2912		}
2913		destination = reflect.MakeSlice(reflect.TypeOf([]NullTime{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2914	case spannerTypeArrayOfNonNullDate:
2915		if reflect.ValueOf(v).IsNil() {
2916			return []civil.Date(nil), nil
2917		}
2918		destination = reflect.MakeSlice(reflect.TypeOf([]civil.Date{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2919	case spannerTypeArrayOfNullDate:
2920		if reflect.ValueOf(v).IsNil() {
2921			return []NullDate(nil), nil
2922		}
2923		destination = reflect.MakeSlice(reflect.TypeOf([]NullDate{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2924	case spannerTypeArrayOfNonNullNumeric:
2925		if reflect.ValueOf(v).IsNil() {
2926			return []big.Rat(nil), nil
2927		}
2928		destination = reflect.MakeSlice(reflect.TypeOf([]big.Rat{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2929	case spannerTypeArrayOfNullNumeric:
2930		if reflect.ValueOf(v).IsNil() {
2931			return []NullNumeric(nil), nil
2932		}
2933		destination = reflect.MakeSlice(reflect.TypeOf([]NullNumeric{}), reflect.ValueOf(v).Len(), reflect.ValueOf(v).Cap())
2934	default:
2935		// This should not be possible.
2936		return nil, fmt.Errorf("unknown decodable type found: %v", sourceType)
2937	}
2938	// destination has been initialized. Convert and copy the input value to
2939	// destination. That must be done per element if the input type is a slice
2940	// or an array.
2941	if destination.Kind() == reflect.Slice || destination.Kind() == reflect.Array {
2942		sourceSlice := reflect.ValueOf(v)
2943		for i := 0; i < destination.Len(); i++ {
2944			source := sourceSlice.Index(i)
2945			destination.Index(i).Set(source.Convert(destination.Type().Elem()))
2946		}
2947	} else {
2948		source := reflect.ValueOf(v)
2949		destination.Set(source.Convert(destination.Type()))
2950	}
2951	// Return the converted value.
2952	return destination.Interface(), nil
2953}
2954
2955// Encodes a Go struct value/ptr in v to the spanner Value and Type protos. v
2956// itself must be non-nil.
2957func encodeStruct(v interface{}) (*proto3.Value, *sppb.Type, error) {
2958	typ := reflect.TypeOf(v)
2959	val := reflect.ValueOf(v)
2960
2961	// Pointer to struct.
2962	if typ.Kind() == reflect.Ptr && typ.Elem().Kind() == reflect.Struct {
2963		typ = typ.Elem()
2964		if val.IsNil() {
2965			// nil pointer to struct, representing a NULL STRUCT value. Use a
2966			// dummy value to get the type.
2967			_, st, err := encodeStruct(reflect.Zero(typ).Interface())
2968			if err != nil {
2969				return nil, nil, err
2970			}
2971			return nullProto(), st, nil
2972		}
2973		val = val.Elem()
2974	}
2975
2976	if typ.Kind() != reflect.Struct {
2977		return nil, nil, errEncoderUnsupportedType(v)
2978	}
2979
2980	stf := make([]*sppb.StructType_Field, 0, typ.NumField())
2981	stv := make([]*proto3.Value, 0, typ.NumField())
2982
2983	for i := 0; i < typ.NumField(); i++ {
2984		// If the field has a 'spanner' tag, use the value of that tag as the field name.
2985		// This is used to build STRUCT types with unnamed/duplicate fields.
2986		sf := typ.Field(i)
2987		fval := val.Field(i)
2988
2989		// Embedded fields are not allowed.
2990		if sf.Anonymous {
2991			return nil, nil, errUnsupportedEmbeddedStructFields(sf.Name)
2992		}
2993
2994		// Unexported fields are ignored.
2995		if !fval.CanInterface() {
2996			continue
2997		}
2998
2999		fname, ok := sf.Tag.Lookup("spanner")
3000		if !ok {
3001			fname = sf.Name
3002		}
3003
3004		eval, etype, err := encodeValue(fval.Interface())
3005		if err != nil {
3006			return nil, nil, err
3007		}
3008
3009		stf = append(stf, mkField(fname, etype))
3010		stv = append(stv, eval)
3011	}
3012
3013	return listProto(stv...), structType(stf...), nil
3014}
3015
3016// Encodes a slice of Go struct values/ptrs in v to the spanner Value and Type
3017// protos. v itself must be non-nil.
3018func encodeStructArray(v interface{}) (*proto3.Value, *sppb.Type, error) {
3019	etyp := reflect.TypeOf(v).Elem()
3020	sliceval := reflect.ValueOf(v)
3021
3022	// Slice of pointers to structs.
3023	if etyp.Kind() == reflect.Ptr {
3024		etyp = etyp.Elem()
3025	}
3026
3027	// Use a dummy struct value to get the element type.
3028	_, elemTyp, err := encodeStruct(reflect.Zero(etyp).Interface())
3029	if err != nil {
3030		return nil, nil, err
3031	}
3032
3033	// nil slice represents a NULL array-of-struct.
3034	if sliceval.IsNil() {
3035		return nullProto(), listType(elemTyp), nil
3036	}
3037
3038	values := make([]*proto3.Value, 0, sliceval.Len())
3039
3040	for i := 0; i < sliceval.Len(); i++ {
3041		ev, _, err := encodeStruct(sliceval.Index(i).Interface())
3042		if err != nil {
3043			return nil, nil, err
3044		}
3045		values = append(values, ev)
3046	}
3047	return listProto(values...), listType(elemTyp), nil
3048}
3049
3050func isStructOrArrayOfStructValue(v interface{}) bool {
3051	typ := reflect.TypeOf(v)
3052	if typ.Kind() == reflect.Slice {
3053		typ = typ.Elem()
3054	}
3055	if typ.Kind() == reflect.Ptr {
3056		typ = typ.Elem()
3057	}
3058	return typ.Kind() == reflect.Struct
3059}
3060
3061func isSupportedMutationType(v interface{}) bool {
3062	switch v.(type) {
3063	case nil, string, *string, NullString, []string, []*string, []NullString,
3064		[]byte, [][]byte,
3065		int, []int, int64, *int64, []int64, []*int64, NullInt64, []NullInt64,
3066		bool, *bool, []bool, []*bool, NullBool, []NullBool,
3067		float64, *float64, []float64, []*float64, NullFloat64, []NullFloat64,
3068		time.Time, *time.Time, []time.Time, []*time.Time, NullTime, []NullTime,
3069		civil.Date, *civil.Date, []civil.Date, []*civil.Date, NullDate, []NullDate,
3070		GenericColumnValue:
3071		return true
3072	default:
3073		// Check if the custom type implements spanner.Encoder interface.
3074		if _, ok := v.(Encoder); ok {
3075			return true
3076		}
3077
3078		decodableType := getDecodableSpannerType(v, false)
3079		return decodableType != spannerTypeUnknown && decodableType != spannerTypeInvalid
3080	}
3081}
3082
3083// encodeValueArray encodes a Value array into a proto3.ListValue.
3084func encodeValueArray(vs []interface{}) (*proto3.ListValue, error) {
3085	lv := &proto3.ListValue{}
3086	lv.Values = make([]*proto3.Value, 0, len(vs))
3087	for _, v := range vs {
3088		if !isSupportedMutationType(v) {
3089			return nil, errEncoderUnsupportedType(v)
3090		}
3091		pb, _, err := encodeValue(v)
3092		if err != nil {
3093			return nil, err
3094		}
3095		lv.Values = append(lv.Values, pb)
3096	}
3097	return lv, nil
3098}
3099
3100// encodeArray assumes that all values of the array element type encode without
3101// error.
3102func encodeArray(len int, at func(int) interface{}) (*proto3.Value, error) {
3103	vs := make([]*proto3.Value, len)
3104	var err error
3105	for i := 0; i < len; i++ {
3106		vs[i], _, err = encodeValue(at(i))
3107		if err != nil {
3108			return nil, err
3109		}
3110	}
3111	return listProto(vs...), nil
3112}
3113
3114func spannerTagParser(t reflect.StructTag) (name string, keep bool, other interface{}, err error) {
3115	if s := t.Get("spanner"); s != "" {
3116		if s == "-" {
3117			return "", false, nil, nil
3118		}
3119		return s, true, nil, nil
3120	}
3121	return "", true, nil, nil
3122}
3123
3124var fieldCache = fields.NewCache(spannerTagParser, nil, nil)
3125
3126func trimDoubleQuotes(payload []byte) ([]byte, error) {
3127	if len(payload) <= 1 || payload[0] != '"' || payload[len(payload)-1] != '"' {
3128		return nil, fmt.Errorf("payload is too short or not wrapped with double quotes: got %q", string(payload))
3129	}
3130	// Remove the double quotes at the beginning and the end.
3131	return payload[1 : len(payload)-1], nil
3132}
3133