-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
func.go
208 lines (180 loc) · 4.13 KB
/
func.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package dasel
import (
"fmt"
"reflect"
"strings"
)
type ErrUnknownFunction struct {
Function string
}
func (e ErrUnknownFunction) Error() string {
return fmt.Sprintf("unknown function: %s", e.Function)
}
func (e ErrUnknownFunction) Is(other error) bool {
_, ok := other.(*ErrUnknownFunction)
return ok
}
type ErrUnexpectedFunctionArgs struct {
Function string
Args []string
Message string
}
func (e ErrUnexpectedFunctionArgs) Error() string {
return fmt.Sprintf("unexpected function args: %s(%s): %s", e.Function, strings.Join(e.Args, ", "), e.Message)
}
func (e ErrUnexpectedFunctionArgs) Is(other error) bool {
o, ok := other.(*ErrUnexpectedFunctionArgs)
if !ok {
return false
}
if o.Function != "" && o.Function != e.Function {
return false
}
if o.Message != "" && o.Message != e.Message {
return false
}
if o.Args != nil && !reflect.DeepEqual(o.Args, e.Args) {
return false
}
return true
}
func standardFunctions() *FunctionCollection {
collection := &FunctionCollection{}
collection.Add(
// Generic
ThisFunc,
LenFunc,
KeyFunc,
KeysFunc,
MergeFunc,
CountFunc,
MapOfFunc,
TypeFunc,
JoinFunc,
StringFunc,
NullFunc,
// Selectors
IndexFunc,
AllFunc,
FirstFunc,
LastFunc,
PropertyFunc,
AppendFunc,
OrDefaultFunc,
// Filters
FilterFunc,
FilterOrFunc,
// Comparisons
EqualFunc,
MoreThanFunc,
LessThanFunc,
AndFunc,
OrFunc,
NotFunc,
// Metadata
MetadataFunc,
ParentFunc,
)
return collection
}
// SelectorFunc is a function that can be executed in a selector.
type SelectorFunc func(c *Context, step *Step, args []string) (Values, error)
type FunctionCollection struct {
functions []Function
}
func (fc *FunctionCollection) ParseSelector(part string) *Selector {
for _, f := range fc.functions {
if s := f.AlternativeSelector(part); s != nil {
return s
}
}
return nil
}
func (fc *FunctionCollection) Add(fs ...Function) {
fc.functions = append(fc.functions, fs...)
}
func (fc *FunctionCollection) GetAll() map[string]SelectorFunc {
res := make(map[string]SelectorFunc)
for _, f := range fc.functions {
res[f.Name()] = f.Run
}
return res
}
func (fc *FunctionCollection) Get(name string) (SelectorFunc, error) {
if f, ok := fc.GetAll()[name]; ok {
return f, nil
}
return nil, &ErrUnknownFunction{Function: name}
}
type Function interface {
Name() string
Run(c *Context, s *Step, args []string) (Values, error)
AlternativeSelector(part string) *Selector
}
type BasicFunction struct {
name string
runFn func(c *Context, s *Step, args []string) (Values, error)
alternativeSelectorFn func(part string) *Selector
}
func (bf BasicFunction) Name() string {
return bf.name
}
func (bf BasicFunction) Run(c *Context, s *Step, args []string) (Values, error) {
return bf.runFn(c, s, args)
}
func (bf BasicFunction) AlternativeSelector(part string) *Selector {
if bf.alternativeSelectorFn == nil {
return nil
}
return bf.alternativeSelectorFn(part)
}
func requireNoArgs(name string, args []string) error {
if len(args) > 0 {
return &ErrUnexpectedFunctionArgs{
Function: name,
Args: args,
Message: "0 arguments expected",
}
}
return nil
}
func requireExactlyXArgs(name string, args []string, x int) error {
if len(args) != x {
return &ErrUnexpectedFunctionArgs{
Function: name,
Args: args,
Message: fmt.Sprintf("exactly %d arguments expected", x),
}
}
return nil
}
func requireXOrMoreArgs(name string, args []string, x int) error {
if len(args) < x {
return &ErrUnexpectedFunctionArgs{
Function: name,
Args: args,
Message: fmt.Sprintf("expected %d or more arguments", x),
}
}
return nil
}
func requireXOrLessArgs(name string, args []string, x int) error {
if len(args) > x {
return &ErrUnexpectedFunctionArgs{
Function: name,
Args: args,
Message: fmt.Sprintf("expected %d or less arguments", x),
}
}
return nil
}
func requireModulusXArgs(name string, args []string, x int) error {
if len(args)%x != 0 {
return &ErrUnexpectedFunctionArgs{
Function: name,
Args: args,
Message: fmt.Sprintf("expected arguments in groups of %d", x),
}
}
return nil
}