MCPcopy Create free account
hub / github.com/bytebase/bytebase / ParseFilter

Function ParseFilter

backend/api/v1/common.go:89–120  ·  view source on GitHub ↗

ParseFilter will parse the simple filter. TODO(rebelice): support more complex filter. Currently we support the following syntax: 1. for single expression: i. defined as `key comparator "val"`. ii. Comparator can be `=`, `!=`, `>`, `>=`, `<`, `<=`. iii. If val doesn't contain space, we can omit t

(filter string)

Source from the content-addressed store, hash-verified

87// i. We only support && currently.
88// ii. defined as `key comparator "val" && key comparator "val" && ...`.
89func ParseFilter(filter string) ([]Expression, error) {
90 if filter == "" {
91 return nil, nil
92 }
93
94 normalized, quotedString, err := normalizeFilter(filter)
95 if err != nil {
96 return nil, err
97 }
98
99 var result []Expression
100 nextStringPos := 0
101
102 // Split the normalized filter by " && " to get the list of expressions.
103 expressions := strings.Split(normalized, " && ")
104 for _, expressionString := range expressions {
105 expr, err := parseExpression(expressionString)
106 if err != nil {
107 return nil, err
108 }
109 if expr.Value == "?" {
110 if nextStringPos >= len(quotedString) {
111 return nil, errors.Errorf("invalid filter %q", filter)
112 }
113 expr.Value = quotedString[nextStringPos]
114 nextStringPos++
115 }
116 result = append(result, expr)
117 }
118
119 return result, nil
120}
121
122func parseExpression(expr string) (Expression, error) {
123 // Split the expression by " " to get the key, comparator and val.

Callers 1

TestParseFilterFunction · 0.85

Calls 3

normalizeFilterFunction · 0.85
parseExpressionFunction · 0.85
ErrorfMethod · 0.80

Tested by 1

TestParseFilterFunction · 0.68