package decoder

import (
	
	
	
)

var (
	nilValue = reflect.ValueOf(nil)
)

func (,  reflect.Value) error {
	if .Type().Kind() != reflect.Ptr {
		return fmt.Errorf("invalid dst type. required pointer type: %T", .Type())
	}
	,  := castValue(.Elem().Type(), )
	if  != nil {
		return 
	}
	.Elem().Set()
	return nil
}

func castValue( reflect.Type,  reflect.Value) (reflect.Value, error) {
	switch .Kind() {
	case reflect.Int:
		,  := castInt()
		if  != nil {
			return nilValue, 
		}
		return reflect.ValueOf(int(.Int())), nil
	case reflect.Int8:
		,  := castInt()
		if  != nil {
			return nilValue, 
		}
		return reflect.ValueOf(int8(.Int())), nil
	case reflect.Int16:
		,  := castInt()
		if  != nil {
			return nilValue, 
		}
		return reflect.ValueOf(int16(.Int())), nil
	case reflect.Int32:
		,  := castInt()
		if  != nil {
			return nilValue, 
		}
		return reflect.ValueOf(int32(.Int())), nil
	case reflect.Int64:
		return castInt()
	case reflect.Uint:
		,  := castUint()
		if  != nil {
			return nilValue, 
		}
		return reflect.ValueOf(uint(.Uint())), nil
	case reflect.Uint8:
		,  := castUint()
		if  != nil {
			return nilValue, 
		}
		return reflect.ValueOf(uint8(.Uint())), nil
	case reflect.Uint16:
		,  := castUint()
		if  != nil {
			return nilValue, 
		}
		return reflect.ValueOf(uint16(.Uint())), nil
	case reflect.Uint32:
		,  := castUint()
		if  != nil {
			return nilValue, 
		}
		return reflect.ValueOf(uint32(.Uint())), nil
	case reflect.Uint64:
		return castUint()
	case reflect.Uintptr:
		,  := castUint()
		if  != nil {
			return nilValue, 
		}
		return reflect.ValueOf(uintptr(.Uint())), nil
	case reflect.String:
		return castString()
	case reflect.Bool:
		return castBool()
	case reflect.Float32:
		,  := castFloat()
		if  != nil {
			return nilValue, 
		}
		return reflect.ValueOf(float32(.Float())), nil
	case reflect.Float64:
		return castFloat()
	case reflect.Array:
		return castArray(, )
	case reflect.Slice:
		return castSlice(, )
	case reflect.Map:
		return castMap(, )
	case reflect.Struct:
		return castStruct(, )
	}
	return , nil
}

func castInt( reflect.Value) (reflect.Value, error) {
	switch .Type().Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return , nil
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return reflect.ValueOf(int64(.Uint())), nil
	case reflect.String:
		,  := strconv.ParseInt(.String(), 10, 64)
		if  != nil {
			return nilValue, 
		}
		return reflect.ValueOf(), nil
	case reflect.Bool:
		if .Bool() {
			return reflect.ValueOf(int64(1)), nil
		}
		return reflect.ValueOf(int64(0)), nil
	case reflect.Float32, reflect.Float64:
		return reflect.ValueOf(int64(.Float())), nil
	case reflect.Array:
		if .Len() > 0 {
			return (.Index(0))
		}
		return nilValue, fmt.Errorf("failed to cast to int64 from empty array")
	case reflect.Slice:
		if .Len() > 0 {
			return (.Index(0))
		}
		return nilValue, fmt.Errorf("failed to cast to int64 from empty slice")
	case reflect.Interface:
		return (reflect.ValueOf(.Interface()))
	case reflect.Map:
		return nilValue, fmt.Errorf("failed to cast to int64 from map")
	case reflect.Struct:
		return nilValue, fmt.Errorf("failed to cast to int64 from struct")
	case reflect.Ptr:
		return (.Elem())
	}
	return nilValue, fmt.Errorf("failed to cast to int64 from %s", .Type().Kind())
}

