vendor/github.com/pelletier/go-toml/marshal.go
changeset 256 6d9efbef00a9
parent 251 1c52a0eeb952
child 260 445e01aede7e
--- a/vendor/github.com/pelletier/go-toml/marshal.go	Mon Jun 07 20:58:18 2021 +0200
+++ b/vendor/github.com/pelletier/go-toml/marshal.go	Sun Jul 11 10:35:56 2021 +0200
@@ -2,6 +2,7 @@
 
 import (
 	"bytes"
+	"encoding"
 	"errors"
 	"fmt"
 	"io"
@@ -17,14 +18,17 @@
 	tagFieldComment = "comment"
 	tagCommented    = "commented"
 	tagMultiline    = "multiline"
+	tagLiteral      = "literal"
 	tagDefault      = "default"
 )
 
 type tomlOpts struct {
 	name         string
+	nameFromTag  bool
 	comment      string
 	commented    bool
 	multiline    bool
+	literal      bool
 	include      bool
 	omitempty    bool
 	defaultValue string
@@ -44,6 +48,7 @@
 	comment      string
 	commented    string
 	multiline    string
+	literal      string
 	defaultValue string
 }
 
@@ -52,15 +57,16 @@
 	comment:      tagFieldComment,
 	commented:    tagCommented,
 	multiline:    tagMultiline,
+	literal:      tagLiteral,
 	defaultValue: tagDefault,
 }
 
-type marshalOrder int
+type MarshalOrder int
 
 // Orders the Encoder can write the fields to the output stream.
 const (
 	// Sort fields alphabetically.
-	OrderAlphabetical marshalOrder = iota + 1
+	OrderAlphabetical MarshalOrder = iota + 1
 	// Preserve the order the fields are encountered. For example, the order of fields in
 	// a struct.
 	OrderPreserve
@@ -68,9 +74,13 @@
 
 var timeType = reflect.TypeOf(time.Time{})
 var marshalerType = reflect.TypeOf(new(Marshaler)).Elem()
+var unmarshalerType = reflect.TypeOf(new(Unmarshaler)).Elem()
+var textMarshalerType = reflect.TypeOf(new(encoding.TextMarshaler)).Elem()
+var textUnmarshalerType = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
 var localDateType = reflect.TypeOf(LocalDate{})
 var localTimeType = reflect.TypeOf(LocalTime{})
 var localDateTimeType = reflect.TypeOf(LocalDateTime{})
+var mapStringInterfaceType = reflect.TypeOf(map[string]interface{}{})
 
 // Check if the given marshal type maps to a Tree primitive
 func isPrimitive(mtype reflect.Type) bool {
@@ -88,12 +98,16 @@
 	case reflect.String:
 		return true
 	case reflect.Struct:
-		return mtype == timeType || mtype == localDateType || mtype == localDateTimeType || mtype == localTimeType || isCustomMarshaler(mtype)
+		return isTimeType(mtype)
 	default:
 		return false
 	}
 }
 
+func isTimeType(mtype reflect.Type) bool {
+	return mtype == timeType || mtype == localDateType || mtype == localDateTimeType || mtype == localTimeType
+}
+
 // Check if the given marshal type maps to a Tree slice or array
 func isTreeSequence(mtype reflect.Type) bool {
 	switch mtype.Kind() {
@@ -106,6 +120,30 @@
 	}
 }
 
