Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 5 additions & 136 deletions packages/codec-url/src/simple/encoder/visitors/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@
* view the LICENSE file that was distributed with this source code.
*/

import {
URLFilterOperator,
parseFilterWireValue,
serializeFilterValue,
} from '@rapiq/parser-simple';
import { encodeFilterWireValue } from '@rapiq/parser-simple';
import type { IFilterVisitor, IFiltersVisitor } from '@rapiq/core';
import {
AdapterError,
Filter,
FilterCompoundOperator,
FilterFieldOperator,
Filters,
} from '@rapiq/core';

Expand Down Expand Up @@ -64,137 +59,11 @@ IFilterVisitor<RecordSerializer> {
throw AdapterError.featureUnsupported('filters:field:duplicate');
}

this.serializer.set(expr.field, this.serializeCondition(expr));
// the wire grammar (marker spelling, raw LIKE text, collision
// verification) is owned by @rapiq/parser-simple — a Filter
// leaf structurally satisfies the codec's condition input.
this.serializer.set(expr.field, encodeFilterWireValue(expr));

return this.serializer;
}

protected serializeCondition(expr: Filter) : string {
switch (expr.operator) {
case FilterFieldOperator.EQUAL:
case FilterFieldOperator.IN: {
return this.verifyWireValue(expr, serializeFilterValue(expr.value));
}
case FilterFieldOperator.NOT_EQUAL:
case FilterFieldOperator.NOT_IN: {
return this.verifyWireValue(
expr,
URLFilterOperator.NEGATION + serializeFilterValue(expr.value),
);
}
case FilterFieldOperator.LESS_THAN: {
return this.verifyWireValue(
expr,
URLFilterOperator.LESS_THAN + serializeFilterValue(expr.value),
);
}
case FilterFieldOperator.LESS_THAN_EQUAL: {
return this.verifyWireValue(
expr,
URLFilterOperator.LESS_THAN_EQUAL + serializeFilterValue(expr.value),
);
}
case FilterFieldOperator.GREATER_THAN: {
return this.verifyWireValue(
expr,
URLFilterOperator.GREATER_THAN + serializeFilterValue(expr.value),
);
}
case FilterFieldOperator.GREATER_THAN_EQUAL: {
return this.verifyWireValue(
expr,
URLFilterOperator.GREATER_THAN_EQUAL + serializeFilterValue(expr.value),
);
}
case FilterFieldOperator.CONTAINS: {
return this.verifyWireValue(
expr,
URLFilterOperator.LIKE + this.serializeLikeText(expr) + URLFilterOperator.LIKE,
);
}
case FilterFieldOperator.NOT_CONTAINS: {
return this.verifyWireValue(
expr,
URLFilterOperator.NEGATION + URLFilterOperator.LIKE +
this.serializeLikeText(expr) + URLFilterOperator.LIKE,
);
}
case FilterFieldOperator.STARTS_WITH: {
return this.verifyWireValue(
expr,
this.serializeLikeText(expr) + URLFilterOperator.LIKE,
);
}
case FilterFieldOperator.NOT_STARTS_WITH: {
return this.verifyWireValue(
expr,
URLFilterOperator.NEGATION + this.serializeLikeText(expr) + URLFilterOperator.LIKE,
);
}
case FilterFieldOperator.ENDS_WITH: {
return this.verifyWireValue(
expr,
URLFilterOperator.LIKE + this.serializeLikeText(expr),
);
}
case FilterFieldOperator.NOT_ENDS_WITH: {
return this.verifyWireValue(
expr,
URLFilterOperator.NEGATION + URLFilterOperator.LIKE + this.serializeLikeText(expr),
);
}
default: {
// REGEX, MOD, EXISTS, ELEM_MATCH, ... have no simple-dialect
// wire syntax and would decode as plain equality.
throw AdapterError.operatorUnsupported(expr.operator);
}
}
}

/**
* LIKE inner text travels raw (the decoder applies no scalar
* coercion and no comma-splitting to it) — strings pass through
* verbatim, everything else uses the scalar wire form.
*/
protected serializeLikeText(expr: Filter) : string {
if (typeof expr.value === 'string') {
if (expr.value.length === 0) {
// decodes to an empty match text, which the parser drops.
throw AdapterError.featureUnsupported('filters:value:empty');
}

return expr.value;
}

return serializeFilterValue(expr.value);
}

