github.com/cornelk/go-cloud@v0.17.1/docstore/driver/codec.go (about)

     1  // Copyright 2019 The Go Cloud Development Kit Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // TODO(jba): support struct tags.
    16  // TODO(jba): for efficiency, enable encoding of only a subset of field paths.
    17  
    18  package driver
    19  
    20  import (
    21  	"encoding"
    22  	"fmt"
    23  	"reflect"
    24  	"strconv"
    25  
    26  	"github.com/cornelk/go-cloud/docstore/internal/fields"
    27  	"github.com/cornelk/go-cloud/internal/gcerr"
    28  	"github.com/golang/protobuf/proto"
    29  )
    30  
    31  var (
    32  	binaryMarshalerType   = reflect.TypeOf((*encoding.BinaryMarshaler)(nil)).Elem()
    33  	binaryUnmarshalerType = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem()
    34  	textMarshalerType     = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
    35  	textUnmarshalerType   = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
    36  	protoMessageType      = reflect.TypeOf((*proto.Message)(nil)).Elem()
    37  )
    38  
    39  // An Encoder encodes Go values in some other form (e.g. JSON, protocol buffers).
    40  // The encoding protocol is designed to avoid losing type information by passing
    41  // values using interface{}. An Encoder is responsible for storing the value
    42  // it is encoding.
    43  //
    44  // Because all drivers must support the same set of values, the encoding methods
    45  // (with the exception of EncodeStruct) do not return errors. EncodeStruct is special
    46  // because it is an escape hatch for arbitrary structs, not all of which may be
    47  // encodable.
    48  type Encoder interface {
    49  	// These methods all encode and store a single Go value.
    50  	EncodeNil()
    51  	EncodeBool(bool)
    52  	EncodeString(string)
    53  	EncodeInt(int64)
    54  	EncodeUint(uint64)
    55  	EncodeFloat(float64)
    56  	EncodeBytes([]byte)
    57  
    58  	// EncodeList is called when a slice or array is encountered (except for a
    59  	// []byte, which is handled by EncodeBytes). Its argument is the length of the
    60  	// slice or array. The encoding algorithm will call the returned Encoder that
    61  	// many times to encode the successive values of the list. After each such call,
    62  	// ListIndex will be called with the index of the element just encoded.
    63  	//
    64  	// For example, []string{"a", "b"} will result in these calls:
    65  	//     enc2 := enc.EncodeList(2)
    66  	//     enc2.EncodeString("a")
    67  	//     enc2.ListIndex(0)
    68  	//     enc2.EncodeString("b")
    69  	//     enc2.ListIndex(1)
    70  	EncodeList(n int) Encoder
    71  	ListIndex(i int)
    72  
    73  	// EncodeMap is called when a map is encountered. Its argument is the number of
    74  	// fields in the map. The encoding algorithm will call the returned Encoder that
    75  	// many times to encode the successive values of the map. After each such call,
    76  	// MapKey will be called with the key of the element just encoded.
    77  	//
    78  	// For example, map[string}int{"A": 1, "B": 2} will result in these calls:
    79  	//     enc2 := enc.EncodeMap(2)
    80  	//     enc2.EncodeInt(1)
    81  	//     enc2.MapKey("A")
    82  	//     enc2.EncodeInt(2)
    83  	//     enc2.MapKey("B")
    84  	//
    85  	// EncodeMap is also called for structs. The map then consists of the exported
    86  	// fields of the struct. For struct{A, B int}{1, 2}, if EncodeStruct returns
    87  	// false, the same sequence of calls as above will occur.
    88  	EncodeMap(n int) Encoder
    89  	MapKey(string)
    90  
    91  	// If the encoder wants to encode a value in a special way it should do so here
    92  	// and return true along with any error from the encoding. Otherwise, it should
    93  	// return false.
    94  	EncodeSpecial(v reflect.Value) (bool, error)
    95  }
    96  
    97  // Encode encodes the value using the given Encoder. It traverses the value,
    98  // iterating over arrays, slices, maps and the exported fields of structs. If it
    99  // encounters a non-nil pointer, it encodes the value that it points to.
   100  // Encode treats a few interfaces specially:
   101  //
   102  // If the value implements encoding.BinaryMarshaler, Encode invokes MarshalBinary
   103  // on it and encodes the resulting byte slice.
   104  //
   105  // If the value implements encoding.TextMarshaler, Encode invokes MarshalText on it
   106  // and encodes the resulting string.
   107  //
   108  // If the value implements proto.Message, Encode invokes proto.Marshal on it and encodes
   109  // the resulting byte slice. Here proto is the package "github.com/golang/protobuf/proto".
   110  //
   111  // Not every map key type can be encoded. Only strings, integers (signed or
   112  // unsigned), and types that implement encoding.TextMarshaler are permitted as map
   113  // keys. These restrictions match exactly those of the encoding/json package.
   114  func Encode(v reflect.Value, e Encoder) error {
   115  	return wrap(encode(v, e), gcerr.InvalidArgument)
   116  }
   117  
   118  func encode(v reflect.Value, enc Encoder) error {
   119  	if !v.IsValid() {
   120  		enc.EncodeNil()
   121  		return nil
   122  	}
   123  	done, err := enc.EncodeSpecial(v)
   124  	if done {
   125  		return err
   126  	}
   127  	if v.Type().Implements(binaryMarshalerType) {
   128  		bytes, err := v.Interface().(encoding.BinaryMarshaler).MarshalBinary()
   129  		if err != nil {
   130  			return err
   131  		}
   132  		enc.EncodeBytes(bytes)
   133  		return nil
   134  	}
   135  	if v.Type().Implements(protoMessageType) {
   136  		if v.IsNil() {
   137  			enc.EncodeNil()
   138  		} else {
   139  			bytes, err := proto.Marshal(v.Interface().(proto.Message))
   140  			if err != nil {
   141  				return err
   142  			}
   143  			enc.EncodeBytes(bytes)
   144  		}
   145  		return nil
   146  	}
   147  	if v.Type().Implements(textMarshalerType) {
   148  		bytes, err := v.Interface().(encoding.TextMarshaler).MarshalText()
   149  		if err != nil {
   150  			return err
   151  		}
   152  		enc.EncodeString(string(bytes))
   153  		return nil
   154  	}
   155  	switch v.Kind() {
   156  	case reflect.Bool:
   157  		enc.EncodeBool(v.Bool())
   158  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   159  		enc.EncodeInt(v.Int())
   160  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   161  		enc.EncodeUint(v.Uint())
   162  	case reflect.Float32, reflect.Float64:
   163  		enc.EncodeFloat(v.Float())
   164  	case reflect.String:
   165  		enc.EncodeString(v.String())
   166  	case reflect.Slice:
   167  		if v.IsNil() {
   168  			enc.EncodeNil()
   169  			return nil
   170  		}
   171  		fallthrough
   172  	case reflect.Array:
   173  		return encodeList(v, enc)
   174  	case reflect.Map:
   175  		return encodeMap(v, enc)
   176  	case reflect.Ptr:
   177  		if v.IsNil() {
   178  			enc.EncodeNil()
   179  			return nil
   180  		}
   181  		return encode(v.Elem(), enc)
   182  	case reflect.Interface:
   183  		if v.IsNil() {
   184  			enc.EncodeNil()
   185  			return nil
   186  		}
   187  		return encode(v.Elem(), enc)
   188  
   189  	case reflect.Struct:
   190  		fields, err := fieldCache.Fields(v.Type())
   191  		if err != nil {
   192  			return err
   193  		}
   194  		return encodeStructWithFields(v, fields, enc)
   195  
   196  	default:
   197  		return gcerr.Newf(gcerr.InvalidArgument, nil, "cannot encode type %s", v.Type())
   198  	}
   199  	return nil
   200  }
   201  
   202  // Encode an array or non-nil slice.
   203  func encodeList(v reflect.Value, enc Encoder) error {
   204  	// Byte slices encode specially.
   205  	if v.Type().Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 {
   206  		enc.EncodeBytes(v.Bytes())
   207  		return nil
   208  	}
   209  	n := v.Len()
   210  	enc2 := enc.EncodeList(n)
   211  	for i := 0; i < n; i++ {
   212  		if err := encode(v.Index(i), enc2); err != nil {
   213  			return err
   214  		}
   215  		enc2.ListIndex(i)
   216  	}
   217  	return nil
   218  }
   219  
   220  // Encode a map.
   221  func encodeMap(v reflect.Value, enc Encoder) error {
   222  	if v.IsNil() {
   223  		enc.EncodeNil()
   224  		return nil
   225  	}
   226  	keys := v.MapKeys()
   227  	enc2 := enc.EncodeMap(len(keys))
   228  	for _, k := range keys {
   229  		sk, err := stringifyMapKey(k)
   230  		if err != nil {
   231  			return err
   232  		}
   233  		if err := encode(v.MapIndex(k), enc2); err != nil {
   234  			return err
   235  		}
   236  		enc2.MapKey(sk)
   237  	}
   238  	return nil
   239  }
   240  
   241  // k is the key of a map. Encode it as a string.
   242  // Only strings, integers (signed or unsigned), and types that implement
   243  // encoding.TextMarshaler are supported.
   244  func stringifyMapKey(k reflect.Value) (string, error) {
   245  	// This is basically reflectWithString.resolve, from encoding/json/encode.go.
   246  	if k.Kind() == reflect.String {
   247  		return k.String(), nil
   248  	}
   249  	if tm, ok := k.Interface().(encoding.TextMarshaler); ok {
   250  		b, err := tm.MarshalText()
   251  		if err != nil {
   252  			return "", err
   253  		}
   254  		return string(b), nil
   255  	}
   256  	switch k.Kind() {
   257  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   258  		return strconv.FormatInt(k.Int(), 10), nil
   259  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   260  		return strconv.FormatUint(k.Uint(), 10), nil
   261  	default:
   262  		return "", gcerr.Newf(gcerr.InvalidArgument, nil, "cannot encode key %v of type %s", k, k.Type())
   263  	}
   264  }
   265  
   266  func encodeStructWithFields(v reflect.Value, fields fields.List, e Encoder) error {
   267  	e2 := e.EncodeMap(len(fields))
   268  	for _, f := range fields {
   269  		fv, ok := fieldByIndex(v, f.Index)
   270  		if !ok {
   271  			// if !ok, then f is a field in an embedded pointer to struct, and that embedded pointer
   272  			// is nil in v. In other words, the field exists in the struct type, but not this particular
   273  			// struct value. So we just ignore it.
   274  			continue
   275  		}
   276  		if f.ParsedTag.(tagOptions).omitEmpty && IsEmptyValue(fv) {
   277  			continue
   278  		}
   279  		if err := encode(fv, e2); err != nil {
   280  			return err
   281  		}
   282  		e2.MapKey(f.Name)
   283  	}
   284  	return nil
   285  }
   286  
   287  // fieldByIndex retrieves the field of v at the given index if present.
   288  // v must be a struct. index must refer to a valid field of v's type.
   289  // The second return value is false if there is a nil embedded pointer
   290  // along the path denoted by index.
   291  //
   292  // From encoding/json/encode.go.
   293  func fieldByIndex(v reflect.Value, index []int) (reflect.Value, bool) {
   294  	for _, i := range index {
   295  		if v.Kind() == reflect.Ptr {
   296  			if v.IsNil() {
   297  				return reflect.Value{}, false
   298  			}
   299  			v = v.Elem()
   300  		}
   301  		v = v.Field(i)
   302  	}
   303  	return v, true
   304  }
   305  
   306  ////////////////////////////////////////////////////////////////
   307  
   308  // TODO(jba): consider a fast path: if we are decoding into a struct, assume the same struct
   309  // was used to encode. Then we can build a map from field names to functions, where each
   310  // function avoids all the tests of Decode and contains just the code for setting the field.
   311  
   312  // TODO(jba): provide a way to override the check on missing fields.
   313  
   314  // A Decoder decodes data that was produced by Encode back into Go values.
   315  // Each Decoder instance is responsible for decoding one value.
   316  type Decoder interface {
   317  	// The AsXXX methods each report whether the value being decoded can be represented as
   318  	// a particular Go type. If so, the method should return the value as that type, and true;
   319  	// otherwise it should return the zero value and false.
   320  	AsString() (string, bool)
   321  	AsInt() (int64, bool)
   322  	AsUint() (uint64, bool)
   323  	AsFloat() (float64, bool)
   324  	AsBytes() ([]byte, bool)
   325  	AsBool() (bool, bool)
   326  	AsNull() bool
   327  
   328  	// ListLen should return the length of the value being decoded and true, if the
   329  	// value can be decoded into a slice or array. Otherwise, ListLen should return
   330  	// (0, false).
   331  	ListLen() (int, bool)
   332  
   333  	// If ListLen returned true, then DecodeList will be called. It should iterate
   334  	// over the value being decoded in sequence from index 0, invoking the callback
   335  	// for each element with the element's index and a Decoder for the element.
   336  	// If the callback returns false, DecodeList should return immediately.
   337  	DecodeList(func(int, Decoder) bool)
   338  
   339  	// MapLen should return the number of fields of the value being decoded and true,
   340  	// if the value can be decoded into a map or struct. Otherwise, it should return
   341  	// (0, false).
   342  	MapLen() (int, bool)
   343  
   344  	// DecodeMap iterates over the fields of the value being decoded, invoke the
   345  	// callback on each with field name, a Decoder for the field value, and a bool
   346  	// to indicate whether or not to use exact match for the field names. It will
   347  	// be called when MapLen returns true or decoding a struct. If the callback
   348  	// returns false, DecodeMap should return immediately.
   349  	DecodeMap(func(string, Decoder, bool) bool)
   350  
   351  	// AsInterface should decode the value into the Go value that best represents it.
   352  	AsInterface() (interface{}, error)
   353  
   354  	// If the decoder wants to decode a value in a special way it should do so here
   355  	// and return true, the decoded value, and any error from the decoding.
   356  	// Otherwise, it should return false.
   357  	AsSpecial(reflect.Value) (bool, interface{}, error)
   358  
   359  	// String should return a human-readable representation of the Decoder, for error messages.
   360  	String() string
   361  }
   362  
   363  // Decode decodes the value held in the Decoder d into v.
   364  // Decode creates slices, maps and pointer elements as needed.
   365  // It treats values that implement encoding.BinaryUnmarshaler,
   366  // encoding.TextUnmarshaler and proto.Message specially; see Encode.
   367  func Decode(v reflect.Value, d Decoder) error {
   368  	return wrap(decode(v, d), gcerr.InvalidArgument)
   369  }
   370  
   371  func decode(v reflect.Value, d Decoder) error {
   372  	if !v.CanSet() {
   373  		return fmt.Errorf("while decoding: cannot set %+v", v)
   374  	}
   375  	// A Null value sets anything nullable to nil.
   376  	// If the value isn't nullable, we keep going.
   377  	// TODO(jba): should we treat decoding a null into a non-nullable as an error, or
   378  	// ignore it like encoding/json does?
   379  	if d.AsNull() {
   380  		switch v.Kind() {
   381  		case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
   382  			v.Set(reflect.Zero(v.Type()))
   383  			return nil
   384  		}
   385  	}
   386  
   387  	if done, val, err := d.AsSpecial(v); done {
   388  		if err != nil {
   389  			return err
   390  		}
   391  		v.Set(reflect.ValueOf(val))
   392  		return nil
   393  	}
   394  
   395  	// Handle implemented interfaces first.
   396  	if reflect.PtrTo(v.Type()).Implements(binaryUnmarshalerType) {
   397  		if b, ok := d.AsBytes(); ok {
   398  			return v.Addr().Interface().(encoding.BinaryUnmarshaler).UnmarshalBinary(b)
   399  		}
   400  		return decodingError(v, d)
   401  	}
   402  	if reflect.PtrTo(v.Type()).Implements(protoMessageType) {
   403  		if b, ok := d.AsBytes(); ok {
   404  			return proto.Unmarshal(b, v.Addr().Interface().(proto.Message))
   405  		}
   406  		return decodingError(v, d)
   407  	}
   408  	if reflect.PtrTo(v.Type()).Implements(textUnmarshalerType) {
   409  		if s, ok := d.AsString(); ok {
   410  			return v.Addr().Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(s))
   411  		}
   412  		return decodingError(v, d)
   413  	}
   414  
   415  	switch v.Kind() {
   416  	case reflect.Bool:
   417  		if b, ok := d.AsBool(); ok {
   418  			v.SetBool(b)
   419  			return nil
   420  		}
   421  
   422  	case reflect.String:
   423  		if s, ok := d.AsString(); ok {
   424  			v.SetString(s)
   425  			return nil
   426  		}
   427  
   428  	case reflect.Float32, reflect.Float64:
   429  		if f, ok := d.AsFloat(); ok {
   430  			v.SetFloat(f)
   431  			return nil
   432  		}
   433  
   434  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   435  		i, ok := d.AsInt()
   436  		if !ok {
   437  			// Accept a floating-point number with integral value.
   438  			f, ok := d.AsFloat()
   439  			if !ok {
   440  				return decodingError(v, d)
   441  			}
   442  			i = int64(f)
   443  			if float64(i) != f {
   444  				return gcerr.Newf(gcerr.InvalidArgument, nil, "float %f does not fit into %s", f, v.Type())
   445  			}
   446  		}
   447  		if v.OverflowInt(i) {
   448  			return overflowError(i, v.Type())
   449  		}
   450  		v.SetInt(i)
   451  		return nil
   452  
   453  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   454  		u, ok := d.AsUint()
   455  		if !ok {
   456  			// Accept a floating-point number with integral value.
   457  			f, ok := d.AsFloat()
   458  			if !ok {
   459  				return decodingError(v, d)
   460  			}
   461  			u = uint64(f)
   462  			if float64(u) != f {
   463  				return gcerr.Newf(gcerr.InvalidArgument, nil, "float %f does not fit into %s", f, v.Type())
   464  			}
   465  		}
   466  		if v.OverflowUint(u) {
   467  			return overflowError(u, v.Type())
   468  		}
   469  		v.SetUint(u)
   470  		return nil
   471  
   472  	case reflect.Slice, reflect.Array:
   473  		return decodeList(v, d)
   474  
   475  	case reflect.Map:
   476  		return decodeMap(v, d)
   477  
   478  	case reflect.Ptr:
   479  		// If the pointer is nil, set it to a zero value.
   480  		if v.IsNil() {
   481  			v.Set(reflect.New(v.Type().Elem()))
   482  		}
   483  		return decode(v.Elem(), d)
   484  
   485  	case reflect.Struct:
   486  		return decodeStruct(v, d)
   487  
   488  	case reflect.Interface:
   489  		if v.NumMethod() == 0 { // empty interface
   490  			// If v holds a pointer, set the pointer.
   491  			if !v.IsNil() && v.Elem().Kind() == reflect.Ptr {
   492  				return decode(v.Elem(), d)
   493  			}
   494  			// Otherwise, create a fresh value.
   495  			x, err := d.AsInterface()
   496  			if err != nil {
   497  				return err
   498  			}
   499  			v.Set(reflect.ValueOf(x))
   500  			return nil
   501  		}
   502  		// Any other kind of interface is an error???
   503  	}
   504  
   505  	return decodingError(v, d)
   506  }
   507  
   508  func decodeList(v reflect.Value, d Decoder) error {
   509  	// If we're decoding into a byte slice or array, and the decoded value
   510  	// supports that, then do the decoding.
   511  	if v.Type().Elem().Kind() == reflect.Uint8 {
   512  		if b, ok := d.AsBytes(); ok {
   513  			v.SetBytes(b)
   514  			return nil
   515  		}
   516  		// Fall through to decode the []byte as an ordinary slice.
   517  	}
   518  	dlen, ok := d.ListLen()
   519  	if !ok {
   520  		return decodingError(v, d)
   521  	}
   522  	err := prepareLength(v, dlen)
   523  	if err != nil {
   524  		return err
   525  	}
   526  	d.DecodeList(func(i int, vd Decoder) bool {
   527  		if err != nil || i >= dlen {
   528  			return false
   529  		}
   530  		err = decode(v.Index(i), vd)
   531  		return err == nil
   532  	})
   533  	return err
   534  }
   535  
   536  // v must be a slice or array. We want it to be of length wantLen. Prepare it as
   537  // necessary (details described in the code below), and return its resulting length.
   538  // If an array is too short, return an error. This behavior differs from
   539  // encoding/json, which just populates a short array with whatever it can and drops
   540  // the rest. That can lose data.
   541  func prepareLength(v reflect.Value, wantLen int) error {
   542  	vLen := v.Len()
   543  	if v.Kind() == reflect.Slice {
   544  		// Construct a slice of the right size, avoiding allocation if possible.
   545  		switch {
   546  		case vLen < wantLen: // v too short
   547  			if v.Cap() >= wantLen { // extend its length if there's room
   548  				v.SetLen(wantLen)
   549  			} else { // else make a new one
   550  				v.Set(reflect.MakeSlice(v.Type(), wantLen, wantLen))
   551  			}
   552  		case vLen > wantLen: // v too long; truncate it
   553  			v.SetLen(wantLen)
   554  		}
   555  	} else { // array
   556  		switch {
   557  		case vLen < wantLen: // v too short
   558  			return gcerr.Newf(gcerr.InvalidArgument, nil, "array length %d is too short for incoming list of length %d",
   559  				vLen, wantLen)
   560  		case vLen > wantLen: // v too long; set extra elements to zero
   561  			z := reflect.Zero(v.Type().Elem())
   562  			for i := wantLen; i < vLen; i++ {
   563  				v.Index(i).Set(z)
   564  			}
   565  		}
   566  	}
   567  	return nil
   568  }
   569  
   570  // Since a map value is not settable via reflection, this function always creates a
   571  // new element for each corresponding map key. Existing values of v are overwritten.
   572  // This happens even if the map value is something like a pointer to a struct, where
   573  // we could in theory populate the existing struct value instead of discarding it.
   574  // This behavior matches encoding/json.
   575  func decodeMap(v reflect.Value, d Decoder) error {
   576  	mapLen, ok := d.MapLen()
   577  	if !ok {
   578  		return decodingError(v, d)
   579  	}
   580  	t := v.Type()
   581  	if v.IsNil() {
   582  		v.Set(reflect.MakeMapWithSize(t, mapLen))
   583  	}
   584  	et := t.Elem()
   585  	var err error
   586  	kt := v.Type().Key()
   587  	d.DecodeMap(func(key string, vd Decoder, _ bool) bool {
   588  		if err != nil {
   589  			return false
   590  		}
   591  		el := reflect.New(et).Elem()
   592  		err = decode(el, vd)
   593  		if err != nil {
   594  			return false
   595  		}
   596  		vk, e := unstringifyMapKey(key, kt)
   597  		if e != nil {
   598  			err = e
   599  			return false
   600  		}
   601  		v.SetMapIndex(vk, el)
   602  		return err == nil
   603  	})
   604  	return err
   605  }
   606  
   607  // Given a map key encoded as a string, and the type of the map key, convert the key
   608  // into the type.
   609  // For example, if we are decoding the key "3" for a map[int]interface{}, then key is "3"
   610  // and keyType is reflect.Int.
   611  func unstringifyMapKey(key string, keyType reflect.Type) (reflect.Value, error) {
   612  	// This code is mostly from the middle of decodeState.object in encoding/json/decode.go.
   613  	// Except for literalStore, which I don't understand.
   614  	// TODO(jba): understand literalStore.
   615  	switch {
   616  	case keyType.Kind() == reflect.String:
   617  		return reflect.ValueOf(key).Convert(keyType), nil
   618  	case reflect.PtrTo(keyType).Implements(textUnmarshalerType):
   619  		tu := reflect.New(keyType)
   620  		if err := tu.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(key)); err != nil {
   621  			return reflect.Value{}, err
   622  		}
   623  		return tu.Elem(), nil
   624  	case keyType.Kind() == reflect.Interface && keyType.NumMethod() == 0:
   625  		// TODO: remove this case? encoding/json doesn't support it.
   626  		return reflect.ValueOf(key), nil
   627  	default:
   628  		switch keyType.Kind() {
   629  		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   630  			n, err := strconv.ParseInt(key, 10, 64)
   631  			if err != nil {
   632  				return reflect.Value{}, err
   633  			}
   634  			if reflect.Zero(keyType).OverflowInt(n) {
   635  				return reflect.Value{}, overflowError(n, keyType)
   636  			}
   637  			return reflect.ValueOf(n).Convert(keyType), nil
   638  		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   639  			n, err := strconv.ParseUint(key, 10, 64)
   640  			if err != nil {
   641  				return reflect.Value{}, err
   642  			}
   643  			if reflect.Zero(keyType).OverflowUint(n) {
   644  				return reflect.Value{}, overflowError(n, keyType)
   645  			}
   646  			return reflect.ValueOf(n).Convert(keyType), nil
   647  		default:
   648  			return reflect.Value{}, gcerr.Newf(gcerr.InvalidArgument, nil, "invalid key type %s", keyType)
   649  		}
   650  	}
   651  }
   652  
   653  func decodeStruct(v reflect.Value, d Decoder) error {
   654  	fs, err := fieldCache.Fields(v.Type())
   655  	if err != nil {
   656  		return err
   657  	}
   658  	d.DecodeMap(func(key string, d2 Decoder, exactMatch bool) bool {
   659  		if err != nil {
   660  			return false
   661  		}
   662  		var f *fields.Field
   663  		if exactMatch {
   664  			f = fs.MatchExact(key)
   665  		} else {
   666  			f = fs.MatchFold(key)
   667  		}
   668  		if f == nil {
   669  			err = gcerr.Newf(gcerr.InvalidArgument, nil, "no field matching %q in %s", key, v.Type())
   670  			return false
   671  		}
   672  		fv, ok := fieldByIndexCreate(v, f.Index)
   673  		if !ok {
   674  			err = gcerr.Newf(gcerr.InvalidArgument, nil,
   675  				"setting field %q in %s: cannot create embedded pointer field of unexported type",
   676  				key, v.Type())
   677  			return false
   678  		}
   679  		err = decode(fv, d2)
   680  		return err == nil
   681  	})
   682  	return err
   683  }
   684  
   685  // fieldByIndexCreate retrieves the the field of v at the given index if present,
   686  // creating embedded struct pointers where necessary.
   687  // v must be a struct. index must refer to a valid field of v's type.
   688  // The second return value is false If there is a nil embedded pointer of unexported
   689  // type along the path denoted by index. (We cannot create such pointers.)
   690  func fieldByIndexCreate(v reflect.Value, index []int) (reflect.Value, bool) {
   691  	for _, i := range index {
   692  		if v.Kind() == reflect.Ptr {
   693  			if v.IsNil() {
   694  				if !v.CanSet() {
   695  					return reflect.Value{}, false
   696  				}
   697  				v.Set(reflect.New(v.Type().Elem()))
   698  			}
   699  			v = v.Elem()
   700  		}
   701  		v = v.Field(i)
   702  	}
   703  	return v, true
   704  }
   705  
   706  func decodingError(v reflect.Value, d Decoder) error {
   707  	return gcerr.Newf(gcerr.InvalidArgument, nil, "cannot set type %s to %v", v.Type(), d)
   708  }
   709  
   710  func overflowError(x interface{}, t reflect.Type) error {
   711  	return gcerr.Newf(gcerr.InvalidArgument, nil, "value %v overflows type %s", x, t)
   712  }
   713  
   714  func wrap(err error, code gcerr.ErrorCode) error {
   715  	if _, ok := err.(*gcerr.Error); !ok && err != nil {
   716  		err = gcerr.New(code, err, 2, err.Error())
   717  	}
   718  	return err
   719  }
   720  
   721  var fieldCache = fields.NewCache(parseTag, nil, nil)
   722  
   723  // IsEmptyValue returns whether or not v is a zero value of its type.
   724  // Copied from encoding/json, go 1.12.
   725  func IsEmptyValue(v reflect.Value) bool {
   726  	switch k := v.Kind(); k {
   727  	case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
   728  		return v.Len() == 0
   729  	case reflect.Bool:
   730  		return !v.Bool()
   731  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   732  		return v.Int() == 0
   733  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   734  		return v.Uint() == 0
   735  	case reflect.Float32, reflect.Float64:
   736  		return v.Float() == 0
   737  	case reflect.Interface, reflect.Ptr:
   738  		return v.IsNil()
   739  	}
   740  	return false
   741  }
   742  
   743  // Options for struct tags.
   744  type tagOptions struct {
   745  	omitEmpty bool // do not encode value if empty
   746  }
   747  
   748  // parseTag interprets docstore struct field tags.
   749  func parseTag(t reflect.StructTag) (name string, keep bool, other interface{}, err error) {
   750  	var opts []string
   751  	name, keep, opts = fields.ParseStandardTag("docstore", t)
   752  	tagOpts := tagOptions{}
   753  	for _, opt := range opts {
   754  		switch opt {
   755  		case "omitempty":
   756  			tagOpts.omitEmpty = true
   757  		default:
   758  			return "", false, nil, gcerr.Newf(gcerr.InvalidArgument, nil, "unknown tag option: %q", opt)
   759  		}
   760  	}
   761  	return name, keep, tagOpts, nil
   762  }