| 1 | // Copyright 2012 The Gorilla Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package schema |
| 6 | |
| 7 | import ( |
| 8 | "reflect" |
| 9 | "strconv" |
| 10 | |
| 11 | utils "github.com/gofiber/utils/v2" |
| 12 | ) |
| 13 | |
| 14 | type Converter func(string) reflect.Value |
| 15 | |
| 16 | var ( |
| 17 | invalidValue = reflect.Value{} |
| 18 | boolType = reflect.Bool |
| 19 | float32Type = reflect.Float32 |
| 20 | float64Type = reflect.Float64 |
| 21 | intType = reflect.Int |
| 22 | int8Type = reflect.Int8 |
| 23 | int16Type = reflect.Int16 |
| 24 | int32Type = reflect.Int32 |
| 25 | int64Type = reflect.Int64 |
| 26 | stringType = reflect.String |
| 27 | uintType = reflect.Uint |
| 28 | uint8Type = reflect.Uint8 |
| 29 | uint16Type = reflect.Uint16 |
| 30 | uint32Type = reflect.Uint32 |
| 31 | uint64Type = reflect.Uint64 |
| 32 | ) |
| 33 | |
| 34 | // builtinConverters is the single source of truth for type converters. |
| 35 | var builtinConverters = map[reflect.Kind]Converter{ |
| 36 | boolType: convertBool, |
| 37 | float32Type: convertFloat32, |
| 38 | float64Type: convertFloat64, |
| 39 | intType: convertInt, |
| 40 | int8Type: convertInt8, |
| 41 | int16Type: convertInt16, |
| 42 | int32Type: convertInt32, |
| 43 | int64Type: convertInt64, |
| 44 | stringType: convertString, |
| 45 | uintType: convertUint, |
| 46 | uint8Type: convertUint8, |
| 47 | uint16Type: convertUint16, |
| 48 | uint32Type: convertUint32, |
| 49 | uint64Type: convertUint64, |
| 50 | } |
| 51 | |
| 52 | // builtinConvertersArray is an array indexed by reflect.Kind for O(1) lookup. |
| 53 | var builtinConvertersArray [reflect.UnsafePointer + 1]Converter |
| 54 | |
| 55 | func init() { |
| 56 | for k, conv := range builtinConverters { |
| 57 | builtinConvertersArray[k] = conv |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // getBuiltinConverter returns the converter for a kind using O(1) array lookup. |
| 62 | func getBuiltinConverter(k reflect.Kind) Converter { |
| 63 | if k <= reflect.UnsafePointer { |
| 64 | return builtinConvertersArray[k] |
| 65 | } |
| 66 | return nil |
| 67 | } |
| 68 | |
| 69 | func convertBool(value string) reflect.Value { |
| 70 | if value == "on" { |
| 71 | return reflect.ValueOf(true) |
| 72 | } else if v, err := strconv.ParseBool(value); err == nil { |
| 73 | return reflect.ValueOf(v) |
| 74 | } |
| 75 | return invalidValue |
| 76 | } |
| 77 | |
| 78 | func convertFloat32(value string) reflect.Value { |
| 79 | if v, err := utils.ParseFloat32(value); err == nil { |
| 80 | return reflect.ValueOf(v) |
| 81 | } |
| 82 | return invalidValue |
| 83 | } |
| 84 | |
| 85 | func convertFloat64(value string) reflect.Value { |
| 86 | if v, err := utils.ParseFloat64(value); err == nil { |
| 87 | return reflect.ValueOf(v) |
| 88 | } |
| 89 | return invalidValue |
| 90 | } |
| 91 | |
| 92 | // Native int/uint parsing goes through utils.ParseInt/ParseUint (which have |
| 93 | // a SWAR fast path) with an inline native-fit guard: `int64(int(v)) == v` |
| 94 | // compiles away on 64-bit and rejects values on 32-bit that a plain int() |
| 95 | // conversion would silently truncate. The guard is written inline at each |
| 96 | // call site so the ~8ns parse doesn't pay a wrapper call frame. |
| 97 | |
| 98 | func convertInt(value string) reflect.Value { |
| 99 | if v, err := utils.ParseInt(value); err == nil && int64(int(v)) == v { |
| 100 | return reflect.ValueOf(int(v)) |
| 101 | } |
| 102 | return invalidValue |
| 103 | } |
| 104 | |
| 105 | func convertInt8(value string) reflect.Value { |
| 106 | if v, err := utils.ParseInt8(value); err == nil { |
| 107 | return reflect.ValueOf(v) |
| 108 | } |
| 109 | return invalidValue |
| 110 | } |
| 111 | |
| 112 | func convertInt16(value string) reflect.Value { |
| 113 | if v, err := utils.ParseInt16(value); err == nil { |
| 114 | return reflect.ValueOf(v) |
| 115 | } |
| 116 | return invalidValue |
| 117 | } |
| 118 | |
| 119 | func convertInt32(value string) reflect.Value { |
| 120 | if v, err := utils.ParseInt32(value); err == nil { |
| 121 | return reflect.ValueOf(v) |
| 122 | } |
| 123 | return invalidValue |
| 124 | } |
| 125 | |
| 126 | func convertInt64(value string) reflect.Value { |
| 127 | if v, err := utils.ParseInt(value); err == nil { |
| 128 | return reflect.ValueOf(v) |
| 129 | } |
| 130 | return invalidValue |
| 131 | } |
| 132 | |
| 133 | func convertString(value string) reflect.Value { |
| 134 | return reflect.ValueOf(value) |
| 135 | } |
| 136 | |
| 137 | func convertUint(value string) reflect.Value { |
| 138 | if v, err := utils.ParseUint(value); err == nil && uint64(uint(v)) == v { |
| 139 | return reflect.ValueOf(uint(v)) |
| 140 | } |
| 141 | return invalidValue |
| 142 | } |
| 143 | |
| 144 | func convertUint8(value string) reflect.Value { |
| 145 | if v, err := utils.ParseUint8(value); err == nil { |
| 146 | return reflect.ValueOf(v) |
| 147 | } |
| 148 | return invalidValue |
| 149 | } |
| 150 | |
| 151 | func convertUint16(value string) reflect.Value { |
| 152 | if v, err := utils.ParseUint16(value); err == nil { |
| 153 | return reflect.ValueOf(v) |
| 154 | } |
| 155 | return invalidValue |
| 156 | } |
| 157 | |
| 158 | func convertUint32(value string) reflect.Value { |
| 159 | if v, err := utils.ParseUint32(value); err == nil { |
| 160 | return reflect.ValueOf(v) |
| 161 | } |
| 162 | return invalidValue |
| 163 | } |
| 164 | |
| 165 | func convertUint64(value string) reflect.Value { |
| 166 | if v, err := utils.ParseUint(value); err == nil { |
| 167 | return reflect.ValueOf(v) |
| 168 | } |
| 169 | return invalidValue |
| 170 | } |
| 171 | |
| 172 | // parseNative* adapt the builtin parsers to the (T, bool) shape used by the |
| 173 | // native slice decode path, mirroring setBuiltinKind's per-kind behavior. |
| 174 | |
| 175 | func parseNativeString(s string) (string, bool) { return s, true } |
| 176 | |
| 177 | func parseNativeInt(s string) (int, bool) { |
| 178 | n, err := utils.ParseInt(s) |
| 179 | return int(n), err == nil && int64(int(n)) == n |
| 180 | } |
| 181 | |
| 182 | func parseNativeInt64(s string) (int64, bool) { |
| 183 | n, err := utils.ParseInt(s) |
| 184 | return n, err == nil |
| 185 | } |
| 186 | |
| 187 | func parseNativeUint(s string) (uint, bool) { |
| 188 | n, err := utils.ParseUint(s) |
| 189 | return uint(n), err == nil && uint64(uint(n)) == n |
| 190 | } |
| 191 | |
| 192 | func parseNativeUint64(s string) (uint64, bool) { |
| 193 | n, err := utils.ParseUint(s) |
| 194 | return n, err == nil |
| 195 | } |
| 196 | |
| 197 | func parseNativeFloat64(s string) (float64, bool) { |
| 198 | f, err := utils.ParseFloat64(s) |
| 199 | return f, err == nil |
| 200 | } |
| 201 | |
| 202 | func parseNativeBool(s string) (bool, bool) { |
| 203 | if s == "on" { |
| 204 | return true, true |
| 205 | } |
| 206 | b, err := strconv.ParseBool(s) |
| 207 | return b, err == nil |
| 208 | } |
| 209 | |
| 210 | // setBuiltinKind parses val and assigns it directly into v for builtin |
| 211 | // convertible kinds, avoiding the reflect.Value boxing of the Converter API. |
| 212 | // handled reports whether the kind is builtin-convertible; ok reports whether |
| 213 | // val parsed successfully. v is only modified on success. |
| 214 | func setBuiltinKind(v reflect.Value, k reflect.Kind, val string) (handled, ok bool) { |
| 215 | switch k { |
| 216 | case boolType: |
| 217 | if val == "on" { |
| 218 | v.SetBool(true) |
| 219 | return true, true |
| 220 | } |
| 221 | b, err := strconv.ParseBool(val) |
| 222 | if err != nil { |
| 223 | return true, false |
| 224 | } |
| 225 | v.SetBool(b) |
| 226 | case stringType: |
| 227 | v.SetString(val) |
| 228 | case intType: |
| 229 | n, err := utils.ParseInt(val) |
| 230 | if err != nil || int64(int(n)) != n { |
| 231 | return true, false |
| 232 | } |
| 233 | v.SetInt(n) |
| 234 | case int8Type: |
| 235 | n, err := utils.ParseInt8(val) |
| 236 | if err != nil { |
| 237 | return true, false |
| 238 | } |
| 239 | v.SetInt(int64(n)) |
| 240 | case int16Type: |
| 241 | n, err := utils.ParseInt16(val) |
| 242 | if err != nil { |
| 243 | return true, false |
| 244 | } |
| 245 | v.SetInt(int64(n)) |
| 246 | case int32Type: |
| 247 | n, err := utils.ParseInt32(val) |
| 248 | if err != nil { |
| 249 | return true, false |
| 250 | } |
| 251 | v.SetInt(int64(n)) |
| 252 | case int64Type: |
| 253 | n, err := utils.ParseInt(val) |
| 254 | if err != nil { |
| 255 | return true, false |
| 256 | } |
| 257 | v.SetInt(n) |
| 258 | case uintType: |
| 259 | n, err := utils.ParseUint(val) |
| 260 | if err != nil || uint64(uint(n)) != n { |
| 261 | return true, false |
| 262 | } |
| 263 | v.SetUint(n) |
| 264 | case uint8Type: |
| 265 | n, err := utils.ParseUint8(val) |
| 266 | if err != nil { |
| 267 | return true, false |
| 268 | } |
| 269 | v.SetUint(uint64(n)) |
| 270 | case uint16Type: |
| 271 | n, err := utils.ParseUint16(val) |
| 272 | if err != nil { |
| 273 | return true, false |
| 274 | } |
| 275 | v.SetUint(uint64(n)) |
| 276 | case uint32Type: |
| 277 | n, err := utils.ParseUint32(val) |
| 278 | if err != nil { |
| 279 | return true, false |
| 280 | } |
| 281 | v.SetUint(uint64(n)) |
| 282 | case uint64Type: |
| 283 | n, err := utils.ParseUint(val) |
| 284 | if err != nil { |
| 285 | return true, false |
| 286 | } |
| 287 | v.SetUint(n) |
| 288 | case float32Type: |
| 289 | f, err := utils.ParseFloat32(val) |
| 290 | if err != nil { |
| 291 | return true, false |
| 292 | } |
| 293 | v.SetFloat(float64(f)) |
| 294 | case float64Type: |
| 295 | f, err := utils.ParseFloat64(val) |
| 296 | if err != nil { |
| 297 | return true, false |
| 298 | } |
| 299 | v.SetFloat(f) |
| 300 | default: |
| 301 | return false, false |
| 302 | } |
| 303 | return true, true |
| 304 | } |