Skip to content

Commit

Permalink
Merge branch 'main' of github.com:rudderlabs/rudder-json-template-engine
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Nov 9, 2023
2 parents 9aceb6f + 65e62e1 commit d9e218c
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 25 deletions.
21 changes: 16 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,24 @@
}
},
{
"name": "Test Scenario",
"program": "${workspaceFolder}/test/test_scenario.ts",
"runtimeExecutable": "/usr/local/bin/node",
"name": "Jest Scenario",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"request": "launch",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
"skipFiles": ["<node_internals>/**"],
"args": ["--scenario=${input:scenario}", "--index=${input:index}"],
"type": "node"
"args": [
"scenario.test",
"--config",
"jest.config.ts",
"--scenario=${input:scenario}",
"--index=${input:index}"
],
"type": "node",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
}
},
{
"runtimeExecutable": "/usr/local/bin/node",
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [0.8.1](https://github.com/rudderlabs/rudder-json-template-engine/compare/v0.8.0...v0.8.1) (2023-10-18)


### Bug Fixes

* **INT-833:** conditional expression parsing is failing parse objects ([#46](https://github.com/rudderlabs/rudder-json-template-engine/issues/46)) ([bf27bae](https://github.com/rudderlabs/rudder-json-template-engine/commit/bf27baec9011d54d449adf8c1b4f5ce72d7f3444))

## [0.8.0](https://github.com/rudderlabs/rudder-json-template-engine/compare/v0.7.0...v0.8.0) (2023-10-10)


Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rudderstack/json-template-engine",
"version": "0.8.0",
"version": "0.8.1",
"homepage": "https://github.com/rudderlabs/rudder-json-template-engine",
"description": "A library for evaluating JSON template expressions.",
"main": "build/index.js",
Expand Down
13 changes: 8 additions & 5 deletions src/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export class JsonTemplateLexer {
private readonly codeChars: string[];
private buf: Token[];
private idx = 0;
private lastParsedToken?: Token;

constructor(template: string) {
this.buf = [];
Expand All @@ -29,6 +28,11 @@ export class JsonTemplateLexer {
return this.idx;
}

reset(idx: number) {
this.idx = idx;
this.buf = [];
}

getCode(start: number, end: number): string {
return this.codeChars.slice(start, end).join('');
}
Expand Down Expand Up @@ -272,12 +276,11 @@ export class JsonTemplateLexer {
lex(): Token {
if (this.buf[0]) {
this.idx = this.buf[0].range[1];
this.lastParsedToken = this.buf[0];
let token = this.buf[0];
this.buf = this.buf.slice(1);
return this.lastParsedToken;
return token;
}
this.lastParsedToken = this.advance();
return this.lastParsedToken;
return this.advance();
}

static isLiteralToken(token: Token) {
Expand Down
12 changes: 10 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
LoopExpression,
IncrementExpression,
} from './types';
import { JsonTemplateParserError } from './errors';
import { JsonTemplateLexerError, JsonTemplateParserError } from './errors';
import { BINDINGS_PARAM_KEY, DATA_PARAM_KEY } from './constants';
import { JsonTemplateLexer } from './lexer';
import { CommonUtils } from './utils';
Expand Down Expand Up @@ -471,8 +471,16 @@ export class JsonTemplateParser {
}

private parseConditionalBodyExpr(): Expression {
const currentIndex = this.lexer.currentIndex();
if (this.lexer.match('{')) {
return this.parseCurlyBlockExpr({ blockEnd: '}', parentType: SyntaxType.CONDITIONAL_EXPR });
try {
return this.parseObjectExpr();
} catch (error: any) {
if (error instanceof JsonTemplateLexerError) {
this.lexer.reset(currentIndex);
}
return this.parseCurlyBlockExpr({ blockEnd: '}', parentType: SyntaxType.CONDITIONAL_EXPR });
}
}
return this.parseBaseExpr();
}
Expand Down
22 changes: 14 additions & 8 deletions test/scenarios/conditions/data.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { Scenario } from '../../types';

export const data: Scenario[] = [
{
templatePath: 'empty_if.jt',
error: 'Empty statements are not allowed in loop and condtional expressions',
},
{
templatePath: 'empty_then.jt',
error: 'Empty statements are not allowed in loop and condtional expressions',
},
{
templatePath: 'if_block.jt',
input: {
Expand Down Expand Up @@ -58,6 +50,20 @@ export const data: Scenario[] = [
},
output: 5,
},
{
templatePath: 'objects.jt',
input: {
a: 5,
},
output: { message: 'a > 1' },
},
{
templatePath: 'objects.jt',
input: {
a: 0,
},
output: { message: 'a <= 1' },
},
{
input: {
a: 5,
Expand Down
1 change: 0 additions & 1 deletion test/scenarios/conditions/empty_if.jt

This file was deleted.

1 change: 0 additions & 1 deletion test/scenarios/conditions/empty_then.jt

This file was deleted.

1 change: 1 addition & 0 deletions test/scenarios/conditions/objects.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.a > 1 ? { message: "a > 1" } : { message: "a <= 1" }
6 changes: 6 additions & 0 deletions test_engine_local1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { JsonTemplateEngine } from './src';

console.log(
JsonTemplateEngine.translate(`
.output ? .output : { "foo": "bar" }`),
);

0 comments on commit d9e218c

Please sign in to comment.