1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
package bexpr
import (
"reflect"
"strconv"
)
// CoerceInt conforms to the FieldValueCoercionFn signature
// and can be used to convert the raw string value of
// an expression into an `int`
func CoerceInt(value string) (interface{}, error) {
i, err := strconv.ParseInt(value, 0, 0)
return int(i), err
}
// CoerceInt8 conforms to the FieldValueCoercionFn signature
// and can be used to convert the raw string value of
// an expression into an `int8`
func CoerceInt8(value string) (interface{}, error) {
i, err := strconv.ParseInt(value, 0, 8)
return int8(i), err
}
// CoerceInt16 conforms to the FieldValueCoercionFn signature
// and can be used to convert the raw string value of
// an expression into an `int16`
func CoerceInt16(value string) (interface{}, error) {
i, err := strconv.ParseInt(value, 0, 16)
return int16(i), err
}
// CoerceInt32 conforms to the FieldValueCoercionFn signature
// and can be used to convert the raw string value of
// an expression into an `int32`
func CoerceInt32(value string) (interface{}, error) {
i, err := strconv.ParseInt(value, 0, 32)
return int32(i), err
}
// CoerceInt64 conforms to the FieldValueCoercionFn signature
// and can be used to convert the raw string value of
// an expression into an `int64`
func CoerceInt64(value string) (interface{}, error) {
i, err := strconv.ParseInt(value, 0, 64)
return int64(i), err
}
// CoerceUint conforms to the FieldValueCoercionFn signature
// and can be used to convert the raw string value of
// an expression into an `int`
func CoerceUint(value string) (interface{}, error) {
i, err := strconv.ParseUint(value, 0, 0)
return uint(i), err
}
// CoerceUint8 conforms to the FieldValueCoercionFn signature
// and can be used to convert the raw string value of
// an expression into an `int8`
func CoerceUint8(value string) (interface{}, error) {
i, err := strconv.ParseUint(value, 0, 8)
return uint8(i), err
}
// CoerceUint16 conforms to the FieldValueCoercionFn signature
// and can be used to convert the raw string value of
// an expression into an `int16`
func CoerceUint16(value string) (interface{}, error) {
i, err := strconv.ParseUint(value, 0, 16)
return uint16(i), err
}
// CoerceUint32 conforms to the FieldValueCoercionFn signature
// and can be used to convert the raw string value of
// an expression into an `int32`
func CoerceUint32(value string) (interface{}, error) {
i, err := strconv.ParseUint(value, 0, 32)
return uint32(i), err
}
// CoerceUint64 conforms to the FieldValueCoercionFn signature
// and can be used to convert the raw string value of
// an expression into an `int64`
func CoerceUint64(value string) (interface{}, error) {
i, err := strconv.ParseUint(value, 0, 64)
return uint64(i), err
}
// CoerceBool conforms to the FieldValueCoercionFn signature
// and can be used to convert the raw string value of
// an expression into a `bool`
func CoerceBool(value string) (interface{}, error) {
return strconv.ParseBool(value)
}
// CoerceFloat32 conforms to the FieldValueCoercionFn signature
// and can be used to convert the raw string value of
// an expression into an `float32`
func CoerceFloat32(value string) (interface{}, error) {
// ParseFloat always returns a float64 but ensures
// it can be converted to a float32 without changing
// its value
f, err := strconv.ParseFloat(value, 32)
return float32(f), err
}
// CoerceFloat64 conforms to the FieldValueCoercionFn signature
// and can be used to convert the raw string value of
// an expression into an `float64`
func CoerceFloat64(value string) (interface{}, error) {
return strconv.ParseFloat(value, 64)
}
// CoerceString conforms to the FieldValueCoercionFn signature
// and can be used to convert the raw string value of
// an expression into a `string`
func CoerceString(value string) (interface{}, error) {
return value, nil
}
var primitiveCoercionFns = map[reflect.Kind]FieldValueCoercionFn{
reflect.Bool: CoerceBool,
reflect.Int: CoerceInt,
reflect.Int8: CoerceInt8,
reflect.Int16: CoerceInt16,
reflect.Int32: CoerceInt32,
reflect.Int64: CoerceInt64,
reflect.Uint: CoerceUint,
reflect.Uint8: CoerceUint8,
reflect.Uint16: CoerceUint16,
reflect.Uint32: CoerceUint32,
reflect.Uint64: CoerceUint64,
reflect.Float32: CoerceFloat32,
reflect.Float64: CoerceFloat64,
reflect.String: CoerceString,
}
|