+// Check if the given marshal type maps to a slice or array of a custom marshaler type
+func isCustomMarshalerSequence(mtype reflect.Type) bool {
+	switch mtype.Kind() {
+	case reflect.Ptr:
+		return isCustomMarshalerSequence(mtype.Elem())
+	case reflect.Slice, reflect.Array:
+		return isCustomMarshaler(mtype.Elem()) || isCustomMarshaler(reflect.New(mtype.Elem()).Type())
+	default:
+		return false
+	}
+}
+
+// Check if the given marshal type maps to a slice or array of a text marshaler type
+func isTextMarshalerSequence(mtype reflect.Type) bool {
+	switch mtype.Kind() {
+	case reflect.Ptr:
+		return isTextMarshalerSequence(mtype.Elem())
+	case reflect.Slice, reflect.Array:
+		return isTextMarshaler(mtype.Elem()) || isTextMarshaler(reflect.New(mtype.Elem()).Type())
+	default:
+		return false
+	}
+}
+
 // Check if the given marshal type maps to a non-Tree slice or array
 func isOtherSequence(mtype reflect.Type) bool {
 	switch mtype.Kind() {
@@ -140,12 +178,42 @@
 	return mval.Interface().(Marshaler).MarshalTOML()
 }
 
+func isTextMarshaler(mtype reflect.Type) bool {
+	return mtype.Implements(textMarshalerType) && !isTimeType(mtype)
+}
+
+func callTextMarshaler(mval reflect.Value) ([]byte, error) {
+	return mval.Interface().(encoding.TextMarshaler).MarshalText()
+}
+
+func isCustomUnmarshaler(mtype reflect.Type) bool {
+	return mtype.Implements(unmarshalerType)
+}
+
+func callCustomUnmarshaler(mval reflect.Value, tval interface{}) error {
+	return mval.Interface().(Unmarshaler).UnmarshalTOML(tval)
+}
+
+func isTextUnmarshaler(mtype reflect.Type) bool {
+	return mtype.Implements(textUnmarshalerType)
+}
+
+func callTextUnmarshaler(mval reflect.Value, text []byte) error {
+	return mval.Interface().(encoding.TextUnmarshaler).UnmarshalText(text)
+}
+
 // Marshaler is the interface implemented by types that
 // can marshal themselves into valid TOML.
 type Marshaler interface {
 	MarshalTOML() ([]byte, error)
 }
 
+// Unmarshaler is the interface implemented by types that
+// can unmarshal a TOML description of themselves.
+type Unmarshaler interface {
+	UnmarshalTOML(interface{}) error
+}
+
 /*
 Marshal returns the TOML encoding of v.  Behavior is similar to the Go json
 encoder, except that there is no concept of a Marshaler interface or MarshalTOML
@@ -190,20 +258,24 @@
 	w io.Writer
 	encOpts
 	annotation
-	line  int
-	col   int
-	order marshalOrder
+	line            int
+	col             int
+	order           MarshalOrder
+	promoteAnon     bool
+	compactComments bool
+	indentation     string
 }
 
 // NewEncoder returns a new encoder that writes to w.
 func NewEncoder(w io.Writer) *Encoder {
 	return &Encoder{
-		w:          w,
-		encOpts:    encOptsDefaults,
-		annotation: annotationDefault,
-		line:       0,
-		col:        1,
-		order:      OrderAlphabetical,
+		w:           w,
+		encOpts:     encOptsDefaults,
+		annotation:  annotationDefault,
+		line:        0,
+		col:         1,
+		order:       OrderAlphabetical,
+		indentation: "  ",
 	}
 }
 
@@ -250,11 +322,17 @@
 }
 
 // Order allows to change in which order fields will be written to the output stream.
-func (e *Encoder) Order(ord marshalOrder) *Encoder {
+func (e *Encoder) Order(ord MarshalOrder) *Encoder {
 	e.order = ord
 	return e
 }
 
+// Indentation allows to change indentation when marshalling.
+func (e *Encoder) Indentation(indent string) *Encoder {
+	e.indentation = indent
+	return e
+}
+
 // SetTagName allows changing default tag "toml"
 func (e *Encoder) SetTagName(v string) *Encoder {
 	e.tag = v
@@ -279,8 +357,37 @@
 	return e
 }
 
+// PromoteAnonymous allows to change how anonymous struct fields are marshaled.
+// Usually, they are marshaled as if the inner exported fields were fields in
+// the outer struct. However, if an anonymous struct field is given a name in
+// its TOML tag, it is treated like a regular struct field with that name.
+// rather than being anonymous.
+//
+// In case anonymous promotion is enabled, all anonymous structs are promoted
+// and treated like regular struct fields.
+func (e *Encoder) PromoteAnonymous(promote bool) *Encoder {
+	e.promoteAnon = promote
+	return e
+}
+
+// CompactComments removes the new line before each comment in the tree.
+func (e *Encoder) CompactComments(cc bool) *Encoder {
+	e.compactComments = cc
+	return e
+}
+
 func (e *Encoder) marshal(v interface{}) ([]byte, error) {
+	// Check if indentation is valid
+	for _, char := range e.indentation {
+		if !isSpace(char) {
+			return []byte{}, fmt.Errorf("invalid indentation: must only contains space or tab characters")
+		}
+	}
+
 	mtype := reflect.TypeOf(v)
+	if mtype == nil {
+		return []byte{}, errors.New("nil cannot be marshaled to TOML")
+	}
 
 	switch mtype.Kind() {
 	case reflect.Struct, reflect.Map:
@@ -288,6 +395,9 @@
 		if mtype.Elem().Kind() != reflect.Struct {
 			return []byte{}, errors.New("Only pointer to struct can be marshaled to TOML")
 		}
+		if reflect.ValueOf(v).IsNil() {
+			return []byte{}, errors.New("nil pointer cannot be marshaled to TOML")
+		}
 	default:
 		return []byte{}, errors.New("Only a struct or map can be marshaled to TOML")
 	}
@@ -296,13 +406,16 @@
 	if isCustomMarshaler(mtype) {
 		return callCustomMarshaler(sval)
 	}
+	if isTextMarshaler(mtype) {
+		return callTextMarshaler(sval)
+	}
 	t, err := e.valueToTree(mtype, sval)
 	if err != nil {
 		return []byte{}, err
 	}
 
 	var buf bytes.Buffer
-	_, err = t.writeToOrdered(&buf, "", "", 0, e.arraysOneElementPerLine, e.order)
+	_, err = t.writeToOrdered(&buf, "", "", 0, e.arraysOneElementPerLine, e.order, e.indentation, e.compactComments, false)
 
 	return buf.Bytes(), err
 }
@@ -320,20 +433,30 @@
 	tval := e.nextTree()
 	switch mtype.Kind() {
 	case reflect.Struct:
-		for i := 0; i < mtype.NumField(); i++ {
-			mtypef, mvalf := mtype.Field(i), mval.Field(i)
-			opts := tomlOptions(mtypef, e.annotation)
-			if opts.include && (!opts.omitempty || !isZero(mvalf)) {
-				val, err := e.valueToToml(mtypef.Type, mvalf)
-				if err != nil {
-					return nil, err
+		switch mval.Interface().(type) {
+		case Tree:
+			reflect.ValueOf(tval).Elem().Set(mval)
+		default:
+			for i := 0; i < mtype.NumField(); i++ {
+				mtypef, mvalf := mtype.Field(i), mval.Field(i)
+				opts := tomlOptions(mtypef, e.annotation)
+				if opts.include && ((mtypef.Type.Kind() != reflect.Interface && !opts.omitempty) || !isZero(mvalf)) {
+					val, err := e.valueToToml(mtypef.Type, mvalf)
+					if err != nil {
+						return nil, err
+					}
+					if tree, ok := val.(*Tree); ok && mtypef.Anonymous && !opts.nameFromTag && !e.promoteAnon {
+						e.appendTree(tval, tree)
+					} else {
+						val = e.wrapTomlValue(val, tval)
+						tval.SetPathWithOptions([]string{opts.name}, SetOptions{
+							Comment:   opts.comment,
+							Commented: opts.commented,
+							Multiline: opts.multiline,
+							Literal:   opts.literal,
+						}, val)
+					}
 				}
-
-				tval.SetWithOptions(opts.name, SetOptions{
-					Comment:   opts.comment,
-					Commented: opts.commented,
-					Multiline: opts.multiline,
-				}, val)
 			}
 		}
 	case reflect.Map:
@@ -358,18 +481,22 @@
 		}
 		for _, key := range keys {
 			mvalf := mval.MapIndex(key)
+			if (mtype.Elem().Kind() == reflect.Ptr || mtype.Elem().Kind() == reflect.Interface) && mvalf.IsNil() {
+				continue
+			}
 			val, err := e.valueToToml(mtype.Elem(), mvalf)
 			if err != nil {
 				return nil, err
 			}
+			val = e.wrapTomlValue(val, tval)
 			if e.quoteMapKeys {
-				keyStr, err := tomlValueStringRepresentation(key.String(), "", e.arraysOneElementPerLine)
+				keyStr, err := tomlValueStringRepresentation(key.String(), "", "", e.order, e.arraysOneElementPerLine)
 				if err != nil {
 					return nil, err
 				}
 				tval.SetPath([]string{keyStr}, val)
 			} else {
-				tval.Set(key.String(), val)
+				tval.SetPath([]string{key.String()}, val)
 			}
 		}
 	}
@@ -404,19 +531,32 @@
 
 // Convert given marshal value to toml value
 func (e *Encoder) valueToToml(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
-	e.line++
 	if mtype.Kind() == reflect.Ptr {
-		return e.valueToToml(mtype.Elem(), mval.Elem())
+		switch {
+		case isCustomMarshaler(mtype):
+			return callCustomMarshaler(mval)
+		case isTextMarshaler(mtype):
+			b, err := callTextMarshaler(mval)
+			return string(b), err
+		default:
+			return e.valueToToml(mtype.Elem(), mval.Elem())
+		}
+	}
+	if mtype.Kind() == reflect.Interface {
+		return e.valueToToml(mval.Elem().Type(), mval.Elem())
 	}
 	switch {
 	case isCustomMarshaler(mtype):
 		return callCustomMarshaler(mval)
+	case isTextMarshaler(mtype):
+		b, err := callTextMarshaler(mval)
+		return string(b), err
 	case isTree(mtype):
 		return e.valueToTree(mtype, mval)
+	case isOtherSequence(mtype), isCustomMarshalerSequence(mtype), isTextMarshalerSequence(mtype):
+		return e.valueToOtherSlice(mtype, mval)
 	case isTreeSequence(mtype):
 		return e.valueToTreeSlice(mtype, mval)
-	case isOtherSequence(mtype):
-		return e.valueToOtherSlice(mtype, mval)
 	default:
 		switch mtype.Kind() {
 		case reflect.Bool:
@@ -440,6 +580,39 @@
 	}
 }
 
+func (e *Encoder) appendTree(t, o *Tree) error {
+	for key, value := range o.values {
+		if _, ok := t.values[key]; ok {
+			continue
+		}
+		if tomlValue, ok := value.(*tomlValue); ok {
+			tomlValue.position.Col = t.position.Col
+		}
+		t.values[key] = value
+	}
+	return nil
+}
+
+// Create a toml value with the current line number as the position line
+func (e *Encoder) wrapTomlValue(val interface{}, parent *Tree) interface{} {
+	_, isTree := val.(*Tree)
+	_, isTreeS := val.([]*Tree)
+	if isTree || isTreeS {
+		e.line++
+		return val
+	}
+
+	ret := &tomlValue{
+		value: val,
+		position: Position{
+			e.line,
+			parent.position.Col,
+		},
+	}
+	e.line++
+	return ret
+}
+
 // Unmarshal attempts to unmarshal the Tree into a Go struct pointed by v.
 // Neither Unmarshaler interfaces nor UnmarshalTOML functions are supported for
 // sub-structs, and only definite types can be unmarshaled.
@@ -492,6 +665,8 @@
 	tval *Tree
 	encOpts
 	tagName string
+	strict  bool
+	visitor visitorState
 }
 
 // NewDecoder returns a new decoder that reads from r.
@@ -522,8 +697,18 @@
 	return d
 }
 
+// Strict allows changing to strict decoding. Any fields that are found in the
+// input data and do not have a corresponding struct member cause an error.
+func (d *Decoder) Strict(strict bool) *Decoder {
+	d.strict = strict
+	return d
+}
+
 func (d *Decoder) unmarshal(v interface{}) error {
 	mtype := reflect.TypeOf(v)
+	if mtype == nil {
+		return errors.New("nil cannot be unmarshaled from TOML")
+	}
 	if mtype.Kind() != reflect.Ptr {
 		return errors.New("only a pointer to struct or map can be unmarshaled from TOML")
 	}
@@ -532,16 +717,29 @@
 
 	switch elem.Kind() {
 	case reflect.Struct, reflect.Map:
+	case reflect.Interface:
+		elem = mapStringInterfaceType
 	default:
 		return errors.New("only a pointer to struct or map can be unmarshaled from TOML")
 	}
 
+	if reflect.ValueOf(v).IsNil() {
+		return errors.New("nil pointer cannot be unmarshaled from TOML")
+	}
+
 	vv := reflect.ValueOf(v).Elem()
 
+	if d.strict {
+		d.visitor = newVisitorState(d.tval)
+	}
+
 	sval, err := d.valueFromTree(elem, d.tval, &vv)
 	if err != nil {
 		return err
 	}
+	if err := d.visitor.validate(); err != nil {
+		return err
+	}
 	reflect.ValueOf(v).Elem().Set(sval)
 	return nil
 }
@@ -552,6 +750,21 @@
 	if mtype.Kind() == reflect.Ptr {
 		return d.unwrapPointer(mtype, tval, mval1)
 	}
+
+	// Check if pointer to value implements the Unmarshaler interface.
+	if mvalPtr := reflect.New(mtype); isCustomUnmarshaler(mvalPtr.Type()) {
+		d.visitor.visitAll()
+
+		if tval == nil {
+			return mvalPtr.Elem(), nil
+		}
+
+		if err := callCustomUnmarshaler(mvalPtr, tval.ToMap()); err != nil {
+			return reflect.ValueOf(nil), fmt.Errorf("unmarshal toml: %v", err)
+		}
+		return mvalPtr.Elem(), nil
+	}
+
 	var mval reflect.Value
 	switch mtype.Kind() {
 	case reflect.Struct:
@@ -561,11 +774,17 @@
 			mval = reflect.New(mtype).Elem()
 		}
 
-		for i := 0; i < mtype.NumField(); i++ {
-			mtypef := mtype.Field(i)
-			an := annotation{tag: d.tagName}
-			opts := tomlOptions(mtypef, an)
-			if opts.include {
+		switch mval.Interface().(type) {
+		case Tree:
+			mval.Set(reflect.ValueOf(tval).Elem())
+		default:
+			for i := 0; i < mtype.NumField(); i++ {
+				mtypef := mtype.Field(i)
+				an := annotation{tag: d.tagName}
+				opts := tomlOptions(mtypef, an)
+				if !opts.include {
+					continue
+				}
 				baseKey := opts.name
 				keysToTry := []string{
 					baseKey,
@@ -575,20 +794,25 @@
 				}
 
 				found := false
-				for _, key := range keysToTry {
-					exists := tval.Has(key)
-					if !exists {
-						continue
+				if tval != nil {
+					for _, key := range keysToTry {
+						exists := tval.HasPath([]string{key})
+						if !exists {
+							continue
+						}
+
+						d.visitor.push(key)
+						val := tval.GetPath([]string{key})
+						fval := mval.Field(i)
+						mvalf, err := d.valueFromToml(mtypef.Type, val, &fval)
+						if err != nil {
+							return mval, formatError(err, tval.GetPositionPath([]string{key}))
+						}
+						mval.Field(i).Set(mvalf)
+						found = true
+						d.visitor.pop()
+						break
 					}
-					val := tval.Get(key)
-					fval := mval.Field(i)
-					mvalf, err := d.valueFromToml(mtypef.Type, val, &fval)
-					if err != nil {
-						return mval, formatError(err, tval.GetPosition(key))
-					}
-					mval.Field(i).Set(mvalf)
-					found = true
-					break
 				}
 
 				if !found && opts.defaultValue != "" {
@@ -596,37 +820,66 @@
 					var val interface{}
 					var err error
 					switch mvalf.Kind() {
-					case reflect.Bool:
-						val, err = strconv.ParseBool(opts.defaultValue)
-						if err != nil {
-							return mval.Field(i), err
-						}
-					case reflect.Int:
-						val, err = strconv.Atoi(opts.defaultValue)
-						if err != nil {
-							return mval.Field(i), err
-						}
 					case reflect.String:
 						val = opts.defaultValue
+					case reflect.Bool:
+						val, err = strconv.ParseBool(opts.defaultValue)
+					case reflect.Uint:
+						val, err = strconv.ParseUint(opts.defaultValue, 10, 0)
+					case reflect.Uint8:
+						val, err = strconv.ParseUint(opts.defaultValue, 10, 8)
+					case reflect.Uint16:
+						val, err = strconv.ParseUint(opts.defaultValue, 10, 16)
+					case reflect.Uint32:
+						val, err = strconv.ParseUint(opts.defaultValue, 10, 32)
+					case reflect.Uint64:
+						val, err = strconv.ParseUint(opts.defaultValue, 10, 64)
+					case reflect.Int:
+						val, err = strconv.ParseInt(opts.defaultValue, 10, 0)
+					case reflect.Int8:
+						val, err = strconv.ParseInt(opts.defaultValue, 10, 8)
+					case reflect.Int16:
+						val, err = strconv.ParseInt(opts.defaultValue, 10, 16)
+					case reflect.Int32:
+						val, err = strconv.ParseInt(opts.defaultValue, 10, 32)
 					case reflect.Int64:
-						val, err = strconv.ParseInt(opts.defaultValue, 10, 64)
-						if err != nil {
-							return mval.Field(i), err
+						// Check if the provided number has a non-numeric extension.
+						var hasExtension bool
+						if len(opts.defaultValue) > 0 {
+							lastChar := opts.defaultValue[len(opts.defaultValue)-1]
+							if lastChar < '0' || lastChar > '9' {
+								hasExtension = true
+							}
 						}
+						// If the value is a time.Duration with extension, parse as duration.
+						// If the value is an int64 or a time.Duration without extension, parse as number.
+						if hasExtension && mvalf.Type().String() == "time.Duration" {
+							val, err = time.ParseDuration(opts.defaultValue)
+						} else {
+							val, err = strconv.ParseInt(opts.defaultValue, 10, 64)
+						}
+					case reflect.Float32:
+						val, err = strconv.ParseFloat(opts.defaultValue, 32)
 					case reflect.Float64:
 						val, err = strconv.ParseFloat(opts.defaultValue, 64)
-						if err != nil {
-							return mval.Field(i), err
-						}
 					default:
-						return mval.Field(i), fmt.Errorf("unsuported field type for default option")
+						return mvalf, fmt.Errorf("unsupported field type for default option")
 					}
-					mval.Field(i).Set(reflect.ValueOf(val))
+
+					if err != nil {
+						return mvalf, err
+					}
+					mvalf.Set(reflect.ValueOf(val).Convert(mvalf.Type()))
 				}
 
-				// save the old behavior above and try to check anonymous structs
-				if !found && opts.defaultValue == "" && mtypef.Anonymous && mtypef.Type.Kind() == reflect.Struct {
-					v, err := d.valueFromTree(mtypef.Type, tval, nil)
+				// save the old behavior above and try to check structs
+				if !found && opts.defaultValue == "" && mtypef.Type.Kind() == reflect.Struct {
+					tmpTval := tval
+					if !mtypef.Anonymous {
+						tmpTval = nil
+					}
+					fval := mval.Field(i)
+					v, err := d.valueFromTree(mtypef.Type, tmpTval, &fval)
 					if err != nil {
 						return v, err
 					}
@@ -637,13 +890,15 @@
 	case reflect.Map:
 		mval = reflect.MakeMap(mtype)
 		for _, key := range tval.Keys() {
+			d.visitor.push(key)
 			// TODO: path splits key
 			val := tval.GetPath([]string{key})
 			mvalf, err := d.valueFromToml(mtype.Elem(), val, nil)
 			if err != nil {
-				return mval, formatError(err, tval.GetPosition(key))
+				return mval, formatError(err, tval.GetPositionPath([]string{key}))
 			}
 			mval.SetMapIndex(reflect.ValueOf(key).Convert(mtype.Key()), mvalf)
+			d.visitor.pop()
 		}
 	}
 	return mval, nil
@@ -651,22 +906,52 @@
 
 // Convert toml value to marshal struct/map slice, using marshal type
 func (d *Decoder) valueFromTreeSlice(mtype reflect.Type, tval []*Tree) (reflect.Value, error) {
-	mval := reflect.MakeSlice(mtype, len(tval), len(tval))
+	mval, err := makeSliceOrArray(mtype, len(tval))
+	if err != nil {
+		return mval, err
+	}
+
 	for i := 0; i < len(tval); i++ {
+		d.visitor.push(strconv.Itoa(i))
 		val, err := d.valueFromTree(mtype.Elem(), tval[i], nil)
 		if err != nil {
 			return mval, err
 		}
 		mval.Index(i).Set(val)
+		d.visitor.pop()
+	}
+	return mval, nil
+}
+
+// Convert toml value to marshal primitive slice, using marshal type
+func (d *Decoder) valueFromOtherSlice(mtype reflect.Type, tval []interface{}) (reflect.Value, error) {
+	mval, err := makeSliceOrArray(mtype, len(tval))
+	if err != nil {
+		return mval, err
+	}
+
+	for i := 0; i < len(tval); i++ {
+		val, err := d.valueFromToml(mtype.Elem(), tval[i], nil)
+		if err != nil {
+			return mval, err
+		}
+		mval.Index(i).Set(val)
 	}
 	return mval, nil
 }
 
 // Convert toml value to marshal primitive slice, using marshal type
-func (d *Decoder) valueFromOtherSlice(mtype reflect.Type, tval []interface{}) (reflect.Value, error) {
-	mval := reflect.MakeSlice(mtype, len(tval), len(tval))
-	for i := 0; i < len(tval); i++ {
-		val, err := d.valueFromToml(mtype.Elem(), tval[i], nil)
+func (d *Decoder) valueFromOtherSliceI(mtype reflect.Type, tval interface{}) (reflect.Value, error) {
+	val := reflect.ValueOf(tval)
+	length := val.Len()
+
+	mval, err := makeSliceOrArray(mtype, length)
+	if err != nil {
+		return mval, err
+	}
+
+	for i := 0; i < length; i++ {
+		val, err := d.valueFromToml(mtype.Elem(), val.Index(i).Interface(), nil)
 		if err != nil {
 			return mval, err
 		}
@@ -675,6 +960,21 @@
 	return mval, nil
 }
 
+// Create a new slice or a new array with specified length
+func makeSliceOrArray(mtype reflect.Type, tLength int) (reflect.Value, error) {
+	var mval reflect.Value
+	switch mtype.Kind() {
+	case reflect.Slice:
+		mval = reflect.MakeSlice(mtype, tLength, tLength)
+	case reflect.Array:
+		mval = reflect.New(reflect.ArrayOf(mtype.Len(), mtype.Elem())).Elem()
+		if tLength > mtype.Len() {
+			return mval, fmt.Errorf("unmarshal: TOML array length (%v) exceeds destination array length (%v)", tLength, mtype.Len())
+		}
+	}
+	return mval, nil
+}
+
 // Convert toml value to marshal value, using marshal type. When mval1 is non-nil
 // and the given type is a struct value, merge fields into it.
 func (d *Decoder) valueFromToml(mtype reflect.Type, tval interface{}, mval1 *reflect.Value) (reflect.Value, error) {
@@ -692,18 +992,63 @@
 		if isTree(mtype) {
 			return d.valueFromTree(mtype, t, mval11)
 		}
+
+		if mtype.Kind() == reflect.Interface {
+			if mval1 == nil || mval1.IsNil() {
+				return d.valueFromTree(reflect.TypeOf(map[string]interface{}{}), t, nil)
+			} else {
+				return d.valueFromToml(mval1.Elem().Type(), t, nil)
+			}
+		}
+
 		return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to a tree", tval, tval)
 	case []*Tree:
 		if isTreeSequence(mtype) {
 			return d.valueFromTreeSlice(mtype, t)
 		}
+		if mtype.Kind() == reflect.Interface {
+			if mval1 == nil || mval1.IsNil() {
+				return d.valueFromTreeSlice(reflect.TypeOf([]map[string]interface{}{}), t)
+			} else {
+				ival := mval1.Elem()
+				return d.valueFromToml(mval1.Elem().Type(), t, &ival)
+			}
+		}
 		return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to trees", tval, tval)
 	case []interface{}:
+		d.visitor.visit()
 		if isOtherSequence(mtype) {
 			return d.valueFromOtherSlice(mtype, t)
 		}
+		if mtype.Kind() == reflect.Interface {
+			if mval1 == nil || mval1.IsNil() {
+				return d.valueFromOtherSlice(reflect.TypeOf([]interface{}{}), t)
+			} else {
+				ival := mval1.Elem()
+				return d.valueFromToml(mval1.Elem().Type(), t, &ival)
+			}
+		}
 		return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to a slice", tval, tval)
 	default:
+		d.visitor.visit()
+		mvalPtr := reflect.New(mtype)
+
+		// Check if pointer to value implements the Unmarshaler interface.
+		if isCustomUnmarshaler(mvalPtr.Type()) {
+			if err := callCustomUnmarshaler(mvalPtr, tval); err != nil {
+				return reflect.ValueOf(nil), fmt.Errorf("unmarshal toml: %v", err)
+			}
+			return mvalPtr.Elem(), nil
+		}
+
+		// Check if pointer to value implements the encoding.TextUnmarshaler.
+		if isTextUnmarshaler(mvalPtr.Type()) && !isTimeType(mtype) {
+			if err := d.unmarshalText(tval, mvalPtr); err != nil {
+				return reflect.ValueOf(nil), fmt.Errorf("unmarshal text: %v", err)
+			}
+			return mvalPtr.Elem(), nil
+		}
+
 		switch mtype.Kind() {
 		case reflect.Bool, reflect.Struct:
 			val := reflect.ValueOf(tval)
@@ -754,38 +1099,50 @@
 				}
 				return reflect.ValueOf(d), nil
 			}
-			if !val.Type().ConvertibleTo(mtype) {
+			if !val.Type().ConvertibleTo(mtype) || val.Kind() == reflect.Float64 {
 				return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v", tval, tval, mtype.String())
 			}
-			if reflect.Indirect(reflect.New(mtype)).OverflowInt(val.Convert(mtype).Int()) {
+			if reflect.Indirect(reflect.New(mtype)).OverflowInt(val.Convert(reflect.TypeOf(int64(0))).Int()) {
 				return reflect.ValueOf(nil), fmt.Errorf("%v(%T) would overflow %v", tval, tval, mtype.String())
 			}
 
 			return val.Convert(mtype), nil
 		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
 			val := reflect.ValueOf(tval)
-			if !val.Type().ConvertibleTo(mtype) {
+			if !val.Type().ConvertibleTo(mtype) || val.Kind() == reflect.Float64 {
 				return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v", tval, tval, mtype.String())
 			}
 
 			if val.Convert(reflect.TypeOf(int(1))).Int() < 0 {
 				return reflect.ValueOf(nil), fmt.Errorf("%v(%T) is negative so does not fit in %v", tval, tval, mtype.String())
 			}
-			if reflect.Indirect(reflect.New(mtype)).OverflowUint(uint64(val.Convert(mtype).Uint())) {
+			if reflect.Indirect(reflect.New(mtype)).OverflowUint(val.Convert(reflect.TypeOf(uint64(0))).Uint()) {
 				return reflect.ValueOf(nil), fmt.Errorf("%v(%T) would overflow %v", tval, tval, mtype.String())
 			}
 
 			return val.Convert(mtype), nil
 		case reflect.Float32, reflect.Float64:
 			val := reflect.ValueOf(tval)
-			if !val.Type().ConvertibleTo(mtype) {
+			if !val.Type().ConvertibleTo(mtype) || val.Kind() == reflect.Int64 {
 				return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v", tval, tval, mtype.String())
 			}
-			if reflect.Indirect(reflect.New(mtype)).OverflowFloat(val.Convert(mtype).Float()) {
+			if reflect.Indirect(reflect.New(mtype)).OverflowFloat(val.Convert(reflect.TypeOf(float64(0))).Float()) {
 				return reflect.ValueOf(nil), fmt.Errorf("%v(%T) would overflow %v", tval, tval, mtype.String())
 			}
 
 			return val.Convert(mtype), nil
+		case reflect.Interface:
+			if mval1 == nil || mval1.IsNil() {
+				return reflect.ValueOf(tval), nil
+			} else {
+				ival := mval1.Elem()
+				return d.valueFromToml(mval1.Elem().Type(), t, &ival)
+			}
+		case reflect.Slice, reflect.Array:
+			if isOtherSequence(mtype) && isOtherSequence(reflect.TypeOf(t)) {
+				return d.valueFromOtherSliceI(mtype, t)
+			}
+			return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v(%v)", tval, tval, mtype, mtype.Kind())
 		default:
 			return reflect.ValueOf(nil), fmt.Errorf("Can't convert %v(%T) to %v(%v)", tval, tval, mtype, mtype.Kind())
 		}
@@ -795,7 +1152,7 @@
 func (d *Decoder) unwrapPointer(mtype reflect.Type, tval interface{}, mval1 *reflect.Value) (reflect.Value, error) {
 	var melem *reflect.Value
 
-	if mval1 != nil && !mval1.IsNil() && mtype.Elem().Kind() == reflect.Struct {
+	if mval1 != nil && !mval1.IsNil() && (mtype.Elem().Kind() == reflect.Struct || mtype.Elem().Kind() == reflect.Interface) {
 		elem := mval1.Elem()
 		melem = &elem
 	}
@@ -809,6 +1166,12 @@
 	return mval, nil
 }
 
+func (d *Decoder) unmarshalText(tval interface{}, mval reflect.Value) error {
+	var buf bytes.Buffer
+	fmt.Fprint(&buf, tval)
+	return callTextUnmarshaler(mval, buf.Bytes())
+}
+
 func tomlOptions(vf reflect.StructField, an annotation) tomlOpts {
 	tag := vf.Tag.Get(an.tag)
 	parse := strings.Split(tag, ",")
@@ -818,12 +1181,15 @@
 	}
 	commented, _ := strconv.ParseBool(vf.Tag.Get(an.commented))
 	multiline, _ := strconv.ParseBool(vf.Tag.Get(an.multiline))
+	literal, _ := strconv.ParseBool(vf.Tag.Get(an.literal))
 	defaultValue := vf.Tag.Get(tagDefault)
 	result := tomlOpts{
 		name:         vf.Name,
+		nameFromTag:  false,
 		comment:      comment,
 		commented:    commented,
 		multiline:    multiline,
+		literal:      literal,
 		include:      true,
 		omitempty:    false,
 		defaultValue: defaultValue,
@@ -833,6 +1199,7 @@
 			result.include = false
 		} else {
 			result.name = strings.Trim(parse[0], " ")
+			result.nameFromTag = true
 		}
 	}
 	if vf.PkgPath != "" {
@@ -849,11 +1216,7 @@
 
 func isZero(val reflect.Value) bool {
 	switch val.Type().Kind() {
-	case reflect.Map:
-		fallthrough
-	case reflect.Array:
-		fallthrough
-	case reflect.Slice:
+	case reflect.Slice, reflect.Array, reflect.Map:
 		return val.Len() == 0
 	default:
 		return reflect.DeepEqual(val.Interface(), reflect.Zero(val.Type()).Interface())
@@ -866,3 +1229,80 @@
 	}
 	return fmt.Errorf("%s: %s", pos, err)
 }
+
+// visitorState keeps track of which keys were unmarshaled.
+type visitorState struct {
+	tree   *Tree
+	path   []string
+	keys   map[string]struct{}
+	active bool
+}
+
+func newVisitorState(tree *Tree) visitorState {
+	path, result := []string{}, map[string]struct{}{}
+	insertKeys(path, result, tree)
+	return visitorState{
+		tree:   tree,
+		path:   path[:0],
+		keys:   result,
+		active: true,
+	}
+}
+
+func (s *visitorState) push(key string) {
+	if s.active {
+		s.path = append(s.path, key)
+	}
+}
+
+func (s *visitorState) pop() {
+	if s.active {
+		s.path = s.path[:len(s.path)-1]
+	}
+}
+
+func (s *visitorState) visit() {
+	if s.active {
+		delete(s.keys, strings.Join(s.path, "."))
+	}
+}
+
+func (s *visitorState) visitAll() {
+	if s.active {
+		for k := range s.keys {
+			if strings.HasPrefix(k, strings.Join(s.path, ".")) {
+				delete(s.keys, k)
+			}
+		}
+	}
+}
+
+func (s *visitorState) validate() error {
+	if !s.active {
+		return nil
+	}
+	undecoded := make([]string, 0, len(s.keys))
+	for key := range s.keys {
+		undecoded = append(undecoded, key)
+	}
+	sort.Strings(undecoded)
+	if len(undecoded) > 0 {
+		return fmt.Errorf("undecoded keys: %q", undecoded)
+	}
+	return nil
+}
+
+func insertKeys(path []string, m map[string]struct{}, tree *Tree) {
+	for k, v := range tree.values {
+		switch node := v.(type) {
+		case []*Tree:
+			for i, item := range node {
+				insertKeys(append(path, k, strconv.Itoa(i)), m, item)
+			}
+		case *Tree:
+			insertKeys(append(path, k), m, node)
+		case *tomlValue:
+			m[strings.Join(append(path, k), ".")] = struct{}{}
+		}
+	}
+}