-
Notifications
You must be signed in to change notification settings - Fork 30
/
filters.go
140 lines (125 loc) · 4.35 KB
/
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
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
// SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
//
// SPDX-License-Identifier: Apache-2.0
package topo
import (
"strings"
topoapi "github.com/onosproject/onos-api/go/onos/topo"
"github.com/spf13/cobra"
)
func compileFilters(cmd *cobra.Command, objectType topoapi.Object_Type) *topoapi.Filters {
aspects, _ := cmd.Flags().GetStringSlice("with-aspect")
filters := &topoapi.Filters{WithAspects: aspects}
lq, _ := cmd.Flags().GetString("label")
filters.LabelFilters = compileLabelFilters(lq)
filters.ObjectTypes = []topoapi.Object_Type{objectType}
if objectType == topoapi.Object_ENTITY || objectType == topoapi.Object_RELATION {
kq, _ := cmd.Flags().GetString("kind")
filters.KindFilter = compileKindFilter(kq)
}
return filters
}
func compileLabelFilters(query string) []*topoapi.Filter {
filters := make([]*topoapi.Filter, 0)
fields := strings.Split(query, " && ")
for _, field := range fields {
filter, _ := compileLabelFilter(strings.TrimSpace(field))
if filter != nil {
filters = append(filters, filter)
}
}
return filters
}
func compileLabelFilter(field string) (*topoapi.Filter, error) {
if strings.Contains(field, " !in (") {
key := extractKey(field, " !in (")
values := extractValues(field)
return &topoapi.Filter{
Filter: &topoapi.Filter_Not{Not: &topoapi.NotFilter{
Inner: &topoapi.Filter{Filter: &topoapi.Filter_In{In: &topoapi.InFilter{Values: values}}}},
},
Key: key,
}, nil
} else if strings.Contains(field, " in (") {
key := extractKey(field, " in (")
values := extractValues(field)
return &topoapi.Filter{
Filter: &topoapi.Filter_In{In: &topoapi.InFilter{Values: values}},
Key: key,
}, nil
} else if strings.Contains(field, "!=") {
key := extractKey(field, "!=")
value := extractValue(field)
return &topoapi.Filter{
Filter: &topoapi.Filter_Not{Not: &topoapi.NotFilter{
Inner: &topoapi.Filter{Filter: &topoapi.Filter_Equal_{Equal_: &topoapi.EqualFilter{Value: value}}}},
},
Key: key,
}, nil
} else if strings.Contains(field, "=") {
key := extractKey(field, "=")
value := extractValue(field)
return &topoapi.Filter{
Filter: &topoapi.Filter_Equal_{Equal_: &topoapi.EqualFilter{Value: value}},
Key: key,
}, nil
}
return nil, nil
}
func extractKey(field string, sep string) string {
return strings.TrimSpace(strings.Split(field, sep)[0])
}
func extractValue(field string) string {
return strings.TrimSpace(strings.Split(field, "=")[1])
}
func extractValues(field string) []string {
gs := strings.Split(strings.Split(strings.Split(field, "(")[1], ")")[0], ",")
values := make([]string, 0, len(gs))
for _, v := range gs {
values = append(values, strings.TrimSpace(v))
}
return values
}
func compileKindFilter(query string) *topoapi.Filter {
if len(query) == 0 {
return nil
}
// parse queries of form "!in (a, b, c)"
// do this before the positive form of inclusion b/c containing !in ( is more restrictive and precise
if strings.Contains(query, "!in (") {
values := extractValues(query)
return &topoapi.Filter{
Filter: &topoapi.Filter_Not{Not: &topoapi.NotFilter{
Inner: &topoapi.Filter{Filter: &topoapi.Filter_In{In: &topoapi.InFilter{Values: values}}}},
},
}
// parse queries of form "in ( a,b,c )"
} else if strings.Contains(query, "in (") {
values := extractValues(query)
return &topoapi.Filter{
Filter: &topoapi.Filter_In{In: &topoapi.InFilter{Values: values}},
}
// parse queries of form "!= a", again do more restrictive Contains check first
} else if strings.Contains(query, "!=") {
value := extractValue(query)
return &topoapi.Filter{
Filter: &topoapi.Filter_Not{Not: &topoapi.NotFilter{
Inner: &topoapi.Filter{Filter: &topoapi.Filter_Equal_{Equal_: &topoapi.EqualFilter{Value: value}}}},
},
}
// parse queries of form "= a"
} else if strings.Contains(query, "=") {
value := extractValue(query)
return &topoapi.Filter{
Filter: &topoapi.Filter_Equal_{Equal_: &topoapi.EqualFilter{Value: value}},
}
// parse queries of the form "a" or "a, b, c"
// shortcut for "in(a)" (which is equivalent to "=a") or "in(a, b, c)"
} else if !strings.Contains(query, "(") && !strings.Contains(query, ")") && !strings.Contains(query, "!") {
values := extractValues(" (" + query + ") ")
return &topoapi.Filter{
Filter: &topoapi.Filter_In{In: &topoapi.InFilter{Values: values}},
}
}
return nil
}