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)
| 87 | // i. We only support && currently. |
| 88 | // ii. defined as `key comparator "val" && key comparator "val" && ...`. |
| 89 | func 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 | |
| 122 | func parseExpression(expr string) (Expression, error) { |
| 123 | // Split the expression by " " to get the key, comparator and val. |