Spaces:
Running
Running
File size: 23,138 Bytes
b110593 |
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 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 |
// _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
// CONTACT: [email protected]
//
package validation
import (
"context"
"encoding/json"
"fmt"
"regexp"
"strings"
"time"
"github.com/google/uuid"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/schema"
"github.com/weaviate/weaviate/entities/schema/crossref"
)
const (
// ErrorInvalidSingleRef message
ErrorInvalidSingleRef string = "only direct references supported at the moment, concept references not supported yet: class '%s' with property '%s' requires exactly 1 arguments: 'beacon'. Check your input schema, got: %#v"
// ErrorMissingSingleRefCRef message
ErrorMissingSingleRefCRef string = "only direct references supported at the moment, concept references not supported yet: class '%s' with property '%s' requires exactly 1 argument: 'beacon' is missing, check your input schema"
// ErrorCrefInvalidURI message
ErrorCrefInvalidURI string = "class '%s' with property '%s' is not a valid URI: %s"
// ErrorCrefInvalidURIPath message
ErrorCrefInvalidURIPath string = "class '%s' with property '%s' does not contain a valid path, must have 2 segments: /<kind>/<id>"
// ErrorMissingSingleRefLocationURL message
ErrorMissingSingleRefLocationURL string = "class '%s' with property '%s' requires exactly 3 arguments: 'beacon', 'locationUrl' and 'type'. 'locationUrl' is missing, check your input schema"
// ErrorMissingSingleRefType message
ErrorMissingSingleRefType string = "class '%s' with property '%s' requires exactly 3 arguments: 'beacon', 'locationUrl' and 'type'. 'type' is missing, check your input schema"
)
func (v *Validator) properties(ctx context.Context, class *models.Class,
incomingObject *models.Object, existingObject *models.Object,
) error {
className := incomingObject.Class
isp := incomingObject.Properties
vectorWeights := incomingObject.VectorWeights
tenant := incomingObject.Tenant
if existingObject != nil && tenant != existingObject.Tenant {
return fmt.Errorf("tenant mismatch, expected %s but got %s", existingObject.Tenant, tenant)
}
if vectorWeights != nil {
res, err := v.validateVectorWeights(vectorWeights)
if err != nil {
return fmt.Errorf("vector weights: %v", err)
}
vectorWeights = res
}
if isp == nil {
// no properties means nothing to validate
return nil
}
inputSchema, ok := isp.(map[string]interface{})
if !ok {
return fmt.Errorf("could not recognize object's properties: %v", isp)
}
returnSchema := map[string]interface{}{}
for propertyKey, propertyValue := range inputSchema {
if propertyValue == nil {
continue // nil values are removed and filtered out
}
// properties in the class are saved with lower case first letter
propertyKeyLowerCase := strings.ToLower(propertyKey[:1])
if len(propertyKey) > 1 {
propertyKeyLowerCase += propertyKey[1:]
}
property, err := schema.GetPropertyByName(class, propertyKeyLowerCase)
if err != nil {
return err
}
dataType, err := schema.GetPropertyDataType(class, propertyKeyLowerCase)
if err != nil {
return err
}
// autodetect to_class in references
if dataType.String() == schema.DataTypeCRef.String() {
propertyValueSlice, ok := propertyValue.([]interface{})
if !ok {
return fmt.Errorf("reference property is not a slice %v", propertyValue)
}
for i := range propertyValueSlice {
propertyValueMap, ok := propertyValueSlice[i].(map[string]interface{})
if !ok {
return fmt.Errorf("reference property is not a map %v", propertyValueMap)
}
beacon := propertyValueMap["beacon"].(string)
beaconParsed, err := crossref.Parse(beacon)
if err != nil {
return err
}
if beaconParsed.Class == "" {
prop, err := schema.GetPropertyByName(class, schema.LowercaseFirstLetter(propertyKey))
if err != nil {
return err
}
if len(prop.DataType) > 1 {
continue
}
toClass := prop.DataType[0] // datatype is the name of the class that is referenced
toBeacon := crossref.NewLocalhost(toClass, beaconParsed.TargetID).String()
propertyValue.([]interface{})[i].(map[string]interface{})["beacon"] = toBeacon
}
}
}
var data interface{}
if schema.IsNested(*dataType) {
data, err = v.extractAndValidateNestedProperty(ctx, propertyKeyLowerCase, propertyValue, className,
dataType, property.NestedProperties)
} else {
data, err = v.extractAndValidateProperty(ctx, propertyKeyLowerCase, propertyValue, className, dataType, tenant)
}
if err != nil {
return err
}
returnSchema[propertyKeyLowerCase] = data
}
incomingObject.Properties = returnSchema
incomingObject.VectorWeights = vectorWeights
return nil
}
func nestedPropertiesToMap(nestedProperties []*models.NestedProperty) map[string]*models.NestedProperty {
nestedPropertiesMap := map[string]*models.NestedProperty{}
for _, nestedProperty := range nestedProperties {
nestedPropertiesMap[nestedProperty.Name] = nestedProperty
}
return nestedPropertiesMap
}
// TODO nested
// refactor/simplify + improve recurring error msgs on nested properties
func (v *Validator) extractAndValidateNestedProperty(ctx context.Context, propertyName string,
val interface{}, className string, dataType *schema.DataType, nestedProperties []*models.NestedProperty,
) (interface{}, error) {
var data interface{}
var err error
switch *dataType {
case schema.DataTypeObject:
data, err = objectVal(ctx, v, val, propertyName, className, nestedPropertiesToMap(nestedProperties))
if err != nil {
return nil, fmt.Errorf("invalid object property '%s' on class '%s': %w", propertyName, className, err)
}
case schema.DataTypeObjectArray:
data, err = objectArrayVal(ctx, v, val, propertyName, className, nestedPropertiesToMap(nestedProperties))
if err != nil {
return nil, fmt.Errorf("invalid object[] property '%s' on class '%s': %w", propertyName, className, err)
}
default:
return nil, fmt.Errorf("unrecognized data type '%s'", *dataType)
}
return data, nil
}
func objectVal(ctx context.Context, v *Validator, val interface{}, propertyPrefix string,
className string, nestedPropertiesMap map[string]*models.NestedProperty,
) (map[string]interface{}, error) {
typed, ok := val.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("object must be a map, but got: %T", val)
}
for nestedKey, nestedValue := range typed {
propertyName := propertyPrefix + "." + nestedKey
nestedProperty, ok := nestedPropertiesMap[nestedKey]
if !ok {
return nil, fmt.Errorf("unknown property '%s'", propertyName)
}
nestedDataType, err := schema.GetValueDataTypeFromString(nestedProperty.DataType[0])
if err != nil {
return nil, fmt.Errorf("property '%s': %w", propertyName, err)
}
var data interface{}
if schema.IsNested(*nestedDataType) {
data, err = v.extractAndValidateNestedProperty(ctx, propertyName, nestedValue,
className, nestedDataType, nestedProperty.NestedProperties)
} else {
data, err = v.extractAndValidateProperty(ctx, propertyName, nestedValue,
className, nestedDataType, "")
// tenant isn't relevant for nested properties since crossrefs are not allowed
}
if err != nil {
return nil, fmt.Errorf("property '%s': %w", propertyName, err)
}
typed[nestedKey] = data
}
return typed, nil
}
func objectArrayVal(ctx context.Context, v *Validator, val interface{}, propertyPrefix string,
className string, nestedPropertiesMap map[string]*models.NestedProperty,
) (interface{}, error) {
typed, ok := val.([]interface{})
if !ok {
return nil, fmt.Errorf("not an object array, but %T", val)
}
for i := range typed {
data, err := objectVal(ctx, v, typed[i], propertyPrefix, className, nestedPropertiesMap)
if err != nil {
return nil, fmt.Errorf("invalid object '%d' in array: %w", i, err)
}
typed[i] = data
}
return typed, nil
}
func (v *Validator) extractAndValidateProperty(ctx context.Context, propertyName string, pv interface{},
className string, dataType *schema.DataType, tenant string,
) (interface{}, error) {
var (
data interface{}
err error
)
switch *dataType {
case schema.DataTypeCRef:
data, err = v.cRef(ctx, propertyName, pv, className, tenant)
if err != nil {
return nil, fmt.Errorf("invalid cref: %s", err)
}
case schema.DataTypeText:
data, err = stringVal(pv)
if err != nil {
return nil, fmt.Errorf("invalid text property '%s' on class '%s': %s", propertyName, className, err)
}
case schema.DataTypeUUID:
asStr, err := stringVal(pv)
if err != nil {
return nil, fmt.Errorf("invalid uuid property '%s' on class '%s': %s", propertyName, className, err)
}
data, err = uuid.Parse(asStr)
if err != nil {
return nil, fmt.Errorf("invalid uuid property '%s' on class '%s': %s", propertyName, className, err)
}
case schema.DataTypeInt:
data, err = intVal(pv)
if err != nil {
return nil, fmt.Errorf("invalid integer property '%s' on class '%s': %s", propertyName, className, err)
}
case schema.DataTypeNumber:
data, err = numberVal(pv)
if err != nil {
return nil, fmt.Errorf("invalid number property '%s' on class '%s': %s", propertyName, className, err)
}
case schema.DataTypeBoolean:
data, err = boolVal(pv)
if err != nil {
return nil, fmt.Errorf("invalid boolean property '%s' on class '%s': %s", propertyName, className, err)
}
case schema.DataTypeDate:
data, err = dateVal(pv)
if err != nil {
return nil, fmt.Errorf("invalid date property '%s' on class '%s': %s", propertyName, className, err)
}
case schema.DataTypeGeoCoordinates:
data, err = geoCoordinates(pv)
if err != nil {
return nil, fmt.Errorf("invalid geoCoordinates property '%s' on class '%s': %s", propertyName, className, err)
}
case schema.DataTypePhoneNumber:
data, err = phoneNumber(pv)
if err != nil {
return nil, fmt.Errorf("invalid phoneNumber property '%s' on class '%s': %s", propertyName, className, err)
}
case schema.DataTypeBlob:
data, err = blobVal(pv)
if err != nil {
return nil, fmt.Errorf("invalid blob property '%s' on class '%s': %s", propertyName, className, err)
}
case schema.DataTypeTextArray:
data, err = stringArrayVal(pv, "text")
if err != nil {
return nil, fmt.Errorf("invalid text array property '%s' on class '%s': %s", propertyName, className, err)
}
case schema.DataTypeIntArray:
data, err = intArrayVal(pv)
if err != nil {
return nil, fmt.Errorf("invalid integer array property '%s' on class '%s': %s", propertyName, className, err)
}
case schema.DataTypeNumberArray:
data, err = numberArrayVal(pv)
if err != nil {
return nil, fmt.Errorf("invalid number array property '%s' on class '%s': %s", propertyName, className, err)
}
case schema.DataTypeBooleanArray:
data, err = boolArrayVal(pv)
if err != nil {
return nil, fmt.Errorf("invalid boolean array property '%s' on class '%s': %s", propertyName, className, err)
}
case schema.DataTypeDateArray:
data, err = dateArrayVal(pv)
if err != nil {
return nil, fmt.Errorf("invalid date array property '%s' on class '%s': %s", propertyName, className, err)
}
case schema.DataTypeUUIDArray:
data, err = ParseUUIDArray(pv)
if err != nil {
return nil, fmt.Errorf("invalid uuid array property '%s' on class '%s': %s", propertyName, className, err)
}
// deprecated string
case schema.DataTypeString:
data, err = stringVal(pv)
if err != nil {
return nil, fmt.Errorf("invalid string property '%s' on class '%s': %s", propertyName, className, err)
}
// deprecated string
case schema.DataTypeStringArray:
data, err = stringArrayVal(pv, "string")
if err != nil {
return nil, fmt.Errorf("invalid string array property '%s' on class '%s': %s", propertyName, className, err)
}
default:
return nil, fmt.Errorf("unrecognized data type '%s'", *dataType)
}
return data, nil
}
func (v *Validator) cRef(ctx context.Context, propertyName string, pv interface{},
className, tenant string,
) (interface{}, error) {
switch refValue := pv.(type) {
case map[string]interface{}:
return nil, fmt.Errorf("reference must be an array, but got a map: %#v", refValue)
case []interface{}:
crefs := models.MultipleRef{}
for _, ref := range refValue {
refTyped, ok := ref.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Multiple references in %s.%s should be a list of maps, but we got: %T",
className, propertyName, ref)
}
cref, err := v.parseAndValidateSingleRef(ctx, propertyName, refTyped, className, tenant)
if err != nil {
return nil, err
}
crefs = append(crefs, cref)
}
return crefs, nil
default:
return nil, fmt.Errorf("invalid ref type. Needs to be []map, got %T", pv)
}
}
func stringVal(val interface{}) (string, error) {
typed, ok := val.(string)
if !ok {
return "", fmt.Errorf("not a string, but %T", val)
}
return typed, nil
}
func boolVal(val interface{}) (bool, error) {
typed, ok := val.(bool)
if !ok {
return false, fmt.Errorf("not a bool, but %T", val)
}
return typed, nil
}
func dateVal(val interface{}) (time.Time, error) {
var data time.Time
var err error
var ok bool
errorInvalidDate := "requires a string with a RFC3339 formatted date, but the given value is '%v'"
var dateString string
if dateString, ok = val.(string); !ok {
return time.Time{}, fmt.Errorf(errorInvalidDate, val)
}
// Parse the time as this has to be correct
data, err = time.Parse(time.RFC3339, dateString)
// Return if there is an error while parsing
if err != nil {
return time.Time{}, fmt.Errorf(errorInvalidDate, val)
}
return data, nil
}
func intVal(val interface{}) (interface{}, error) {
var data interface{}
var ok bool
var err error
errInvalidInteger := "requires an integer, the given value is '%v'"
errInvalidIntegerConvertion := "the JSON number '%v' could not be converted to an int"
// Return err when the input can not be casted to json.Number
if _, ok = val.(json.Number); !ok {
// If value is not a json.Number, it could be an int, which is fine
if data, ok = val.(int64); !ok {
// If value is not a json.Number, it could be an int, which is fine when the float does not contain a decimal
if data, ok = val.(float64); ok {
// Check whether the float is containing a decimal
if data != float64(int64(data.(float64))) {
return nil, fmt.Errorf(errInvalidInteger, val)
}
} else {
// If it is not a float, it is certainly not a integer, return the err
return nil, fmt.Errorf(errInvalidInteger, val)
}
}
} else if data, err = val.(json.Number).Int64(); err != nil {
// Return err when the input can not be converted to an int
return nil, fmt.Errorf(errInvalidIntegerConvertion, val)
}
return data, nil
}
func numberVal(val interface{}) (interface{}, error) {
var data interface{}
var ok bool
var err error
errInvalidFloat := "requires a float, the given value is '%v'"
errInvalidFloatConvertion := "the JSON number '%v' could not be converted to a float."
if _, ok = val.(json.Number); !ok {
if data, ok = val.(float64); !ok {
data64, ok := val.(int64)
if !ok {
return nil, fmt.Errorf(errInvalidFloat, val)
}
data = float64(data64)
}
} else if data, err = val.(json.Number).Float64(); err != nil {
return nil, fmt.Errorf(errInvalidFloatConvertion, val)
}
return data, nil
}
func geoCoordinates(input interface{}) (*models.GeoCoordinates, error) {
inputMap, ok := input.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("geoCoordinates must be a map, but got: %T", input)
}
lon, ok := inputMap["longitude"]
if !ok {
return nil, fmt.Errorf("geoCoordinates is missing required field 'longitude'")
}
lat, ok := inputMap["latitude"]
if !ok {
return nil, fmt.Errorf("geoCoordinates is missing required field 'latitude'")
}
lonFloat, err := parseCoordinate(lon)
if err != nil {
return nil, fmt.Errorf("invalid longitude: %s", err)
}
latFloat, err := parseCoordinate(lat)
if err != nil {
return nil, fmt.Errorf("invalid latitude: %s", err)
}
return &models.GeoCoordinates{
Longitude: ptFloat32(float32(lonFloat)),
Latitude: ptFloat32(float32(latFloat)),
}, nil
}
func ptFloat32(in float32) *float32 {
return &in
}
func phoneNumber(data interface{}) (*models.PhoneNumber, error) {
dataMap, ok := data.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("phoneNumber must be a map, but got: %T", data)
}
input, ok := dataMap["input"]
if !ok {
return nil, fmt.Errorf("phoneNumber is missing required field 'input'")
}
inputString, ok := input.(string)
if !ok {
return nil, fmt.Errorf("phoneNumber.input must be a string")
}
var defaultCountryString string
defaultCountry, ok := dataMap["defaultCountry"]
if !ok {
defaultCountryString = ""
} else {
defaultCountryString, ok = defaultCountry.(string)
if !ok {
return nil, fmt.Errorf("phoneNumber.defaultCountry must be a string")
}
}
return parsePhoneNumber(inputString, defaultCountryString)
}
func parseCoordinate(raw interface{}) (float64, error) {
switch v := raw.(type) {
case json.Number:
asFloat, err := v.Float64()
if err != nil {
return 0, fmt.Errorf("cannot interpret as float: %s", err)
}
return asFloat, nil
case float64:
return v, nil
default:
return 0, fmt.Errorf("must be json.Number or float, but got %T", raw)
}
}
func blobVal(val interface{}) (string, error) {
typed, ok := val.(string)
if !ok {
return "", fmt.Errorf("not a blob base64 string, but %T", val)
}
base64Regex := regexp.MustCompile(`^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$`)
ok = base64Regex.MatchString(typed)
if !ok {
return "", fmt.Errorf("not a valid blob base64 string")
}
return typed, nil
}
func (v *Validator) parseAndValidateSingleRef(ctx context.Context, propertyName string,
pvcr map[string]interface{}, className, tenant string,
) (*models.SingleRef, error) {
delete(pvcr, "href")
// Return different types of errors for cref input
if len(pvcr) != 1 {
// Give an error if the cref is not filled with correct number of properties
return nil, fmt.Errorf(
ErrorInvalidSingleRef,
className,
propertyName,
pvcr,
)
} else if _, ok := pvcr["beacon"]; !ok {
// Give an error if the cref is not filled with correct properties (beacon)
return nil, fmt.Errorf(
ErrorMissingSingleRefCRef,
className,
propertyName,
)
}
ref, err := crossref.Parse(pvcr["beacon"].(string))
if err != nil {
return nil, fmt.Errorf("invalid reference: %s", err)
}
errVal := fmt.Sprintf("'cref' %s:%s", className, propertyName)
ref, err = v.ValidateSingleRef(ref.SingleRef())
if err != nil {
return nil, err
}
if err = v.ValidateExistence(ctx, ref, errVal, tenant); err != nil {
return nil, err
}
// Validate whether reference exists based on given Type
return ref.SingleRef(), nil
}
// vectorWeights are passed as a non-typed interface{}, this is due to a
// limitation in go-swagger which itself is coming from swagger 2.0 which does
// not have support for arbitrary key/value objects
//
// we must thus validate that it's a map and they keys are strings
// NOTE: We are not validating the semantic correctness of the equations
// themselves, as they are in the contextinoary's responsibility
func (v *Validator) validateVectorWeights(in interface{}) (map[string]string, error) {
asMap, ok := in.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("must be key/value object with strings as keys and values, got %#v", in)
}
out := make(map[string]string, len(asMap))
for key, value := range asMap {
asString, ok := value.(string)
if !ok {
return nil, fmt.Errorf("key '%s': incorrect datatype: must be string, got %T", key, value)
}
out[key] = asString
}
return out, nil
}
func stringArrayVal(val interface{}, typeName string) ([]interface{}, error) {
typed, ok := val.([]interface{})
if !ok {
return nil, fmt.Errorf("not a %s array, but %T", typeName, val)
}
for i := range typed {
if _, err := stringVal(typed[i]); err != nil {
return nil, fmt.Errorf("invalid %s array value: %s", typeName, val)
}
}
return typed, nil
}
func intArrayVal(val interface{}) ([]interface{}, error) {
typed, ok := val.([]interface{})
if !ok {
return nil, fmt.Errorf("not an integer array, but %T", val)
}
for i := range typed {
if _, err := intVal(typed[i]); err != nil {
return nil, fmt.Errorf("invalid integer array value: %s", val)
}
}
return typed, nil
}
func numberArrayVal(val interface{}) ([]interface{}, error) {
typed, ok := val.([]interface{})
if !ok {
return nil, fmt.Errorf("not an integer array, but %T", val)
}
for i := range typed {
data, err := numberVal(typed[i])
if err != nil {
return nil, fmt.Errorf("invalid integer array value: %s", val)
}
typed[i] = data
}
return typed, nil
}
func boolArrayVal(val interface{}) ([]interface{}, error) {
typed, ok := val.([]interface{})
if !ok {
return nil, fmt.Errorf("not a boolean array, but %T", val)
}
for i := range typed {
if _, err := boolVal(typed[i]); err != nil {
return nil, fmt.Errorf("invalid boolean array value: %s", val)
}
}
return typed, nil
}
func dateArrayVal(val interface{}) ([]interface{}, error) {
typed, ok := val.([]interface{})
if !ok {
return nil, fmt.Errorf("not a date array, but %T", val)
}
for i := range typed {
if _, err := dateVal(typed[i]); err != nil {
return nil, fmt.Errorf("invalid date array value: %s", val)
}
}
return typed, nil
}
func ParseUUIDArray(in any) ([]uuid.UUID, error) {
var err error
if parsed, ok := in.([]uuid.UUID); ok {
return parsed, nil
}
asSlice, ok := in.([]any)
if !ok {
return nil, fmt.Errorf("not a slice type: %T", in)
}
d := make([]uuid.UUID, len(asSlice))
for i, elem := range asSlice {
asUUID, ok := elem.(uuid.UUID)
if ok {
d[i] = asUUID
continue
}
asStr, ok := elem.(string)
if !ok {
return nil, fmt.Errorf("array element neither uuid.UUID nor str, but: %T", elem)
}
d[i], err = uuid.Parse(asStr)
if err != nil {
return nil, fmt.Errorf("at pos %d: %w", i, err)
}
}
return d, nil
}
|