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
12 changes: 4 additions & 8 deletions js/httpql/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ type Test =
const readFile = (path: string) => {
return fs.readFileSync(
join(fileURLToPath(import.meta.url), "../../../../tests/httpql", path),
"utf-8"
"utf-8",
);
}
};

describe("httpql", () => {
describe("ast", () => {
Expand Down Expand Up @@ -57,9 +57,7 @@ describe("httpql", () => {
for (const c of cases) {
it(`Case ${c}`, () => {
const input = readFile(`regex/${c}/input.httpql`);
const test: Test = JSON.parse(
readFile(`regex/${c}/test.json`).trim(),
);
const test: Test = JSON.parse(readFile(`regex/${c}/test.json`).trim());

const query = deserialize(input);
if (test.expect === "err") {
Expand All @@ -78,9 +76,7 @@ describe("httpql", () => {
for (const c of cases) {
it(`Case ${c}`, () => {
const input = readFile(`string/${c}/input.httpql`);
const test: Test = JSON.parse(
readFile(`string/${c}/test.json`).trim(),
);
const test: Test = JSON.parse(readFile(`string/${c}/test.json`).trim());

const query = deserialize(input);
if (test.expect === "err") {
Expand Down
32 changes: 32 additions & 0 deletions js/streamql/src/deserialize/clause.preset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { SyntaxNode } from "@lezer/common";
import { err, ok, type Result } from "neverthrow";

import { InvalidQuery, type StreamQLError } from "../errors.js";
import { terms } from "../parser/index.js";
import type { ExprPreset } from "../primitives.js";
import { getChildString, isPresent } from "../utils.js";

import { deserializeString } from "./string.js";

export const deserializePresetClause = (
node: SyntaxNode,
doc: string,
): Result<ExprPreset, StreamQLError> => {
const nameNode = node.getChild(terms.PresetNameExpression);
if (isPresent(nameNode)) {
return deserializeString(nameNode, doc).map(({ value }) => {
return {
name: value,
};
});
}

const aliasStr = getChildString(node, terms.PresetAliasExpression, doc);
if (isPresent(aliasStr)) {
return ok({
alias: aliasStr,
});
}

return err(new InvalidQuery());
};
8 changes: 8 additions & 0 deletions js/streamql/src/deserialize/clause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ import { terms } from "../parser/index.js";
import { type Query } from "../primitives.js";
import { isPresent } from "../utils.js";

import { deserializePresetClause } from "./clause.preset.js";
import { deserializeStreamClause } from "./clause.stream.js";
import { deserializeWsClause } from "./clause.websocket.js";

export const deserializeClause = (
node: SyntaxNode,
doc: string,
): Result<Query, StreamQLError> => {
const presetClause = node.getChild(terms.PresetClause);
if (isPresent(presetClause)) {
return deserializePresetClause(presetClause, doc).map((preset) => ({
preset,
}));
}

const streamClause = node.getChild(terms.StreamClause);
if (isPresent(streamClause)) {
return deserializeStreamClause(streamClause, doc).map((stream) => ({
Expand Down
23 changes: 23 additions & 0 deletions js/streamql/src/deserialize/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,27 @@ describe("deserialize", () => {
],
});
});
it("should parse StreamQL preset alias expression", () => {
const query = "preset:my-preset";

const filter = deserialize(query)._unsafeUnwrap();

expect(filter).to.deep.equal({
preset: {
alias: "my-preset",
},
});
});

it("should parse StreamQL preset name expression", () => {
const query = 'preset:"My preset"';

const filter = deserialize(query)._unsafeUnwrap();

expect(filter).to.deep.equal({
preset: {
name: "My preset",
},
});
});
});
12 changes: 4 additions & 8 deletions js/streamql/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const readFile = (path: string) => {
console.log(fileURLToPath(import.meta.url));
return fs.readFileSync(
join(fileURLToPath(import.meta.url), "../../../../tests/streamql", path),
"utf-8"
"utf-8",
);
}
};

describe("streamql", () => {
describe("ast", () => {
Expand Down Expand Up @@ -56,9 +56,7 @@ describe("streamql", () => {
for (const c of cases) {
it(`Case ${c}`, () => {
const input = readFile(`regex/${c}/input.streamql`);
const test: Test = JSON.parse(
readFile(`regex/${c}/test.json`).trim(),
);
const test: Test = JSON.parse(readFile(`regex/${c}/test.json`).trim());

const query = deserialize(input);
if (test.expect === "err") {
Expand All @@ -77,9 +75,7 @@ describe("streamql", () => {
for (const c of cases) {
it(`Case ${c}`, () => {
const input = readFile(`string/${c}/input.streamql`);
const test: Test = JSON.parse(
readFile(`string/${c}/test.json`).trim(),
);
const test: Test = JSON.parse(readFile(`string/${c}/test.json`).trim());

const query = deserialize(input);
if (test.expect === "err") {
Expand Down
3 changes: 3 additions & 0 deletions js/streamql/src/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type Maybe<T> = T | undefined | null;
export type Query = {
AND?: Maybe<[Query, Query]>;
OR?: Maybe<[Query, Query]>;
preset?: Maybe<ExprPreset>;
websocket?: Maybe<ClauseWs>;
stream?: Maybe<ClauseStream>;
};
Expand All @@ -29,6 +30,8 @@ export type ExprInt = {
value: number;
};

export type ExprPreset = { alias: string } | { name: string };

export type ExprString = {
operator: OperatorString;
value: string;
Expand Down
14 changes: 14 additions & 0 deletions js/streamql/src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
ExprBool,
ExprDate,
ExprInt,
ExprPreset,
ExprString,
Query,
} from "./primitives.js";
Expand All @@ -19,6 +20,9 @@ export const serialize = (query: Query): Result<string, StreamQLError> => {
const serializeClauseRequestResponse = (
query: Query,
): Result<string, StreamQLError> => {
if (isPresent(query.preset)) {
return serializeExprPreset(query.preset);
}
if (isPresent(query.stream)) {
return serializeClauseStream(query.stream).map((str) => `stream.${str}`);
}
Expand Down Expand Up @@ -90,6 +94,16 @@ const serializeClauseStream = (
return err(new InvalidQuery());
};

const serializeExprPreset = (
preset: ExprPreset,
): Result<string, StreamQLError> => {
if ("alias" in preset) {
return ok(`preset:${preset.alias}`);
} else {
return ok(`preset:"${preset.name}"`);
}
};

const serializeExprInt = (value: ExprInt): Result<string, StreamQLError> => {
return ok(`${value.operator.toLowerCase()}:${value.value}`);
};
Expand Down