-
Notifications
You must be signed in to change notification settings - Fork 0
/
databuilder.go
170 lines (149 loc) · 3.63 KB
/
databuilder.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package databuilder
import (
"context"
"reflect"
"runtime"
"k8s.io/apimachinery/pkg/util/sets"
)
/*
* Before going throught this code please read - https://go.dev/blog/laws-of-reflection
*/
type builder struct {
Builder interface{}
In []string
Out string
Name string
}
type db struct {
builders map[string]*builder
outSet sets.String
}
func (d *db) AddBuilders(builders ...interface{}) error {
// initialize
if d.builders == nil {
d.builders = make(map[string]*builder)
}
if d.outSet == nil {
d.outSet = sets.NewString()
}
// go through all builders and add them
for i := range builders {
b := builders[i]
if b == nil {
return ErrInvalidBuilder
}
if err := d.add(b); err != nil {
return err
}
}
return nil
}
func (d *db) add(bldr interface{}) error {
b, err := getBuilder(bldr)
if err != nil {
return err
}
// check for name
if _, ok := d.builders[b.Name]; ok {
return nil
}
//check for outSet
if d.outSet.Has(b.Out) {
return ErrMultipleBuilderSameOutput
}
d.builders[b.Name] = b
d.outSet.Insert(b.Out)
return nil
}
func (d *db) Compile(init ...interface{}) (Plan, error) {
initialialData := make([]string, 0, len(init))
for _, inter := range init {
if inter == nil {
continue
}
t := reflect.TypeOf(inter)
if t.Kind() != reflect.Struct {
return nil, ErrInvalidBuilderInput
}
initialialData = append(initialialData, getStructName(t))
}
order, err := resolveDependencies(d.builders, initialialData...)
if err != nil {
return nil, err
}
return newPlan(order, initialialData)
}
// IsValidBuilder checks if the given function is valid or not
func IsValidBuilder(builder interface{}) error {
t := reflect.TypeOf(builder)
if t.Kind() != reflect.Func {
// Input can only be a function
return ErrInvalidBuilderKind
}
if t.NumOut() != 2 {
// should return a struct and an error
return ErrInvalidBuilderNumOutput
}
if t.Out(0).Kind() != reflect.Struct {
// first return argument should always be a struct
return ErrInvalidBuilderFirstOutput
}
if t.Out(1).Kind() != reflect.Interface {
// second return argument should always be an Interface
return ErrInvalidBuilderSecondOutput
}
if !t.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
// second return argument should always be an error
return ErrInvalidBuilderSecondOutput
}
if t.NumIn() > 0 {
// first input should always be context.Context
if t.In(0).Kind() != reflect.Interface {
return ErrInvalidBuilderMissingContext
}
if !t.In(0).Implements(reflect.TypeOf((*context.Context)(nil)).Elem()) {
return ErrInvalidBuilderMissingContext
}
// other inputs should all be structs
for i := 1; i < t.NumIn(); i++ {
if t.In(i).Kind() != reflect.Struct {
// checks for vardic functions as well
return ErrInvalidBuilderInput
}
if getStructName(t.In(i)) == getStructName(t.Out(0)) {
return ErrSameInputAsOutput
}
}
} else {
return ErrInvalidBuilderMissingContext
}
return nil
}
func getBuilder(bldr interface{}) (*builder, error) {
if err := IsValidBuilder(bldr); err != nil {
return nil, err
}
t := reflect.TypeOf(bldr)
out := getStructName(t.Out(0))
name := getFuncName(bldr)
b := &builder{
Out: out,
Builder: bldr,
Name: name,
}
// first in context.Context so we start from second
for i := 1; i < t.NumIn(); i++ {
b.In = append(b.In, getStructName(t.In(i)))
}
return b, nil
}
func getFuncName(bldr interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(bldr).Pointer()).Name()
}
func getStructName(t reflect.Type) string {
return t.PkgPath() + "." + t.Name()
}
// New Creates a new DataBuilder
func New() DataBuilder {
return &db{}
}