-
Notifications
You must be signed in to change notification settings - Fork 8
/
store.go
89 lines (73 loc) · 1.67 KB
/
store.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
package zebra
import (
"errors"
)
type Operator uint8
// Constants defined for QueryOperator type.
const (
MatchEqual Operator = iota
MatchNotEqual
MatchIn
MatchNotIn
)
// Command struct for label queries.
type Query struct {
Key string `json:"key"`
Op Operator `json:"op"`
Values []string `json:"values"`
}
var (
ErrNotFound = errors.New("resource not found in store")
ErrInvalidResource = errors.New("create/delete on invalid resource")
ErrInvalidQuery = errors.New("invalid query")
)
// Store interface requires basic store functionalities.
type Store interface {
Initialize() error
Wipe() error
Clear() error
Load() (*ResourceMap, error)
Create(res Resource) error
Delete(res Resource) error
Query() *ResourceMap
QueryUUID(uuids []string) *ResourceMap
QueryType(types []string) *ResourceMap
QueryLabel(query Query) (*ResourceMap, error)
QueryProperty(query Query) (*ResourceMap, error)
}
func (q *Query) Validate() error {
if (q.Op == MatchEqual || q.Op == MatchNotEqual) && len(q.Values) != 1 {
return ErrInvalidQuery
}
if q.Op > MatchNotIn {
return ErrInvalidQuery
}
return nil
}
func (o *Operator) MarshalText() ([]byte, error) {
opMap := map[Operator]string{
MatchEqual: "==",
MatchNotEqual: "!=",
MatchIn: "in",
MatchNotIn: "notin",
}
opVal, ok := opMap[*o]
if !ok {
return []byte(""), ErrInvalidQuery
}
return []byte(opVal), nil
}
func (o *Operator) UnmarshalText(data []byte) error {
opMap := map[string]Operator{
"==": MatchEqual,
"!=": MatchNotEqual,
"in": MatchIn,
"notin": MatchNotIn,
}
op, ok := opMap[string(data)]
if !ok {
return ErrInvalidQuery
}
*o = op
return nil
}