func castUint( reflect.Value) (reflect.Value, error) {
	switch .Type().Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return reflect.ValueOf(uint64(.Int())), nil
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return , nil
	case reflect.String:
		,  := strconv.ParseUint(.String(), 10, 64)
		if  != nil {
			return nilValue, 
		}
		return reflect.ValueOf(), nil
	case reflect.Bool:
		if .Bool() {
			return reflect.ValueOf(uint64(1)), nil
		}
		return reflect.ValueOf(uint64(0)), nil
	case reflect.Float32, reflect.Float64:
		return reflect.ValueOf(uint64(.Float())), nil
	case reflect.Array:
		if .Len() > 0 {
			return (.Index(0))
		}
		return nilValue, fmt.Errorf("failed to cast to uint64 from empty array")
	case reflect.Slice:
		if .Len() > 0 {
			return (.Index(0))
		}
		return nilValue, fmt.Errorf("failed to cast to uint64 from empty slice")
	case reflect.Interface:
		return (reflect.ValueOf(.Interface()))
	case reflect.Map:
		return nilValue, fmt.Errorf("failed to cast to uint64 from map")
	case reflect.Struct:
		return nilValue, fmt.Errorf("failed to cast to uint64 from struct")
	case reflect.Ptr:
		return (.Elem())
	}
	return nilValue, fmt.Errorf("failed to cast to uint64 from %s", .Type().Kind())
}

func castString( reflect.Value) (reflect.Value, error) {
	switch .Type().Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return reflect.ValueOf(fmt.Sprint(.Int())), nil
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return reflect.ValueOf(fmt.Sprint(.Uint())), nil
	case reflect.String:
		return , nil
	case reflect.Bool:
		if .Bool() {
			return reflect.ValueOf("true"), nil
		}
		return reflect.ValueOf("false"), nil
	case reflect.Float32, reflect.Float64:
		return reflect.ValueOf(fmt.Sprint(.Float())), nil
	case reflect.Array:
		if .Len() > 0 {
			return (.Index(0))
		}
		return nilValue, fmt.Errorf("failed to cast to string from empty array")
	case reflect.Slice:
		if .Len() > 0 {
			return (.Index(0))
		}
		return nilValue, fmt.Errorf("failed to cast to string from empty slice")
	case reflect.Interface:
		return (reflect.ValueOf(.Interface()))
	case reflect.Map:
		return nilValue, fmt.Errorf("failed to cast to string from map")
	case reflect.Struct:
		return nilValue, fmt.Errorf("failed to cast to string from struct")
	case reflect.Ptr:
		return (.Elem())
	}
	return nilValue, fmt.Errorf("failed to cast to string from %s", .Type().Kind())
}

func castBool( reflect.Value) (reflect.Value, error) {
	switch .Type().Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		switch .Int() {
		case 0:
			return reflect.ValueOf(false), nil
		case 1:
			return reflect.ValueOf(true), nil
		}
		return nilValue, fmt.Errorf("failed to cast to bool from %d", .Int())
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		switch .Uint() {
		case 0:
			return reflect.ValueOf(false), nil
		case 1:
			return reflect.ValueOf(true), nil
		}
		return nilValue, fmt.Errorf("failed to cast to bool from %d", .Uint())
	case reflect.String:
		,  := strconv.ParseBool(.String())
		if  != nil {
			return nilValue, 
		}
		return reflect.ValueOf(), nil
	case reflect.Bool:
		return , nil
	case reflect.Float32, reflect.Float64:
		switch .Float() {
		case 0:
			return reflect.ValueOf(false), nil
		case 1:
			return reflect.ValueOf(true), nil
		}
		return nilValue, fmt.Errorf("failed to cast to bool from %f", .Float())
	case reflect.Array:
		if .Len() > 0 {
			return (.Index(0))
		}
		return nilValue, fmt.Errorf("failed to cast to string from empty array")
	case reflect.Slice:
		if .Len() > 0 {
			return (.Index(0))
		}
		return nilValue, fmt.Errorf("failed to cast to string from empty slice")
	case reflect.Interface:
		return (reflect.ValueOf(.Interface()))
	case reflect.Map:
		return nilValue, fmt.Errorf("failed to cast to string from map")
	case reflect.Struct:
		return nilValue, fmt.Errorf("failed to cast to string from struct")
	case reflect.Ptr:
		return (.Elem())
	}
	return nilValue, fmt.Errorf("failed to cast to bool from %s", .Type().Kind())
}

