package msgpack

import (
	
	
	

	
)

var errArrayStruct = errors.New("msgpack: number of fields in array-encoded struct has changed")

var (
	mapStringStringPtrType = reflect.TypeOf((*map[string]string)(nil))
	mapStringStringType    = mapStringStringPtrType.Elem()
)

var (
	mapStringInterfacePtrType = reflect.TypeOf((*map[string]interface{})(nil))
	mapStringInterfaceType    = mapStringInterfacePtrType.Elem()
)

func ( *Decoder,  reflect.Value) error {
	,  := .DecodeMapLen()
	if  != nil {
		return 
	}

	 := .Type()
	if  == -1 {
		.Set(reflect.Zero())
		return nil
	}

	if .IsNil() {
		.Set(reflect.MakeMap())
	}
	if  == 0 {
		return nil
	}

	return .decodeTypedMapValue(, )
}

func ( *Decoder) () (interface{}, error) {
	if .mapDecoder != nil {
		return .mapDecoder()
	}
	return .DecodeMap()
}

// DecodeMapLen decodes map length. Length is -1 when map is nil.
func ( *Decoder) () (int, error) {
	,  := .readCode()
	if  != nil {
		return 0, 
	}

	if msgpcode.IsExt() {
		if  = .skipExtHeader();  != nil {
			return 0, 
		}

		,  = .readCode()
		if  != nil {
			return 0, 
		}
	}
	return .mapLen()
}

func ( *Decoder) ( byte) (int, error) {
	if  == msgpcode.Nil {
		return -1, nil
	}
	if  >= msgpcode.FixedMapLow &&  <= msgpcode.FixedMapHigh {
		return int( & msgpcode.FixedMapMask), nil
	}
	if  == msgpcode.Map16 {
		,  := .uint16()
		return int(), 
	}
	if  == msgpcode.Map32 {
		,  := .uint32()
		return int(), 
	}
	return 0, unexpectedCodeError{code: , hint: "map length"}
}

func ( *Decoder,  reflect.Value) error {
	 := .Addr().Convert(mapStringStringPtrType).Interface().(*map[string]string)
	return .decodeMapStringStringPtr()
}

func ( *Decoder) ( *map[string]string) error {
	,  := .DecodeMapLen()
	if  != nil {
		return 
	}
	if  == -1 {
		* = nil
		return nil
	}

	 := *
	if  == nil {
		* = make(map[string]string, min(, maxMapSize))
		 = *
	}

	for  := 0;  < ; ++ {
		,  := .DecodeString()
		if  != nil {
			return 
		}
		,  := .DecodeString()
		if  != nil {
			return 
		}
		[] = 
	}

	return nil
}

func ( *Decoder,  reflect.Value) error {
	 := .Addr().Convert(mapStringInterfacePtrType).Interface().(*map[string]interface{})
	return .decodeMapStringInterfacePtr()
}

func ( *Decoder) ( *map[string]interface{}) error {
	,  := .DecodeMap()
	if  != nil {
		return 
	}
	* = 
	return nil
}

func ( *Decoder) () (map[string]interface{}, error) {
	,  := .DecodeMapLen()
	if  != nil {
		return nil, 
	}

	if  == -1 {
		return nil, nil
	}

	 := make(map[string]interface{}, min(, maxMapSize))

	for  := 0;  < ; ++ {
		,  := .DecodeString()
		if  != nil {
			return nil, 
		}
		,  := .decodeInterfaceCond()
		if  != nil {
			return nil, 
		}
		[] = 
	}

	return , nil
}

func ( *Decoder) () (map[interface{}]interface{}, error) {
	,  := .DecodeMapLen()
	if  != nil {
		return nil, 
	}

	if  == -1 {
		return nil, nil
	}

	 := make(map[interface{}]interface{}, min(, maxMapSize))

	for  := 0;  < ; ++ {
		,  := .decodeInterfaceCond()
		if  != nil {
			return nil, 
		}

		,  := .decodeInterfaceCond()
		if  != nil {
			return nil, 
		}

		[] = 
	}

	return , nil
}

// DecodeTypedMap decodes a typed map. Typed map is a map that has a fixed type for keys and values.
// Key and value types may be different.
func ( *Decoder) () (interface{}, error) {
	,  := .DecodeMapLen()
	if  != nil {
		return nil, 
	}
	if  <= 0 {
		return nil, nil
	}

	,  := .decodeInterfaceCond()
	if  != nil {
		return nil, 
	}

	,  := .decodeInterfaceCond()
	if  != nil {
		return nil, 
	}

	 := reflect.TypeOf()
	 := reflect.TypeOf()

	if !.Comparable() {
		return nil, fmt.Errorf("msgpack: unsupported map key: %s", .String())
	}

	 := reflect.MapOf(, )
	 := reflect.MakeMap()
	.SetMapIndex(reflect.ValueOf(), reflect.ValueOf())

	--
	if  := .decodeTypedMapValue(, );  != nil {
		return nil, 
	}

	return .Interface(), nil
}

func ( *Decoder) ( reflect.Value,  int) error {
	 := .Type()
	 := .Key()
	 := .Elem()

	for  := 0;  < ; ++ {
		 := reflect.New().Elem()
		if  := .DecodeValue();  != nil {
			return 
		}

		 := reflect.New().Elem()
		if  := .DecodeValue();  != nil {
			return 
		}

		.SetMapIndex(, )
	}

	return nil
}

func ( *Decoder) ( byte) error {
	,  := .mapLen()
	if  != nil {
		return 
	}
	for  := 0;  < ; ++ {
		if  := .Skip();  != nil {
			return 
		}
		if  := .Skip();  != nil {
			return 
		}
	}
	return nil
}

func ( *Decoder,  reflect.Value) error {
	,  := .readCode()
	if  != nil {
		return 
	}

	,  := .mapLen()
	if  == nil {
		return .decodeStruct(, )
	}

	var  error
	,  = .arrayLen()
	if  != nil {
		return 
	}

	if  <= 0 {
		.Set(reflect.Zero(.Type()))
		return nil
	}

	 := structs.Fields(.Type(), .structTag)
	if  != len(.List) {
		return errArrayStruct
	}

	for ,  := range .List {
		if  := .DecodeValue(, );  != nil {
			return 
		}
	}

	return nil
}

func ( *Decoder) ( reflect.Value,  int) error {
	if  == -1 {
		.Set(reflect.Zero(.Type()))
		return nil
	}

	 := structs.Fields(.Type(), .structTag)
	for  := 0;  < ; ++ {
		,  := .decodeStringTemp()
		if  != nil {
			return 
		}

		if  := .Map[];  != nil {
			if  := .DecodeValue(, );  != nil {
				return 
			}
			continue
		}

		if .flags&disallowUnknownFieldsFlag != 0 {
			return fmt.Errorf("msgpack: unknown field %q", )
		}
		if  := .Skip();  != nil {
			return 
		}
	}

	return nil
}