-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabuilder.go
More file actions
174 lines (154 loc) · 3.81 KB
/
databuilder.go
File metadata and controls
174 lines (154 loc) · 3.81 KB
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
171
172
173
174
package databuilder
import (
"context"
"reflect"
)
/*
* Before going throught this code please read - https://go.dev/blog/laws-of-reflection
*/
// SupportPackageIsVersion1 is a compile-time assertion constant.
// Downstream packages reference this to enforce version compatibility.
const SupportPackageIsVersion1 = true
type builder struct {
fnValue reflect.Value // cached reflect.ValueOf(builder func) to avoid repeated reflection
In []string
Out string
Name string
}
type db struct {
builders map[string]*builder
outSet stringSet
}
func (d *db) AddBuilders(builders ...any) error {
// initialize
if d.builders == nil {
d.builders = make(map[string]*builder)
}
if d.outSet == nil {
d.outSet = newStringSet()
}
// 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 any) 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 ...any) (Plan, error) {
initialData := 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
}
initialData = append(initialData, cachedStructName(t))
}
order, err := resolveDependencies(d.builders, initialData...)
if err != nil {
return nil, err
}
return newPlan(order, initialData)
}
// IsValidBuilder checks if the given function is valid or not
func IsValidBuilder(builder any) error {
if builder == nil {
return ErrInvalidBuilder
}
t := reflect.TypeOf(builder)
if t.Kind() != reflect.Func {
// Input can only be a function
return ErrInvalidBuilderKind
}
if reflect.ValueOf(builder).IsNil() {
return ErrInvalidBuilder
}
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 cachedStructName(t.In(i)) == cachedStructName(t.Out(0)) {
return ErrSameInputAsOutput
}
}
} else {
return ErrInvalidBuilderMissingContext
}
return nil
}
func getBuilder(bldr any) (*builder, error) {
if err := IsValidBuilder(bldr); err != nil {
return nil, err
}
fnValue := reflect.ValueOf(bldr)
if fnValue.IsNil() {
return nil, ErrInvalidBuilder
}
t := fnValue.Type()
out := cachedStructName(t.Out(0))
name := resolveFuncName(fnValue.Pointer())
b := &builder{
Out: out,
fnValue: fnValue,
Name: name,
}
// first in context.Context so we start from second
for i := 1; i < t.NumIn(); i++ {
b.In = append(b.In, cachedStructName(t.In(i)))
}
return b, nil
}
// New Creates a new DataBuilder
func New() DataBuilder {
return &db{}
}