|
| 1 | +package labelfilter |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "slices" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +// NormalizeLabels converts a slice of name/value label pairs into a map keyed |
| 10 | +// by lowercased label name, with each value being a slice of lowercased values. |
| 11 | +// This supports labels where a single name can have multiple values (e.g. multiple |
| 12 | +// "_policy" labels on the same evidence). |
| 13 | +func NormalizeLabels(labels []struct{ Name, Value string }) map[string][]string { |
| 14 | + result := make(map[string][]string, len(labels)) |
| 15 | + for _, l := range labels { |
| 16 | + key := strings.ToLower(strings.TrimSpace(l.Name)) |
| 17 | + val := strings.ToLower(strings.TrimSpace(l.Value)) |
| 18 | + if key == "" { |
| 19 | + continue |
| 20 | + } |
| 21 | + result[key] = append(result[key], val) |
| 22 | + } |
| 23 | + return result |
| 24 | +} |
| 25 | + |
| 26 | +// MatchLabels evaluates a filter Scope against a normalized label map. |
| 27 | +// A nil scope matches everything (empty filter = match all). |
| 28 | +// Semantics mirror the SQL evaluator in evidence.go (case-insensitive via pre-normalized map). |
| 29 | +// Returns an error if an unknown query operator is encountered. |
| 30 | +func MatchLabels(scope *Scope, labels map[string][]string) (bool, error) { |
| 31 | + if scope == nil { |
| 32 | + return true, nil |
| 33 | + } |
| 34 | + return matchScope(*scope, labels) |
| 35 | +} |
| 36 | + |
| 37 | +func matchScope(scope Scope, labels map[string][]string) (bool, error) { |
| 38 | + if scope.IsCondition() { |
| 39 | + return matchCondition(*scope.Condition, labels), nil |
| 40 | + } |
| 41 | + if scope.IsQuery() { |
| 42 | + return matchQuery(*scope.Query, labels) |
| 43 | + } |
| 44 | + // Empty scope (neither condition nor query) matches everything. |
| 45 | + return true, nil |
| 46 | +} |
| 47 | + |
| 48 | +func matchCondition(cond Condition, labels map[string][]string) bool { |
| 49 | + key := strings.ToLower(strings.TrimSpace(cond.Label)) |
| 50 | + val := strings.ToLower(strings.TrimSpace(cond.Value)) |
| 51 | + |
| 52 | + values, exists := labels[key] |
| 53 | + |
| 54 | + switch cond.Operator { |
| 55 | + case "!=": |
| 56 | + // Not-equals: true if the label doesn't exist or none of its values match. |
| 57 | + if !exists { |
| 58 | + return true |
| 59 | + } |
| 60 | + if slices.Contains(values, val) { |
| 61 | + return false |
| 62 | + } |
| 63 | + return true |
| 64 | + default: |
| 65 | + // Equals (default): true if any value for this label matches. |
| 66 | + if !exists { |
| 67 | + return false |
| 68 | + } |
| 69 | + return slices.Contains(values, val) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func matchQuery(query Query, labels map[string][]string) (bool, error) { |
| 74 | + op := strings.ToLower(query.Operator) |
| 75 | + switch op { |
| 76 | + case "and": |
| 77 | + for _, scope := range query.Scopes { |
| 78 | + match, err := matchScope(scope, labels) |
| 79 | + if err != nil { |
| 80 | + return false, err |
| 81 | + } |
| 82 | + if !match { |
| 83 | + return false, nil |
| 84 | + } |
| 85 | + } |
| 86 | + return true, nil |
| 87 | + case "or": |
| 88 | + for _, scope := range query.Scopes { |
| 89 | + match, err := matchScope(scope, labels) |
| 90 | + if err != nil { |
| 91 | + return false, err |
| 92 | + } |
| 93 | + if match { |
| 94 | + return true, nil |
| 95 | + } |
| 96 | + } |
| 97 | + return len(query.Scopes) == 0, nil |
| 98 | + default: |
| 99 | + return false, fmt.Errorf("unrecognised query operator: %s", query.Operator) |
| 100 | + } |
| 101 | +} |
0 commit comments