func (d *Decoder) Strict(strict bool) *Decoder {
	d.strict = strict
	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.
package toml
import (
	"bytes"
	"encoding"
	"errors"
	"fmt"
	"io"
	"reflect"
	"sort"
	"strconv"
	"strings"
	"time"
)
const (
	tagFieldName    = "toml"
	tagFieldComment = "comment"
	tagCommented    = "commented"
	tagMultiline    = "multiline"
	tagDefault      = "default"
)
type tomlOpts struct {
	name         string
	nameFromTag  bool
	comment      string
	commented    bool
	multiline    bool
	include      bool
	omitempty    bool
	defaultValue string
}
type encOpts struct {
	quoteMapKeys            bool
	arraysOneElementPerLine bool
}
var encOptsDefaults = encOpts{
	quoteMapKeys: false,
}
type annotation struct {
	tag          string
	comment      string
	commented    string
	multiline    string
	defaultValue string
}
var annotationDefault = annotation{
	tag:          tagFieldName,
	comment:      tagFieldComment,
	commented:    tagCommented,
	multiline:    tagMultiline,
	defaultValue: tagDefault,
}
type marshalOrder int
const (
	OrderAlphabetical marshalOrder = iota + 1
	OrderPreserve
)
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{}{})
func isPrimitive(mtype reflect.Type) bool {
	switch mtype.Kind() {
	case reflect.Ptr:
		return isPrimitive(mtype.Elem())
	case reflect.Bool:
		return true
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return true
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		return true
	case reflect.Float32, reflect.Float64:
		return true
	case reflect.String:
		return true
	case reflect.Struct:
		return isTimeType(mtype)
	default:
		return false
	}
}
func isTimeType(mtype reflect.Type) bool {
	return mtype == timeType || mtype == localDateType || mtype == localDateTimeType || mtype == localTimeType
}
func isTreeSequence(mtype reflect.Type) bool {
	switch mtype.Kind() {
	case reflect.Ptr:
		return isTreeSequence(mtype.Elem())
	case reflect.Slice, reflect.Array:
		return isTree(mtype.Elem())
	default:
		return false
	}
}
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
	}
}
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
	}
}
func isOtherSequence(mtype reflect.Type) bool {
	switch mtype.Kind() {
	case reflect.Ptr:
		return isOtherSequence(mtype.Elem())
	case reflect.Slice, reflect.Array:
		return !isTreeSequence(mtype)
	default:
		return false
	}
}
func isTree(mtype reflect.Type) bool {
	switch mtype.Kind() {
	case reflect.Ptr:
		return isTree(mtype.Elem())
	case reflect.Map:
		return true
	case reflect.Struct:
		return !isPrimitive(mtype)
	default:
		return false
	}
}
func isCustomMarshaler(mtype reflect.Type) bool {
	return mtype.Implements(marshalerType)
}
func callCustomMarshaler(mval reflect.Value) ([]byte, error) {
	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)
}
type Marshaler interface {
	MarshalTOML() ([]byte, error)
}
type Unmarshaler interface {
	UnmarshalTOML(interface{}) error
}
func Marshal(v interface{}) ([]byte, error) {
	return NewEncoder(nil).marshal(v)
}
type Encoder struct {
	w io.Writer
	encOpts
	annotation
	line        int
	col         int
	order       marshalOrder
	promoteAnon bool
	indentation string
}
func NewEncoder(w io.Writer) *Encoder {
	return &Encoder{
		w:           w,
		encOpts:     encOptsDefaults,
		annotation:  annotationDefault,
		line:        0,
		col:         1,
		order:       OrderAlphabetical,
		indentation: "  ",
	}
}
func (e *Encoder) Encode(v interface{}) error {
	b, err := e.marshal(v)
	if err != nil {
		return err
	}
	if _, err := e.w.Write(b); err != nil {
		return err
	}
	return nil
}
func (e *Encoder) QuoteMapKeys(v bool) *Encoder {
	e.quoteMapKeys = v
	return e
}
func (e *Encoder) ArraysWithOneElementPerLine(v bool) *Encoder {
	e.arraysOneElementPerLine = v
	return e
}
func (e *Encoder) Order(ord marshalOrder) *Encoder {
	e.order = ord
	return e
}
func (e *Encoder) Indentation(indent string) *Encoder {
	e.indentation = indent
	return e
}
func (e *Encoder) SetTagName(v string) *Encoder {
	e.tag = v
	return e
}
func (e *Encoder) SetTagComment(v string) *Encoder {
	e.comment = v
	return e
}
func (e *Encoder) SetTagCommented(v string) *Encoder {
	e.commented = v
	return e
}
func (e *Encoder) SetTagMultiline(v string) *Encoder {
	e.multiline = v
	return e
}
func (e *Encoder) PromoteAnonymous(promote bool) *Encoder {
	e.promoteAnon = promote
	return e
}
func (e *Encoder) marshal(v interface{}) ([]byte, error) {
	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:
	case reflect.Ptr:
		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")
	}
	sval := reflect.ValueOf(v)
	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, e.indentation, false)
	return buf.Bytes(), err
}
func (e *Encoder) nextTree() *Tree {
	return newTreeWithPosition(Position{Line: e.line, Col: 1})
}
func (e *Encoder) valueToTree(mtype reflect.Type, mval reflect.Value) (*Tree, error) {
	if mtype.Kind() == reflect.Ptr {
		return e.valueToTree(mtype.Elem(), mval.Elem())
	}
	tval := e.nextTree()
	switch mtype.Kind() {
	case reflect.Struct:
		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,
						}, val)
					}
				}
			}
		}
	case reflect.Map:
		keys := mval.MapKeys()
		if e.order == OrderPreserve && len(keys) > 0 {
			typ := keys[0].Type()
			kind := keys[0].Kind()
			if kind == reflect.String {
				ikeys := make([]string, len(keys))
				for i := range keys {
					ikeys[i] = keys[i].Interface().(string)
				}
				sort.Strings(ikeys)
				for i := range ikeys {
					keys[i] = reflect.ValueOf(ikeys[i]).Convert(typ)
				}
			}
		}
		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.order, e.arraysOneElementPerLine)
				if err != nil {
					return nil, err
				}
				tval.SetPath([]string{keyStr}, val)
			} else {
				tval.SetPath([]string{key.String()}, val)
			}
		}
	}
	return tval, nil
}
func (e *Encoder) valueToTreeSlice(mtype reflect.Type, mval reflect.Value) ([]*Tree, error) {
	tval := make([]*Tree, mval.Len(), mval.Len())
	for i := 0; i < mval.Len(); i++ {
		val, err := e.valueToTree(mtype.Elem(), mval.Index(i))
		if err != nil {
			return nil, err
		}
		tval[i] = val
	}
	return tval, nil
}
func (e *Encoder) valueToOtherSlice(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
	tval := make([]interface{}, mval.Len(), mval.Len())
	for i := 0; i < mval.Len(); i++ {
		val, err := e.valueToToml(mtype.Elem(), mval.Index(i))
		if err != nil {
			return nil, err
		}
		tval[i] = val
	}
	return tval, nil
}
func (e *Encoder) valueToToml(mtype reflect.Type, mval reflect.Value) (interface{}, error) {
	if mtype.Kind() == reflect.Ptr {
		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)
	default:
		switch mtype.Kind() {
		case reflect.Bool:
			return mval.Bool(), nil
		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
			if mtype.Kind() == reflect.Int64 && mtype == reflect.TypeOf(time.Duration(1)) {
				return fmt.Sprint(mval), nil
			}
			return mval.Int(), nil
		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
			return mval.Uint(), nil
		case reflect.Float32, reflect.Float64:
			return mval.Float(), nil
		case reflect.String:
			return mval.String(), nil
		case reflect.Struct:
			return mval.Interface(), nil
		default:
			return nil, fmt.Errorf("Marshal can't handle %v(%v)", mtype, mtype.Kind())
		}
	}
}
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
}
func (e *Encoder) wrapTomlValue(val interface{}, parent *Tree) interface{} {
	_, isTree := val.(*Tree)
	_, isTreeS := val.([]*Tree)
	if isTree || isTreeS {
		return val
	}
	ret := &tomlValue{
		value: val,
		position: Position{
			e.line,
			parent.position.Col,
		},
	}
	e.line++
	return ret
}
func (t *Tree) Unmarshal(v interface{}) error {
	d := Decoder{tval: t, tagName: tagFieldName}
	return d.unmarshal(v)
}
func (t *Tree) Marshal() ([]byte, error) {
	var buf bytes.Buffer
	_, err := t.WriteTo(&buf)
	if err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}
func Unmarshal(data []byte, v interface{}) error {
	t, err := LoadReader(bytes.NewReader(data))
	if err != nil {
		return err
	}
	return t.Unmarshal(v)
}
type Decoder struct {
	r    io.Reader
	tval *Tree
	encOpts
	tagName string
	strict  bool
	visitor visitorState
}
func NewDecoder(r io.Reader) *Decoder {
	return &Decoder{
		r:       r,
		encOpts: encOptsDefaults,
		tagName: tagFieldName,
	}
}
func (d *Decoder) Decode(v interface{}) error {
	var err error
	d.tval, err = LoadReader(d.r)
	if err != nil {
		return err
	}
	return d.unmarshal(v)
}
func (d *Decoder) SetTagName(v string) *Decoder {
	d.tagName = v
	return d
}