// converts to either an int64 or a float64
converts to either an int64 or a float64
// converts to either an int64 or a float64
func toNumber(v interface{}) (interface{}, error) {
	if str, ok := v.(string); ok {
		iv, err := strconv.ParseInt(str, 10, 64)
		if err == nil {
			return iv, nil
		}
		fv, err := strconv.ParseFloat(str, 64)
		if err == nil {
			return fv, nil
		}
		return nil, errors.Errorf("%v is not a float64 or int64", v)
	}
	val := reflect.Indirect(reflect.ValueOf(v))
	switch val.Kind() {
	case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
		return val.Int(), nil
	case reflect.Uint8, reflect.Uint16, reflect.Uint32:
		return int64(val.Uint()), nil
	case reflect.Uint, reflect.Uint64:
		tv := val.Uint()
		if tv <= math.MaxInt64 {
			return int64(tv), nil
		}
		return math.MaxInt64, errors.Errorf("%v is too big", tv)
	case reflect.Float32, reflect.Float64:
		return val.Float(), nil
	case reflect.Bool:
		if val.Bool() == true {
			return 1, nil
		}
		return 0, nil
	default:
		return nil, errors.Errorf("cannot convert %v to float64 or int64", v)
	}
}
func toNumber ( v interface { } ) ( interface { } , error ) { if str , ok := v . ( string ) ; ok { iv , err := strconv . ParseInt ( str , 10 , 64 ) if err == nil { return iv , nil } fv , err := strconv . ParseFloat ( str , 64 ) if err == nil { return fv , nil } return nil , errors . Errorf ( " " , v ) } val := reflect . Indirect ( reflect . ValueOf ( v ) ) switch val . Kind ( ) { case reflect . Int8 , reflect . Int16 , reflect . Int32 , reflect . Int64 , reflect . Int : return val . Int ( ) , nil case reflect . Uint8 , reflect . Uint16 , reflect . Uint32 : return int64 ( val . Uint ( ) ) , nil case reflect . Uint , reflect . Uint64 : tv := val . Uint ( ) if tv <= math . MaxInt64 { return int64 ( tv ) , nil } return math . MaxInt64 , errors . Errorf ( " " , tv ) case reflect . Float32 , reflect . Float64 : return val . Float ( ) , nil case reflect . Bool : if val . Bool ( ) == true { return 1 , nil } return 0 , nil default : return nil , errors . Errorf ( " " , v ) } }
converts to either an int64 or a float64
converts to either an int64 or a float64