Skip to content

Commit

Permalink
parser is fully separated
Browse files Browse the repository at this point in the history
  • Loading branch information
Afsin Ustundag committed Jun 24, 2015
1 parent a075d28 commit 3cc7dd2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 45 deletions.
16 changes: 8 additions & 8 deletions lib/jsonpath/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,11 @@ var tracerAdapter = (function () {
method: Tracer.tracePostdefinedFunctionGen(parameter + '()')
};
}
},
property: function (parameter) {
return {
method: Tracer.tracePropertyGen(parameter)
};
}
};

Expand All @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion lib/jsonpath/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
97 changes: 61 additions & 36 deletions test/jsonpath/test-normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand Down

0 comments on commit 3cc7dd2

Please sign in to comment.