|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Annotated |
| 4 | + |
| 5 | +import pydantic |
| 6 | + |
| 7 | +NonEmptyStr = Annotated[str, pydantic.StringConstraints(min_length=1)] |
| 8 | + |
| 9 | + |
| 10 | +class _BaseModel(pydantic.BaseModel): |
| 11 | + model_config = {"extra": "forbid"} |
| 12 | + |
| 13 | + |
| 14 | +# --- Leaf argument models --- |
| 15 | + |
| 16 | + |
| 17 | +class KeyExists(_BaseModel): |
| 18 | + key: NonEmptyStr |
| 19 | + |
| 20 | + |
| 21 | +class ValueContains(_BaseModel): |
| 22 | + key: NonEmptyStr |
| 23 | + value_substring: NonEmptyStr |
| 24 | + |
| 25 | + |
| 26 | +class ValueIn(_BaseModel): |
| 27 | + key: NonEmptyStr |
| 28 | + values: list[NonEmptyStr] = pydantic.Field(min_length=1) |
| 29 | + |
| 30 | + |
| 31 | +class ValueEquals(_BaseModel): |
| 32 | + key: NonEmptyStr |
| 33 | + value: NonEmptyStr |
| 34 | + |
| 35 | + |
| 36 | +class TimeRange(_BaseModel): |
| 37 | + """AwareDatetime requires timezone info (e.g. "2024-01-01T00:00:00Z"). |
| 38 | + Naive datetimes like "2024-01-01T00:00:00" are rejected, preventing |
| 39 | + ambiguous timestamps that could silently resolve to the wrong timezone.""" |
| 40 | + |
| 41 | + key: NonEmptyStr |
| 42 | + start_time: pydantic.AwareDatetime |
| 43 | + end_time: pydantic.AwareDatetime | None = None |
| 44 | + |
| 45 | + |
| 46 | +# --- Predicate wrapper models (one field each) --- |
| 47 | + |
| 48 | + |
| 49 | +class KeyExistsPredicate(_BaseModel): |
| 50 | + key_exists: KeyExists |
| 51 | + |
| 52 | + |
| 53 | +class ValueContainsPredicate(_BaseModel): |
| 54 | + value_contains: ValueContains |
| 55 | + |
| 56 | + |
| 57 | +class ValueInPredicate(_BaseModel): |
| 58 | + value_in: ValueIn |
| 59 | + |
| 60 | + |
| 61 | +class ValueEqualsPredicate(_BaseModel): |
| 62 | + value_equals: ValueEquals |
| 63 | + |
| 64 | + |
| 65 | +class TimeRangePredicate(_BaseModel): |
| 66 | + time_range: TimeRange |
| 67 | + |
| 68 | + |
| 69 | +LeafPredicate = ( |
| 70 | + KeyExistsPredicate |
| 71 | + | ValueContainsPredicate |
| 72 | + | ValueInPredicate |
| 73 | + | ValueEqualsPredicate |
| 74 | + | TimeRangePredicate |
| 75 | +) |
| 76 | + |
| 77 | + |
| 78 | +class NotPredicate(_BaseModel): |
| 79 | + not_: LeafPredicate = pydantic.Field(alias="not") |
| 80 | + |
| 81 | + |
| 82 | +class AndPredicate(_BaseModel): |
| 83 | + and_: list["Predicate"] = pydantic.Field(alias="and", min_length=1) |
| 84 | + |
| 85 | + |
| 86 | +class OrPredicate(_BaseModel): |
| 87 | + or_: list["Predicate"] = pydantic.Field(alias="or", min_length=1) |
| 88 | + |
| 89 | + |
| 90 | +Predicate = ( |
| 91 | + KeyExistsPredicate |
| 92 | + | ValueContainsPredicate |
| 93 | + | ValueInPredicate |
| 94 | + | ValueEqualsPredicate |
| 95 | + | TimeRangePredicate |
| 96 | + | NotPredicate |
| 97 | + | AndPredicate |
| 98 | + | OrPredicate |
| 99 | +) |
| 100 | + |
| 101 | +# Resolve forward reference to "Predicate" in recursive and/or models |
| 102 | +AndPredicate.model_rebuild() |
| 103 | +OrPredicate.model_rebuild() |
| 104 | + |
| 105 | + |
| 106 | +class FilterQuery(_BaseModel): |
| 107 | + """Root: must be exactly one of {"and": [...]} or {"or": [...]}.""" |
| 108 | + |
| 109 | + and_: list[Predicate] | None = pydantic.Field(None, alias="and", min_length=1) |
| 110 | + or_: list[Predicate] | None = pydantic.Field(None, alias="or", min_length=1) |
| 111 | + |
| 112 | + @pydantic.model_validator(mode="after") |
| 113 | + def _exactly_one_root_operator(self) -> FilterQuery: |
| 114 | + has_and = self.and_ is not None |
| 115 | + has_or = self.or_ is not None |
| 116 | + if has_and == has_or: |
| 117 | + raise ValueError("FilterQuery root must have exactly one of 'and' or 'or'.") |
| 118 | + return self |
0 commit comments