aip-160
A parser for Google AIP-160 list filters, with an optional conversion to SeaORM conditions.
The parser follows the AIP-160 EBNF grammar. It is a hand-written recursive-descent parser with no grammar-generator dependency.
Installation
[]
# Parsing only
= "0.2"
# Parsing and conversion to SeaORM 2.0 conditions
= { = "0.2", = ["sea-orm"] }
Parse a filter
use ;
let filter = parse_filter.unwrap;
let Some = &filter.expression else ;
let Restriction = &children else ;
assert_eq!;
assert_eq!;
assert_eq!;
Filter implements Display. The output is the canonical form of the filter, with explicit parentheses.
parse_filter limits the nesting depth to DEFAULT_MAX_DEPTH. Use parse_filter_with_max_depth to set a different limit.
Supported syntax
| Element | Example | Notes |
|---|---|---|
| Comparators | a = 1, a != 1, a < 1, a <= 1, a > 1, a >= 1 |
|
| Has | labels:env, tags:"prod" |
|
AND |
a = 1 AND b = 2 |
Lowest precedence. Case-sensitive. |
| Sequence | a = 1 b = 2 |
Whitespace-separated terms. Binds tighter than AND. |
OR |
a = 1 OR a = 2 |
Highest precedence. Case-sensitive. |
| Negation | NOT a = 1, -a = 1 |
|
| Grouping | (a = 1 OR b = 2) AND c = 3 |
|
| Global restriction | prod |
A comparable with no comparator. |
| Traversal | labels.env = prod |
|
| Function call | regex(name, "^prod") |
Parsed only. The service decides which functions it supports. |
| Unquoted text | ACTIVE, 42, 2.5, true, 20s |
Enum values, numbers, booleans and durations. |
| Quoted string | "abc", 'abc' |
May start or end with the * wildcard. |
Precedence follows AIP-160: OR binds tighter than a sequence, and a sequence binds tighter than AND. So a AND b OR c means a AND (b OR c).
The parser keeps literals as written. Value has helpers that read a literal as a boolean, a number, a duration or a wildcard match.
Convert to a SeaORM condition
Enable the sea-orm feature. The conversion supports SeaORM 2.0.
use ;
let filter = parse_filter?;
let condition = filter.?;
find.filter.all.await?;
The conversion supports restrictions on top-level columns, joined with AND, OR, NOT and sequences. A field name matches a column directly, or after a conversion from snake_case to PascalCase. Each literal is read with the type of its column:
| Column type | Accepted literal |
|---|---|
| boolean | true, false |
| signed or unsigned integer | 42, -7 |
| float, double, decimal, money | 2.5, 1e3 |
| enum | ACTIVE or "ACTIVE", cast to the database enum type |
| uuid, timestamp, date, time | a quoted string, cast to the column type |
| other types | text or a quoted string |
field = null and field != null become IS NULL and IS NOT NULL. On string columns, field = "abc*", "*abc" and "*abc*" become LIKE patterns, and field:"abc" matches a substring.
Traversal, function calls and global restrictions return FilterError::Unsupported. An unknown field returns FilterError::InvalidField. A literal that does not match the column type returns FilterError::InvalidValue. Map these errors to INVALID_ARGUMENT, as AIP-160 requires.
Changes from 0.1
ORnow binds tighter thanAND, as AIP-160 requires. In 0.1,a AND b OR cmeant(a AND b) OR c.- Unquoted text values, traversal, function calls, global restrictions,
-negation, sequences and*wildcards are new. - The AST is new. See the
astmodule. - The
sea-ormfeature uses SeaORM 2.0 instead of 1.1. - The
pestdependency is removed.
License
MIT or Apache-2.0.