vendor/github.com/pelletier/go-toml/v2/unmarshaler.go
changeset 265 05c40b36d3b2
parent 262 8d3354485fc3
equal deleted inserted replaced
264:8f478162d991 265:05c40b36d3b2
    10 	"reflect"
    10 	"reflect"
    11 	"strings"
    11 	"strings"
    12 	"sync/atomic"
    12 	"sync/atomic"
    13 	"time"
    13 	"time"
    14 
    14 
    15 	"github.com/pelletier/go-toml/v2/internal/ast"
       
    16 	"github.com/pelletier/go-toml/v2/internal/danger"
    15 	"github.com/pelletier/go-toml/v2/internal/danger"
    17 	"github.com/pelletier/go-toml/v2/internal/tracker"
    16 	"github.com/pelletier/go-toml/v2/internal/tracker"
       
    17 	"github.com/pelletier/go-toml/v2/unstable"
    18 )
    18 )
    19 
    19 
    20 // Unmarshal deserializes a TOML document into a Go value.
    20 // Unmarshal deserializes a TOML document into a Go value.
    21 //
    21 //
    22 // It is a shortcut for Decoder.Decode() with the default options.
    22 // It is a shortcut for Decoder.Decode() with the default options.
    23 func Unmarshal(data []byte, v interface{}) error {
    23 func Unmarshal(data []byte, v interface{}) error {
    24 	p := parser{}
    24 	p := unstable.Parser{}
    25 	p.Reset(data)
    25 	p.Reset(data)
    26 	d := decoder{p: &p}
    26 	d := decoder{p: &p}
    27 
    27 
    28 	return d.FromParser(v)
    28 	return d.FromParser(v)
    29 }
    29 }
    99 	b, err := ioutil.ReadAll(d.r)
    99 	b, err := ioutil.ReadAll(d.r)
   100 	if err != nil {
   100 	if err != nil {
   101 		return fmt.Errorf("toml: %w", err)
   101 		return fmt.Errorf("toml: %w", err)
   102 	}
   102 	}
   103 
   103 
   104 	p := parser{}
   104 	p := unstable.Parser{}
   105 	p.Reset(b)
   105 	p.Reset(b)
   106 	dec := decoder{
   106 	dec := decoder{
   107 		p: &p,
   107 		p: &p,
   108 		strict: strict{
   108 		strict: strict{
   109 			Enabled: d.strict,
   109 			Enabled: d.strict,
   113 	return dec.FromParser(v)
   113 	return dec.FromParser(v)
   114 }
   114 }
   115 
   115 
   116 type decoder struct {
   116 type decoder struct {
   117 	// Which parser instance in use for this decoding session.
   117 	// Which parser instance in use for this decoding session.
   118 	p *parser
   118 	p *unstable.Parser
   119 
   119 
   120 	// Flag indicating that the current expression is stashed.
   120 	// Flag indicating that the current expression is stashed.
   121 	// If set to true, calling nextExpr will not actually pull a new expression
   121 	// If set to true, calling nextExpr will not actually pull a new expression
   122 	// but turn off the flag instead.
   122 	// but turn off the flag instead.
   123 	stashedExpr bool
   123 	stashedExpr bool
   155 		return fmt.Errorf("toml: cannot decode TOML %s into struct field %s.%s of type %s", toml, ctx.Struct, f.Name, f.Type)
   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 	}
   156 	}
   157 	return fmt.Errorf("toml: cannot decode TOML %s into a Go value of type %s", toml, target)
   157 	return fmt.Errorf("toml: cannot decode TOML %s into a Go value of type %s", toml, target)
   158 }
   158 }
   159 
   159 
   160 func (d *decoder) expr() *ast.Node {
   160 func (d *decoder) expr() *unstable.Node {
   161 	return d.p.Expression()
   161 	return d.p.Expression()
   162 }
   162 }
   163 
   163 
   164 func (d *decoder) nextExpr() bool {
   164 func (d *decoder) nextExpr() bool {
   165 	if d.stashedExpr {
   165 	if d.stashedExpr {
   206 		r.Set(reflect.ValueOf(newMap))
   206 		r.Set(reflect.ValueOf(newMap))
   207 	}
   207 	}
   208 
   208 
   209 	err := d.fromParser(r)
   209 	err := d.fromParser(r)
   210 	if err == nil {
   210 	if err == nil {
   211 		return d.strict.Error(d.p.data)
   211 		return d.strict.Error(d.p.Data())
   212 	}
   212 	}
   213 
   213 
   214 	var e *decodeError
   214 	var e *unstable.ParserError
   215 	if errors.As(err, &e) {
   215 	if errors.As(err, &e) {
   216 		return wrapDecodeError(d.p.data, e)
   216 		return wrapDecodeError(d.p.Data(), e)
   217 	}
   217 	}
   218 
   218 
   219 	return err
   219 	return err
   220 }
   220 }
   221 
   221 
   232 
   232 
   233 /*
   233 /*
   234 Rules for the unmarshal code:
   234 Rules for the unmarshal code:
   235 
   235 
   236 - The stack is used to keep track of which values need to be set where.
   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.
   237 - handle* functions <=> switch on a given unstable.Kind.
   238 - unmarshalX* functions need to unmarshal a node of kind X.
   238 - unmarshalX* functions need to unmarshal a node of kind X.
   239 - An "object" is either a struct or a map.
   239 - An "object" is either a struct or a map.
   240 */
   240 */
   241 
   241 
   242 func (d *decoder) handleRootExpression(expr *ast.Node, v reflect.Value) error {
   242 func (d *decoder) handleRootExpression(expr *unstable.Node, v reflect.Value) error {
   243 	var x reflect.Value
   243 	var x reflect.Value
   244 	var err error
   244 	var err error
   245 
   245 
   246 	if !(d.skipUntilTable && expr.Kind == ast.KeyValue) {
   246 	if !(d.skipUntilTable && expr.Kind == unstable.KeyValue) {
   247 		err = d.seen.CheckExpression(expr)
   247 		err = d.seen.CheckExpression(expr)
   248 		if err != nil {
   248 		if err != nil {
   249 			return err
   249 			return err
   250 		}
   250 		}
   251 	}
   251 	}
   252 
   252 
   253 	switch expr.Kind {
   253 	switch expr.Kind {
   254 	case ast.KeyValue:
   254 	case unstable.KeyValue:
   255 		if d.skipUntilTable {
   255 		if d.skipUntilTable {
   256 			return nil
   256 			return nil
   257 		}
   257 		}
   258 		x, err = d.handleKeyValue(expr, v)
   258 		x, err = d.handleKeyValue(expr, v)
   259 	case ast.Table:
   259 	case unstable.Table:
   260 		d.skipUntilTable = false
   260 		d.skipUntilTable = false
   261 		d.strict.EnterTable(expr)
   261 		d.strict.EnterTable(expr)
   262 		x, err = d.handleTable(expr.Key(), v)
   262 		x, err = d.handleTable(expr.Key(), v)
   263 	case ast.ArrayTable:
   263 	case unstable.ArrayTable:
   264 		d.skipUntilTable = false
   264 		d.skipUntilTable = false
   265 		d.strict.EnterArrayTable(expr)
   265 		d.strict.EnterArrayTable(expr)
   266 		x, err = d.handleArrayTable(expr.Key(), v)
   266 		x, err = d.handleArrayTable(expr.Key(), v)
   267 	default:
   267 	default:
   268 		panic(fmt.Errorf("parser should not permit expression of kind %s at document root", expr.Kind))
   268 		panic(fmt.Errorf("parser should not permit expression of kind %s at document root", expr.Kind))
   269 	}
   269 	}
   270 
   270 
   271 	if d.skipUntilTable {
   271 	if d.skipUntilTable {
   272 		if expr.Kind == ast.Table || expr.Kind == ast.ArrayTable {
   272 		if expr.Kind == unstable.Table || expr.Kind == unstable.ArrayTable {
   273 			d.strict.MissingTable(expr)
   273 			d.strict.MissingTable(expr)
   274 		}
   274 		}
   275 	} else if err == nil && x.IsValid() {
   275 	} else if err == nil && x.IsValid() {
   276 		v.Set(x)
   276 		v.Set(x)
   277 	}
   277 	}
   278 
   278 
   279 	return err
   279 	return err
   280 }
   280 }
   281 
   281 
   282 func (d *decoder) handleArrayTable(key ast.Iterator, v reflect.Value) (reflect.Value, error) {
   282 func (d *decoder) handleArrayTable(key unstable.Iterator, v reflect.Value) (reflect.Value, error) {
   283 	if key.Next() {
   283 	if key.Next() {
   284 		return d.handleArrayTablePart(key, v)
   284 		return d.handleArrayTablePart(key, v)
   285 	}
   285 	}
   286 	return d.handleKeyValues(v)
   286 	return d.handleKeyValues(v)
   287 }
   287 }
   288 
   288 
   289 func (d *decoder) handleArrayTableCollectionLast(key ast.Iterator, v reflect.Value) (reflect.Value, error) {
   289 func (d *decoder) handleArrayTableCollectionLast(key unstable.Iterator, v reflect.Value) (reflect.Value, error) {
   290 	switch v.Kind() {
   290 	switch v.Kind() {
   291 	case reflect.Interface:
   291 	case reflect.Interface:
   292 		elem := v.Elem()
   292 		elem := v.Elem()
   293 		if !elem.IsValid() {
   293 		if !elem.IsValid() {
   294 			elem = reflect.New(sliceInterfaceType).Elem()
   294 			elem = reflect.New(sliceInterfaceType).Elem()
   337 		}
   337 		}
   338 		return reflect.Append(v, elem), nil
   338 		return reflect.Append(v, elem), nil
   339 	case reflect.Array:
   339 	case reflect.Array:
   340 		idx := d.arrayIndex(true, v)
   340 		idx := d.arrayIndex(true, v)
   341 		if idx >= v.Len() {
   341 		if idx >= v.Len() {
   342 			return v, fmt.Errorf("toml: cannot decode array table into %s at position %d", v.Type(), idx)
   342 			return v, fmt.Errorf("%s at position %d", d.typeMismatchError("array table", v.Type()), idx)
   343 		}
   343 		}
   344 		elem := v.Index(idx)
   344 		elem := v.Index(idx)
   345 		_, err := d.handleArrayTable(key, elem)
   345 		_, err := d.handleArrayTable(key, elem)
   346 		return v, err
   346 		return v, err
   347 	default:
   347 	default:
   348 		return reflect.Value{}, fmt.Errorf("toml: cannot decode array table into a %s", v.Type())
   348 		return reflect.Value{}, d.typeMismatchError("array table", v.Type())
   349 	}
   349 	}
   350 }
   350 }
   351 
   351 
   352 // When parsing an array table expression, each part of the key needs to be
   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
   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
   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.
   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) {
   356 func (d *decoder) handleArrayTableCollection(key unstable.Iterator, v reflect.Value) (reflect.Value, error) {
   357 	if key.IsLast() {
   357 	if key.IsLast() {
   358 		return d.handleArrayTableCollectionLast(key, v)
   358 		return d.handleArrayTableCollectionLast(key, v)
   359 	}
   359 	}
   360 
   360 
   361 	switch v.Kind() {
   361 	switch v.Kind() {
   388 
   388 
   389 		return v, err
   389 		return v, err
   390 	case reflect.Array:
   390 	case reflect.Array:
   391 		idx := d.arrayIndex(false, v)
   391 		idx := d.arrayIndex(false, v)
   392 		if idx >= v.Len() {
   392 		if idx >= v.Len() {
   393 			return v, fmt.Errorf("toml: cannot decode array table into %s at position %d", v.Type(), idx)
   393 			return v, fmt.Errorf("%s at position %d", d.typeMismatchError("array table", v.Type()), idx)
   394 		}
   394 		}
   395 		elem := v.Index(idx)
   395 		elem := v.Index(idx)
   396 		_, err := d.handleArrayTable(key, elem)
   396 		_, err := d.handleArrayTable(key, elem)
   397 		return v, err
   397 		return v, err
   398 	}
   398 	}
   399 
   399 
   400 	return d.handleArrayTable(key, v)
   400 	return d.handleArrayTable(key, v)
   401 }
   401 }
   402 
   402 
   403 func (d *decoder) handleKeyPart(key ast.Iterator, v reflect.Value, nextFn handlerFn, makeFn valueMakerFn) (reflect.Value, error) {
   403 func (d *decoder) handleKeyPart(key unstable.Iterator, v reflect.Value, nextFn handlerFn, makeFn valueMakerFn) (reflect.Value, error) {
   404 	var rv reflect.Value
   404 	var rv reflect.Value
   405 
   405 
   406 	// First, dispatch over v to make sure it is a valid object.
   406 	// First, dispatch over v to make sure it is a valid object.
   407 	// There is no guarantee over what it could be.
   407 	// There is no guarantee over what it could be.
   408 	switch v.Kind() {
   408 	switch v.Kind() {
   516 }
   516 }
   517 
   517 
   518 // HandleArrayTablePart navigates the Go structure v using the key v. It is
   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
   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.
   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) {
   521 func (d *decoder) handleArrayTablePart(key unstable.Iterator, v reflect.Value) (reflect.Value, error) {
   522 	var makeFn valueMakerFn
   522 	var makeFn valueMakerFn
   523 	if key.IsLast() {
   523 	if key.IsLast() {
   524 		makeFn = makeSliceInterface
   524 		makeFn = makeSliceInterface
   525 	} else {
   525 	} else {
   526 		makeFn = makeMapStringInterface
   526 		makeFn = makeMapStringInterface
   528 	return d.handleKeyPart(key, v, d.handleArrayTableCollection, makeFn)
   528 	return d.handleKeyPart(key, v, d.handleArrayTableCollection, makeFn)
   529 }
   529 }
   530 
   530 
   531 // HandleTable returns a reference when it has checked the next expression but
   531 // HandleTable returns a reference when it has checked the next expression but
   532 // cannot handle it.
   532 // cannot handle it.
   533 func (d *decoder) handleTable(key ast.Iterator, v reflect.Value) (reflect.Value, error) {
   533 func (d *decoder) handleTable(key unstable.Iterator, v reflect.Value) (reflect.Value, error) {
   534 	if v.Kind() == reflect.Slice {
   534 	if v.Kind() == reflect.Slice {
   535 		if v.Len() == 0 {
   535 		if v.Len() == 0 {
   536 			return reflect.Value{}, newDecodeError(key.Node().Data, "cannot store a table in a slice")
   536 			return reflect.Value{}, unstable.NewParserError(key.Node().Data, "cannot store a table in a slice")
   537 		}
   537 		}
   538 		elem := v.Index(v.Len() - 1)
   538 		elem := v.Index(v.Len() - 1)
   539 		x, err := d.handleTable(key, elem)
   539 		x, err := d.handleTable(key, elem)
   540 		if err != nil {
   540 		if err != nil {
   541 			return reflect.Value{}, err
   541 			return reflect.Value{}, err
   558 // non-key-value.
   558 // non-key-value.
   559 func (d *decoder) handleKeyValues(v reflect.Value) (reflect.Value, error) {
   559 func (d *decoder) handleKeyValues(v reflect.Value) (reflect.Value, error) {
   560 	var rv reflect.Value
   560 	var rv reflect.Value
   561 	for d.nextExpr() {
   561 	for d.nextExpr() {
   562 		expr := d.expr()
   562 		expr := d.expr()
   563 		if expr.Kind != ast.KeyValue {
   563 		if expr.Kind != unstable.KeyValue {
   564 			// Stash the expression so that fromParser can just loop and use
   564 			// Stash the expression so that fromParser can just loop and use
   565 			// the right handler.
   565 			// the right handler.
   566 			// We could just recurse ourselves here, but at least this gives a
   566 			// We could just recurse ourselves here, but at least this gives a
   567 			// chance to pop the stack a bit.
   567 			// chance to pop the stack a bit.
   568 			d.stashExpr()
   568 			d.stashExpr()
   585 	}
   585 	}
   586 	return rv, nil
   586 	return rv, nil
   587 }
   587 }
   588 
   588 
   589 type (
   589 type (
   590 	handlerFn    func(key ast.Iterator, v reflect.Value) (reflect.Value, error)
   590 	handlerFn    func(key unstable.Iterator, v reflect.Value) (reflect.Value, error)
   591 	valueMakerFn func() reflect.Value
   591 	valueMakerFn func() reflect.Value
   592 )
   592 )
   593 
   593 
   594 func makeMapStringInterface() reflect.Value {
   594 func makeMapStringInterface() reflect.Value {
   595 	return reflect.MakeMap(mapStringInterfaceType)
   595 	return reflect.MakeMap(mapStringInterfaceType)
   597 
   597 
   598 func makeSliceInterface() reflect.Value {
   598 func makeSliceInterface() reflect.Value {
   599 	return reflect.MakeSlice(sliceInterfaceType, 0, 16)
   599 	return reflect.MakeSlice(sliceInterfaceType, 0, 16)
   600 }
   600 }
   601 
   601 
   602 func (d *decoder) handleTablePart(key ast.Iterator, v reflect.Value) (reflect.Value, error) {
   602 func (d *decoder) handleTablePart(key unstable.Iterator, v reflect.Value) (reflect.Value, error) {
   603 	return d.handleKeyPart(key, v, d.handleTable, makeMapStringInterface)
   603 	return d.handleKeyPart(key, v, d.handleTable, makeMapStringInterface)
   604 }
   604 }
   605 
   605 
   606 func (d *decoder) tryTextUnmarshaler(node *ast.Node, v reflect.Value) (bool, error) {
   606 func (d *decoder) tryTextUnmarshaler(node *unstable.Node, v reflect.Value) (bool, error) {
   607 	// Special case for time, because we allow to unmarshal to it from
   607 	// Special case for time, because we allow to unmarshal to it from
   608 	// different kind of AST nodes.
   608 	// different kind of AST nodes.
   609 	if v.Type() == timeType {
   609 	if v.Type() == timeType {
   610 		return false, nil
   610 		return false, nil
   611 	}
   611 	}
   612 
   612 
   613 	if v.CanAddr() && v.Addr().Type().Implements(textUnmarshalerType) {
   613 	if v.CanAddr() && v.Addr().Type().Implements(textUnmarshalerType) {
   614 		err := v.Addr().Interface().(encoding.TextUnmarshaler).UnmarshalText(node.Data)
   614 		err := v.Addr().Interface().(encoding.TextUnmarshaler).UnmarshalText(node.Data)
   615 		if err != nil {
   615 		if err != nil {
   616 			return false, newDecodeError(d.p.Raw(node.Raw), "%w", err)
   616 			return false, unstable.NewParserError(d.p.Raw(node.Raw), "%w", err)
   617 		}
   617 		}
   618 
   618 
   619 		return true, nil
   619 		return true, nil
   620 	}
   620 	}
   621 
   621 
   622 	return false, nil
   622 	return false, nil
   623 }
   623 }
   624 
   624 
   625 func (d *decoder) handleValue(value *ast.Node, v reflect.Value) error {
   625 func (d *decoder) handleValue(value *unstable.Node, v reflect.Value) error {
   626 	for v.Kind() == reflect.Ptr {
   626 	for v.Kind() == reflect.Ptr {
   627 		v = initAndDereferencePointer(v)
   627 		v = initAndDereferencePointer(v)
   628 	}
   628 	}
   629 
   629 
   630 	ok, err := d.tryTextUnmarshaler(value, v)
   630 	ok, err := d.tryTextUnmarshaler(value, v)
   631 	if ok || err != nil {
   631 	if ok || err != nil {
   632 		return err
   632 		return err
   633 	}
   633 	}
   634 
   634 
   635 	switch value.Kind {
   635 	switch value.Kind {
   636 	case ast.String:
   636 	case unstable.String:
   637 		return d.unmarshalString(value, v)
   637 		return d.unmarshalString(value, v)
   638 	case ast.Integer:
   638 	case unstable.Integer:
   639 		return d.unmarshalInteger(value, v)
   639 		return d.unmarshalInteger(value, v)
   640 	case ast.Float:
   640 	case unstable.Float:
   641 		return d.unmarshalFloat(value, v)
   641 		return d.unmarshalFloat(value, v)
   642 	case ast.Bool:
   642 	case unstable.Bool:
   643 		return d.unmarshalBool(value, v)
   643 		return d.unmarshalBool(value, v)
   644 	case ast.DateTime:
   644 	case unstable.DateTime:
   645 		return d.unmarshalDateTime(value, v)
   645 		return d.unmarshalDateTime(value, v)
   646 	case ast.LocalDate:
   646 	case unstable.LocalDate:
   647 		return d.unmarshalLocalDate(value, v)
   647 		return d.unmarshalLocalDate(value, v)
   648 	case ast.LocalTime:
   648 	case unstable.LocalTime:
   649 		return d.unmarshalLocalTime(value, v)
   649 		return d.unmarshalLocalTime(value, v)
   650 	case ast.LocalDateTime:
   650 	case unstable.LocalDateTime:
   651 		return d.unmarshalLocalDateTime(value, v)
   651 		return d.unmarshalLocalDateTime(value, v)
   652 	case ast.InlineTable:
   652 	case unstable.InlineTable:
   653 		return d.unmarshalInlineTable(value, v)
   653 		return d.unmarshalInlineTable(value, v)
   654 	case ast.Array:
   654 	case unstable.Array:
   655 		return d.unmarshalArray(value, v)
   655 		return d.unmarshalArray(value, v)
   656 	default:
   656 	default:
   657 		panic(fmt.Errorf("handleValue not implemented for %s", value.Kind))
   657 		panic(fmt.Errorf("handleValue not implemented for %s", value.Kind))
   658 	}
   658 	}
   659 }
   659 }
   660 
   660 
   661 func (d *decoder) unmarshalArray(array *ast.Node, v reflect.Value) error {
   661 func (d *decoder) unmarshalArray(array *unstable.Node, v reflect.Value) error {
   662 	switch v.Kind() {
   662 	switch v.Kind() {
   663 	case reflect.Slice:
   663 	case reflect.Slice:
   664 		if v.IsNil() {
   664 		if v.IsNil() {
   665 			v.Set(reflect.MakeSlice(v.Type(), 0, 16))
   665 			v.Set(reflect.MakeSlice(v.Type(), 0, 16))
   666 		} else {
   666 		} else {
   727 	}
   727 	}
   728 
   728 
   729 	return nil
   729 	return nil
   730 }
   730 }
   731 
   731 
   732 func (d *decoder) unmarshalInlineTable(itable *ast.Node, v reflect.Value) error {
   732 func (d *decoder) unmarshalInlineTable(itable *unstable.Node, v reflect.Value) error {
   733 	// Make sure v is an initialized object.
   733 	// Make sure v is an initialized object.
   734 	switch v.Kind() {
   734 	switch v.Kind() {
   735 	case reflect.Map:
   735 	case reflect.Map:
   736 		if v.IsNil() {
   736 		if v.IsNil() {
   737 			v.Set(reflect.MakeMap(v.Type()))
   737 			v.Set(reflect.MakeMap(v.Type()))
   744 			elem = makeMapStringInterface()
   744 			elem = makeMapStringInterface()
   745 			v.Set(elem)
   745 			v.Set(elem)
   746 		}
   746 		}
   747 		return d.unmarshalInlineTable(itable, elem)
   747 		return d.unmarshalInlineTable(itable, elem)
   748 	default:
   748 	default:
   749 		return newDecodeError(itable.Data, "cannot store inline table in Go type %s", v.Kind())
   749 		return unstable.NewParserError(itable.Data, "cannot store inline table in Go type %s", v.Kind())
   750 	}
   750 	}
   751 
   751 
   752 	it := itable.Children()
   752 	it := itable.Children()
   753 	for it.Next() {
   753 	for it.Next() {
   754 		n := it.Node()
   754 		n := it.Node()
   763 	}
   763 	}
   764 
   764 
   765 	return nil
   765 	return nil
   766 }
   766 }
   767 
   767 
   768 func (d *decoder) unmarshalDateTime(value *ast.Node, v reflect.Value) error {
   768 func (d *decoder) unmarshalDateTime(value *unstable.Node, v reflect.Value) error {
   769 	dt, err := parseDateTime(value.Data)
   769 	dt, err := parseDateTime(value.Data)
   770 	if err != nil {
   770 	if err != nil {
   771 		return err
   771 		return err
   772 	}
   772 	}
   773 
   773 
   774 	v.Set(reflect.ValueOf(dt))
   774 	v.Set(reflect.ValueOf(dt))
   775 	return nil
   775 	return nil
   776 }
   776 }
   777 
   777 
   778 func (d *decoder) unmarshalLocalDate(value *ast.Node, v reflect.Value) error {
   778 func (d *decoder) unmarshalLocalDate(value *unstable.Node, v reflect.Value) error {
   779 	ld, err := parseLocalDate(value.Data)
   779 	ld, err := parseLocalDate(value.Data)
   780 	if err != nil {
   780 	if err != nil {
   781 		return err
   781 		return err
   782 	}
   782 	}
   783 
   783 
   790 	v.Set(reflect.ValueOf(ld))
   790 	v.Set(reflect.ValueOf(ld))
   791 
   791 
   792 	return nil
   792 	return nil
   793 }
   793 }
   794 
   794 
   795 func (d *decoder) unmarshalLocalTime(value *ast.Node, v reflect.Value) error {
   795 func (d *decoder) unmarshalLocalTime(value *unstable.Node, v reflect.Value) error {
   796 	lt, rest, err := parseLocalTime(value.Data)
   796 	lt, rest, err := parseLocalTime(value.Data)
   797 	if err != nil {
   797 	if err != nil {
   798 		return err
   798 		return err
   799 	}
   799 	}
   800 
   800 
   801 	if len(rest) > 0 {
   801 	if len(rest) > 0 {
   802 		return newDecodeError(rest, "extra characters at the end of a local time")
   802 		return unstable.NewParserError(rest, "extra characters at the end of a local time")
   803 	}
   803 	}
   804 
   804 
   805 	v.Set(reflect.ValueOf(lt))
   805 	v.Set(reflect.ValueOf(lt))
   806 	return nil
   806 	return nil
   807 }
   807 }
   808 
   808 
   809 func (d *decoder) unmarshalLocalDateTime(value *ast.Node, v reflect.Value) error {
   809 func (d *decoder) unmarshalLocalDateTime(value *unstable.Node, v reflect.Value) error {
   810 	ldt, rest, err := parseLocalDateTime(value.Data)
   810 	ldt, rest, err := parseLocalDateTime(value.Data)
   811 	if err != nil {
   811 	if err != nil {
   812 		return err
   812 		return err
   813 	}
   813 	}
   814 
   814 
   815 	if len(rest) > 0 {
   815 	if len(rest) > 0 {
   816 		return newDecodeError(rest, "extra characters at the end of a local date time")
   816 		return unstable.NewParserError(rest, "extra characters at the end of a local date time")
   817 	}
   817 	}
   818 
   818 
   819 	if v.Type() == timeType {
   819 	if v.Type() == timeType {
   820 		cast := ldt.AsTime(time.Local)
   820 		cast := ldt.AsTime(time.Local)
   821 
   821 
   826 	v.Set(reflect.ValueOf(ldt))
   826 	v.Set(reflect.ValueOf(ldt))
   827 
   827 
   828 	return nil
   828 	return nil
   829 }
   829 }
   830 
   830 
   831 func (d *decoder) unmarshalBool(value *ast.Node, v reflect.Value) error {
   831 func (d *decoder) unmarshalBool(value *unstable.Node, v reflect.Value) error {
   832 	b := value.Data[0] == 't'
   832 	b := value.Data[0] == 't'
   833 
   833 
   834 	switch v.Kind() {
   834 	switch v.Kind() {
   835 	case reflect.Bool:
   835 	case reflect.Bool:
   836 		v.SetBool(b)
   836 		v.SetBool(b)
   837 	case reflect.Interface:
   837 	case reflect.Interface:
   838 		v.Set(reflect.ValueOf(b))
   838 		v.Set(reflect.ValueOf(b))
   839 	default:
   839 	default:
   840 		return newDecodeError(value.Data, "cannot assign boolean to a %t", b)
   840 		return unstable.NewParserError(value.Data, "cannot assign boolean to a %t", b)
   841 	}
   841 	}
   842 
   842 
   843 	return nil
   843 	return nil
   844 }
   844 }
   845 
   845 
   846 func (d *decoder) unmarshalFloat(value *ast.Node, v reflect.Value) error {
   846 func (d *decoder) unmarshalFloat(value *unstable.Node, v reflect.Value) error {
   847 	f, err := parseFloat(value.Data)
   847 	f, err := parseFloat(value.Data)
   848 	if err != nil {
   848 	if err != nil {
   849 		return err
   849 		return err
   850 	}
   850 	}
   851 
   851 
   852 	switch v.Kind() {
   852 	switch v.Kind() {
   853 	case reflect.Float64:
   853 	case reflect.Float64:
   854 		v.SetFloat(f)
   854 		v.SetFloat(f)
   855 	case reflect.Float32:
   855 	case reflect.Float32:
   856 		if f > math.MaxFloat32 {
   856 		if f > math.MaxFloat32 {
   857 			return newDecodeError(value.Data, "number %f does not fit in a float32", f)
   857 			return unstable.NewParserError(value.Data, "number %f does not fit in a float32", f)
   858 		}
   858 		}
   859 		v.SetFloat(f)
   859 		v.SetFloat(f)
   860 	case reflect.Interface:
   860 	case reflect.Interface:
   861 		v.Set(reflect.ValueOf(f))
   861 		v.Set(reflect.ValueOf(f))
   862 	default:
   862 	default:
   863 		return newDecodeError(value.Data, "float cannot be assigned to %s", v.Kind())
   863 		return unstable.NewParserError(value.Data, "float cannot be assigned to %s", v.Kind())
   864 	}
   864 	}
   865 
   865 
   866 	return nil
   866 	return nil
   867 }
   867 }
   868 
   868 
   884 	if m < uint64(maxUint) {
   884 	if m < uint64(maxUint) {
   885 		maxUint = int64(m)
   885 		maxUint = int64(m)
   886 	}
   886 	}
   887 }
   887 }
   888 
   888 
   889 func (d *decoder) unmarshalInteger(value *ast.Node, v reflect.Value) error {
   889 func (d *decoder) unmarshalInteger(value *unstable.Node, v reflect.Value) error {
   890 	i, err := parseInteger(value.Data)
   890 	i, err := parseInteger(value.Data)
   891 	if err != nil {
   891 	if err != nil {
   892 		return err
   892 		return err
   893 	}
   893 	}
   894 
   894 
   965 	v.Set(r)
   965 	v.Set(r)
   966 
   966 
   967 	return nil
   967 	return nil
   968 }
   968 }
   969 
   969 
   970 func (d *decoder) unmarshalString(value *ast.Node, v reflect.Value) error {
   970 func (d *decoder) unmarshalString(value *unstable.Node, v reflect.Value) error {
   971 	switch v.Kind() {
   971 	switch v.Kind() {
   972 	case reflect.String:
   972 	case reflect.String:
   973 		v.SetString(string(value.Data))
   973 		v.SetString(string(value.Data))
   974 	case reflect.Interface:
   974 	case reflect.Interface:
   975 		v.Set(reflect.ValueOf(string(value.Data)))
   975 		v.Set(reflect.ValueOf(string(value.Data)))
   976 	default:
   976 	default:
   977 		return newDecodeError(d.p.Raw(value.Raw), "cannot store TOML string into a Go %s", v.Kind())
   977 		return unstable.NewParserError(d.p.Raw(value.Raw), "cannot store TOML string into a Go %s", v.Kind())
   978 	}
   978 	}
   979 
   979 
   980 	return nil
   980 	return nil
   981 }
   981 }
   982 
   982 
   983 func (d *decoder) handleKeyValue(expr *ast.Node, v reflect.Value) (reflect.Value, error) {
   983 func (d *decoder) handleKeyValue(expr *unstable.Node, v reflect.Value) (reflect.Value, error) {
   984 	d.strict.EnterKeyValue(expr)
   984 	d.strict.EnterKeyValue(expr)
   985 
   985 
   986 	v, err := d.handleKeyValueInner(expr.Key(), expr.Value(), v)
   986 	v, err := d.handleKeyValueInner(expr.Key(), expr.Value(), v)
   987 	if d.skipUntilTable {
   987 	if d.skipUntilTable {
   988 		d.strict.MissingField(expr)
   988 		d.strict.MissingField(expr)
   992 	d.strict.ExitKeyValue(expr)
   992 	d.strict.ExitKeyValue(expr)
   993 
   993 
   994 	return v, err
   994 	return v, err
   995 }
   995 }
   996 
   996 
   997 func (d *decoder) handleKeyValueInner(key ast.Iterator, value *ast.Node, v reflect.Value) (reflect.Value, error) {
   997 func (d *decoder) handleKeyValueInner(key unstable.Iterator, value *unstable.Node, v reflect.Value) (reflect.Value, error) {
   998 	if key.Next() {
   998 	if key.Next() {
   999 		// Still scoping the key
   999 		// Still scoping the key
  1000 		return d.handleKeyValuePart(key, value, v)
  1000 		return d.handleKeyValuePart(key, value, v)
  1001 	}
  1001 	}
  1002 	// Done scoping the key.
  1002 	// Done scoping the key.
  1003 	// v is whatever Go value we need to fill.
  1003 	// v is whatever Go value we need to fill.
  1004 	return reflect.Value{}, d.handleValue(value, v)
  1004 	return reflect.Value{}, d.handleValue(value, v)
  1005 }
  1005 }
  1006 
  1006 
  1007 func (d *decoder) handleKeyValuePart(key ast.Iterator, value *ast.Node, v reflect.Value) (reflect.Value, error) {
  1007 func (d *decoder) handleKeyValuePart(key unstable.Iterator, value *unstable.Node, v reflect.Value) (reflect.Value, error) {
  1008 	// contains the replacement for v
  1008 	// contains the replacement for v
  1009 	var rv reflect.Value
  1009 	var rv reflect.Value
  1010 
  1010 
  1011 	// First, dispatch over v to make sure it is a valid object.
  1011 	// First, dispatch over v to make sure it is a valid object.
  1012 	// There is no guarantee over what it could be.
  1012 	// There is no guarantee over what it could be.