forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
116 lines (100 loc) · 3.42 KB
/
wrapper.go
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
package validation
import (
"fmt"
"reflect"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
)
type WrappingValidator struct {
validate *reflect.Value
validateUpdate *reflect.Value
}
func (v *WrappingValidator) Validate(obj runtime.Object) field.ErrorList {
return callValidate(reflect.ValueOf(obj), *v.validate)
}
func (v *WrappingValidator) ValidateUpdate(obj, old runtime.Object) field.ErrorList {
if v.validateUpdate == nil {
// if there is no update validation, fail.
return field.ErrorList{field.Forbidden(field.NewPath("obj"), fmt.Sprintf("%v", obj))}
}
return callValidateUpdate(reflect.ValueOf(obj), reflect.ValueOf(old), *v.validateUpdate)
}
func NewValidationWrapper(validateFunction interface{}, validateUpdateFunction interface{}) (*WrappingValidator, error) {
validateFunctionValue := reflect.ValueOf(validateFunction)
validateType := validateFunctionValue.Type()
if err := verifyValidateFunctionSignature(validateType); err != nil {
return nil, err
}
var validateUpdateFunctionValue *reflect.Value
if validateUpdateFunction != nil {
functionValue := reflect.ValueOf(validateUpdateFunction)
validateUpdateType := functionValue.Type()
if err := verifyValidateUpdateFunctionSignature(validateUpdateType); err != nil {
return nil, err
}
validateUpdateFunctionValue = &functionValue
}
return &WrappingValidator{&validateFunctionValue, validateUpdateFunctionValue}, nil
}
func verifyValidateFunctionSignature(ft reflect.Type) error {
if ft.Kind() != reflect.Func {
return fmt.Errorf("expected func, got: %v", ft)
}
if ft.NumIn() != 1 {
return fmt.Errorf("expected one 'in' param, got: %v", ft)
}
if ft.NumOut() != 1 {
return fmt.Errorf("expected one 'out' param, got: %v", ft)
}
if ft.In(0).Kind() != reflect.Ptr {
return fmt.Errorf("expected pointer arg for 'in' param 0, got: %v", ft)
}
errorType := reflect.TypeOf(&field.ErrorList{}).Elem()
if ft.Out(0) != errorType {
return fmt.Errorf("expected field.ErrorList return, got: %v", ft)
}
return nil
}
func verifyValidateUpdateFunctionSignature(ft reflect.Type) error {
if ft.Kind() != reflect.Func {
return fmt.Errorf("expected func, got: %v", ft)
}
if ft.NumIn() != 2 {
return fmt.Errorf("expected two 'in' params, got: %v", ft)
}
if ft.NumOut() != 1 {
return fmt.Errorf("expected one 'out' param, got: %v", ft)
}
if ft.In(0).Kind() != reflect.Ptr {
return fmt.Errorf("expected pointer arg for 'in' param 0, got: %v", ft)
}
if ft.In(1).Kind() != reflect.Ptr {
return fmt.Errorf("expected pointer arg for 'in' param 1, got: %v", ft)
}
errorType := reflect.TypeOf(&field.ErrorList{}).Elem()
if ft.Out(0) != errorType {
return fmt.Errorf("expected field.ErrorList return, got: %v", ft)
}
return nil
}
// callCustom calls 'custom' with sv & dv. custom must be a conversion function.
func callValidate(obj, validateMethod reflect.Value) field.ErrorList {
args := []reflect.Value{obj}
ret := validateMethod.Call(args)[0].Interface()
// This convolution is necessary because nil interfaces won't convert
// to errors.
if ret == nil {
return nil
}
return ret.(field.ErrorList)
}
func callValidateUpdate(obj, old, validateMethod reflect.Value) field.ErrorList {
args := []reflect.Value{obj, old}
ret := validateMethod.Call(args)[0].Interface()
// This convolution is necessary because nil interfaces won't convert
// to errors.
if ret == nil {
return nil
}
return ret.(field.ErrorList)
}