-
Notifications
You must be signed in to change notification settings - Fork 7
/
view.go
56 lines (43 loc) · 1.05 KB
/
view.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
//go:generate mockgen -source=view.go -destination=test/view.go -package=test_view
package view
import (
api "github.com/wetware/pkg/api/cluster"
)
type (
SelectorStruct = api.View_Selector
ConstraintStruct = api.View_Constraint
Selector func(SelectorStruct) error
Constraint func(ConstraintStruct) error
)
type QueryParams interface {
NewSelector() (api.View_Selector, error)
NewConstraints(int32) (api.View_Constraint_List, error)
}
type Query func(QueryParams) error
func NewQuery(s Selector, cs ...Constraint) Query {
return func(ps QueryParams) error {
if err := bindSelector(s, ps); err != nil {
return err
}
return bindConstraints(cs, ps)
}
}
func bindSelector(s Selector, ps QueryParams) error {
sel, err := ps.NewSelector()
if err != nil {
return err
}
return s(sel)
}
func bindConstraints(cs []Constraint, ps QueryParams) error {
constraint, err := ps.NewConstraints(int32(len(cs)))
if err != nil {
return err
}
for i, bind := range cs {
if err = bind(constraint.At(i)); err != nil {
break
}
}
return err
}