-
Notifications
You must be signed in to change notification settings - Fork 53
/
meta.go
84 lines (72 loc) · 1.84 KB
/
meta.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
package meta
import (
"errors"
"reflect"
"github.com/alecthomas/jsonschema"
)
var ErrUnknownObjectKind = errors.New("unknown object kind")
type TypeMeta struct {
Kind string `json:"kind,omitempty"`
APIVersion string `json:"apiVersion,omitempty"`
}
type ObjectMeta struct {
Name string `json:"name,omitempty"`
}
type Object interface {
GetAPIVersion() string
GetKind() string
}
type NamedObject interface {
Object
GetName() string
}
func (t TypeMeta) GetAPIVersion() string {
return t.APIVersion
}
func (t TypeMeta) GetKind() string {
return t.Kind
}
func (o ObjectMeta) GetName() string {
return o.Name
}
type ObjectList []Object
type ObjectVisitorFunc = interface{} // func(*T) or func(*T, *jsonschema.Schema)
func (l ObjectList) Visit(visitors ...ObjectVisitorFunc) (visitedAny bool) {
// For each object in the list, call each visitor if its argument type
// matches the concrete type of the object.
for _, vf := range visitors {
// get the type of the first arg
fn := reflect.TypeOf(vf)
if fn.Kind() != reflect.Func {
panic("visitor must be a function")
}
switch fn.NumIn() {
case 1: // func(*T)
t := fn.In(0)
fn := reflect.ValueOf(vf)
for _, o := range l {
if reflect.TypeOf(o) == t {
visitedAny = true
fn.Call([]reflect.Value{reflect.ValueOf(o)})
}
}
case 2: // func(*T, *jsonschema.Schema)
t := fn.In(0)
s := fn.In(1)
if s != reflect.TypeOf(&jsonschema.Schema{}) {
panic("second argument must be of type *jsonschema.Schema")
}
fn := reflect.ValueOf(vf)
var emptyIntf interface{}
emptyIntfType := reflect.TypeOf(&emptyIntf).Elem()
for _, o := range l {
if t == emptyIntfType || reflect.TypeOf(o) == t {
visitedAny = true
schema := jsonschema.Reflect(o)
fn.Call([]reflect.Value{reflect.ValueOf(o), reflect.ValueOf(schema)})
}
}
}
}
return
}