From e87fefad82d0b49087c22ceed8095c51948caef8 Mon Sep 17 00:00:00 2001 From: zhangzhq04 Date: Thu, 17 Sep 2020 10:45:11 +0800 Subject: [PATCH] support variables with dash, E.g: data.sotre['soccer-shoes'] --- jsonpath.lua | 2 +- test/data.lua | 4 ++++ test/test.lua | 20 ++++++++++++++++---- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/jsonpath.lua b/jsonpath.lua index d46a55d..3c1c0a4 100755 --- a/jsonpath.lua +++ b/jsonpath.lua @@ -162,7 +162,7 @@ local jsonpath_grammer = (function() local digit = R('09') local letter = R('AZ','az') local alpha0 = R('AZ','az') + '_' - local alphaN = R('AZ','az','09') + '_' + local alphaN = R('AZ','az','09') + '_' + '-' local hexdigit = R('09', 'AF', 'af') local number_hex = P'0' * S'xX' * hexdigit^1 diff --git a/test/data.lua b/test/data.lua index 7ea2492..2685c66 100755 --- a/test/data.lua +++ b/test/data.lua @@ -53,6 +53,10 @@ return { isbn = '0-395-19395-8', price = 22.99 } + }, + ['soccer-shoes'] = { + color = 'blue', + price = 200.00 } } } diff --git a/test/test.lua b/test/test.lua index fc6a56e..6a4da4e 100755 --- a/test/test.lua +++ b/test/test.lua @@ -171,6 +171,12 @@ testParse = { lu.assertNil(err) lu.assertEquals(path, {'$', '..', 'price'}) end, + + testParserPathForSoccerShoes = function() + local path, err = jp.parse('$.store.soccer-shoes') + lu.assertNil(err) + lu.assertEquals(path, { '$', 'store', 'soccer-shoes' }) + end, } @@ -373,7 +379,8 @@ testQuery = { lu.assertNil(err) lu.assertItemsEquals(results, sortByPath({ { path = {'$', 'store', 'book'}, value = data.store.book }, - { path = {'$', 'store', 'bicycle'}, value = data.store.bicycle } + { path = {'$', 'store', 'bicycle'}, value = data.store.bicycle }, + { path = {'$', 'store', 'soccer-shoes'}, value = data.store['soccer-shoes'] } })) end, @@ -385,7 +392,9 @@ testQuery = { { path = {'$', 'store', 'book', 1, 'price'}, value = 12.99 }, { path = {'$', 'store', 'book', 2, 'price'}, value = 8.99 }, { path = {'$', 'store', 'book', 3, 'price'}, value = 22.99 }, - { path = {'$', 'store', 'bicycle', 'price'}, value = 19.95 } + { path = {'$', 'store', 'bicycle', 'price'}, value = 19.95 }, + { path = {'$', 'store', 'soccer-shoes', 'price'}, value = 200.00 } + })) end, @@ -447,6 +456,7 @@ testQuery = { { path = { '$', 'store' }, value = data.store }, { path = { '$', 'store', 'book' }, value = data.store.book }, { path = { '$', 'store', 'bicycle' }, value = data.store.bicycle }, + { path = { '$', 'store', 'soccer-shoes' }, value = data.store['soccer-shoes'] }, { path = { '$', 'store', 'book', 0 }, value = data.store.book[1] }, { path = { '$', 'store', 'book', 1 }, value = data.store.book[2] }, { path = { '$', 'store', 'book', 2 }, value = data.store.book[3] }, @@ -470,7 +480,9 @@ testQuery = { { path = { '$', 'store', 'book', 3, 'isbn' }, value = '0-395-19395-8' }, { path = { '$', 'store', 'book', 3, 'price' }, value = 22.99 }, { path = { '$', 'store', 'bicycle', 'color' }, value = 'red' }, - { path = { '$', 'store', 'bicycle', 'price' }, value = 19.95 } + { path = { '$', 'store', 'bicycle', 'price' }, value = 19.95 }, + { path = { '$', 'store', 'soccer-shoes', 'price' }, value = 200.00 }, + { path = { '$', 'store', 'soccer-shoes', 'color' }, value = 'blue' } })) end, @@ -483,7 +495,7 @@ testQuery = { testObjectSubscriptWildcard = function() local results, err = jp.query(data, '$.store[*]') lu.assertNil(err) - lu.assertItemsEquals(results, { data.store.book, data.store.bicycle }) + lu.assertItemsEquals(results, { data.store.book, data.store.bicycle, data.store['soccer-shoes']}) end, testNoMatchReturnsEmptyArray = function()