/**
* Subset law, enforced pointwise: the wire token must decode back
* to the operator it was serialized from (values may still change
* scalar type — that normalization is part of the wire contract).
* The simple dialect has no escaping, so e.g. an EQUAL on the
* string 'foo~' would silently decode as STARTS_WITH 'foo'.
*/
protected verifyWireValue(expr: Filter, wire: string) : string {
const reparsed = parseFilterWireValue(wire);

let matches = reparsed.operator === expr.operator;

if (!matches && expr.operator === FilterFieldOperator.IN) {
// scalar lists decode as EQUAL and are lifted to IN by the
// parser when the value is (or splits into) an array.
matches = reparsed.operator === FilterFieldOperator.EQUAL;
}

if (!matches && expr.operator === FilterFieldOperator.NOT_IN) {
matches = reparsed.operator === FilterFieldOperator.NOT_EQUAL;
}

if (!matches) {
throw AdapterError.featureUnsupported(`filters:value:${expr.field}`);
}

return wire;
}
}
15 changes: 0 additions & 15 deletions packages/parser-simple/src/parameter/filters/constants.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/parser-simple/src/parameter/filters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* view the LICENSE file that was distributed with this source code.
*/

export * from './constants';
export * from './module';
export * from './types';
export * from './value';
export * from './wire';
37 changes: 8 additions & 29 deletions packages/parser-simple/src/parameter/filters/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import type {
} from '@rapiq/core';

import type { SimpleFiltersParserInput } from './types';
import { parseFilterWireValue } from './value';
import { decodeFilterWireValue } from './wire';

export class SimpleFiltersParser extends BaseParser<
FiltersParseOptions,
Expand Down Expand Up @@ -181,34 +181,24 @@ export class SimpleFiltersParser extends BaseParser<

const resolvedName = [...resolved.path, resolved.name].join('.');

const valueParsed = this.parseValue(data.attributes[key_]);
if (!valueParsed) {
// the wire grammar owns value decoding, including the
// empty-value verdict — the parser only applies the
// schema drop-vs-throw policy.
const decoded = decodeFilterWireValue(data.attributes[key_]);
if (!decoded.success) {
if (scope.throwOnFailure) {
throw FiltersParseError.keyValueInvalid(resolvedName);
}

continue;
}

if (!Array.isArray(valueParsed.value)) {
if (
typeof valueParsed.value === 'string' &&
valueParsed.value.length === 0
) {
if (scope.throwOnFailure) {
throw FiltersParseError.keyValueInvalid(resolvedName);
}

continue;
}
}

const filter = new Filter(
valueParsed.operator,
decoded.condition.operator,
currentKey === DEFAULT_ID ?
resolvedName :
stringifyKey({ path: currentKey, name: resolvedName }),
valueParsed.value,
decoded.condition.value,
);

output.push(filter);
Expand Down Expand Up @@ -245,15 +235,4 @@ export class SimpleFiltersParser extends BaseParser<

return output;
}

protected parseValue(input: unknown) : {
value: unknown,
operator: `${FilterFieldOperator}`
} | undefined {
try {
return parseFilterWireValue(input);
} catch {
return undefined;
}
}
}
21 changes: 8 additions & 13 deletions packages/parser-simple/src/parameter/filters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,18 @@
*/

import type {
ArrayItem,
IsArray,
IsScalar,
NestedKeys,
PrevIndex,
ArrayItem,
IsArray,
IsScalar,
NestedKeys,
PrevIndex,
TypeFromNestedKeyPath,
} from '@rapiq/core';

type ValueWithOperator<V> = V extends string | number ?
V | `!${V}` | `!~${V}` | `~${V}` | `<${V}` | `<=${V}` | `>${V}` | `>=${V}` | null | '!null' :
V extends boolean ?
V | null | '!null' :
never;
import type { FilterWireValueInput } from './wire';

type Value<V> = V extends Array<infer Item> ?
ValueWithOperator<Item> | Array<ValueWithOperator<Item>> :
ValueWithOperator<V> | Array<ValueWithOperator<V>>;
FilterWireValueInput<Item> | Array<FilterWireValueInput<Item>> :
FilterWireValueInput<V> | Array<FilterWireValueInput<V>>;

export type SimpleFiltersParserInputValue<
T,
Expand Down
Loading
Loading