func castFloat( reflect.Value) (reflect.Value, error) {
	switch .Type().Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return reflect.ValueOf(float64(.Int())), nil
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return reflect.ValueOf(float64(.Uint())), nil
	case reflect.String:
		,  := strconv.ParseFloat(.String(), 64)
		if  != nil {
			return nilValue, 
		}
		return reflect.ValueOf(), nil
	case reflect.Bool:
		if .Bool() {
			return reflect.ValueOf(float64(1)), nil
		}
		return reflect.ValueOf(float64(0)), nil
	case reflect.Float32, reflect.Float64:
		return , nil
	case reflect.Array:
		if .Len() > 0 {
			return (.Index(0))
		}
		return nilValue, fmt.Errorf("failed to cast to float64 from empty array")
	case reflect.Slice:
		if .Len() > 0 {
			return (.Index(0))
		}
		return nilValue, fmt.Errorf("failed to cast to float64 from empty slice")
	case reflect.Interface:
		return (reflect.ValueOf(.Interface()))
	case reflect.Map:
		return nilValue, fmt.Errorf("failed to cast to float64 from map")
	case reflect.Struct:
		return nilValue, fmt.Errorf("failed to cast to float64 from struct")
	case reflect.Ptr:
		return (.Elem())
	}
	return nilValue, fmt.Errorf("failed to cast to float64 from %s", .Type().Kind())
}

func castArray( reflect.Type,  reflect.Value) (reflect.Value, error) {
	 := .Type().Kind()
	if  == reflect.Interface {
		return (, reflect.ValueOf(.Interface()))
	}
	if  != reflect.Slice &&  != reflect.Array {
		return nilValue, fmt.Errorf("failed to cast to array from %s", )
	}
	if .Elem() == .Type().Elem() {
		return , nil
	}
	if .Len() != .Len() {
		return nilValue, fmt.Errorf("failed to cast [%d]array from slice of %d length", .Len(), .Len())
	}
	 := reflect.New().Elem()
	for  := 0;  < .Len(); ++ {
		,  := castValue(.Elem(), .Index())
		if  != nil {
			return nilValue, 
		}
		.Index().Set()
	}
	return , nil
}

func castSlice( reflect.Type,  reflect.Value) (reflect.Value, error) {
	 := .Type().Kind()
	if  == reflect.Interface {
		return (, reflect.ValueOf(.Interface()))
	}
	if  != reflect.Slice &&  != reflect.Array {
		return nilValue, fmt.Errorf("failed to cast to slice from %s", )
	}
	if .Elem() == .Type().Elem() {
		return , nil
	}
	 := reflect.MakeSlice(, .Len(), .Len())
	for  := 0;  < .Len(); ++ {
		,  := castValue(.Elem(), .Index())
		if  != nil {
			return nilValue, 
		}
		.Index().Set()
	}
	return , nil
}

func castMap( reflect.Type,  reflect.Value) (reflect.Value, error) {
	 := reflect.MakeMap()
	switch .Type().Kind() {
	case reflect.Map:
		 := .MapRange()
		for .Next() {
			,  := castValue(.Key(), .Key())
			if  != nil {
				return nilValue, 
			}
			,  := castValue(.Elem(), .Value())
			if  != nil {
				return nilValue, 
			}
			.SetMapIndex(, )
		}
		return , nil
	case reflect.Interface:
		return (, reflect.ValueOf(.Interface()))
	case reflect.Slice:
		if .Len() > 0 {
			return (, .Index(0))
		}
		return nilValue, fmt.Errorf("failed to cast to map from empty slice")
	}
	return nilValue, fmt.Errorf("failed to cast to map from %s", .Type().Kind())
}

func castStruct( reflect.Type,  reflect.Value) (reflect.Value, error) {
	 := reflect.New().Elem()
	switch .Type().Kind() {
	case reflect.Map:
		 := .MapRange()
		for .Next() {
			 := .Key()
			,  := castString()
			if  != nil {
				return nilValue, 
			}
			 := .String()
			,  := .FieldByName()
			if  {
				,  := castValue(.Type, .Value())
				if  != nil {
					return nilValue, 
				}
				.FieldByName().Set()
			}
		}
		return , nil
	case reflect.Struct:
		for  := 0;  < .Type().NumField(); ++ {
			 := .Type().Field().Name
			.FieldByName().Set(.FieldByName())
		}
		return , nil
	case reflect.Interface:
		return (, reflect.ValueOf(.Interface()))
	case reflect.Slice:
		if .Len() > 0 {
			return (, .Index(0))
		}
		return nilValue, fmt.Errorf("failed to cast to struct from empty slice")
	default:
		return nilValue, fmt.Errorf("failed to cast to struct from %s", .Type().Kind())
	}
}