// NestedMap returns a deep copy of map[string]interface{} value of a nested field.
// Returns false if value is not found and an error if not a map[string]interface{}.
778398
func NestedMap(obj map[string]interface{}, fields ...string) (map[string]interface{}, bool, error) {
	m, found, err := nestedMapNoCopy(obj, fields...)
	if !found || err != nil {
		return nil, found, err
	}
	return runtime.DeepCopyJSON(m), true, nil
}
742778
func IsMap(err error) bool {
	if err != nil {
		switch err.(type) {
		case *Map:
			return true
		}
	}
	return false
}
1205657
func FlattenJsonMap(m map[string]interface{}) (map[string][]string, error) {
	outMap := make(map[string][]string, len(m))
	if err := flattenJson("", outMap, m); err != nil {
		return nil, err
	}
	return outMap, nil
}
1204602
func CheckNestedMap(
	mapSS map[string]string,
	mapSMI map[string]map[string]int,
	mapASAMSAS map[int][]map[string][]string,
	mapPSSI map[*string]sub.MyInterface,
	mapMPSI map[*MyStruct]int,
	mapMSI map[MyStruct]int,
	mapMSII map[sub.MyInterface]int,
	mapMSIISI map[struct{}]interface{},
	x ...map[sub.MyInterface]int,
) (rmapSS map[string]string, rmapSMI map[string]map[string]int, rmapASAMSAS map[int][]map[string][]string, rmapPSSI map[*string]sub.MyInterface, rmapMPSI map[*MyStruct]int, rmapMSI map[MyStruct]int, rmapMSII map[sub.MyInterface]int, rmapMSIISI map[struct{}]interface{}) {

	return rmapSS, rmapSMI, rmapASAMSAS, rmapPSSI, rmapMPSI, rmapMSI, rmapMSII, rmapMSIISI
}
1093314
func (that *JSONNode) Map(key string) *JSONNode {
	if that.t != TypeUndefined && that.t != TypeMap {
		panic(ErrorMultipleType)
	}
	if that.m == nil {
		that.m = make(map[string]*JSONNode)
		that.t = TypeMap
	}
	if _, ok := that.m[key]; ok {
		return that.m[key]
	}
	that.m[key] = &JSONNode{}
	return that.m[key]
}
1330761
func ParseOrderedMap(data string) ([]string, []string, error) {
	parts := strings.Split(data, MapSeparator)
	keys := make([]string, len(parts))
	values := make([]string, len(parts))
	for i, part := range parts {
		split := strings.SplitN(part, MapEquals, 2)
		if len(split) != 2 {
			return nil, nil, fmt.Errorf("Failed to parse map (key-value pair %v does not contain separator '%v'): %v", i+1, MapEquals, part)
		}
		keys[i] = split[0]
		values[i] = split[1]
	}
	return keys, values, nil
}
377321
func NestedFloat64(obj map[string]interface{}, fields ...string) (float64, bool, error) {
	val, found, err := NestedFieldNoCopy(obj, fields...)
	if !found || err != nil {
		return 0, found, err
	}
	f, ok := val.(float64)
	if !ok {
		return 0, false, fmt.Errorf("%v accessor error: %v is of the type %T, expected float64", jsonPath(fields), val, val)
	}
	return f, true, nil
}
358256
func (sm StringMap) AddMap(m map[string]string) {
	if sm.Size() == 0 {
		sm = m
	} else {
		for k, v := range m {
			sm[k] = v
		}
	}
}
698587
func mergeMaps(m1, m2 map[string]interface{}) map[string]interface{} {
	for k, v := range m2 {
		if _, ok := m1[k]; !ok {
			m1[k] = v
		}
	}
	return m1
}
143687
func nestedVal(name string, tree map[interface{}]interface{}) (interface{}, bool) {
	if sections := strings.Split(name, "."); len(sections) > 1 {
		node := tree
		for _, section := range sections[:len(sections)-1] {
			child, ok := node[section]
			if !ok {
				return nil, false
			}
			ctype, ok := child.(map[interface{}]interface{})
			if !ok {
				return nil, false
			}
			node = ctype
		}
		if val, ok := node[sections[len(sections)-1]]; ok {
			return val, true
		}
	}
	return nil, false
}
1012508
func (m *Meta) fieldsMap() map[string]interface{} {
	return map[string]interface{}{
		"config_service_host": &m.ConfigServiceHost,
		"config_dir":          &m.ConfigDir,
		"tracked_files":       &m.TrackedFiles,
		"fail_on_warnings":    &m.FailOnWarnings,
	}
}
417231
func (s *GeoDistanceSort) NestedFilter(nestedFilter Query) *GeoDistanceSort {
	s.nestedFilter = nestedFilter
	return s
}
596272
func (s *Struct) nested(val reflect.Value) interface{} {
	var finalVal interface{}

	v := reflect.ValueOf(val.Interface())
	if v.Kind() == reflect.Ptr {
		v = v.Elem()
	}

	switch v.Kind() {
	case reflect.Struct:
		n := New(val.Interface())
		n.TagName = s.TagName
		m := n.Map()

		// do not add the converted value if there are no exported fields, ie:
		// time.Time
		if len(m) == 0 {
			finalVal = val.Interface()
		} else {
			finalVal = m
		}
	case reflect.Map:
		// get the element type of the map
		mapElem := val.Type()
		switch val.Type().Kind() {
		case reflect.Ptr, reflect.Array, reflect.Map,
			reflect.Slice, reflect.Chan:
			mapElem = val.Type().Elem()
			if mapElem.Kind() == reflect.Ptr {
				mapElem = mapElem.Elem()
			}
		}

		// only iterate over struct types, ie: map[string]StructType,
		// map[string][]StructType,
		if mapElem.Kind() == reflect.Struct ||
			(mapElem.Kind() == reflect.Slice &&
				mapElem.Elem().Kind() == reflect.Struct) {
			m := make(map[string]interface{}, val.Len())
			for _, k := range val.MapKeys() {
				m[k.String()] = s.nested(val.MapIndex(k))
			}
			finalVal = m
			break
		}

		// TODO(arslan): should this be optional?
		finalVal = val.Interface()
	case reflect.Slice, reflect.Array:
		if val.Type().Kind() == reflect.Interface {
			finalVal = val.Interface()
			break
		}

		// TODO(arslan): should this be optional?
		// do not iterate of non struct types, just pass the value. Ie: []int,
		// []string, co... We only iterate further if it's a struct.
		// i.e []foo or []*foo
		if val.Type().Elem().Kind() != reflect.Struct &&
			!(val.Type().Elem().Kind() == reflect.Ptr &&
				val.Type().Elem().Elem().Kind() == reflect.Struct) {
			finalVal = val.Interface()
			break
		}

		slices := make([]interface{}, val.Len())
		for x := 0; x < val.Len(); x++ {
			slices[x] = s.nested(val.Index(x))
		}
		finalVal = slices
	default:
		finalVal = val.Interface()
	}

	return finalVal
}
745658
func (s GeoDistanceSort) NestedPath(nestedPath string) GeoDistanceSort {
	s.nestedPath = &nestedPath
	return s
}
1313710
func Nested(field *gentypes.FieldType, filter interface{}) *nested {

	// Hm.  Elasticsearch doc seems to insinuate we don't need
	// this path + ".k" but unit tests say otherwise
	fl := []interface{}{
		Term(field.Path+".k", field.Field),
		filter,
	}
	return &nested{&NestedFilter{
		Filter: &and{fl},
		Path:   field.Path,
	}}
}
882601
func (s *ScriptSort) NestedFilter(nestedFilter Query) *ScriptSort {
	s.nestedFilter = nestedFilter
	return s
}
1091572
func NestedFieldNoCopy(obj map[string]interface{}, fields ...string) (interface{}, bool, error) {
	var val interface{} = obj

	for i, field := range fields {
		if val == nil {
			return nil, false, nil
		}
		if m, ok := val.(map[string]interface{}); ok {
			val, ok = m[field]
			if !ok {
				return nil, false, nil
			}
		} else {
			return nil, false, fmt.Errorf("%v accessor error: %v is of the type %T, expected map[string]interface{}", jsonPath(fields[:i+1]), val, val)
		}
	}
	return val, true, nil
}