Skip to content

Commit

Permalink
Merge pull request #856 from saiichihashimoto/greater-or-less-example
Browse files Browse the repository at this point in the history
fix(groq): using boolean comparisons no longer breaks queries
  • Loading branch information
kodiakhq[bot] authored Dec 23, 2024
2 parents cc5611f + e42e7b7 commit c8c1080
Show file tree
Hide file tree
Showing 2 changed files with 269 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/groq/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,11 @@ type EvaluateBooleanOperator<
stronger: infer TStronger;
weaker: infer TWeaker;
}
? Evaluate<TLeft, TScope> extends TStronger
? boolean extends Evaluate<TLeft, TScope>
? Evaluate<TRight, TScope>
: boolean extends Evaluate<TRight, TScope>
? Evaluate<TLeft, TScope>
: Evaluate<TLeft, TScope> extends TStronger
? TStronger
: Evaluate<TRight, TScope> extends TStronger
? TStronger
Expand Down
264 changes: 264 additions & 0 deletions packages/groq/src/operators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,138 @@ describe("operators", () => {
>();
});

it("$param&&true (boolean)", async () => {
const query = "$param&&true";

const tree = parse(query);

const expectedTree = {
left: { name: "param", type: "Parameter" },
right: { type: "Value", value: true },
type: "And",
} as const;

expect(tree).toStrictEqual(expectedTree);
expectType<Parse<typeof query>>().toStrictEqual<
WritableDeep<typeof expectedTree>
>();

const params = { param: true as boolean } as const;

const result = await (await evaluate(tree, { params })).get();

const expectedResult = true;

expect(result).toStrictEqual(expectedResult);
expectType<
ExecuteQuery<
typeof query,
ScopeFromPartialContext<{
parameters: WritableDeep<typeof params>;
}>
>
>().toStrictEqual<typeof expectedResult>();
});

it("$param&&false (boolean)", async () => {
const query = "$param&&false";

const tree = parse(query);

const expectedTree = {
left: { name: "param", type: "Parameter" },
right: { type: "Value", value: false },
type: "And",
} as const;

expect(tree).toStrictEqual(expectedTree);
expectType<Parse<typeof query>>().toStrictEqual<
WritableDeep<typeof expectedTree>
>();

const params = { param: true as boolean } as const;

const result = await (await evaluate(tree, { params })).get();

const expectedResult = false;

expect(result).toStrictEqual(expectedResult);
expectType<
ExecuteQuery<
typeof query,
ScopeFromPartialContext<{
parameters: WritableDeep<typeof params>;
}>
>
>().toStrictEqual<typeof expectedResult>();
});

it("true&&$param (boolean)", async () => {
const query = "true&&$param";

const tree = parse(query);

const expectedTree = {
left: { type: "Value", value: true },
right: { name: "param", type: "Parameter" },
type: "And",
} as const;

expect(tree).toStrictEqual(expectedTree);
expectType<Parse<typeof query>>().toStrictEqual<
WritableDeep<typeof expectedTree>
>();

const params = { param: true as boolean } as const;

const result = await (await evaluate(tree, { params })).get();

const expectedResult = true;

expect(result).toStrictEqual(expectedResult);
expectType<
ExecuteQuery<
typeof query,
ScopeFromPartialContext<{
parameters: WritableDeep<typeof params>;
}>
>
>().toStrictEqual<typeof expectedResult>();
});

it("false&&$param (boolean)", async () => {
const query = "false&&$param";

const tree = parse(query);

const expectedTree = {
left: { type: "Value", value: false },
right: { name: "param", type: "Parameter" },
type: "And",
} as const;

expect(tree).toStrictEqual(expectedTree);
expectType<Parse<typeof query>>().toStrictEqual<
WritableDeep<typeof expectedTree>
>();

const params = { param: true as boolean } as const;

const result = await (await evaluate(tree)).get();

const expectedResult = false;

expect(result).toStrictEqual(expectedResult);
expectType<
ExecuteQuery<
typeof query,
ScopeFromPartialContext<{
parameters: WritableDeep<typeof params>;
}>
>
>().toStrictEqual<typeof expectedResult>();
});

it('""&&false', async () => {
const query = '""&&false';

Expand Down Expand Up @@ -349,6 +481,138 @@ describe("operators", () => {
>();
});

it("$param||true (boolean)", async () => {
const query = "$param||true";

const tree = parse(query);

const expectedTree = {
left: { name: "param", type: "Parameter" },
right: { type: "Value", value: true },
type: "Or",
} as const;

expect(tree).toStrictEqual(expectedTree);
expectType<Parse<typeof query>>().toStrictEqual<
WritableDeep<typeof expectedTree>
>();

const params = { param: true as boolean } as const;

const result = await (await evaluate(tree, { params })).get();

const expectedResult = true;

expect(result).toStrictEqual(expectedResult);
expectType<
ExecuteQuery<
typeof query,
ScopeFromPartialContext<{
parameters: WritableDeep<typeof params>;
}>
>
>().toStrictEqual<typeof expectedResult>();
});

it("$param||false (boolean)", async () => {
const query = "$param||false";

const tree = parse(query);

const expectedTree = {
left: { name: "param", type: "Parameter" },
right: { type: "Value", value: false },
type: "Or",
} as const;

expect(tree).toStrictEqual(expectedTree);
expectType<Parse<typeof query>>().toStrictEqual<
WritableDeep<typeof expectedTree>
>();

const params = { param: true as boolean } as const;

const result = await (await evaluate(tree, { params })).get();

const expectedResult = true as false;

expect(result).toStrictEqual(expectedResult);
expectType<
ExecuteQuery<
typeof query,
ScopeFromPartialContext<{
parameters: WritableDeep<typeof params>;
}>
>
>().toStrictEqual<typeof expectedResult>();
});

it("true||$param (boolean)", async () => {
const query = "true||$param";

const tree = parse(query);

const expectedTree = {
left: { type: "Value", value: true },
right: { name: "param", type: "Parameter" },
type: "Or",
} as const;

expect(tree).toStrictEqual(expectedTree);
expectType<Parse<typeof query>>().toStrictEqual<
WritableDeep<typeof expectedTree>
>();

const params = { param: true as boolean } as const;

const result = await (await evaluate(tree, { params })).get();

const expectedResult = true;

expect(result).toStrictEqual(expectedResult);
expectType<
ExecuteQuery<
typeof query,
ScopeFromPartialContext<{
parameters: WritableDeep<typeof params>;
}>
>
>().toStrictEqual<typeof expectedResult>();
});

it("false||$param (boolean)", async () => {
const query = "false||$param";

const tree = parse(query);

const expectedTree = {
left: { type: "Value", value: false },
right: { name: "param", type: "Parameter" },
type: "Or",
} as const;

expect(tree).toStrictEqual(expectedTree);
expectType<Parse<typeof query>>().toStrictEqual<
WritableDeep<typeof expectedTree>
>();

const params = { param: true as boolean } as const;

const result = await (await evaluate(tree, { params })).get();

const expectedResult = true as false;

expect(result).toStrictEqual(expectedResult);
expectType<
ExecuteQuery<
typeof query,
ScopeFromPartialContext<{
parameters: WritableDeep<typeof params>;
}>
>
>().toStrictEqual<typeof expectedResult>();
});

it('""||true', async () => {
const query = '""||true';

Expand Down

0 comments on commit c8c1080

Please sign in to comment.