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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package schema
import (
"reflect"
"strconv"
)
// Bool returns a Checker that accepts boolean values only.
func Bool() Checker {
return boolC{}
}
type boolC struct{}
func (c boolC) Coerce(v interface{}, path []string) (interface{}, error) {
if v != nil {
switch reflect.TypeOf(v).Kind() {
case reflect.Bool:
return v, nil
case reflect.String:
val, err := strconv.ParseBool(reflect.ValueOf(v).String())
if err == nil {
return val, nil
}
}
}
return nil, error_{"bool", v, path}
}
// Int returns a Checker that accepts any integer value, and returns
// the same value consistently typed as an int64.
func Int() Checker {
return intC{}
}
type intC struct{}
func (c intC) Coerce(v interface{}, path []string) (interface{}, error) {
if v == nil {
return nil, error_{"int", v, path}
}
switch reflect.TypeOf(v).Kind() {
case reflect.Int:
case reflect.Int8:
case reflect.Int16:
case reflect.Int32:
case reflect.Int64:
case reflect.String:
val, err := strconv.ParseInt(reflect.ValueOf(v).String(), 0, 64)
if err == nil {
return val, nil
} else {
return nil, error_{"int", v, path}
}
default:
return nil, error_{"int", v, path}
}
return reflect.ValueOf(v).Int(), nil
}
// Uint returns a Checker that accepts any integer or unsigned value, and
// returns the same value consistently typed as an uint64. If the integer
// value is negative an error is raised.
func Uint() Checker {
return uintC{}
}
type uintC struct{}
func (c uintC) Coerce(v interface{}, path []string) (interface{}, error) {
if v == nil {
return nil, error_{"uint", v, path}
}
switch reflect.TypeOf(v).Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return reflect.ValueOf(v).Uint(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
val := reflect.ValueOf(v).Int()
if val < 0 {
return nil, error_{"uint", v, path}
}
// All positive int64 values fit into uint64.
return uint64(val), nil
case reflect.String:
val, err := strconv.ParseUint(reflect.ValueOf(v).String(), 0, 64)
if err == nil {
return val, nil
} else {
return nil, error_{"uint", v, path}
}
default:
return nil, error_{"uint", v, path}
}
}
// ForceInt returns a Checker that accepts any integer or float value, and
// returns the same value consistently typed as an int. This is required
// in order to handle the interface{}/float64 type conversion performed by
// the JSON serializer used as part of the API infrastructure.
func ForceInt() Checker {
return forceIntC{}
}
type forceIntC struct{}
func (c forceIntC) Coerce(v interface{}, path []string) (interface{}, error) {
if v != nil {
switch vv := reflect.TypeOf(v); vv.Kind() {
case reflect.String:
vstr := reflect.ValueOf(v).String()
intValue, err := strconv.ParseInt(vstr, 0, 64)
if err == nil {
return int(intValue), nil
}
floatValue, err := strconv.ParseFloat(vstr, 64)
if err == nil {
return int(floatValue), nil
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return int(reflect.ValueOf(v).Int()), nil
case reflect.Float32, reflect.Float64:
return int(reflect.ValueOf(v).Float()), nil
}
}
return nil, error_{"number", v, path}
}
// ForceUint returns a Checker that accepts any integer or float value, and
// returns the same value consistently typed as an uint64. This is required
// in order to handle the interface{}/float64 type conversion performed by
// the JSON serializer used as part of the API infrastructure. If the integer
// value is negative an error is raised.
func ForceUint() Checker {
return forceUintC{}
}
type forceUintC struct{}
func (c forceUintC) Coerce(v interface{}, path []string) (interface{}, error) {
if v != nil {
switch vv := reflect.TypeOf(v); vv.Kind() {
case reflect.String:
vstr := reflect.ValueOf(v).String()
intValue, err := strconv.ParseUint(vstr, 0, 64)
if err == nil {
return intValue, nil
}
floatValue, err := strconv.ParseFloat(vstr, 64)
if err == nil {
if floatValue < 0 {
return nil, error_{"uint", v, path}
}
return uint64(floatValue), nil
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return reflect.ValueOf(v).Uint(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
val := reflect.ValueOf(v).Int()
if val < 0 {
return nil, error_{"uint", v, path}
}
// All positive int64 values fit into uint64.
return uint64(val), nil
case reflect.Float32, reflect.Float64:
val := reflect.ValueOf(v).Float()
if val < 0 {
return nil, error_{"uint", v, path}
}
return uint64(val), nil
}
}
return nil, error_{"uint", v, path}
}
// Float returns a Checker that accepts any float value, and returns
// the same value consistently typed as a float64.
func Float() Checker {
return floatC{}
}
type floatC struct{}
func (c floatC) Coerce(v interface{}, path []string) (interface{}, error) {
if v == nil {
return nil, error_{"float", v, path}
}
switch reflect.TypeOf(v).Kind() {
case reflect.Float32, reflect.Float64:
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
default:
return nil, error_{"float", v, path}
}
var floatValue float64
return reflect.ValueOf(v).Convert( reflect.TypeOf(floatValue) ).Float() , nil
}
|