github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/inputvalidation/validators_pointer.go (about) 1 package inputvalidation 2 3 import ( 4 "reflect" 5 6 "github.com/pkg/errors" 7 ) 8 9 // ValidateExactlyOneNotNil validates if exactly one of passed pointers is not a nil. 10 func ValidateExactlyOneNotNil(errorMessage string, ptr interface{}, ptrs ...interface{}) error { 11 ptrs = append(ptrs, ptr) 12 13 ok, err := exactlyOneNotNil(ptrs) 14 if err != nil { 15 return err 16 } 17 18 if !ok { 19 return errors.New(errorMessage) 20 } 21 22 return nil 23 } 24 25 func exactlyOneNotNil(ptrs []interface{}) (bool, error) { 26 notNilFound := 0 27 for _, v := range ptrs { 28 if v == nil { 29 continue 30 } 31 if reflect.ValueOf(v).Kind() != reflect.Ptr { 32 return false, errors.New("field is not a pointer") 33 } 34 if !reflect.ValueOf(v).IsNil() { 35 notNilFound++ 36 } 37 } 38 return notNilFound == 1, nil 39 }