-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs_filters.go
65 lines (57 loc) · 1.34 KB
/
args_filters.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
package jqtop
import (
"fmt"
"github.com/shadyabhi/jqtop/argparser"
)
// filter is a type that represens a single filter
type filter struct {
Negate bool
Function string
Pos int
// We can have n number of Args, should be generic
Args []string
}
// parseFilters parses filters into []filter by reading
// the string that represent filters
func parseFilters(s string) ([]filter, error) {
if s == "" {
return []filter{}, nil
}
filtersAST, err := argparser.ParseFilters(s)
if err != nil {
return []filter{}, fmt.Errorf("Error parsing filter, error: %s", err)
}
filters := []filter{}
validFunctions := getFieldValues(filterFunctions)
for _, f := range filtersAST.Filters {
if !sliceContains(validFunctions, *f.Name) {
return []filter{}, fmt.Errorf("Error parsing filter, invalid action: %s", *f.Name)
}
var negate bool
// For now, we only worry about "!"
// hence named negate
if f.Unaryop == nil {
negate = false
} else {
switch op := *f.Unaryop; op {
case "!":
negate = true
}
}
args := []string{}
for i := range f.Args {
if i == 0 {
args = append(args, *f.Args[i].Variable)
} else {
args = append(args, *f.Args[i].String)
}
}
eachFilter := filter{
Negate: negate,
Function: *f.Name,
Args: args,
}
filters = append(filters, eachFilter)
}
return filters, nil
}