Skip to content

Commit

Permalink
Support root character
Browse files Browse the repository at this point in the history
  • Loading branch information
Glen Van Ginkel committed Oct 10, 2020
1 parent 9ba315d commit 6d2d8f5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum Token {
TOK_RBRACE = 'Rbrace',
TOK_NUMBER = 'Number',
TOK_CURRENT = 'Current',
TOK_ROOT = 'Root',
TOK_EXPREF = 'Expref',
TOK_PIPE = 'Pipe',
TOK_OR = 'Or',
Expand Down Expand Up @@ -74,6 +75,7 @@ export const basicTokens = {
'.': Token.TOK_DOT,
':': Token.TOK_COLON,
'@': Token.TOK_CURRENT,
['$']: Token.TOK_ROOT,
']': Token.TOK_RBRACKET,
'{': Token.TOK_LBRACE,
'}': Token.TOK_RBRACE,
Expand Down
3 changes: 3 additions & 0 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const bindingPower: { [token: string]: number } = {
[Token.TOK_NUMBER]: 0,
[Token.TOK_CURRENT]: 0,
[Token.TOK_EXPREF]: 0,
[Token.TOK_ROOT]: 0,
[Token.TOK_PIPE]: 1,
[Token.TOK_OR]: 2,
[Token.TOK_AND]: 3,
Expand Down Expand Up @@ -134,6 +135,8 @@ class TokenParser {
return this.parseMultiselectList();
case Token.TOK_CURRENT:
return { type: Token.TOK_CURRENT };
case Token.TOK_ROOT:
return { type: Token.TOK_ROOT };
case Token.TOK_EXPREF:
expression = this.expression(bindingPower.Expref);
return { type: 'ExpressionReference', children: [expression] } as ExpressionNode;
Expand Down
4 changes: 4 additions & 0 deletions src/TreeInterpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import { JSONValue } from '.';

export class TreeInterpreter {
runtime: Runtime;
private _rootValue: JSONValue | null = null;

constructor() {
this.runtime = new Runtime(this);
}

search(node: ExpressionNodeTree, value: JSONValue): JSONValue {
this._rootValue = value;
return this.visit(node, value) as JSONValue;
}

Expand Down Expand Up @@ -224,6 +226,8 @@ export class TreeInterpreter {
return this.visit((node as ExpressionNode).children[1], left);
case Token.TOK_CURRENT:
return value;
case Token.TOK_ROOT:
return this._rootValue;
case 'Function':
const resolvedArgs: JSONValue[] = [];
for (let j = 0; j < (node as ExpressionNode).children.length; j += 1) {
Expand Down
22 changes: 22 additions & 0 deletions test/jmespath.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,25 @@ describe('registerFunction', () => {
).toThrow('ArgumentError: optionalArgs() takes 1 arguments but received 3');
});
});

describe('root', () => {
it('$ should give access to the root value', () => {
var value = search({ foo: { bar: 1 } }, 'foo.{ value: $.foo.bar }');
expect(value.value).toBe(1);
});
it('$ should give access to the root value after pipe', () => {
var value = search({ foo: { bar: 1 } }, 'foo | $.foo.bar');
expect(value).toEqual(1);
});
it('$ should give access in expressions', () => {
var value = search([{ foo: { bar: 1 } }, { foo: { bar: 99 } }], 'map(&foo.{boo: bar, root: $}, @)');
expect(value).toEqual([
{ boo: 1, root: [{ foo: { bar: 1 } }, { foo: { bar: 99 } }] },
{ boo: 99, root: [{ foo: { bar: 1 } }, { foo: { bar: 99 } }] },
]);
});
it('$ can be used in parallel', () => {
var value = search([{ foo: { bar: 1 } }, { foo: { bar: 99 } }], '[$[0].foo.bar, $[1].foo.bar]');
expect(value).toEqual([1, 99]);
});
});

0 comments on commit 6d2d8f5

Please sign in to comment.