MCPcopy
hub / github.com/cortexlabs/cortex / StructList

Function StructList

pkg/lib/configreader/reader.go:391–442  ·  view source on GitHub ↗
(dest interface{}, inter interface{}, v *StructListValidation)

Source from the content-addressed store, hash-verified

389}
390
391func StructList(dest interface{}, inter interface{}, v *StructListValidation) (interface{}, []error) {
392 if inter == nil {
393 if v.TreatNullAsEmpty {
394 inter = make([]interface{}, 0)
395 } else {
396 if !v.AllowExplicitNull {
397 return nil, []error{ErrorCannotBeEmptyOrNull(v.Required)}
398 }
399 return nil, nil
400 }
401 }
402
403 interSlice, ok := cast.InterfaceToInterfaceSlice(inter)
404 if !ok {
405 return nil, []error{ErrorInvalidPrimitiveType(inter, PrimTypeList)}
406 }
407
408 if v.MinLength != 0 {
409 if len(interSlice) < v.MinLength {
410 return nil, []error{ErrorTooFewElements(v.MinLength)}
411 }
412 }
413 if v.MaxLength != 0 {
414 if len(interSlice) > v.MaxLength {
415 return nil, []error{ErrorTooManyElements(v.MaxLength)}
416 }
417 }
418 for _, invalidLength := range v.InvalidLengths {
419 if len(interSlice) == invalidLength {
420 return nil, []error{ErrorWrongNumberOfElements(v.InvalidLengths)}
421 }
422 }
423
424 errs := []error{}
425 for i, interItem := range interSlice {
426 val := reflect.New(reflect.ValueOf(dest).Type().Elem().Elem()).Interface()
427 subErrs := Struct(val, interItem, v.StructValidation)
428 var ok bool
429 if errs, ok = errors.AddErrors(errs, subErrs, s.Index(i)); ok {
430 if v.ShortCircuit {
431 return nil, errs
432 }
433 continue
434 }
435 if interItem == nil {
436 val = nil // If the object was nil, set val to nil rather than a pointer to the initialized zero value
437 }
438 dest = appendVal(dest, val)
439 }
440
441 return dest, errs
442}
443
444func InterfaceStruct(inter interface{}, v *InterfaceStructValidation) (interface{}, []error) {
445 if inter == nil {

Callers 1

StructFunction · 0.85

Calls 10

AddErrorsFunction · 0.92
ErrorCannotBeEmptyOrNullFunction · 0.85
ErrorTooFewElementsFunction · 0.85
ErrorTooManyElementsFunction · 0.85
StructFunction · 0.85
appendValFunction · 0.85
TypeMethod · 0.80

Tested by

no test coverage detected