@@ -16,51 +16,50 @@ import (
16
16
//
17
17
// Validation errors are returned to the caller
18
18
func All (in , predicateFn interface {}) (bool , error ) {
19
- var output bool = true ;
19
+
20
20
input := reflect .ValueOf (in )
21
21
predicate := reflect .ValueOf (predicateFn )
22
22
23
23
if predicate .Kind () != reflect .Func {
24
- return output , fmt .Errorf ("predicateFn has to be a function" )
24
+ return false , fmt .Errorf ("predicateFn has to be a function" )
25
25
}
26
26
27
27
predicateFnType := predicate .Type ()
28
28
if predicateFnType .NumIn () != 1 {
29
- return output , fmt .Errorf ("predicate function has to take only one argument" )
29
+ return false , fmt .Errorf ("predicate function has to take only one argument" )
30
30
}
31
31
32
32
if predicateFnType .NumOut () != 1 {
33
- return output , fmt .Errorf ("predicate function should return only one return value" )
33
+ return false , fmt .Errorf ("predicate function should return only one return value" )
34
34
}
35
35
36
36
if predicateFnType .Out (0 ).Kind () != reflect .Bool {
37
- return output , fmt .Errorf ("predicate function should return a boolean value" )
37
+ return false , fmt .Errorf ("predicate function should return a boolean value" )
38
38
}
39
39
40
40
inputKind := input .Kind ()
41
41
if inputKind == reflect .Slice {
42
42
inputSliceElemType := input .Type ().Elem
43
43
predicateFnArgType := predicateFnType .In (0 )
44
44
if inputSliceElemType () != predicateFnArgType {
45
- return output , fmt .Errorf ("predicate function's argument (%s) has to be (%s)" , predicateFnArgType , inputSliceElemType ())
45
+ return false , fmt .Errorf ("predicate function's argument (%s) has to be (%s)" , predicateFnArgType , inputSliceElemType ())
46
46
}
47
47
48
48
for i := 0 ; i < input .Len (); i ++ {
49
49
arg := input .Index (i )
50
50
returnValue := predicate .Call ([]reflect.Value {arg })[0 ]
51
51
if ! returnValue .Bool () {
52
- output = false
53
- break
52
+ return false , nil
54
53
}
55
54
}
56
55
57
- return output , nil
56
+ return true , nil
58
57
}
59
58
60
- return output , fmt .Errorf ("not implemented for (%s)" , inputKind )
59
+ return false , fmt .Errorf ("not implemented for (%s)" , inputKind )
61
60
}
62
61
63
62
// Every is an alias for All function
64
63
func Every (in , predicateFn interface {}) (bool , error ) {
65
64
return All (in , predicateFn )
66
- }
65
+ }
0 commit comments