vendor/github.com/pelletier/go-toml/v2/unmarshaler.go
changeset 260 445e01aede7e
child 262 8d3354485fc3
equal deleted inserted replaced
259:db4911b0c721 260:445e01aede7e
       
     1 package toml
       
     2 
       
     3 import (
       
     4 	"encoding"
       
     5 	"errors"
       
     6 	"fmt"
       
     7 	"io"
       
     8 	"io/ioutil"
       
     9 	"math"
       
    10 	"reflect"
       
    11 	"strings"
       
    12 	"sync/atomic"
       
    13 	"time"
       
    14 
       
    15 	"github.com/pelletier/go-toml/v2/internal/ast"
       
    16 	"github.com/pelletier/go-toml/v2/internal/danger"
       
    17 	"github.com/pelletier/go-toml/v2/internal/tracker"
       
    18 )
       
    19 
       
    20 // Unmarshal deserializes a TOML document into a Go value.
       
    21 //
       
    22 // It is a shortcut for Decoder.Decode() with the default options.
       
    23 func Unmarshal(data []byte, v interface{}) error {
       
    24 	p := parser{}
       
    25 	p.Reset(data)
       
    26 	d := decoder{p: &p}
       
    27 
       
    28 	return d.FromParser(v)
       
    29 }
       
    30 
       
    31 // Decoder reads and decode a TOML document from an input stream.
       
    32 type Decoder struct {
       
    33 	// input
       
    34 	r io.Reader
       
    35 
       
    36 	// global settings
       
    37 	strict bool
       
    38 }
       
    39 
       
    40 // NewDecoder creates a new Decoder that will read from r.
       
    41 func NewDecoder(r io.Reader) *Decoder {
       
    42 	return &Decoder{r: r}
       
    43 }
       
    44 
       
    45 // DisallowUnknownFields causes the Decoder to return an error when the
       
    46 // destination is a struct and the input contains a key that does not match a
       
    47 // non-ignored field.
       
    48 //
       
    49 // In that case, the Decoder returns a StrictMissingError that can be used to
       
    50 // retrieve the individual errors as well as generate a human readable
       
    51 // description of the missing fields.
       
    52 func (d *Decoder) DisallowUnknownFields() *Decoder {
       
    53 	d.strict = true
       
    54 	return d
       
    55 }
       
    56 
       
    57 // Decode the whole content of r into v.
       
    58 //
       
    59 // By default, values in the document that don't exist in the target Go value
       
    60 // are ignored. See Decoder.DisallowUnknownFields() to change this behavior.
       
    61 //
       
    62 // When a TOML local date, time, or date-time is decoded into a time.Time, its
       
    63 // value is represented in time.Local timezone. Otherwise the approriate Local*
       
    64 // structure is used. For time values, precision up to the nanosecond is
       
    65 // supported by truncating extra digits.
       
    66 //
       
    67 // Empty tables decoded in an interface{} create an empty initialized
       
    68 // map[string]interface{}.
       
    69 //
       
    70 // Types implementing the encoding.TextUnmarshaler interface are decoded from a
       
    71 // TOML string.
       
    72 //
       
    73 // When decoding a number, go-toml will return an error if the number is out of
       
    74 // bounds for the target type (which includes negative numbers when decoding
       
    75 // into an unsigned int).
       
    76 //
       
    77 // If an error occurs while decoding the content of the document, this function
       
    78 // returns a toml.DecodeError, providing context about the issue. When using
       
    79 // strict mode and a field is missing, a `toml.StrictMissingError` is
       
    80 // returned. In any other case, this function returns a standard Go error.
       
    81 //
       
    82 // # Type mapping
       
    83 //
       
    84 // List of supported TOML types and their associated accepted Go types:
       
    85 //
       
    86 //	String           -> string
       
    87 //	Integer          -> uint*, int*, depending on size
       
    88 //	Float            -> float*, depending on size
       
    89 //	Boolean          -> bool
       
    90 //	Offset Date-Time -> time.Time
       
    91 //	Local Date-time  -> LocalDateTime, time.Time
       
    92 //	Local Date       -> LocalDate, time.Time
       
    93 //	Local Time       -> LocalTime, time.Time
       
    94 //	Array            -> slice and array, depending on elements types
       
    95 //	Table            -> map and struct
       
    96 //	Inline Table     -> same as Table
       
    97 //	Array of Tables  -> same as Array and Table
       
    98 func (d *Decoder) Decode(v interface{}) error {
       
    99 	b, err := ioutil.ReadAll(d.r)
       
   100 	if err != nil {
       
   101 		return fmt.Errorf("toml: %w", err)
       
   102 	}
       
   103 
       
   104 	p := parser{}
       
   105 	p.Reset(b)
       
   106 	dec := decoder{
       
   107 		p: &p,
       
   108 		strict: strict{
       
   109 			Enabled: d.strict,
       
   110 		},
       
   111 	}
       
   112 
       
   113 	return dec.FromParser(v)
       
   114 }
       
   115 
       
   116 type decoder struct {
       
   117 	// Which parser instance in use for this decoding session.
       
   118 	p *parser
       
   119 
       
   120 	// Flag indicating that the current expression is stashed.
       
   121 	// If set to true, calling nextExpr will not actually pull a new expression
       
   122 	// but turn off the flag instead.
       
   123 	stashedExpr bool
       
   124 
       
   125 	// Skip expressions until a table is found. This is set to true when a
       
   126 	// table could not be create (missing field in map), so all KV expressions
       
   127 	// need to be skipped.
       
   128 	skipUntilTable bool
       
   129 
       
   130 	// Tracks position in Go arrays.
       
   131 	// This is used when decoding [[array tables]] into Go arrays. Given array
       
   132 	// tables are separate TOML expression, we need to keep track of where we
       
   133 	// are at in the Go array, as we can't just introspect its size.
       
   134 	arrayIndexes map[reflect.Value]int
       
   135 
       
   136 	// Tracks keys that have been seen, with which type.
       
   137 	seen tracker.SeenTracker
       
   138 
       
   139 	// Strict mode
       
   140 	strict strict
       
   141 
       
   142 	// Current context for the error.
       
   143 	errorContext *errorContext
       
   144 }
       
   145 
       
   146 type errorContext struct {
       
   147 	Struct reflect.Type
       
   148 	Field  []int
       
   149 }
       
   150 
       
   151 func (d *decoder) typeMismatchError(toml string, target reflect.Type) error {
       
   152 	if d.errorContext != nil && d.errorContext.Struct != nil {
       
   153 		ctx := d.errorContext
       
   154 		f := ctx.Struct.FieldByIndex(ctx.Field)
       
   155 		return fmt.Errorf("toml: cannot decode TOML %s into struct field %s.%s of type %s", toml, ctx.Struct, f.Name, f.Type)
       
   156 	}
       
   157 	return fmt.Errorf("toml: cannot decode TOML %s into a Go value of type %s", toml, target)
       
   158 }
       
   159 
       
   160 func (d *decoder) expr() *ast.Node {
       
   161 	return d.p.Expression()
       
   162 }
       
   163 
       
   164 func (d *decoder) nextExpr() bool {
       
   165 	if d.stashedExpr {
       
   166 		d.stashedExpr = false
       
   167 		return true
       
   168 	}
       
   169 	return d.p.NextExpression()
       
   170 }
       
   171 
       
   172 func (d *decoder) stashExpr() {
       
   173 	d.stashedExpr = true
       
   174 }
       
   175 
       
   176 func (d *decoder) arrayIndex(shouldAppend bool, v reflect.Value) int {
       
   177 	if d.arrayIndexes == nil {
       
   178 		d.arrayIndexes = make(map[reflect.Value]int, 1)
       
   179 	}
       
   180 
       
   181 	idx, ok := d.arrayIndexes[v]
       
   182 
       
   183 	if !ok {
       
   184 		d.arrayIndexes[v] = 0
       
   185 	} else if shouldAppend {
       
   186 		idx++
       
   187 		d.arrayIndexes[v] = idx
       
   188 	}
       
   189 
       
   190 	return idx
       
   191 }
       
   192 
       
   193 func (d *decoder) FromParser(v interface{}) error {
       
   194 	r := reflect.ValueOf(v)
       
   195 	if r.Kind() != reflect.Ptr {
       
   196 		return fmt.Errorf("toml: decoding can only be performed into a pointer, not %s", r.Kind())
       
   197 	}
       
   198 
       
   199 	if r.IsNil() {
       
   200 		return fmt.Errorf("toml: decoding pointer target cannot be nil")
       
   201 	}
       
   202 
       
   203 	r = r.Elem()
       
   204 	if r.Kind() == reflect.Interface && r.IsNil() {
       
   205 		newMap := map[string]interface{}{}
       
   206 		r.Set(reflect.ValueOf(newMap))
       
   207 	}
       
   208 
       
   209 	err := d.fromParser(r)
       
   210 	if err == nil {
       
   211 		return d.strict.Error(d.p.data)
       
   212 	}
       
   213 
       
   214 	var e *decodeError
       
   215 	if errors.As(err, &e) {
       
   216 		return wrapDecodeError(d.p.data, e)
       
   217 	}
       
   218 
       
   219 	return err
       
   220 }
       
   221 
       
   222 func (d *decoder) fromParser(root reflect.Value) error {
       
   223 	for d.nextExpr() {
       
   224 		err := d.handleRootExpression(d.expr(), root)
       
   225 		if err != nil {
       
   226 			return err
       
   227 		}
       
   228 	}
       
   229 
       
   230 	return d.p.Error()
       
   231 }
       
   232 
       
   233 /*
       
   234 Rules for the unmarshal code:
       
   235 
       
   236 - The stack is used to keep track of which values need to be set where.
       
   237 - handle* functions <=> switch on a given ast.Kind.
       
   238 - unmarshalX* functions need to unmarshal a node of kind X.
       
   239 - An "object" is either a struct or a map.
       
   240 */
       
   241 
       
   242 func (d *decoder) handleRootExpression(expr *ast.Node, v reflect.Value) error {
       
   243 	var x reflect.Value
       
   244 	var err error
       
   245 
       
   246 	if !(d.skipUntilTable && expr.Kind == ast.KeyValue) {
       
   247 		err = d.seen.CheckExpression(expr)
       
   248 		if err != nil {
       
   249 			return err
       
   250 		}
       
   251 	}
       
   252 
       
   253 	switch expr.Kind {
       
   254 	case ast.KeyValue:
       
   255 		if d.skipUntilTable {
       
   256 			return nil
       
   257 		}
       
   258 		x, err = d.handleKeyValue(expr, v)
       
   259 	case ast.Table:
       
   260 		d.skipUntilTable = false
       
   261 		d.strict.EnterTable(expr)
       
   262 		x, err = d.handleTable(expr.Key(), v)
       
   263 	case ast.ArrayTable:
       
   264 		d.skipUntilTable = false
       
   265 		d.strict.EnterArrayTable(expr)
       
   266 		x, err = d.handleArrayTable(expr.Key(), v)
       
   267 	default:
       
   268 		panic(fmt.Errorf("parser should not permit expression of kind %s at document root", expr.Kind))
       
   269 	}
       
   270 
       
   271 	if d.skipUntilTable {
       
   272 		if expr.Kind == ast.Table || expr.Kind == ast.ArrayTable {
       
   273 			d.strict.MissingTable(expr)
       
   274 		}
       
   275 	} else if err == nil && x.IsValid() {
       
   276 		v.Set(x)
       
   277 	}
       
   278 
       
   279 	return err
       
   280 }
       
   281 
       
   282 func (d *decoder) handleArrayTable(key ast.Iterator, v reflect.Value) (reflect.Value, error) {
       
   283 	if key.Next() {
       
   284 		return d.handleArrayTablePart(key, v)
       
   285 	}
       
   286 	return d.handleKeyValues(v)
       
   287 }
       
   288 
       
   289 func (d *decoder) handleArrayTableCollectionLast(key ast.Iterator, v reflect.Value) (reflect.Value, error) {
       
   290 	switch v.Kind() {
       
   291 	case reflect.Interface:
       
   292 		elem := v.Elem()
       
   293 		if !elem.IsValid() {
       
   294 			elem = reflect.New(sliceInterfaceType).Elem()
       
   295 			elem.Set(reflect.MakeSlice(sliceInterfaceType, 0, 16))
       
   296 		} else if elem.Kind() == reflect.Slice {
       
   297 			if elem.Type() != sliceInterfaceType {
       
   298 				elem = reflect.New(sliceInterfaceType).Elem()
       
   299 				elem.Set(reflect.MakeSlice(sliceInterfaceType, 0, 16))
       
   300 			} else if !elem.CanSet() {
       
   301 				nelem := reflect.New(sliceInterfaceType).Elem()
       
   302 				nelem.Set(reflect.MakeSlice(sliceInterfaceType, elem.Len(), elem.Cap()))
       
   303 				reflect.Copy(nelem, elem)
       
   304 				elem = nelem
       
   305 			}
       
   306 		}
       
   307 		return d.handleArrayTableCollectionLast(key, elem)
       
   308 	case reflect.Ptr:
       
   309 		elem := v.Elem()
       
   310 		if !elem.IsValid() {
       
   311 			ptr := reflect.New(v.Type().Elem())
       
   312 			v.Set(ptr)
       
   313 			elem = ptr.Elem()
       
   314 		}
       
   315 
       
   316 		elem, err := d.handleArrayTableCollectionLast(key, elem)
       
   317 		if err != nil {
       
   318 			return reflect.Value{}, err
       
   319 		}
       
   320 		v.Elem().Set(elem)
       
   321 
       
   322 		return v, nil
       
   323 	case reflect.Slice:
       
   324 		elemType := v.Type().Elem()
       
   325 		var elem reflect.Value
       
   326 		if elemType.Kind() == reflect.Interface {
       
   327 			elem = makeMapStringInterface()
       
   328 		} else {
       
   329 			elem = reflect.New(elemType).Elem()
       
   330 		}
       
   331 		elem2, err := d.handleArrayTable(key, elem)
       
   332 		if err != nil {
       
   333 			return reflect.Value{}, err
       
   334 		}
       
   335 		if elem2.IsValid() {
       
   336 			elem = elem2
       
   337 		}
       
   338 		return reflect.Append(v, elem), nil
       
   339 	case reflect.Array:
       
   340 		idx := d.arrayIndex(true, v)
       
   341 		if idx >= v.Len() {
       
   342 			return v, fmt.Errorf("toml: cannot decode array table into %s at position %d", v.Type(), idx)
       
   343 		}
       
   344 		elem := v.Index(idx)
       
   345 		_, err := d.handleArrayTable(key, elem)
       
   346 		return v, err
       
   347 	default:
       
   348 		return reflect.Value{}, fmt.Errorf("toml: cannot decode array table into a %s", v.Type())
       
   349 	}
       
   350 }
       
   351 
       
   352 // When parsing an array table expression, each part of the key needs to be
       
   353 // evaluated like a normal key, but if it returns a collection, it also needs to
       
   354 // point to the last element of the collection. Unless it is the last part of
       
   355 // the key, then it needs to create a new element at the end.
       
   356 func (d *decoder) handleArrayTableCollection(key ast.Iterator, v reflect.Value) (reflect.Value, error) {
       
   357 	if key.IsLast() {
       
   358 		return d.handleArrayTableCollectionLast(key, v)
       
   359 	}
       
   360 
       
   361 	switch v.Kind() {
       
   362 	case reflect.Ptr:
       
   363 		elem := v.Elem()
       
   364 		if !elem.IsValid() {
       
   365 			ptr := reflect.New(v.Type().Elem())
       
   366 			v.Set(ptr)
       
   367 			elem = ptr.Elem()
       
   368 		}
       
   369 
       
   370 		elem, err := d.handleArrayTableCollection(key, elem)
       
   371 		if err != nil {
       
   372 			return reflect.Value{}, err
       
   373 		}
       
   374 		if elem.IsValid() {
       
   375 			v.Elem().Set(elem)
       
   376 		}
       
   377 
       
   378 		return v, nil
       
   379 	case reflect.Slice:
       
   380 		elem := v.Index(v.Len() - 1)
       
   381 		x, err := d.handleArrayTable(key, elem)
       
   382 		if err != nil || d.skipUntilTable {
       
   383 			return reflect.Value{}, err
       
   384 		}
       
   385 		if x.IsValid() {
       
   386 			elem.Set(x)
       
   387 		}
       
   388 
       
   389 		return v, err
       
   390 	case reflect.Array:
       
   391 		idx := d.arrayIndex(false, v)
       
   392 		if idx >= v.Len() {
       
   393 			return v, fmt.Errorf("toml: cannot decode array table into %s at position %d", v.Type(), idx)
       
   394 		}
       
   395 		elem := v.Index(idx)
       
   396 		_, err := d.handleArrayTable(key, elem)
       
   397 		return v, err
       
   398 	}
       
   399 
       
   400 	return d.handleArrayTable(key, v)
       
   401 }
       
   402 
       
   403 func (d *decoder) handleKeyPart(key ast.Iterator, v reflect.Value, nextFn handlerFn, makeFn valueMakerFn) (reflect.Value, error) {
       
   404 	var rv reflect.Value
       
   405 
       
   406 	// First, dispatch over v to make sure it is a valid object.
       
   407 	// There is no guarantee over what it could be.
       
   408 	switch v.Kind() {
       
   409 	case reflect.Ptr:
       
   410 		elem := v.Elem()
       
   411 		if !elem.IsValid() {
       
   412 			v.Set(reflect.New(v.Type().Elem()))
       
   413 		}
       
   414 		elem = v.Elem()
       
   415 		return d.handleKeyPart(key, elem, nextFn, makeFn)
       
   416 	case reflect.Map:
       
   417 		vt := v.Type()
       
   418 
       
   419 		// Create the key for the map element. Convert to key type.
       
   420 		mk := reflect.ValueOf(string(key.Node().Data)).Convert(vt.Key())
       
   421 
       
   422 		// If the map does not exist, create it.
       
   423 		if v.IsNil() {
       
   424 			vt := v.Type()
       
   425 			v = reflect.MakeMap(vt)
       
   426 			rv = v
       
   427 		}
       
   428 
       
   429 		mv := v.MapIndex(mk)
       
   430 		set := false
       
   431 		if !mv.IsValid() {
       
   432 			// If there is no value in the map, create a new one according to
       
   433 			// the map type. If the element type is interface, create either a
       
   434 			// map[string]interface{} or a []interface{} depending on whether
       
   435 			// this is the last part of the array table key.
       
   436 
       
   437 			t := vt.Elem()
       
   438 			if t.Kind() == reflect.Interface {
       
   439 				mv = makeFn()
       
   440 			} else {
       
   441 				mv = reflect.New(t).Elem()
       
   442 			}
       
   443 			set = true
       
   444 		} else if mv.Kind() == reflect.Interface {
       
   445 			mv = mv.Elem()
       
   446 			if !mv.IsValid() {
       
   447 				mv = makeFn()
       
   448 			}
       
   449 			set = true
       
   450 		} else if !mv.CanAddr() {
       
   451 			vt := v.Type()
       
   452 			t := vt.Elem()
       
   453 			oldmv := mv
       
   454 			mv = reflect.New(t).Elem()
       
   455 			mv.Set(oldmv)
       
   456 			set = true
       
   457 		}
       
   458 
       
   459 		x, err := nextFn(key, mv)
       
   460 		if err != nil {
       
   461 			return reflect.Value{}, err
       
   462 		}
       
   463 
       
   464 		if x.IsValid() {
       
   465 			mv = x
       
   466 			set = true
       
   467 		}
       
   468 
       
   469 		if set {
       
   470 			v.SetMapIndex(mk, mv)
       
   471 		}
       
   472 	case reflect.Struct:
       
   473 		path, found := structFieldPath(v, string(key.Node().Data))
       
   474 		if !found {
       
   475 			d.skipUntilTable = true
       
   476 			return reflect.Value{}, nil
       
   477 		}
       
   478 
       
   479 		if d.errorContext == nil {
       
   480 			d.errorContext = new(errorContext)
       
   481 		}
       
   482 		t := v.Type()
       
   483 		d.errorContext.Struct = t
       
   484 		d.errorContext.Field = path
       
   485 
       
   486 		f := v.FieldByIndex(path)
       
   487 		x, err := nextFn(key, f)
       
   488 		if err != nil || d.skipUntilTable {
       
   489 			return reflect.Value{}, err
       
   490 		}
       
   491 		if x.IsValid() {
       
   492 			f.Set(x)
       
   493 		}
       
   494 		d.errorContext.Field = nil
       
   495 		d.errorContext.Struct = nil
       
   496 	case reflect.Interface:
       
   497 		if v.Elem().IsValid() {
       
   498 			v = v.Elem()
       
   499 		} else {
       
   500 			v = makeMapStringInterface()
       
   501 		}
       
   502 
       
   503 		x, err := d.handleKeyPart(key, v, nextFn, makeFn)
       
   504 		if err != nil {
       
   505 			return reflect.Value{}, err
       
   506 		}
       
   507 		if x.IsValid() {
       
   508 			v = x
       
   509 		}
       
   510 		rv = v
       
   511 	default:
       
   512 		panic(fmt.Errorf("unhandled part: %s", v.Kind()))
       
   513 	}
       
   514 
       
   515 	return rv, nil
       
   516 }
       
   517 
       
   518 // HandleArrayTablePart navigates the Go structure v using the key v. It is
       
   519 // only used for the prefix (non-last) parts of an array-table. When
       
   520 // encountering a collection, it should go to the last element.
       
   521 func (d *decoder) handleArrayTablePart(key ast.Iterator, v reflect.Value) (reflect.Value, error) {
       
   522 	var makeFn valueMakerFn
       
   523 	if key.IsLast() {
       
   524 		makeFn = makeSliceInterface
       
   525 	} else {
       
   526 		makeFn = makeMapStringInterface
       
   527 	}
       
   528 	return d.handleKeyPart(key, v, d.handleArrayTableCollection, makeFn)
       
   529 }
       
   530 
       
   531 // HandleTable returns a reference when it has checked the next expression but
       
   532 // cannot handle it.
       
   533 func (d *decoder) handleTable(key ast.Iterator, v reflect.Value) (reflect.Value, error) {
       
   534 	if v.Kind() == reflect.Slice {
       
   535 		if v.Len() == 0 {
       
   536 			return reflect.Value{}, newDecodeError(key.Node().Data, "cannot store a table in a slice")
       
   537 		}
       
   538 		elem := v.Index(v.Len() - 1)
       
   539 		x, err := d.handleTable(key, elem)
       
   540 		if err != nil {
       
   541 			return reflect.Value{}, err
       
   542 		}
       
   543 		if x.IsValid() {
       
   544 			elem.Set(x)
       
   545 		}
       
   546 		return reflect.Value{}, nil
       
   547 	}
       
   548 	if key.Next() {
       
   549 		// Still scoping the key
       
   550 		return d.handleTablePart(key, v)
       
   551 	}
       
   552 	// Done scoping the key.
       
   553 	// Now handle all the key-value expressions in this table.
       
   554 	return d.handleKeyValues(v)
       
   555 }
       
   556 
       
   557 // Handle root expressions until the end of the document or the next
       
   558 // non-key-value.
       
   559 func (d *decoder) handleKeyValues(v reflect.Value) (reflect.Value, error) {
       
   560 	var rv reflect.Value
       
   561 	for d.nextExpr() {
       
   562 		expr := d.expr()
       
   563 		if expr.Kind != ast.KeyValue {
       
   564 			// Stash the expression so that fromParser can just loop and use
       
   565 			// the right handler.
       
   566 			// We could just recurse ourselves here, but at least this gives a
       
   567 			// chance to pop the stack a bit.
       
   568 			d.stashExpr()
       
   569 			break
       
   570 		}
       
   571 
       
   572 		err := d.seen.CheckExpression(expr)
       
   573 		if err != nil {
       
   574 			return reflect.Value{}, err
       
   575 		}
       
   576 
       
   577 		x, err := d.handleKeyValue(expr, v)
       
   578 		if err != nil {
       
   579 			return reflect.Value{}, err
       
   580 		}
       
   581 		if x.IsValid() {
       
   582 			v = x
       
   583 			rv = x
       
   584 		}
       
   585 	}
       
   586 	return rv, nil
       
   587 }
       
   588 
       
   589 type (
       
   590 	handlerFn    func(key ast.Iterator, v reflect.Value) (reflect.Value, error)
       
   591 	valueMakerFn func() reflect.Value
       
   592 )
       
   593 
       
   594 func makeMapStringInterface() reflect.Value {
       
   595 	return reflect.MakeMap(mapStringInterfaceType)
       
   596 }
       
   597 
       
   598 func makeSliceInterface() reflect.Value {
       
   599 	return reflect.MakeSlice(sliceInterfaceType, 0, 16)
       
   600 }
       
   601 
       
   602 func (d *decoder) handleTablePart(key ast.Iterator, v reflect.Value) (reflect.Value, error) {
       
   603 	return d.handleKeyPart(key, v, d.handleTable, makeMapStringInterface)
       
   604 }
       
   605 
       
   606 func (d *decoder) tryTextUnmarshaler(node *ast.Node, v reflect.Value) (bool, error) {
       
   607 	// Special case for time, because we allow to unmarshal to it from
       
   608 	// different kind of AST nodes.
       
   609 	if v.Type() == timeType {
       
   610 		return false, nil
       
   611 	}
       
   612 
       
   613 	if v.CanAddr() && v.Addr().Type().Implements(textUnmarshalerType) {
       
   614 		err := v.Addr().Interface().(encoding.TextUnmarshaler).UnmarshalText(node.Data)
       
   615 		if err != nil {
       
   616 			return false, newDecodeError(d.p.Raw(node.Raw), "%w", err)
       
   617 		}
       
   618 
       
   619 		return true, nil
       
   620 	}
       
   621 
       
   622 	return false, nil
       
   623 }
       
   624 
       
   625 func (d *decoder) handleValue(value *ast.Node, v reflect.Value) error {
       
   626 	for v.Kind() == reflect.Ptr {
       
   627 		v = initAndDereferencePointer(v)
       
   628 	}
       
   629 
       
   630 	ok, err := d.tryTextUnmarshaler(value, v)
       
   631 	if ok || err != nil {
       
   632 		return err
       
   633 	}
       
   634 
       
   635 	switch value.Kind {
       
   636 	case ast.String:
       
   637 		return d.unmarshalString(value, v)
       
   638 	case ast.Integer:
       
   639 		return d.unmarshalInteger(value, v)
       
   640 	case ast.Float:
       
   641 		return d.unmarshalFloat(value, v)
       
   642 	case ast.Bool:
       
   643 		return d.unmarshalBool(value, v)
       
   644 	case ast.DateTime:
       
   645 		return d.unmarshalDateTime(value, v)
       
   646 	case ast.LocalDate:
       
   647 		return d.unmarshalLocalDate(value, v)
       
   648 	case ast.LocalTime:
       
   649 		return d.unmarshalLocalTime(value, v)
       
   650 	case ast.LocalDateTime:
       
   651 		return d.unmarshalLocalDateTime(value, v)
       
   652 	case ast.InlineTable:
       
   653 		return d.unmarshalInlineTable(value, v)
       
   654 	case ast.Array:
       
   655 		return d.unmarshalArray(value, v)
       
   656 	default:
       
   657 		panic(fmt.Errorf("handleValue not implemented for %s", value.Kind))
       
   658 	}
       
   659 }
       
   660 
       
   661 func (d *decoder) unmarshalArray(array *ast.Node, v reflect.Value) error {
       
   662 	switch v.Kind() {
       
   663 	case reflect.Slice:
       
   664 		if v.IsNil() {
       
   665 			v.Set(reflect.MakeSlice(v.Type(), 0, 16))
       
   666 		} else {
       
   667 			v.SetLen(0)
       
   668 		}
       
   669 	case reflect.Array:
       
   670 		// arrays are always initialized
       
   671 	case reflect.Interface:
       
   672 		elem := v.Elem()
       
   673 		if !elem.IsValid() {
       
   674 			elem = reflect.New(sliceInterfaceType).Elem()
       
   675 			elem.Set(reflect.MakeSlice(sliceInterfaceType, 0, 16))
       
   676 		} else if elem.Kind() == reflect.Slice {
       
   677 			if elem.Type() != sliceInterfaceType {
       
   678 				elem = reflect.New(sliceInterfaceType).Elem()
       
   679 				elem.Set(reflect.MakeSlice(sliceInterfaceType, 0, 16))
       
   680 			} else if !elem.CanSet() {
       
   681 				nelem := reflect.New(sliceInterfaceType).Elem()
       
   682 				nelem.Set(reflect.MakeSlice(sliceInterfaceType, elem.Len(), elem.Cap()))
       
   683 				reflect.Copy(nelem, elem)
       
   684 				elem = nelem
       
   685 			}
       
   686 		}
       
   687 		err := d.unmarshalArray(array, elem)
       
   688 		if err != nil {
       
   689 			return err
       
   690 		}
       
   691 		v.Set(elem)
       
   692 		return nil
       
   693 	default:
       
   694 		// TODO: use newDecodeError, but first the parser needs to fill
       
   695 		//   array.Data.
       
   696 		return d.typeMismatchError("array", v.Type())
       
   697 	}
       
   698 
       
   699 	elemType := v.Type().Elem()
       
   700 
       
   701 	it := array.Children()
       
   702 	idx := 0
       
   703 	for it.Next() {
       
   704 		n := it.Node()
       
   705 
       
   706 		// TODO: optimize
       
   707 		if v.Kind() == reflect.Slice {
       
   708 			elem := reflect.New(elemType).Elem()
       
   709 
       
   710 			err := d.handleValue(n, elem)
       
   711 			if err != nil {
       
   712 				return err
       
   713 			}
       
   714 
       
   715 			v.Set(reflect.Append(v, elem))
       
   716 		} else { // array
       
   717 			if idx >= v.Len() {
       
   718 				return nil
       
   719 			}
       
   720 			elem := v.Index(idx)
       
   721 			err := d.handleValue(n, elem)
       
   722 			if err != nil {
       
   723 				return err
       
   724 			}
       
   725 			idx++
       
   726 		}
       
   727 	}
       
   728 
       
   729 	return nil
       
   730 }
       
   731 
       
   732 func (d *decoder) unmarshalInlineTable(itable *ast.Node, v reflect.Value) error {
       
   733 	// Make sure v is an initialized object.
       
   734 	switch v.Kind() {
       
   735 	case reflect.Map:
       
   736 		if v.IsNil() {
       
   737 			v.Set(reflect.MakeMap(v.Type()))
       
   738 		}
       
   739 	case reflect.Struct:
       
   740 	// structs are always initialized.
       
   741 	case reflect.Interface:
       
   742 		elem := v.Elem()
       
   743 		if !elem.IsValid() {
       
   744 			elem = makeMapStringInterface()
       
   745 			v.Set(elem)
       
   746 		}
       
   747 		return d.unmarshalInlineTable(itable, elem)
       
   748 	default:
       
   749 		return newDecodeError(itable.Data, "cannot store inline table in Go type %s", v.Kind())
       
   750 	}
       
   751 
       
   752 	it := itable.Children()
       
   753 	for it.Next() {
       
   754 		n := it.Node()
       
   755 
       
   756 		x, err := d.handleKeyValue(n, v)
       
   757 		if err != nil {
       
   758 			return err
       
   759 		}
       
   760 		if x.IsValid() {
       
   761 			v = x
       
   762 		}
       
   763 	}
       
   764 
       
   765 	return nil
       
   766 }
       
   767 
       
   768 func (d *decoder) unmarshalDateTime(value *ast.Node, v reflect.Value) error {
       
   769 	dt, err := parseDateTime(value.Data)
       
   770 	if err != nil {
       
   771 		return err
       
   772 	}
       
   773 
       
   774 	v.Set(reflect.ValueOf(dt))
       
   775 	return nil
       
   776 }
       
   777 
       
   778 func (d *decoder) unmarshalLocalDate(value *ast.Node, v reflect.Value) error {
       
   779 	ld, err := parseLocalDate(value.Data)
       
   780 	if err != nil {
       
   781 		return err
       
   782 	}
       
   783 
       
   784 	if v.Type() == timeType {
       
   785 		cast := ld.AsTime(time.Local)
       
   786 		v.Set(reflect.ValueOf(cast))
       
   787 		return nil
       
   788 	}
       
   789 
       
   790 	v.Set(reflect.ValueOf(ld))
       
   791 
       
   792 	return nil
       
   793 }
       
   794 
       
   795 func (d *decoder) unmarshalLocalTime(value *ast.Node, v reflect.Value) error {
       
   796 	lt, rest, err := parseLocalTime(value.Data)
       
   797 	if err != nil {
       
   798 		return err
       
   799 	}
       
   800 
       
   801 	if len(rest) > 0 {
       
   802 		return newDecodeError(rest, "extra characters at the end of a local time")
       
   803 	}
       
   804 
       
   805 	v.Set(reflect.ValueOf(lt))
       
   806 	return nil
       
   807 }
       
   808 
       
   809 func (d *decoder) unmarshalLocalDateTime(value *ast.Node, v reflect.Value) error {
       
   810 	ldt, rest, err := parseLocalDateTime(value.Data)
       
   811 	if err != nil {
       
   812 		return err
       
   813 	}
       
   814 
       
   815 	if len(rest) > 0 {
       
   816 		return newDecodeError(rest, "extra characters at the end of a local date time")
       
   817 	}
       
   818 
       
   819 	if v.Type() == timeType {
       
   820 		cast := ldt.AsTime(time.Local)
       
   821 
       
   822 		v.Set(reflect.ValueOf(cast))
       
   823 		return nil
       
   824 	}
       
   825 
       
   826 	v.Set(reflect.ValueOf(ldt))
       
   827 
       
   828 	return nil
       
   829 }
       
   830 
       
   831 func (d *decoder) unmarshalBool(value *ast.Node, v reflect.Value) error {
       
   832 	b := value.Data[0] == 't'
       
   833 
       
   834 	switch v.Kind() {
       
   835 	case reflect.Bool:
       
   836 		v.SetBool(b)
       
   837 	case reflect.Interface:
       
   838 		v.Set(reflect.ValueOf(b))
       
   839 	default:
       
   840 		return newDecodeError(value.Data, "cannot assign boolean to a %t", b)
       
   841 	}
       
   842 
       
   843 	return nil
       
   844 }
       
   845 
       
   846 func (d *decoder) unmarshalFloat(value *ast.Node, v reflect.Value) error {
       
   847 	f, err := parseFloat(value.Data)
       
   848 	if err != nil {
       
   849 		return err
       
   850 	}
       
   851 
       
   852 	switch v.Kind() {
       
   853 	case reflect.Float64:
       
   854 		v.SetFloat(f)
       
   855 	case reflect.Float32:
       
   856 		if f > math.MaxFloat32 {
       
   857 			return newDecodeError(value.Data, "number %f does not fit in a float32", f)
       
   858 		}
       
   859 		v.SetFloat(f)
       
   860 	case reflect.Interface:
       
   861 		v.Set(reflect.ValueOf(f))
       
   862 	default:
       
   863 		return newDecodeError(value.Data, "float cannot be assigned to %s", v.Kind())
       
   864 	}
       
   865 
       
   866 	return nil
       
   867 }
       
   868 
       
   869 const (
       
   870 	maxInt = int64(^uint(0) >> 1)
       
   871 	minInt = -maxInt - 1
       
   872 )
       
   873 
       
   874 // Maximum value of uint for decoding. Currently the decoder parses the integer
       
   875 // into an int64. As a result, on architectures where uint is 64 bits, the
       
   876 // effective maximum uint we can decode is the maximum of int64. On
       
   877 // architectures where uint is 32 bits, the maximum value we can decode is
       
   878 // lower: the maximum of uint32. I didn't find a way to figure out this value at
       
   879 // compile time, so it is computed during initialization.
       
   880 var maxUint int64 = math.MaxInt64
       
   881 
       
   882 func init() {
       
   883 	m := uint64(^uint(0))
       
   884 	if m < uint64(maxUint) {
       
   885 		maxUint = int64(m)
       
   886 	}
       
   887 }
       
   888 
       
   889 func (d *decoder) unmarshalInteger(value *ast.Node, v reflect.Value) error {
       
   890 	i, err := parseInteger(value.Data)
       
   891 	if err != nil {
       
   892 		return err
       
   893 	}
       
   894 
       
   895 	var r reflect.Value
       
   896 
       
   897 	switch v.Kind() {
       
   898 	case reflect.Int64:
       
   899 		v.SetInt(i)
       
   900 		return nil
       
   901 	case reflect.Int32:
       
   902 		if i < math.MinInt32 || i > math.MaxInt32 {
       
   903 			return fmt.Errorf("toml: number %d does not fit in an int32", i)
       
   904 		}
       
   905 
       
   906 		r = reflect.ValueOf(int32(i))
       
   907 	case reflect.Int16:
       
   908 		if i < math.MinInt16 || i > math.MaxInt16 {
       
   909 			return fmt.Errorf("toml: number %d does not fit in an int16", i)
       
   910 		}
       
   911 
       
   912 		r = reflect.ValueOf(int16(i))
       
   913 	case reflect.Int8:
       
   914 		if i < math.MinInt8 || i > math.MaxInt8 {
       
   915 			return fmt.Errorf("toml: number %d does not fit in an int8", i)
       
   916 		}
       
   917 
       
   918 		r = reflect.ValueOf(int8(i))
       
   919 	case reflect.Int:
       
   920 		if i < minInt || i > maxInt {
       
   921 			return fmt.Errorf("toml: number %d does not fit in an int", i)
       
   922 		}
       
   923 
       
   924 		r = reflect.ValueOf(int(i))
       
   925 	case reflect.Uint64:
       
   926 		if i < 0 {
       
   927 			return fmt.Errorf("toml: negative number %d does not fit in an uint64", i)
       
   928 		}
       
   929 
       
   930 		r = reflect.ValueOf(uint64(i))
       
   931 	case reflect.Uint32:
       
   932 		if i < 0 || i > math.MaxUint32 {
       
   933 			return fmt.Errorf("toml: negative number %d does not fit in an uint32", i)
       
   934 		}
       
   935 
       
   936 		r = reflect.ValueOf(uint32(i))
       
   937 	case reflect.Uint16:
       
   938 		if i < 0 || i > math.MaxUint16 {
       
   939 			return fmt.Errorf("toml: negative number %d does not fit in an uint16", i)
       
   940 		}
       
   941 
       
   942 		r = reflect.ValueOf(uint16(i))
       
   943 	case reflect.Uint8:
       
   944 		if i < 0 || i > math.MaxUint8 {
       
   945 			return fmt.Errorf("toml: negative number %d does not fit in an uint8", i)
       
   946 		}
       
   947 
       
   948 		r = reflect.ValueOf(uint8(i))
       
   949 	case reflect.Uint:
       
   950 		if i < 0 || i > maxUint {
       
   951 			return fmt.Errorf("toml: negative number %d does not fit in an uint", i)
       
   952 		}
       
   953 
       
   954 		r = reflect.ValueOf(uint(i))
       
   955 	case reflect.Interface:
       
   956 		r = reflect.ValueOf(i)
       
   957 	default:
       
   958 		return d.typeMismatchError("integer", v.Type())
       
   959 	}
       
   960 
       
   961 	if !r.Type().AssignableTo(v.Type()) {
       
   962 		r = r.Convert(v.Type())
       
   963 	}
       
   964 
       
   965 	v.Set(r)
       
   966 
       
   967 	return nil
       
   968 }
       
   969 
       
   970 func (d *decoder) unmarshalString(value *ast.Node, v reflect.Value) error {
       
   971 	switch v.Kind() {
       
   972 	case reflect.String:
       
   973 		v.SetString(string(value.Data))
       
   974 	case reflect.Interface:
       
   975 		v.Set(reflect.ValueOf(string(value.Data)))
       
   976 	default:
       
   977 		return newDecodeError(d.p.Raw(value.Raw), "cannot store TOML string into a Go %s", v.Kind())
       
   978 	}
       
   979 
       
   980 	return nil
       
   981 }
       
   982 
       
   983 func (d *decoder) handleKeyValue(expr *ast.Node, v reflect.Value) (reflect.Value, error) {
       
   984 	d.strict.EnterKeyValue(expr)
       
   985 
       
   986 	v, err := d.handleKeyValueInner(expr.Key(), expr.Value(), v)
       
   987 	if d.skipUntilTable {
       
   988 		d.strict.MissingField(expr)
       
   989 		d.skipUntilTable = false
       
   990 	}
       
   991 
       
   992 	d.strict.ExitKeyValue(expr)
       
   993 
       
   994 	return v, err
       
   995 }
       
   996 
       
   997 func (d *decoder) handleKeyValueInner(key ast.Iterator, value *ast.Node, v reflect.Value) (reflect.Value, error) {
       
   998 	if key.Next() {
       
   999 		// Still scoping the key
       
  1000 		return d.handleKeyValuePart(key, value, v)
       
  1001 	}
       
  1002 	// Done scoping the key.
       
  1003 	// v is whatever Go value we need to fill.
       
  1004 	return reflect.Value{}, d.handleValue(value, v)
       
  1005 }
       
  1006 
       
  1007 func (d *decoder) handleKeyValuePart(key ast.Iterator, value *ast.Node, v reflect.Value) (reflect.Value, error) {
       
  1008 	// contains the replacement for v
       
  1009 	var rv reflect.Value
       
  1010 
       
  1011 	// First, dispatch over v to make sure it is a valid object.
       
  1012 	// There is no guarantee over what it could be.
       
  1013 	switch v.Kind() {
       
  1014 	case reflect.Map:
       
  1015 		vt := v.Type()
       
  1016 
       
  1017 		mk := reflect.ValueOf(string(key.Node().Data))
       
  1018 		mkt := stringType
       
  1019 
       
  1020 		keyType := vt.Key()
       
  1021 		if !mkt.AssignableTo(keyType) {
       
  1022 			if !mkt.ConvertibleTo(keyType) {
       
  1023 				return reflect.Value{}, fmt.Errorf("toml: cannot convert map key of type %s to expected type %s", mkt, keyType)
       
  1024 			}
       
  1025 
       
  1026 			mk = mk.Convert(keyType)
       
  1027 		}
       
  1028 
       
  1029 		// If the map does not exist, create it.
       
  1030 		if v.IsNil() {
       
  1031 			v = reflect.MakeMap(vt)
       
  1032 			rv = v
       
  1033 		}
       
  1034 
       
  1035 		mv := v.MapIndex(mk)
       
  1036 		set := false
       
  1037 		if !mv.IsValid() {
       
  1038 			set = true
       
  1039 			mv = reflect.New(v.Type().Elem()).Elem()
       
  1040 		} else {
       
  1041 			if key.IsLast() {
       
  1042 				var x interface{}
       
  1043 				mv = reflect.ValueOf(&x).Elem()
       
  1044 				set = true
       
  1045 			}
       
  1046 		}
       
  1047 
       
  1048 		nv, err := d.handleKeyValueInner(key, value, mv)
       
  1049 		if err != nil {
       
  1050 			return reflect.Value{}, err
       
  1051 		}
       
  1052 		if nv.IsValid() {
       
  1053 			mv = nv
       
  1054 			set = true
       
  1055 		}
       
  1056 
       
  1057 		if set {
       
  1058 			v.SetMapIndex(mk, mv)
       
  1059 		}
       
  1060 	case reflect.Struct:
       
  1061 		path, found := structFieldPath(v, string(key.Node().Data))
       
  1062 		if !found {
       
  1063 			d.skipUntilTable = true
       
  1064 			break
       
  1065 		}
       
  1066 
       
  1067 		if d.errorContext == nil {
       
  1068 			d.errorContext = new(errorContext)
       
  1069 		}
       
  1070 		t := v.Type()
       
  1071 		d.errorContext.Struct = t
       
  1072 		d.errorContext.Field = path
       
  1073 
       
  1074 		f := v.FieldByIndex(path)
       
  1075 		x, err := d.handleKeyValueInner(key, value, f)
       
  1076 		if err != nil {
       
  1077 			return reflect.Value{}, err
       
  1078 		}
       
  1079 
       
  1080 		if x.IsValid() {
       
  1081 			f.Set(x)
       
  1082 		}
       
  1083 		d.errorContext.Struct = nil
       
  1084 		d.errorContext.Field = nil
       
  1085 	case reflect.Interface:
       
  1086 		v = v.Elem()
       
  1087 
       
  1088 		// Following encoding/json: decoding an object into an
       
  1089 		// interface{}, it needs to always hold a
       
  1090 		// map[string]interface{}. This is for the types to be
       
  1091 		// consistent whether a previous value was set or not.
       
  1092 		if !v.IsValid() || v.Type() != mapStringInterfaceType {
       
  1093 			v = makeMapStringInterface()
       
  1094 		}
       
  1095 
       
  1096 		x, err := d.handleKeyValuePart(key, value, v)
       
  1097 		if err != nil {
       
  1098 			return reflect.Value{}, err
       
  1099 		}
       
  1100 		if x.IsValid() {
       
  1101 			v = x
       
  1102 		}
       
  1103 		rv = v
       
  1104 	case reflect.Ptr:
       
  1105 		elem := v.Elem()
       
  1106 		if !elem.IsValid() {
       
  1107 			ptr := reflect.New(v.Type().Elem())
       
  1108 			v.Set(ptr)
       
  1109 			rv = v
       
  1110 			elem = ptr.Elem()
       
  1111 		}
       
  1112 
       
  1113 		elem2, err := d.handleKeyValuePart(key, value, elem)
       
  1114 		if err != nil {
       
  1115 			return reflect.Value{}, err
       
  1116 		}
       
  1117 		if elem2.IsValid() {
       
  1118 			elem = elem2
       
  1119 		}
       
  1120 		v.Elem().Set(elem)
       
  1121 	default:
       
  1122 		return reflect.Value{}, fmt.Errorf("unhandled kv part: %s", v.Kind())
       
  1123 	}
       
  1124 
       
  1125 	return rv, nil
       
  1126 }
       
  1127 
       
  1128 func initAndDereferencePointer(v reflect.Value) reflect.Value {
       
  1129 	var elem reflect.Value
       
  1130 	if v.IsNil() {
       
  1131 		ptr := reflect.New(v.Type().Elem())
       
  1132 		v.Set(ptr)
       
  1133 	}
       
  1134 	elem = v.Elem()
       
  1135 	return elem
       
  1136 }
       
  1137 
       
  1138 type fieldPathsMap = map[string][]int
       
  1139 
       
  1140 var globalFieldPathsCache atomic.Value // map[danger.TypeID]fieldPathsMap
       
  1141 
       
  1142 func structFieldPath(v reflect.Value, name string) ([]int, bool) {
       
  1143 	t := v.Type()
       
  1144 
       
  1145 	cache, _ := globalFieldPathsCache.Load().(map[danger.TypeID]fieldPathsMap)
       
  1146 	fieldPaths, ok := cache[danger.MakeTypeID(t)]
       
  1147 
       
  1148 	if !ok {
       
  1149 		fieldPaths = map[string][]int{}
       
  1150 
       
  1151 		forEachField(t, nil, func(name string, path []int) {
       
  1152 			fieldPaths[name] = path
       
  1153 			// extra copy for the case-insensitive match
       
  1154 			fieldPaths[strings.ToLower(name)] = path
       
  1155 		})
       
  1156 
       
  1157 		newCache := make(map[danger.TypeID]fieldPathsMap, len(cache)+1)
       
  1158 		newCache[danger.MakeTypeID(t)] = fieldPaths
       
  1159 		for k, v := range cache {
       
  1160 			newCache[k] = v
       
  1161 		}
       
  1162 		globalFieldPathsCache.Store(newCache)
       
  1163 	}
       
  1164 
       
  1165 	path, ok := fieldPaths[name]
       
  1166 	if !ok {
       
  1167 		path, ok = fieldPaths[strings.ToLower(name)]
       
  1168 	}
       
  1169 	return path, ok
       
  1170 }
       
  1171 
       
  1172 func forEachField(t reflect.Type, path []int, do func(name string, path []int)) {
       
  1173 	n := t.NumField()
       
  1174 	for i := 0; i < n; i++ {
       
  1175 		f := t.Field(i)
       
  1176 
       
  1177 		if !f.Anonymous && f.PkgPath != "" {
       
  1178 			// only consider exported fields.
       
  1179 			continue
       
  1180 		}
       
  1181 
       
  1182 		fieldPath := append(path, i)
       
  1183 		fieldPath = fieldPath[:len(fieldPath):len(fieldPath)]
       
  1184 
       
  1185 		name := f.Tag.Get("toml")
       
  1186 		if name == "-" {
       
  1187 			continue
       
  1188 		}
       
  1189 
       
  1190 		if i := strings.IndexByte(name, ','); i >= 0 {
       
  1191 			name = name[:i]
       
  1192 		}
       
  1193 
       
  1194 		if f.Anonymous && name == "" {
       
  1195 			forEachField(f.Type, fieldPath, do)
       
  1196 			continue
       
  1197 		}
       
  1198 
       
  1199 		if name == "" {
       
  1200 			name = f.Name
       
  1201 		}
       
  1202 
       
  1203 		do(name, fieldPath)
       
  1204 	}
       
  1205 }