// convertReflectValueToType trys to covert the reflect.Value to the reflect.Type
// if it can not, it returns the original rv and an error
196285
func convertReflectValueToType(rv reflect.Value, rt reflect.Type) (reflect.Value, error) {
	if rt == interfaceType || rv.Type() == rt {
		// if reflect.Type is interface or the types match, return the provided reflect.Value
		return rv, nil
	}
	if rv.Type().ConvertibleTo(rt) {
		// if reflect can covert, do that conversion and return
		return rv.Convert(rt), nil
	}
	if (rv.Kind() == reflect.Slice || rv.Kind() == reflect.Array) &&
		(rt.Kind() == reflect.Slice || rt.Kind() == reflect.Array) {
		// covert slice or array
		return convertSliceOrArray(rv, rt)
	}
	if rv.Kind() == rt.Kind() {
		// kind matches
		switch rv.Kind() {
		case reflect.Map:
			// convert map
			return convertMap(rv, rt)
		case reflect.Func:
			// for runVMFunction conversions, call convertVMFunctionToType
			return convertVMFunctionToType(rv, rt)
		case reflect.Ptr:
			// both rv and rt are pointers, convert what they are pointing to
			value, err := convertReflectValueToType(rv.Elem(), rt.Elem())
			if err != nil {
				return rv, err
			}
			// need to make a new value to be able to set it
			ptrV, err := makeValue(rt)
			if err != nil {
				return rv, err
			}
			// set value and return new pointer
			ptrV.Elem().Set(value)
			return ptrV, nil
		}
	}
	if rv.Type() == interfaceType {
		if rv.IsNil() {
			// return nil of correct type
			return reflect.Zero(rt), nil
		}
		// try to convert the element
		return convertReflectValueToType(rv.Elem(), rt)
	}

	// TODO: need to handle the case where either rv or rt are a pointer but not both

	return rv, fmt.Errorf("invalid type conversion")
}
1205750
func tryToConvertConstantInt(ctx sessionctx.Context, isUnsigned bool, con *Constant) (_ *Constant, isAlwaysFalse bool) {
	if con.GetType().EvalType() == types.ETInt {
		return con, false
	}
	dt, err := con.Eval(chunk.Row{})
	if err != nil {
		return con, false
	}
	sc := ctx.GetSessionVars().StmtCtx
	fieldType := types.NewFieldType(mysql.TypeLonglong)
	if isUnsigned {
		fieldType.Flag |= mysql.UnsignedFlag
	}
	dt, err = dt.ConvertTo(sc, fieldType)
	if err != nil {
		return con, terror.ErrorEqual(err, types.ErrOverflow)
	}
	return &Constant{
		Value:        dt,
		RetType:      fieldType,
		DeferredExpr: con.DeferredExpr,
	}, false
}
391224
func convertNumber(v reflect.Value, to reflect.Kind) (reflect.Value, error) {
	var n reflect.Value
	if isFloat(to) {
		f, err := toFloat(v)
		if err != nil {
			return n, err
		}
		switch to {
		case reflect.Float32:
			n = reflect.ValueOf(float32(f))
		default:
			n = reflect.ValueOf(float64(f))
		}
	} else if isInt(to) {
		i, err := toInt(v)
		if err != nil {
			return n, err
		}
		switch to {
		case reflect.Int:
			n = reflect.ValueOf(int(i))
		case reflect.Int8:
			n = reflect.ValueOf(int8(i))
		case reflect.Int16:
			n = reflect.ValueOf(int16(i))
		case reflect.Int32:
			n = reflect.ValueOf(int32(i))
		case reflect.Int64:
			n = reflect.ValueOf(int64(i))
		}
	} else if isUint(to) {
		i, err := toUint(v)
		if err != nil {
			return n, err
		}
		switch to {
		case reflect.Uint:
			n = reflect.ValueOf(uint(i))
		case reflect.Uint8:
			n = reflect.ValueOf(uint8(i))
		case reflect.Uint16:
			n = reflect.ValueOf(uint16(i))
		case reflect.Uint32:
			n = reflect.ValueOf(uint32(i))
		case reflect.Uint64:
			n = reflect.ValueOf(uint64(i))
		}

	}

	if !n.IsValid() {
		return n, errors.New("invalid values")
	}

	return n, nil

}
16660
func valueToString(f reflect.Value) []string {
	var v []string

	switch reflect.TypeOf(f.Interface()).Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		v = []string{strconv.FormatInt(f.Int(), 10)}
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		v = []string{strconv.FormatUint(f.Uint(), 10)}
	case reflect.Float32:
		v = []string{strconv.FormatFloat(f.Float(), 'f', 4, 32)}
	case reflect.Float64:
		v = []string{strconv.FormatFloat(f.Float(), 'f', 4, 64)}
	case reflect.Bool:
		v = []string{strconv.FormatBool(f.Bool())}
	case reflect.Slice:
		for i := 0; i < f.Len(); i++ {
			if s := valueToString(f.Index(i)); len(s) == 1 {
				v = append(v, s[0])
			}
		}
	case reflect.String:
		v = []string{f.String()}
	}

	return v
}
953414
func primitiveConversion(inType, outType reflect.Type) (string, bool) {
	switch inType.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
		reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
		reflect.Float32, reflect.Float64:
		switch outType.Kind() {
		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
			reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
			reflect.Float32, reflect.Float64:
			return outType.Name(), true
		}
	}
	return "", false
}
675299
func ConvertType(scheme *runtime.Scheme, in, out runtime.Object) error {
	in = in.DeepCopyObject()
	if err := scheme.Convert(in, out, nil); err != nil {
		return err
	}
	gvkOut := out.GetObjectKind().GroupVersionKind()
	if gvkOut.Kind == "" || gvkOut.Version == "" { // Group can be empty
		// API machinery discards TypeMeta for typed objects. This is annoying.
		gvks, _, err := scheme.ObjectKinds(in)
		if err != nil {
			return err
		}
		out.GetObjectKind().SetGroupVersionKind(gvks[0])
	}
	return nil
}
499856
func (c *Converter) convert(sv, dv reflect.Value, scope *scope) error {
	dt, st := dv.Type(), sv.Type()
	// Apply default values.
	if fv, ok := c.defaultingFuncs[st]; ok {
		if c.Debug != nil {
			c.Debug.Logf("Applying defaults for '%v'", st)
		}
		args := []reflect.Value{sv.Addr()}
		fv.Call(args)
	}

	// Convert sv to dv.
	if fv, ok := c.conversionFuncs[typePair{st, dt}]; ok {
		if c.Debug != nil {
			c.Debug.Logf("Calling custom conversion of '%v' to '%v'", st, dt)
		}
		return c.callCustom(sv, dv, fv, scope)
	}
	if fv, ok := c.generatedConversionFuncs[typePair{st, dt}]; ok {
		if c.Debug != nil {
			c.Debug.Logf("Calling custom conversion of '%v' to '%v'", st, dt)
		}
		return c.callCustom(sv, dv, fv, scope)
	}

	return c.defaultConvert(sv, dv, scope)
}
793463
func Type(v interface{}) ValueType {
	t := Unknown
	switch v.(type) {
	case nil:
		t = Null
	case bool:
		t = Bool
	case string:
		t = String
	case float64:
		t = Number
	case []interface{}:
		t = Array
	case map[string]interface{}:
		t = Object
	}
	return t
}
707386
func toFloat(v reflect.Value) (float64, error) {
	switch v.Kind() {
	case reflect.Float32, reflect.Float64:
		return v.Float(), nil
	case reflect.Interface:
		return toFloat(v.Elem())
	}
	return -1, errors.New("unable to convert value to float")
}
45962
func UnwrapReflectValues(result interface{}) interface{} {
	switch result := result.(type) {
	case []reflect.Value:
		if len(result) == 0 {
			return nil
		} else if len(result) == 1 {
			return result[0].Interface()
		} else {
			values := []interface{}{}

			for _, v := range result {
				values = append(values, v.Interface())
			}

			return values
		}
	default:
		return result
	}
}
1333364
func (t EnumType) ConvertFromModel(value interface{}) (interface{}, error) {
	if value == nil {
		return nil, nil
	}
	converted, err := t.BaseType.ConvertFromModel(value)
	if err != nil {
		return nil, errs.Errorf("error converting enum value: %s", err.Error())
	}
	if !contains(t.Values, converted) {
		return nil, errs.Errorf("value: %+v (%[1]T) is not part of allowed enum values: %+v", value, t.Values)
	}
	return converted, nil
}
483785
func ConvertKeyValue(bKey []byte, bValue []byte, key interface{}, value interface{}) error {
	keyBuf := bytes.NewBuffer(bKey)
	valueBuf := bytes.NewBuffer(bValue)

	if len(bKey) > 0 {
		if err := binary.Read(keyBuf, byteorder.Native, key); err != nil {
			return fmt.Errorf("Unable to convert key: %s", err)
		}
	}

	if len(bValue) > 0 {
		if err := binary.Read(valueBuf, byteorder.Native, value); err != nil {
			return fmt.Errorf("Unable to convert value: %s", err)
		}
	}

	return nil
}
794264
func Convert(value interface{}, t reflect.Kind) (interface{}, error) {

	switch reflect.TypeOf(value).Kind() {
	case reflect.Slice:
		switch t {
		case reflect.String:
			if reflect.TypeOf(value).Elem().Kind() == reflect.Uint8 {
				return string(value.([]byte)), nil
			} else {
				return String(value), nil
			}
		case reflect.Slice:
		default:
			return nil, fmt.Errorf("Could not convert slice into non-slice.")
		}
	case reflect.String:
		switch t {
		case reflect.Slice:
			return Bytes(value), nil
		}
	}

	switch t {

	case reflect.String:
		return String(value), nil

	case reflect.Uint64:
		return Uint64(value), nil

	case reflect.Uint32:
		return uint32(Uint64(value)), nil

	case reflect.Uint16:
		return uint16(Uint64(value)), nil

	case reflect.Uint8:
		return uint8(Uint64(value)), nil

	case reflect.Uint:
		return uint(Uint64(value)), nil

	case reflect.Int64:
		return int64(Int64(value)), nil

	case reflect.Int32:
		return int32(Int64(value)), nil

	case reflect.Int16:
		return int16(Int64(value)), nil

	case reflect.Int8:
		return int8(Int64(value)), nil

	case reflect.Int:
		return int(Int64(value)), nil

	case reflect.Float64:
		return Float64(value), nil

	case reflect.Float32:
		return float32(Float64(value)), nil

	case reflect.Bool:
		return Bool(value), nil

	case reflect.Interface:
		return value, nil

	case KindTime:
		return Time(value), nil

	case KindDuration:
		return Duration(value), nil

	}

	return nil, fmt.Errorf("Could not convert %s into %s.", reflect.TypeOf(value).Kind(), t)
}
101847
func (s *state) validateType(value reflect.Value, typ reflect.Type) reflect.Value {
	if !value.IsValid() {
		if typ == nil || canBeNil(typ) {
			// An untyped nil interface{}. Accept as a proper nil value.
			return reflect.Zero(typ)
		}
		s.errorf("invalid value; expected %s", typ)
	}
	if typ != nil && !value.Type().AssignableTo(typ) {
		if value.Kind() == reflect.Interface && !value.IsNil() {
			value = value.Elem()
			if value.Type().AssignableTo(typ) {
				return value
			}
			// fallthrough
		}
		// Does one dereference or indirection work? We could do more, as we
		// do with method receivers, but that gets messy and method receivers
		// are much more constrained, so it makes more sense there than here.
		// Besides, one is almost always all you need.
		switch {
		case value.Kind() == reflect.Ptr && value.Type().Elem().AssignableTo(typ):
			value = value.Elem()
			if !value.IsValid() {
				s.errorf("dereference of nil pointer of type %s", typ)
			}
		case reflect.PtrTo(value.Type()).AssignableTo(typ) && value.CanAddr():
			value = value.Addr()
		default:
			s.errorf("wrong type for value; expected %s; got %s", typ, value.Type())
		}
	}
	return value
}
121036
func ReflectRecursive(v reflect.Value, visitor visitorFunc) error {
	return reflectRecursive("", v, visitor)
}
320168
func NoConvert(value interface{}, kind reflect.Kind) interface{} {
	switch kind {
	case reflect.Bool:
		return value.(bool)

	case reflect.Int:
		return int64(value.(int))
	case reflect.Int8:
		return int64(value.(int8))
	case reflect.Int16:
		return int64(value.(int16))
	case reflect.Int32:
		return int64(value.(int32))
	case reflect.Int64:
		return value.(int64)

	case reflect.Uint:
		return uint64(value.(uint))
	case reflect.Uint8:
		return uint64(value.(uint8))
	case reflect.Uint16:
		return uint64(value.(uint16))
	case reflect.Uint32:
		return uint64(value.(uint32))
	case reflect.Uint64:
		return value.(uint64)
	case reflect.Uintptr:
		return uint64(value.(uintptr))

	case reflect.Float32:
		return float64(value.(float32))
	case reflect.Float64:
		return value.(float64)

	case reflect.String:
		return value.(string)
	}

	panic(fmt.Errorf("NoConvert: can't convert %#v to %s", value, kind))
}
99284
func toType(p interface{}) reflect.Type {
	t := reflect.ValueOf(p).Type()
	if t.Kind() == reflect.Ptr {
		t = t.Elem()
	}
	return t
}