func (s *Section) MapTo(v interface{}) error {
	return s.mapTo(v, false)
}
MapTo maps section to given struct.
package ini
import (
	"bytes"
	"errors"
	"fmt"
	"reflect"
	"strings"
	"time"
	"unicode"
)
type NameMapper func(string) string
var (
	SnackCase NameMapper = func(raw string) string {
		newstr := make([]rune, 0, len(raw))
		for i, chr := range raw {
			if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
				if i > 0 {
					newstr = append(newstr, '_')
				}
			}
			newstr = append(newstr, unicode.ToUpper(chr))
		}
		return string(newstr)
	}
	TitleUnderscore NameMapper = func(raw string) string {
		newstr := make([]rune, 0, len(raw))
		for i, chr := range raw {
			if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
				if i > 0 {
					newstr = append(newstr, '_')
				}
				chr -= 'A' - 'a'
			}
			newstr = append(newstr, chr)
		}
		return string(newstr)
	}
)
func (s *Section) parseFieldName(raw, actual string) string {
	if len(actual) > 0 {
		return actual
	}
	if s.f.NameMapper != nil {
		return s.f.NameMapper(raw)
	}
	return raw
}
func parseDelim(actual string) string {
	if len(actual) > 0 {
		return actual
	}
	return ","
}
var reflectTime = reflect.TypeOf(time.Now()).Kind()
func setSliceWithProperType(key *Key, field reflect.Value, delim string, allowShadow, isStrict bool) error {
	var strs []string
	if allowShadow {
		strs = key.StringsWithShadows(delim)
	} else {
		strs = key.Strings(delim)
	}
	numVals := len(strs)
	if numVals == 0 {
		return nil
	}
	var vals interface{}
	var err error
	sliceOf := field.Type().Elem().Kind()
	switch sliceOf {
	case reflect.String:
		vals = strs
	case reflect.Int:
		vals, err = key.parseInts(strs, true, false)
	case reflect.Int64:
		vals, err = key.parseInt64s(strs, true, false)
	case reflect.Uint:
		vals, err = key.parseUints(strs, true, false)
	case reflect.Uint64:
		vals, err = key.parseUint64s(strs, true, false)
	case reflect.Float64:
		vals, err = key.parseFloat64s(strs, true, false)
	case reflect.Bool:
		vals, err = key.parseBools(strs, true, false)
	case reflectTime:
		vals, err = key.parseTimesFormat(time.RFC3339, strs, true, false)
	default:
		return fmt.Errorf("unsupported type '[]%s'", sliceOf)
	}
	if err != nil && isStrict {
		return err
	}
	slice := reflect.MakeSlice(field.Type(), numVals, numVals)
	for i := 0; i < numVals; i++ {
		switch sliceOf {
		case reflect.String:
			slice.Index(i).Set(reflect.ValueOf(vals.([]string)[i]))
		case reflect.Int:
			slice.Index(i).Set(reflect.ValueOf(vals.([]int)[i]))
		case reflect.Int64:
			slice.Index(i).Set(reflect.ValueOf(vals.([]int64)[i]))
		case reflect.Uint:
			slice.Index(i).Set(reflect.ValueOf(vals.([]uint)[i]))
		case reflect.Uint64:
			slice.Index(i).Set(reflect.ValueOf(vals.([]uint64)[i]))
		case reflect.Float64:
			slice.Index(i).Set(reflect.ValueOf(vals.([]float64)[i]))
		case reflect.Bool:
			slice.Index(i).Set(reflect.ValueOf(vals.([]bool)[i]))
		case reflectTime:
			slice.Index(i).Set(reflect.ValueOf(vals.([]time.Time)[i]))
		}
	}
	field.Set(slice)
	return nil
}
func wrapStrictError(err error, isStrict bool) error {
	if isStrict {
		return err
	}
	return nil
}
func setWithProperType(t reflect.Type, key *Key, field reflect.Value, delim string, allowShadow, isStrict bool) error {
	vt := t
	isPtr := t.Kind() == reflect.Ptr
	if isPtr {
		vt = t.Elem()
	}
	switch vt.Kind() {
	case reflect.String:
		stringVal := key.String()
		if isPtr {
			field.Set(reflect.ValueOf(&stringVal))
		} else if len(stringVal) > 0 {
			field.SetString(key.String())
		}
	case reflect.Bool:
		boolVal, err := key.Bool()
		if err != nil {
			return wrapStrictError(err, isStrict)
		}
		if isPtr {
			field.Set(reflect.ValueOf(&boolVal))
		} else {
			field.SetBool(boolVal)
		}
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		if vt.Name() == "Duration" {
			durationVal, err := key.Duration()
			if err != nil {
				if intVal, err := key.Int64(); err == nil {
					field.SetInt(intVal)
					return nil
				}
				return wrapStrictError(err, isStrict)
			}
			if isPtr {
				field.Set(reflect.ValueOf(&durationVal))
			} else if int64(durationVal) > 0 {
				field.Set(reflect.ValueOf(durationVal))
			}
			return nil
		}
		intVal, err := key.Int64()
		if err != nil {
			return wrapStrictError(err, isStrict)
		}
		if isPtr {
			pv := reflect.New(t.Elem())
			pv.Elem().SetInt(intVal)
			field.Set(pv)
		} else {
			field.SetInt(intVal)
		}
	case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		durationVal, err := key.Duration()
		if err == nil && uint64(durationVal) > 0 {
			if isPtr {
				field.Set(reflect.ValueOf(&durationVal))
			} else {
				field.Set(reflect.ValueOf(durationVal))
			}
			return nil
		}
		uintVal, err := key.Uint64()
		if err != nil {
			return wrapStrictError(err, isStrict)
		}
		if isPtr {
			pv := reflect.New(t.Elem())
			pv.Elem().SetUint(uintVal)
			field.Set(pv)
		} else {
			field.SetUint(uintVal)
		}
	case reflect.Float32, reflect.Float64:
		floatVal, err := key.Float64()
		if err != nil {
			return wrapStrictError(err, isStrict)
		}
		if isPtr {
			pv := reflect.New(t.Elem())
			pv.Elem().SetFloat(floatVal)
			field.Set(pv)
		} else {
			field.SetFloat(floatVal)
		}
	case reflectTime:
		timeVal, err := key.Time()
		if err != nil {
			return wrapStrictError(err, isStrict)
		}
		if isPtr {
			field.Set(reflect.ValueOf(&timeVal))
		} else {
			field.Set(reflect.ValueOf(timeVal))
		}
	case reflect.Slice:
		return setSliceWithProperType(key, field, delim, allowShadow, isStrict)
	default:
		return fmt.Errorf("unsupported type %q", t)
	}
	return nil
}
func parseTagOptions(tag string) (rawName string, omitEmpty bool, allowShadow bool, allowNonUnique bool, extends bool) {
	opts := strings.SplitN(tag, ",", 5)
	rawName = opts[0]
	for _, opt := range opts[1:] {
		omitEmpty = omitEmpty || (opt == "omitempty")
		allowShadow = allowShadow || (opt == "allowshadow")
		allowNonUnique = allowNonUnique || (opt == "nonunique")
		extends = extends || (opt == "extends")
	}
	return rawName, omitEmpty, allowShadow, allowNonUnique, extends
}
func (s *Section) mapToField(val reflect.Value, isStrict bool, sectionIndex int, sectionName string) error {
	if val.Kind() == reflect.Ptr {
		val = val.Elem()
	}
	typ := val.Type()
	for i := 0; i < typ.NumField(); i++ {
		field := val.Field(i)
		tpField := typ.Field(i)
		tag := tpField.Tag.Get("ini")
		if tag == "-" {
			continue
		}
		rawName, _, allowShadow, allowNonUnique, extends := parseTagOptions(tag)
		fieldName := s.parseFieldName(tpField.Name, rawName)
		if len(fieldName) == 0 || !field.CanSet() {
			continue
		}
		isStruct := tpField.Type.Kind() == reflect.Struct
		isStructPtr := tpField.Type.Kind() == reflect.Ptr && tpField.Type.Elem().Kind() == reflect.Struct
		isAnonymousPtr := tpField.Type.Kind() == reflect.Ptr && tpField.Anonymous
		if isAnonymousPtr {
			field.Set(reflect.New(tpField.Type.Elem()))
		}
		if extends && (isAnonymousPtr || (isStruct && tpField.Anonymous)) {
			if isStructPtr && field.IsNil() {
				field.Set(reflect.New(tpField.Type.Elem()))
			}
			fieldSection := s
			if rawName != "" {
				sectionName = s.name + s.f.options.ChildSectionDelimiter + rawName
				if secs, err := s.f.SectionsByName(sectionName); err == nil && sectionIndex < len(secs) {
					fieldSection = secs[sectionIndex]
				}
			}
			if err := fieldSection.mapToField(field, isStrict, sectionIndex, sectionName); err != nil {
				return fmt.Errorf("map to field %q: %v", fieldName, err)
			}
		} else if isAnonymousPtr || isStruct || isStructPtr {
			if secs, err := s.f.SectionsByName(fieldName); err == nil {
				if len(secs) <= sectionIndex {
					return fmt.Errorf("there are not enough sections (%d <= %d) for the field %q", len(secs), sectionIndex, fieldName)
				}
				if isStructPtr && field.IsNil() {
					field.Set(reflect.New(tpField.Type.Elem()))
				}
				if err = secs[sectionIndex].mapToField(field, isStrict, sectionIndex, fieldName); err != nil {
					return fmt.Errorf("map to field %q: %v", fieldName, err)
				}
				continue
			}
		}
		if allowNonUnique && tpField.Type.Kind() == reflect.Slice {
			newField, err := s.mapToSlice(fieldName, field, isStrict)
			if err != nil {
				return fmt.Errorf("map to slice %q: %v", fieldName, err)
			}
			field.Set(newField)
			continue
		}
		if key, err := s.GetKey(fieldName); err == nil {
			delim := parseDelim(tpField.Tag.Get("delim"))
			if err = setWithProperType(tpField.Type, key, field, delim, allowShadow, isStrict); err != nil {
				return fmt.Errorf("set field %q: %v", fieldName, err)
			}
		}
	}
	return nil
}
func (s *Section) mapToSlice(secName string, val reflect.Value, isStrict bool) (reflect.Value, error) {
	secs, err := s.f.SectionsByName(secName)
	if err != nil {
		return reflect.Value{}, err
	}
	typ := val.Type().Elem()
	for i, sec := range secs {
		elem := reflect.New(typ)
		if err = sec.mapToField(elem, isStrict, i, sec.name); err != nil {
			return reflect.Value{}, fmt.Errorf("map to field from section %q: %v", secName, err)
		}
		val = reflect.Append(val, elem.Elem())
	}
	return val, nil
}
func (s *Section) mapTo(v interface{}, isStrict bool) error {
	typ := reflect.TypeOf(v)
	val := reflect.ValueOf(v)
	if typ.Kind() == reflect.Ptr {
		typ = typ.Elem()
		val = val.Elem()
	} else {
		return errors.New("not a pointer to a struct")
	}
	if typ.Kind() == reflect.Slice {
		newField, err := s.mapToSlice(s.name, val, isStrict)
		if err != nil {
			return err
		}
		val.Set(newField)
		return nil
	}
	return s.mapToField(val, isStrict, 0, s.name)
}