Debian Codegraph @

pubsub/v2/pstest/filtering.go

raw
1 // Copyright 2025 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package pstest
16
17 import (
18 "go.einride.tech/aip/filtering"
19 "go.einride.tech/aip/filtering/exprs"
20 expr "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
21 )
22
23 const (
24 attributesStr = "attributes"
25 hasPrefixStr = "hasPrefix"
26 )
27
28 // ValidateFilter validates if the filter string is parsable.
29 func ValidateFilter(filter string) error {
30 _, err := parseFilter(filter)
31 return err
32 }
33
34 // parseFilter validates a filter string and returns a Filter.
35 func parseFilter(filter string) (filtering.Filter, error) {
36 request := request{filter}
37
38 // Declare the functions and identifiers that are allowed in the filter.
39 declarations, err := filtering.NewDeclarations(
40 filtering.DeclareFunction(
41 hasPrefixStr,
42 filtering.NewFunctionOverload(hasPrefixStr,
43 filtering.TypeBool,
44 filtering.TypeString,
45 filtering.TypeString,
46 ),
47 ),
48 filtering.DeclareIdent(
49 attributesStr,
50 filtering.TypeMap(
51 filtering.TypeString,
52 filtering.TypeString,
53 ),
54 ),
55 filtering.DeclareStandardFunctions(),
56 )
57 if err != nil {
58 return filtering.Filter{}, err
59 }
60 return filtering.ParseFilter(request, declarations)
61 }
62
63 // request implements filtering.Request.
64 type request struct {
65 filter string
66 }
67
68 func (r request) GetFilter() string {
69 return r.filter
70 }
71
72 type messageAttrs map[string]string
73
74 // hasKey returns true if the attribute key exists.
75 func (a messageAttrs) hasKey(key string) bool {
76 _, ok := a[key]
77 return ok
78 }
79
80 // hasExpectedValue returns true if the attribute key exists and has the given value.
81 func (a messageAttrs) hasExpectedValue(key, value string) bool {
82 v, ok := a[key]
83 return ok && v == value
84 }
85
86 // hasPrefix returns true if the attribute key exists and has the given prefix.
87 func (a messageAttrs) hasPrefix(key, prefix string) bool {
88 v, ok := a[key]
89 if !ok {
90 return false
91 }
92 return len(v) >= len(prefix) && v[:len(prefix)] == prefix
93 }
94
95 // atomicMatch matches expressions that are not dependent on child expressions.
96 func atomicMatch(attributes messageAttrs, currExpr *expr.Expr) bool {
97 var key, value string
98 // Match "=" function.
99 // Example: `attributes.name = "com"`
100 matcher := exprs.MatchFunction(
101 filtering.FunctionEquals,
102 exprs.MatchAnyMember(exprs.MatchText(attributesStr), &key),
103 exprs.MatchAnyString(&value),
104 )
105 if matcher(currExpr) {
106 return attributes.hasExpectedValue(key, value)
107 }
108
109 // Match "!=" function.
110 // Example: `attributes.name != "com"`
111 matcher = exprs.MatchFunction(
112 filtering.FunctionNotEquals,
113 exprs.MatchAnyMember(exprs.MatchText(attributesStr), &key),
114 exprs.MatchAnyString(&value),
115 )
116 if matcher(currExpr) {
117 return !attributes.hasExpectedValue(key, value)
118 }
119
120 // Match ":" function.
121 // Example: `attributes:name`
122 matcher = exprs.MatchFunction(
123 filtering.FunctionHas,
124 exprs.MatchText(attributesStr),
125 exprs.MatchAnyString(&key),
126 )
127 if matcher(currExpr) {
128 return attributes.hasKey(key)
129 }
130
131 // Match "hasPrefix" function.
132 // Example: `hasPrefix(attributes.name, "co")`
133 matcher = exprs.MatchFunction(
134 "hasPrefix",
135 exprs.MatchAnyMember(exprs.MatchText(attributesStr), &key),
136 exprs.MatchAnyString(&value),
137 )
138 if matcher(currExpr) {
139 return attributes.hasPrefix(key, value)
140 }
141
142 return true
143 }
144
145 // compositeMatch matches expressions that are dependent on child expressions.
146 func compositeMatch(attributes messageAttrs, currExpr *expr.Expr) bool {
147 // Match "NOT" function.
148 // Example: `NOT attributes:name`
149 var e1, e2 *expr.Expr
150 matcher := exprs.MatchFunction(
151 filtering.FunctionNot,
152 exprs.MatchAny(&e1),
153 )
154 if matcher(currExpr) {
155 return !match(attributes, e1)
156 }
157
158 // Match "AND" function.
159 // Example: `attributes:lang = "en" AND attributes:name`
160 matcher = exprs.MatchFunction(
161 filtering.FunctionAnd,
162 exprs.MatchAny(&e1),
163 exprs.MatchAny(&e2),
164 )
165 if matcher(currExpr) {
166 return match(attributes, e1) && match(attributes, e2)
167 }
168
169 // Match "OR" function.
170 // Example: `attributes:lang = "en" OR attributes:name`
171 matcher = exprs.MatchFunction(
172 filtering.FunctionOr,
173 exprs.MatchAny(&e1),
174 exprs.MatchAny(&e2),
175 )
176 if matcher(currExpr) {
177 return match(attributes, e1) || match(attributes, e2)
178 }
179
180 return true
181 }
182
183 func match(attributes messageAttrs, currExpr *expr.Expr) bool {
184 // atomicMatch first to avoid deep recursion.
185 return atomicMatch(attributes, currExpr) && compositeMatch(attributes, currExpr)
186 }
187
188 // getAttrsFunc is a function that returns attributes from an item with any type.
189 type getAttrsFunc[T any] func(T) messageAttrs
190
191 // Make it generic so it's easy to be tested.
192 //
193 // Accept a map as input to efficiently delete unmatched items.
194 func filterByAttrs[T map[K]U, U any, K comparable](items T, filter *filtering.Filter, getAttrs getAttrsFunc[U]) {
195 for key, item := range items {
196 walkFn := func(currExpr, parentExpr *expr.Expr) bool {
197 _, ok := currExpr.ExprKind.(*expr.Expr_CallExpr) // only match call expressions
198 if !ok {
199 return true
200 }
201 attrs := getAttrs(item)
202 result := match(attrs, currExpr)
203 if !result && parentExpr == nil {
204 delete(items, key)
205 }
206 return result
207 }
208 filtering.Walk(walkFn, filter.CheckedExpr.Expr)
209 }
210 }