Skip to content

Commit

Permalink
fix: allow plucked join one field to be filtered on properly, bump pa…
Browse files Browse the repository at this point in the history
…ckages
  • Loading branch information
Jesse Zhang committed May 29, 2018
1 parent e2fbdc3 commit bf70a58
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 37 deletions.
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module.exports = {
moduleFileExtensions: ["ts", "js"],
setupTestFrameworkScriptFile: "<rootDir>/src/tests/bootstrap.ts",
collectCoverage: true,
mapCoverage: true,
collectCoverageFrom: [
"src/**/*.ts",
"!src/**/index.ts",
Expand Down
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@lchemy/api-filter-parser",
"description": "",
"version": "1.0.0-beta.0",
"version": "1.0.0-beta.1",
"contributors": [
"Jesse Zhang"
],
Expand All @@ -23,28 +23,28 @@
},
"dependencies": {
"@lchemy/antlr4ts": "^0.4.1-alpha.1",
"@lchemy/orm": "^1.0.0-beta.0"
"@lchemy/orm": "^1.0.0-beta.3"
},
"devDependencies": {
"@lchemy/antlr4ts-cli": "^0.4.0-alpha.6",
"@types/jest": "^22.1.2",
"@types/knex": "^0.14.7",
"coveralls": "^3.0.0",
"@types/jest": "^22.2.3",
"@types/knex": "^0.14.14",
"coveralls": "^3.0.1",
"del": "^3.0.0",
"fs-extra": "^5.0.0",
"glob-promise": "^3.3.0",
"fs-extra": "^6.0.1",
"glob-promise": "^3.4.0",
"gulp": "^4.0.0",
"gulp-plumber": "^1.2.0",
"gulp-tslint": "^8.1.3",
"gulp-typescript": "^4.0.1",
"jest": "^22.3.0",
"jest-cli": "^22.3.0",
"knex": "^0.14.3",
"merge2": "^1.2.1",
"shelljs": "^0.8.1",
"ts-jest": "^22.0.4",
"tslint": "^5.9.1",
"typescript": "^2.7.2",
"gulp-typescript": "^4.0.2",
"jest": "^23.0.1",
"jest-cli": "^23.0.1",
"knex": "^0.14.6",
"merge2": "^1.2.2",
"shelljs": "^0.8.2",
"ts-jest": "^22.4.6",
"tslint": "^5.10.0",
"typescript": "^2.8.3",
"vinyl-map": "^1.0.2"
}
}
8 changes: 7 additions & 1 deletion src/parse-api-filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ describe("parse api filter", () => {
await expect(filterPromise).rejects.toThrow();
});

it("should parse for plucked join one", async () => {
it("should parse for plucked join one exists", async () => {
const filter = await parseApiFilter($classesOrm, `teacherName exists`);
expect(filter).toBeInstanceOf(models.AndFilterGroup);

Expand All @@ -220,6 +220,12 @@ describe("parse api filter", () => {
const { left } = expressions.find((expr) => expr instanceof models.IsNotNullFilterNode) as models.IsNotNullFilterNode;
expect(left.toString()).toBe("classes.teacherName.name");
});

it("should parse for plucked join one filters", async () => {
const filter = await parseApiFilter($classesOrm, `teacherName eq "test"`);
expect(filter).toBeInstanceOf(models.EqualFilterNode);
expect((filter as models.EqualFilterNode).left.toString()).toBe("classes.teacherName.name");
});
});

describe("join many operations", () => {
Expand Down
31 changes: 12 additions & 19 deletions src/visitors/expression-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ export class ExpressionVisitor extends AbstractParseTreeVisitor<Filter> implemen

visitOpNullCheckExpression(ctx: OpNullCheckExpressionContext): Filter {
const op = ctx.OP_NULL_CHECK().text,
value = ctx.value().accept(this.valueVisitor) as any;

assertColumnValue(value);
value = toColumnValue(ctx.value().accept(this.valueVisitor));

switch (op) {
case "is null":
Expand All @@ -89,9 +87,7 @@ export class ExpressionVisitor extends AbstractParseTreeVisitor<Filter> implemen
const op = ctx.OP_COMPARISON().text;

const [left, right] = ctx.value().map((valueCtx) => {
const value = valueCtx.accept(this.valueVisitor) as any;
assertColumnValue(value);
return value;
return toColumnValue(valueCtx.accept(this.valueVisitor));
});

switch (op) {
Expand All @@ -116,9 +112,7 @@ export class ExpressionVisitor extends AbstractParseTreeVisitor<Filter> implemen
const op = ctx.OP_LIKE().text;

const [left, right] = ctx.value().map((valueCtx) => {
const value = valueCtx.accept(this.valueVisitor) as any;
assertColumnValue(value);
return value;
return toColumnValue(valueCtx.accept(this.valueVisitor));
});

switch (op) {
Expand All @@ -139,9 +133,7 @@ export class ExpressionVisitor extends AbstractParseTreeVisitor<Filter> implemen
const op = ctx.OP_BETWEEN().text;

const [left, start, end] = ctx.value().map((valueCtx) => {
const value = valueCtx.accept(this.valueVisitor) as any;
assertColumnValue(value);
return value;
return toColumnValue(valueCtx.accept(this.valueVisitor));
});

switch (op) {
Expand All @@ -158,9 +150,7 @@ export class ExpressionVisitor extends AbstractParseTreeVisitor<Filter> implemen
const op = ctx.OP_IN().text;

const [left, ...values] = ctx.value().map((valueCtx) => {
const value = valueCtx.accept(this.valueVisitor) as any;
assertColumnValue(value);
return value;
return toColumnValue(valueCtx.accept(this.valueVisitor));
});

switch (op) {
Expand Down Expand Up @@ -219,15 +209,15 @@ export class ExpressionVisitor extends AbstractParseTreeVisitor<Filter> implemen
visitOpHaveCountExpression(ctx: OpHaveCountExpressionContext): Filter {
const op = ctx.OP_COMPARISON().text;

const [left, right] = ctx.value().map((valueCtx) => {
const [left, rawRight] = ctx.value().map((valueCtx) => {
return valueCtx.accept(this.valueVisitor) as any;
});

if (!isJoinManyField(left) && !isPartitionedJoinManyField(left) && !isPluckedJoinManyField(left)) {
throw new Error(`Expected ${ left } to be a join many field`);
}

assertColumnValue(right);
const right = toColumnValue(rawRight);

switch (op) {
case "eq":
Expand Down Expand Up @@ -256,15 +246,18 @@ export class ExpressionVisitor extends AbstractParseTreeVisitor<Filter> implemen
}
}

function assertColumnValue(value: ValueType): void {
function toColumnValue(value: ValueType): number | string | ColumnField | DerivedField | WrappedRaw {
if (
typeof value === "number" ||
typeof value === "string" ||
value instanceof ColumnField ||
value instanceof DerivedField ||
value instanceof WrappedRaw
) {
return;
return value;
}
if (isPluckedJoinOneField(value)) {
return value["🜁"].pluckField;
}
throw new Error(`Expected ${ value } to be number, string, column field, derived field, or raw`);
}
Expand Down
1 change: 1 addition & 0 deletions wallaby.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = () => {
"src/**/*.spec.ts"
],
filesWithNoCoverageCalculated: [
"src/codegen/**/*.ts",
"src/tests/**/*.ts"
],
env: {
Expand Down

0 comments on commit bf70a58

Please sign in to comment.