-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
build_options.go
42 lines (34 loc) · 948 Bytes
/
build_options.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
package registry
import (
"fmt"
"reflect"
)
type BuildOption func(v interface{}) error
// ValidateImplements will verify that the built value implements the given interface
//
// Pass interfaces using "(*I)(nil)", like in this example:
//
// type MyInterface interface {
// MyMethod(int) bool
// }
//
// ValidateImplements((*MyInterface)(nil))
//
// You will only be able to validate interfaces with exported methods, you will not be
// able to validate any that have one or more unexported methods.
func ValidateImplements(checkV interface{}) BuildOption {
checkT := reflect.TypeOf(checkV)
if checkT.Kind() == reflect.Ptr {
checkT = checkT.Elem()
}
if checkT.Kind() != reflect.Interface {
panic(fmt.Sprintf("%T is not an interface", checkV))
}
return func(v interface{}) error {
t := reflect.TypeOf(v)
if !t.Implements(checkT) {
return fmt.Errorf("%T does not implement %T", v, checkV)
}
return nil
}
}