diff --git a/lib/jsonpath/index.js b/lib/jsonpath/index.js index e6f8644..8b3317d 100644 --- a/lib/jsonpath/index.js +++ b/lib/jsonpath/index.js @@ -397,6 +397,11 @@ var tracerAdapter = (function () { method: Tracer.tracePostdefinedFunctionGen(parameter + '()') }; } + }, + property: function (parameter) { + return { + method: Tracer.tracePropertyGen(parameter) + }; } }; @@ -407,14 +412,9 @@ var tracerAdapter = (function () { }; adapter.exprToMethod = function (expr, opts) { - if (typeof expr === 'object') { - var type = expr.type; - var parameter = expr.parameter; - return this.converter[type](parameter, opts); - } - return { - method: Tracer.tracePropertyGen(expr) - }; + var type = expr.type; + var parameter = expr.parameter; + return this.converter[type](parameter, opts); }; adapter.exprToMethodUpdateOptions = function (expr, opts) { diff --git a/lib/jsonpath/parser.js b/lib/jsonpath/parser.js index 329238d..6cae146 100644 --- a/lib/jsonpath/parser.js +++ b/lib/jsonpath/parser.js @@ -144,7 +144,10 @@ var updateNodesWithProperty = function (property, nodes) { }); return; } - nodes.push(property); + nodes.push({ + type: 'property', + parameter: property + }); }; var normalize = exports.normalize = function (expr) { diff --git a/test/jsonpath/test-normalize.js b/test/jsonpath/test-normalize.js index 3ae1f66..fd1cedd 100644 --- a/test/jsonpath/test-normalize.js +++ b/test/jsonpath/test-normalize.js @@ -7,95 +7,120 @@ var jsonpath = require('../../lib/jsonpath/parser'); var expect = chai.expect; describe('jsonpath normalization', function () { + var toNode = function (property) { + return { + type: 'property', + parameter: property + }; + }; + it('$.store.book[*].author', function () { var actual = jsonpath.normalize('$.store.book[*].author'); - expect(actual).to.deep.equal([jsonpath.types.ROOT, 'store', 'book', jsonpath.types.WILDCARD, 'author']); + var expected = [ + jsonpath.types.ROOT, + toNode('store'), + toNode('book'), + jsonpath.types.WILDCARD, + toNode('author') + ]; + expect(actual).to.deep.equal(expected); }); it('$..author', function () { var actual = jsonpath.normalize('$..author'); - expect(actual).to.deep.equal([jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, 'author']); + var expected = [ + jsonpath.types.ROOT, + jsonpath.types.RECURSIVE_DESCENT, + toNode('author') + ]; + expect(actual).to.deep.equal(expected); }); it('$.store.*', function () { var actual = jsonpath.normalize('$.store.*'); - expect(actual).to.deep.equal([jsonpath.types.ROOT, 'store', jsonpath.types.WILDCARD]); + var expected = [jsonpath.types.ROOT, toNode('store'), jsonpath.types.WILDCARD]; + expect(actual).to.deep.equal(expected); }); it('$.store..price', function () { var actual = jsonpath.normalize('$.store..price'); - expect(actual).to.deep.equal([jsonpath.types.ROOT, 'store', jsonpath.types.RECURSIVE_DESCENT, 'price']); + var expected = [jsonpath.types.ROOT, toNode('store'), jsonpath.types.RECURSIVE_DESCENT, toNode('price')]; + expect(actual).to.deep.equal(expected); }); it('$..book[2]', function () { var actual = jsonpath.normalize('$..book[2]'); - var propertiesObject = { - type: 'properties', - parameter: [2] - }; - expect(actual).to.deep.equal([jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, 'book', propertiesObject]); + var expected = [ + jsonpath.types.ROOT, + jsonpath.types.RECURSIVE_DESCENT, + toNode('book'), { + type: 'properties', + parameter: [2] + } + ]; + expect(actual).to.deep.equal(expected); }); it('$..book[(@.length-1)]', function () { var actual = jsonpath.normalize('$..book[(@.length-1)]'); - var scriptNode = { + var expected = [jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, toNode('book'), { type: 'script', parameter: '@.length-1' - }; - expect(actual).to.deep.equal([jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, 'book', scriptNode]); + }]; + expect(actual).to.deep.equal(expected); }); it('$..book[-1:]', function () { var actual = jsonpath.normalize('$..book[-1:]'); - var propertiesObject = { + var expected = [jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, toNode('book'), { type: 'properties', parameter: [{ start: -1, end: null, step: 1 }] - }; - expect(actual).to.deep.equal([jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, 'book', propertiesObject]); + }]; + expect(actual).to.deep.equal(expected); }); it('$..book[1,2]', function () { var actual = jsonpath.normalize('$..book[1,2]'); - var propertiesObject = { + var expected = [jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, toNode('book'), { type: 'properties', parameter: [1, 2] - }; - expect(actual).to.deep.equal([jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, 'book', propertiesObject]); + }]; + expect(actual).to.deep.equal(expected); }); it('$..book[:2]', function () { var actual = jsonpath.normalize('$..book[:2]'); - var propertiesObject = { + var expected = [jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, toNode('book'), { type: 'properties', parameter: [{ start: 0, end: 2, step: 1 }] - }; - expect(actual).to.deep.equal([jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, 'book', propertiesObject]); + }]; + expect(actual).to.deep.equal(expected); }); it('$..book[*][category,author]', function () { var actual = jsonpath.normalize('$..book[*][category,author]'); - var propertiesObject = { + var expected = [jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, toNode('book'), jsonpath.types.WILDCARD, { type: 'properties', parameter: ['category', 'author'] - }; - expect(actual).to.deep.equal([jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, 'book', jsonpath.types.WILDCARD, propertiesObject]); + }]; + expect(actual).to.deep.equal(expected); }); it('$..book[?(@.isbn)]', function () { var actual = jsonpath.normalize('$..book[?(@.isbn)]'); - var filterScriptNode = { + var expected = [jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, toNode('book'), { type: 'filter_script', parameter: '@.isbn' - }; - expect(actual).to.deep.equal([jsonpath.types.ROOT, jsonpath.types.RECURSIVE_DESCENT, 'book', filterScriptNode]); + }]; + expect(actual).to.deep.equal(expected); }); it('$..[?(@.price>19)]^', function () { @@ -114,29 +139,29 @@ describe('jsonpath normalization', function () { it('$.store.book[?(@path !== "$[\'store\'][\'book\'][0]")]', function () { var actual = jsonpath.normalize('$.store.book[?(@path !== "$[\'store\'][\'book\'][0]")]'); - var filterScriptNode = { + var expected = [jsonpath.types.ROOT, toNode('store'), toNode('book'), { type: 'filter_script', parameter: '@path !== "$[\'store\'][\'book\'][0]"' - }; - expect(actual).to.deep.equal([jsonpath.types.ROOT, 'store', 'book', filterScriptNode]); + }]; + expect(actual).to.deep.equal(expected); }); it('$.store..price.round()', function () { var actual = jsonpath.normalize('$.store..price.round()'); - var fnObject = { + var expected = [jsonpath.types.ROOT, toNode('store'), jsonpath.types.RECURSIVE_DESCENT, toNode('price'), { type: 'fn', parameter: 'round' - }; - expect(actual).to.deep.equal([jsonpath.types.ROOT, 'store', jsonpath.types.RECURSIVE_DESCENT, 'price', fnObject]); + }]; + expect(actual).to.deep.equal(expected); }); it('$.link[$.obj.library.books[0].references[*]].title', function () { var actual = jsonpath.normalize('$.link[$.obj.library.books[0].references[*]].title'); - var subpathObject = { + var expected = [jsonpath.types.ROOT, toNode('link'), { type: 'subpath', parameter: '$.obj.library.books[0].references[*]' - }; - expect(actual).to.deep.equal([jsonpath.types.ROOT, 'link', subpathObject, 'title']); + }, toNode('title')]; + expect(actual).to.deep.equal(expected); }); it('$', function () {