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