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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
| package util
import ( "fmt" "strconv" "strings" "time"
"github.com/google/uuid" "github.com/pkg/errors" )
func DefaultValue[T any]() T { var ret T return ret }
func ParseBoolSimple(r any) bool { ret, _ := ParseBool(r, "", true) return ret }
func ParseFloatSimple(r any) float64 { ret, _ := ParseFloat(r, "", true) return ret }
func ParseIntSimple(r any) int { ret, _ := ParseInt(r, "", true) return ret }
func ParseInt16Simple(r any) int16 { ret, _ := ParseInt16(r, "", true) return ret }
func ParseInt32Simple(r any) int32 { ret, _ := ParseInt32(r, "", true) return ret }
func ParseInt64Simple(r any) int64 { ret, _ := ParseInt64(r, "", true) return ret }
func ParseJSONSimple(r any) any { ret, _ := ParseJSON(r, "", true) return ret }
func ParseMapSimple(r any) ValueMap { ret, _ := ParseMap(r, "", true) return ret }
func ParseOrderedMapSimple(r any) *OrderedMap[any] { ret, _ := ParseOrderedMap(r, "", true) return ret }
func ParseStringSimple(r any) string { ret, _ := ParseString(r, "", true) return ret }
func ParseTimeSimple(r any) *time.Time { ret, _ := ParseTime(r, "", true) return ret }
func ParseUUIDSimple(r any) *uuid.UUID { ret, _ := ParseUUID(r, "", true) return ret }
func ParseBool(r any, path string, allowEmpty bool) (bool, error) { switch t := r.(type) { case bool: return t, nil case string: return t == BoolTrue, nil case nil: if !allowEmpty { return false, errors.Errorf("could not find bool for path [%s]", path) } return false, nil default: return false, invalidTypeError(path, "bool", t) } }
func ParseFloat(r any, path string, allowEmpty bool) (float64, error) { switch t := r.(type) { case int: return float64(t), nil case int64: return float64(t), nil case float64: return t, nil case string: ret, err := strconv.ParseFloat(t, 32) return ret, err case nil: if !allowEmpty { return 0, errors.Errorf("could not find float for path [%s]", path) } return 0, nil default: return 0, invalidTypeError(path, "float", t) } }
func ParseInt(r any, path string, allowEmpty bool) (int, error) { ret, err := ParseInt64(r, path, allowEmpty) if err != nil { return 0, err } return int(ret), nil }
func ParseInt16(r any, path string, allowEmpty bool) (int16, error) { switch t := r.(type) { case int: return int16(t), nil case int16: return t, nil case int32: return int16(t), nil case int64: return int16(t), nil case float64: return int16(t), nil case string: ret, err := strconv.ParseInt(t, 10, 16) return int16(ret), err case []byte: ret, err := strconv.ParseInt(string(t), 10, 16) return int16(ret), err case nil: if !allowEmpty { return 0, errors.Errorf("could not find int16 for path [%s]", path) } return 0, nil default: return 0, invalidTypeError(path, "int16", t) } }
func ParseInt32(r any, path string, allowEmpty bool) (int32, error) { switch t := r.(type) { case int: return int32(t), nil case int32: return t, nil case int64: return int32(t), nil case float64: return int32(t), nil case string: ret, err := strconv.ParseInt(t, 10, 32) return int32(ret), err case []byte: ret, err := strconv.ParseInt(string(t), 10, 32) return int32(ret), err case nil: if !allowEmpty { return 0, errors.Errorf("could not find int32 for path [%s]", path) } return 0, nil default: return 0, invalidTypeError(path, "int32", t) } }
func ParseInt64(r any, path string, allowEmpty bool) (int64, error) { switch t := r.(type) { case int: return int64(t), nil case int32: return int64(t), nil case int64: return t, nil case float64: return int64(t), nil case string: ret, err := strconv.ParseInt(t, 10, 64) return ret, err case []byte: ret, err := strconv.ParseInt(string(t), 10, 64) return ret, err case nil: if !allowEmpty { return 0, errors.Errorf("could not find int for path [%s]", path) } return 0, nil default: return 0, invalidTypeError(path, "int", t) } }
func ParseJSON(r any, path string, allowEmpty bool) (any, error) { switch t := r.(type) { case []byte: return FromJSONAny(t) case string: return FromJSONAny([]byte(t)) case nil: if !allowEmpty { return nil, errors.Errorf("could not find json for path [%s]", path) } return nil, nil default: return t, nil } }
func ParseMap(r any, path string, allowEmpty bool) (ValueMap, error) { switch t := r.(type) { case ValueMap: if (!allowEmpty) && len(t) == 0 { return nil, errors.New("empty map") } return t, nil case map[string]any: if (!allowEmpty) && len(t) == 0 { return nil, errors.New("empty map") } return t, nil case string: if strings.TrimSpace(t) == "" { return nil, nil } ret, err := FromJSONMap([]byte(t)) if err != nil { return nil, wrapError(path, "time", errors.Wrap(err, "invalid JSON")) } return ret, err case []byte: if len(t) == 0 { return nil, nil } ret, err := FromJSONMap(t) if err != nil { return nil, wrapError(path, "time", errors.Wrap(err, "invalid JSON")) } return ret, err case nil: if !allowEmpty { return nil, errors.Errorf("could not find map for path [%s]", path) } return nil, nil default: return nil, invalidTypeError(path, "map", t) } }
func ParseOrderedMap(r any, path string, allowEmpty bool) (*OrderedMap[any], error) { switch t := r.(type) { case *OrderedMap[any]: if (!allowEmpty) && len(t.Map) == 0 { return nil, errors.New("empty map") } return t, nil case ValueMap: if (!allowEmpty) && len(t) == 0 { return nil, errors.New("empty map") } o := NewOrderedMap[any](false, len(t)) for k, v := range t { o.Set(k, v) } return o, nil case map[string]any: if (!allowEmpty) && len(t) == 0 { return nil, errors.New("empty map") } o := NewOrderedMap[any](false, len(t)) for k, v := range t { o.Set(k, v) } return o, nil case string: if strings.TrimSpace(t) == "" { return nil, nil } ret, err := FromJSONOrderedMap[any]([]byte(t)) if err != nil { return nil, wrapError(path, "time", errors.Wrap(err, "invalid JSON")) } return ret, err case []byte: if len(t) == 0 { return nil, nil } ret, err := FromJSONOrderedMap[any](t) if err != nil { return nil, wrapError(path, "time", errors.Wrap(err, "invalid JSON")) } return ret, err case nil: if !allowEmpty { return nil, errors.Errorf("could not find ordered map for path [%s]", path) } return nil, nil default: return nil, invalidTypeError(path, "ordered map", t) } }
func ParseString(r any, path string, allowEmpty bool) (string, error) { switch t := r.(type) { case rune: return string(t), nil case string: if (!allowEmpty) && t == "" { return "", errors.New("empty string") } return t, nil case []byte: if (!allowEmpty) && len(t) == 0 { return "", errors.New("empty string") } return string(t), nil case []string: if (!allowEmpty) && (len(t) == 0 || t[0] == "") { return "", errors.New("empty string") } return StringJoin(t, "||"), nil case nil: if !allowEmpty { return "", errors.Errorf("could not find string for path [%s]", path) } return "", nil default: return fmt.Sprint(t), nil } }
func ParseTime(r any, path string, allowEmpty bool) (*time.Time, error) { switch t := r.(type) { case time.Time: return &t, nil case *time.Time: if t == nil && (!allowEmpty) { return nil, errors.New("empty time") } return t, nil case string: ret, err := TimeFromString(t) if err != nil { return nil, wrapError(path, t, err) } if ret == nil && (!allowEmpty) { return nil, errors.Errorf("invalid time [%s]", t) } return ret, nil case nil: if !allowEmpty { return nil, errors.Errorf("could not find time for path [%s]", path) } return nil, nil default: return nil, invalidTypeError(path, "time", t) } }
func ParseUUID(r any, path string, allowEmpty bool) (*uuid.UUID, error) { switch t := r.(type) { case *uuid.UUID: if t == nil && (!allowEmpty) { return nil, errors.New("empty uuid") } return t, nil case uuid.UUID: if t == uuid.Nil && (!allowEmpty) { return nil, errors.New("empty uuid") } return &t, nil case []byte: if len(t) == 16 { ret, err := uuid.FromBytes(t) if err != nil { return nil, err } if ret == uuid.Nil && (!allowEmpty) { return nil, errors.Errorf("could not parse uuid from path [%s]", path) } return &ret, nil } return nil, errors.Errorf("invalid uuid bytes with length [%d]", len(t)) case string: if t == "" && allowEmpty { return nil, nil } ret, err := uuid.Parse(t) if err != nil { return nil, err } if ret == uuid.Nil && (!allowEmpty) { return nil, errors.Errorf("could not find uuid for path [%s]", path) } return &ret, nil case nil: if !allowEmpty { return nil, errors.Errorf("could not find uuid for path [%s]", path) } return nil, nil default: return nil, invalidTypeError(path, "uuid", t) } }
func invalidTypeError(path string, t string, observed any) error { return errors.Errorf("unable to parse [%s] at path [%s], invalid type [%T]", t, path, observed) }
func wrapError(path string, t string, err error) error { if err == nil { return nil } return errors.Wrapf(err, "error parsing [%s] as [%s]", path, t) }
|