From 903f33078b5eaeaf5c6a4edc1e8349b4aea1da6f Mon Sep 17 00:00:00 2001 From: "d.belincev" Date: Tue, 20 Jan 2026 17:07:52 +0300 Subject: [PATCH 1/8] fix: add validation and conversion types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit При сравнении числа с булево, выдавало ошибку т.к. tonumber не мог привести его к числу. Добавил проверки на типы, преобразование булева в число и обратно. Исключил арифметические операции с булево и сравнения с строковым типом. --- jsonpath.lua | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/jsonpath.lua b/jsonpath.lua index 07bf2c9..b3d1b35 100755 --- a/jsonpath.lua +++ b/jsonpath.lua @@ -297,9 +297,18 @@ local function eval_ast(ast, obj) -- null must never be equal to other boolean, invert op1 return not op1 else + if type(op2) == 'string' then + return {} + end + if type(op2) == 'number' then + return op2 ~= 0 + end return (op2 and true or false) end elseif type(op1) == 'number' then + if type(op2) == 'boolean' then + return op2 and 1 or 0 + end return tonumber(op2) elseif type(op1) == 'cdata' and tostring(ffi.typeof(op1)) == 'ctype' then return tonumber(op2) @@ -314,6 +323,10 @@ local function eval_ast(ast, obj) return op1 and true or false end + local function is_bool(val) + return type(val) == 'boolean' + end + -- Helper helper: evaluate variable expression inside abstract syntax tree local function eval_var(expr, obj) if obj == nil then @@ -408,14 +421,29 @@ local function eval_ast(ast, obj) return nil, err end if operator == '+' then + if is_bool(op1) or is_bool(op2) then + return nil, "Prohibited arithmetic op on bool" + end op1 = tonumber(op1) + tonumber(op2) elseif operator == '-' then + if is_bool(op1) or is_bool(op2) then + return nil, "Prohibited arithmetic op on bool" + end op1 = tonumber(op1) - tonumber(op2) elseif operator == '*' then + if is_bool(op1) or is_bool(op2) then + return nil, "Prohibited arithmetic op on bool" + end op1 = tonumber(op1) * tonumber(op2) elseif operator == '/' then + if is_bool(op1) or is_bool(op2) then + return nil, "Prohibited arithmetic op on bool" + end op1 = tonumber(op1) / tonumber(op2) elseif operator == '%' then + if is_bool(op1) or is_bool(op2) then + return nil, "Prohibited arithmetic op on bool" + end op1 = tonumber(op1) % tonumber(op2) elseif operator:upper() == 'AND' or operator == '&&' then op1 = notempty(op1) and notempty(op2) @@ -426,22 +454,22 @@ local function eval_ast(ast, obj) elseif operator == '<>' or operator == '!=' then op1 = op1 ~= match_type(op1, op2) elseif operator == '>' then - if is_null(op1) then + if is_null(op1) or is_bool(op1) then return false end op1 = op1 > match_type(op1, op2) elseif operator == '>=' then - if is_null(op1) then + if is_null(op1) or is_bool(op1) then return false end op1 = op1 >= match_type(op1, op2) elseif operator == '<' then - if is_null(op1) then + if is_null(op1) or is_bool(op1) then return false end op1 = op1 < match_type(op1, op2) elseif operator == '<=' then - if is_null(op1) then + if is_null(op1) or is_bool(op1) then return false end op1 = op1 <= match_type(op1, op2) From 105282d25399ee5a1d065225ae1868ece9078dd6 Mon Sep 17 00:00:00 2001 From: "d.belincev" Date: Wed, 21 Jan 2026 16:44:12 +0300 Subject: [PATCH 2/8] tests --- test/test.lua | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/test/test.lua b/test/test.lua index ff565de..d392ec8 100755 --- a/test/test.lua +++ b/test/test.lua @@ -946,6 +946,112 @@ testQuery = { lu.assertNil(err) lu.assertItemsEquals(result, { array[2], array[3] }) end, + + testFilterIntBoolComparison = function () + local array = { + { id = 1, value = 0 }, + { id = 2, value = 1 }, + { id = 3, value = 2 }, + } + local result, err = jp.query(array, '$[?(@.value==true)]') + lu.assertNil(err) + lu.assertItemsEquals(result, { array[2] }) + + local result, err = jp.query(array, '$[?(@.value>true)]') + lu.assertNil(err) + lu.assertItemsEquals(result, { array[3] }) + + local result, err = jp.query(array, '$[?(@.value>=true)]') + lu.assertNil(err) + lu.assertItemsEquals(result, { array[2], array[3] }) + + local result, err = jp.query(array, '$[?(@.value1)]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + + local result, err = jp.query(array, '$[?(@.value>=1)]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + + local result, err = jp.query(array, '$[?(@.value<1)]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + + local result, err = jp.query(array, '$[?(@.value<=1)]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + end, + + testFilterBoolStrComparison = function () + local array = { + { id = 1, value = true }, + { id = 2, value = false }, + } + local result, err = jp.query(array, '$[?(@.value=="1")]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + + local result, err = jp.query(array, '$[?(@.value>"1")]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + + local result, err = jp.query(array, '$[?(@.value>="1")]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + + local result, err = jp.query(array, '$[?(@.value<"1")]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + + local result, err = jp.query(array, '$[?(@.value<="1")]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + end, + + testFilterArithmeticOpOnBool = function () + local array = { + { id = 1, value = 0 }, + { id = 1, value = 1 }, + { id = 2, value = 2 }, + } + local result, err = jp.query(array, '$[?(@.value==true+1)]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + + local result, err = jp.query(array, '$[?(@.value==true*1)]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + + local result, err = jp.query(array, '$[?(@.value==true/1)]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + + local result, err = jp.query(array, '$[?(@.value==true%1)]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + + local result, err = jp.query(array, '$[?(@.value<>false+1)]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + end, } From 2b84ab49dd3f0a97a211071c4efb659380258efb Mon Sep 17 00:00:00 2001 From: "d.belincev" Date: Fri, 23 Jan 2026 14:36:53 +0300 Subject: [PATCH 3/8] fix: arihmetic op, redesign comporation --- jsonpath.lua | 119 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 77 insertions(+), 42 deletions(-) diff --git a/jsonpath.lua b/jsonpath.lua index b3d1b35..ab526f7 100755 --- a/jsonpath.lua +++ b/jsonpath.lua @@ -291,31 +291,46 @@ end)() local function eval_ast(ast, obj) -- Helper helper: match type of second operand to type of first operand - local function match_type(op1, op2) + local function match_cmp_type(op1, op2, compare) if type(op1) == 'boolean' then if is_null(op2) then - -- null must never be equal to other boolean, invert op1 - return not op1 + return not op1, nil else - if type(op2) == 'string' then - return {} + if type(op2) == 'string' then + return nil, "cannot compare boolean with string" end if type(op2) == 'number' then - return op2 ~= 0 + if compare then + return nil, "cannot compare boolean with number" + end + return op2 ~= 0, nil + end + if type(op2) == 'boolean' then + return op2, nil end - return (op2 and true or false) + return (op2 and true or false), nil end elseif type(op1) == 'number' then if type(op2) == 'boolean' then - return op2 and 1 or 0 + return op2 and 1 or 0, nil + end + if type(op2) == 'string' then + local num = tonumber(op2) + if num == nil then + return nil, "cannot compare number with non-numeric string" + end + return num, nil end - return tonumber(op2) + return tonumber(op2), nil elseif type(op1) == 'cdata' and tostring(ffi.typeof(op1)) == 'ctype' then - return tonumber(op2) + return tonumber(op2), nil elseif is_null(op1) then - return op2 + if compare then + return nil, "cannot compare null with other values" + end + return op2, nil end - return tostring(op2 or '') + return tostring(op2 or ''), nil end -- Helper helper: convert operand to boolean @@ -323,8 +338,10 @@ local function eval_ast(ast, obj) return op1 and true or false end - local function is_bool(val) - return type(val) == 'boolean' + local function is_str_or_int(val) + return type(val) == 'string' or + type(val) == 'number' or + (type(val) == 'cdata' and tostring(ffi.typeof(val)) == 'ctype') end -- Helper helper: evaluate variable expression inside abstract syntax tree @@ -421,28 +438,34 @@ local function eval_ast(ast, obj) return nil, err end if operator == '+' then - if is_bool(op1) or is_bool(op2) then - return nil, "Prohibited arithmetic op on bool" + if is_str_or_int(op1) and is_str_or_int(op2) then + op1 = tonumber(op1) + tonumber(op2) + else + return nil, "Only operations on strings and numbers are allowed." end - op1 = tonumber(op1) + tonumber(op2) elseif operator == '-' then - if is_bool(op1) or is_bool(op2) then - return nil, "Prohibited arithmetic op on bool" + if is_str_or_int(op1) and is_str_or_int(op2) then + op1 = tonumber(op1) - tonumber(op2) + else + return nil, "Only operations on strings and numbers are allowed." end - op1 = tonumber(op1) - tonumber(op2) elseif operator == '*' then - if is_bool(op1) or is_bool(op2) then - return nil, "Prohibited arithmetic op on bool" + if is_str_or_int(op1) and is_str_or_int(op2) then + op1 = tonumber(op1) * tonumber(op2) + else + return nil, "Only operations on strings and numbers are allowed." end - op1 = tonumber(op1) * tonumber(op2) elseif operator == '/' then - if is_bool(op1) or is_bool(op2) then - return nil, "Prohibited arithmetic op on bool" + if is_str_or_int(op1) and is_str_or_int(op2) then + op1 = tonumber(op1) / tonumber(op2) + else + return nil, "Only operations on strings and numbers are allowed." end - op1 = tonumber(op1) / tonumber(op2) elseif operator == '%' then - if is_bool(op1) or is_bool(op2) then - return nil, "Prohibited arithmetic op on bool" + if is_str_or_int(op1) and is_str_or_int(op2) then + op1 = tonumber(op1) % tonumber(op2) + else + return nil, "Only operations on strings and numbers are allowed." end op1 = tonumber(op1) % tonumber(op2) elseif operator:upper() == 'AND' or operator == '&&' then @@ -450,29 +473,41 @@ local function eval_ast(ast, obj) elseif operator:upper() == 'OR' or operator == '||' then op1 = notempty(op1) or notempty(op2) elseif operator == '=' or operator == '==' then - op1 = op1 == match_type(op1, op2) + local op2, err = match_cmp_type(op1, op2, false) + if err then + return nil, err + end + op1 = op1 == op2 elseif operator == '<>' or operator == '!=' then - op1 = op1 ~= match_type(op1, op2) + local op2, err = match_cmp_type(op1, op2, false) + if err then + return nil, err + end + op1 = op1 ~= op2 elseif operator == '>' then - if is_null(op1) or is_bool(op1) then - return false + local op2, err = match_cmp_type(op1, op2, true) + if err then + return nil, err end - op1 = op1 > match_type(op1, op2) + op1 = op1 > op2 elseif operator == '>=' then - if is_null(op1) or is_bool(op1) then - return false + local op2, err = match_cmp_type(op1, op2, true) + if err then + return nil, err end - op1 = op1 >= match_type(op1, op2) + op1 = op1 >= op2 elseif operator == '<' then - if is_null(op1) or is_bool(op1) then - return false + local op2, err = match_cmp_type(op1, op2, true) + if err then + return nil, err end - op1 = op1 < match_type(op1, op2) + op1 = op1 < op2 elseif operator == '<=' then - if is_null(op1) or is_bool(op1) then - return false + local op2, err = match_cmp_type(op1, op2, true) + if err then + return nil, err end - op1 = op1 <= match_type(op1, op2) + op1 = op1 <= op2 else return nil, 'unknown expression operator "' .. operator .. '"' end From a371e8de2934d800a8db5adb8c1792a6767e2a2f Mon Sep 17 00:00:00 2001 From: "d.belincev" Date: Mon, 2 Feb 2026 16:57:39 +0300 Subject: [PATCH 4/8] fix: tabulation, covert str in arithmetic op --- jsonpath.lua | 33 ++++++++++++++++++++++++++------- test/test.lua | 18 ++++++++++++++++++ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/jsonpath.lua b/jsonpath.lua index ab526f7..fdf8bda 100755 --- a/jsonpath.lua +++ b/jsonpath.lua @@ -291,7 +291,7 @@ end)() local function eval_ast(ast, obj) -- Helper helper: match type of second operand to type of first operand - local function match_cmp_type(op1, op2, compare) + local function match_cmp_type(op1, op2, compare) if type(op1) == 'boolean' then if is_null(op2) then return not op1, nil @@ -439,35 +439,54 @@ local function eval_ast(ast, obj) end if operator == '+' then if is_str_or_int(op1) and is_str_or_int(op2) then - op1 = tonumber(op1) + tonumber(op2) + local num1, num2 = tonumber(op1), tonumber(op2) + if not (num1 and num2) then + return nil, "Cannot perform arithmetic on non-numeric strings" + end + op1 = num1 + num2 else return nil, "Only operations on strings and numbers are allowed." end elseif operator == '-' then if is_str_or_int(op1) and is_str_or_int(op2) then - op1 = tonumber(op1) - tonumber(op2) + local num1, num2 = tonumber(op1), tonumber(op2) + if not (num1 and num2) then + return nil, "Cannot perform arithmetic on non-numeric strings" + end + op1 = num1 - num2 else return nil, "Only operations on strings and numbers are allowed." end elseif operator == '*' then if is_str_or_int(op1) and is_str_or_int(op2) then - op1 = tonumber(op1) * tonumber(op2) + local num1, num2 = tonumber(op1), tonumber(op2) + if not (num1 and num2) then + return nil, "Cannot perform arithmetic on non-numeric strings" + end + op1 = num1 * num2 else return nil, "Only operations on strings and numbers are allowed." end elseif operator == '/' then if is_str_or_int(op1) and is_str_or_int(op2) then - op1 = tonumber(op1) / tonumber(op2) + local num1, num2 = tonumber(op1), tonumber(op2) + if not (num1 and num2) then + return nil, "Cannot perform arithmetic on non-numeric strings" + end + op1 = num1 / num2 else return nil, "Only operations on strings and numbers are allowed." end elseif operator == '%' then if is_str_or_int(op1) and is_str_or_int(op2) then - op1 = tonumber(op1) % tonumber(op2) + local num1, num2 = tonumber(op1), tonumber(op2) + if not (num1 and num2) then + return nil, "Cannot perform arithmetic on non-numeric strings" + end + op1 = num1 % num2 else return nil, "Only operations on strings and numbers are allowed." end - op1 = tonumber(op1) % tonumber(op2) elseif operator:upper() == 'AND' or operator == '&&' then op1 = notempty(op1) and notempty(op2) elseif operator:upper() == 'OR' or operator == '||' then diff --git a/test/test.lua b/test/test.lua index d392ec8..05aa4f2 100755 --- a/test/test.lua +++ b/test/test.lua @@ -1052,6 +1052,24 @@ testQuery = { lu.assertNil(err) lu.assertItemsEquals(result, {}) end, + + testFilterArithmeticOp = function () + local array = { + { id = 1, value = 0 }, + { id = 1, value = "a" }, + } + local result, err = jp.query(array, '$[?(@.value=="a"+"b")]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + + local result, err = jp.query(array, '$[?(@.value=="a"+null)]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + + local result, err = jp.query(array, '$[?(@.value=="a"+1)]') + lu.assertNil(err) + lu.assertItemsEquals(result, {}) + end, } From 20c71fe92098c9bf041b619c189b6a34993c9a82 Mon Sep 17 00:00:00 2001 From: Ilya Potemin Date: Thu, 5 Feb 2026 12:38:24 +0300 Subject: [PATCH 5/8] fix: separate operand cast strategies for different binary operator types Streamlines type cast logic depending on 4 different operator type - arithmetic, logical, equality and comparison --- jsonpath.lua | 410 ++++++++++++++++++++++++++++++++------------------ test/test.lua | 6 +- 2 files changed, 270 insertions(+), 146 deletions(-) diff --git a/jsonpath.lua b/jsonpath.lua index fdf8bda..6ef01d8 100755 --- a/jsonpath.lua +++ b/jsonpath.lua @@ -286,63 +286,270 @@ local jsonpath_grammer = (function() return jsonpath end)() +--- @alias Operator 1|2|3|4|5|6|7|8|9|10|11|12|13 +--- @alias OperatorType 1|2|3|4 + +--- @type Operator[] +local OPERATORS = { + --- Arithmetic operators + ADD = 1, + SUB = 2, + MUL = 3, + DIV = 4, + MOD = 5, + --- Logical operators + AND = 6, + OR = 7, + --- Equality + EQ = 8, + NEQ = 9, + --- Comparison + GT = 10, + GTE = 11, + LT = 12, + LTE = 13, +} + +local OPERATOR_TYPES = { + ARITHMETIC = 1, + LOGICAL = 2, + EQUALITY = 3, + COMPARISON = 4, +} + +--- @param op Operator|number +--- @return OperatorType|0 op_type +local function get_operator_type(op) + if op >= 1 and op <= 5 then + return OPERATOR_TYPES.ARITHMETIC + elseif op == 6 or op == 7 then + return OPERATOR_TYPES.LOGICAL + elseif op == 8 or op == 9 then + return OPERATOR_TYPES.EQUALITY + elseif op >= 10 and op <= 13 then + return OPERATOR_TYPES.COMPARISON + end + return 0 +end --- Helper: evaluate abstract syntax tree. Called recursively. -local function eval_ast(ast, obj) +--- @type { [Operator]: fun(any,any): any } +local OPERATORS_FN = { + --- Arithmetic + function(l, r) + return l + r + end, + function(l, r) + return l - r + end, + function(l, r) + return l * r + end, + function(l, r) + return l / r + end, + function(l, r) + return l % r + end, + --- Logic (boolean operands only) + function(l, r) + return l and r + end, + function(l, r) + return l or r + end, + --- Eq + function (l, r) + return l == r + end, + function (l, r) + return l ~= r + end, + --- Cmp + function (l, r) + return l > r + end, + function (l, r) + return l >= r + end, + function (l, r) + return l < r + end, + function (l, r) + return l <= r + end, +} + +--- @return Operator | 0 +local function parse_operator(op) + if op == '+' then + return OPERATORS.ADD + elseif op == '-' then + return OPERATORS.SUB + elseif op == '*' then + return OPERATORS.MUL + elseif op == '/' then + return OPERATORS.DIV + elseif op == '%' then + return OPERATORS.MOD + elseif op:upper() == 'AND' or op == '&&' then + return OPERATORS.AND + elseif op:upper() == 'OR' or op == '||' then + return OPERATORS.OR + elseif op == '=' or op == '==' then + return OPERATORS.EQ + elseif op == '<>' or op == '!=' then + return OPERATORS.NEQ + elseif op == '>' then + return OPERATORS.GT + elseif op == '>=' then + return OPERATORS.GTE + elseif op == '<' then + return OPERATORS.LT + elseif op == '<=' then + return OPERATORS.LTE + else + return 0 + end +end - -- Helper helper: match type of second operand to type of first operand - local function match_cmp_type(op1, op2, compare) - if type(op1) == 'boolean' then - if is_null(op2) then - return not op1, nil - else - if type(op2) == 'string' then - return nil, "cannot compare boolean with string" - end - if type(op2) == 'number' then - if compare then - return nil, "cannot compare boolean with number" - end - return op2 ~= 0, nil - end - if type(op2) == 'boolean' then - return op2, nil - end - return (op2 and true or false), nil +--- Computes type casts and executes binary operator +--- +--- @param op Operator Operator to execute +--- @param lval any Left value of binary operator +--- @param rval any Right value of binary operator +--- @param op_str string String representation of operator, used in error reporting +--- @return any|nil val Result value +--- @return nil|string err Error, if cast has failed +local function exec_binary_op(op, lval, rval, op_str) + local l_type = type(lval) + local r_type = type(rval) + local op_type = get_operator_type(op) + + -- convert these long int numbers to normal numbers + if l_type == 'cdata' and lval ~= NULL and tostring(ffi.typeof(lval)) == 'ctype' then + l_type = "number" + lval = tonumber(lval) + end + if r_type == 'cdata' and lval ~= NULL and tostring(ffi.typeof(rval)) == 'ctype' then + r_type = "number" + rval = tonumber(rval) + end + + if op_type == OPERATOR_TYPES.ARITHMETIC then + -- arithmetic ops allowed only on numbers + if l_type == "string" then + lval = tonumber(lval) + if lval == nil then + return nil, ("can not parse string lvalue as number for operation %s"):format(op_str) end - elseif type(op1) == 'number' then - if type(op2) == 'boolean' then - return op2 and 1 or 0, nil + elseif l_type ~= "number" then + return nil, ("lvalue is not a number for operation %s"):format(op_str) + end + if r_type == "string" then + rval = tonumber(rval) + if rval == nil then + return nil, ("can not parse string rvalue as number for operation %s"):format(op_str) end - if type(op2) == 'string' then - local num = tonumber(op2) - if num == nil then - return nil, "cannot compare number with non-numeric string" - end - return num, nil + elseif r_type ~= "number" then + return nil, ("rvalue is not a number for operation %s"):format(op_str) + end + elseif op_type == OPERATOR_TYPES.LOGICAL then + -- everything which is not null is a true boolean + if l_type ~= "boolean" then + lval = not is_null(lval) + end + if r_type ~= "boolean" then + rval = not is_null(rval) + end + elseif op_type == OPERATOR_TYPES.EQUALITY then + -- cast numbers and booleans to string, if other operand is string + if l_type == "string" and r_type == "number" then + r_type = "string" + rval = tostring(rval) + elseif l_type == "string" and r_type == "boolean" then + r_type = "string" + rval = tostring(rval) + end + if r_type == "string" and l_type == "number" then + l_type = "string" + lval = tostring(lval) + elseif r_type == "string" and l_type == "boolean" then + l_type = "string" + lval = tostring(lval) + end + + -- cast booleans as numbers + if l_type == "number" and r_type == "boolean" then + r_type = "number" + rval = rval and 1 or 0 + end + if r_type == "number" and l_type == "boolean" then + l_type = "number" + lval = lval and 1 or 0 + end + + -- special comparisons when lvalue or rvalue is null + local lval_is_null, rval_is_null = is_null(lval), is_null(rval) + if lval_is_null and rval_is_null then + -- null == null -> true + return op == OPERATORS.EQ + end + if rval_is_null or lval_is_null then + -- something == null -> false + -- something != null -> true + -- null == something -> false + -- null != something -> true + return not (op == OPERATORS.EQ) + end + + -- bypass default operator functions for non-matching types + if l_type ~= r_type and op == OPERATORS.EQ then + -- values of different types are never equal + return false, nil + elseif l_type ~= r_type and op == OPERATORS.NEQ then + -- values of different types are always not equal + return true, nil + end + elseif op_type == OPERATOR_TYPES.COMPARISON then + -- allow to compare numbers with booleans + if l_type == "number" and r_type == "boolean" then + r_type = "number" + rval = rval and 1 or 0 + end + if r_type == "number" and l_type == "boolean" then + l_type = "number" + lval = lval and 1 or 0 + end + + -- try to parse string as number, if other operand is number + if l_type == "number" and r_type == "string" then + local num_rval = tonumber(rval) + if num_rval ~= nil then + r_type = "number" + rval = num_rval end - return tonumber(op2), nil - elseif type(op1) == 'cdata' and tostring(ffi.typeof(op1)) == 'ctype' then - return tonumber(op2), nil - elseif is_null(op1) then - if compare then - return nil, "cannot compare null with other values" + end + if r_type == "number" and l_type == "string" then + local num_lval = tonumber(lval) + if num_lval ~= nil then + l_type = "number" + lval = num_lval end - return op2, nil end - return tostring(op2 or ''), nil - end - -- Helper helper: convert operand to boolean - local function notempty(op1) - return op1 and true or false + -- must be the same type + if l_type ~= r_type then + return nil, ("can not apply %s on types %s and %s"):format(op_str, l_type, r_type) + end + else + return nil, ("unknown operator %s"):format(op_str) end - local function is_str_or_int(val) - return type(val) == 'string' or - type(val) == 'number' or - (type(val) == 'cdata' and tostring(ffi.typeof(val)) == 'ctype') - end + return OPERATORS_FN[op](lval, rval), nil +end + +-- Helper: evaluate abstract syntax tree. Called recursively. +local function eval_ast(ast, obj) -- Helper helper: evaluate variable expression inside abstract syntax tree local function eval_var(expr, obj) @@ -429,107 +636,24 @@ local function eval_ast(ast, obj) return nil, err end for i = 3, #expr, 2 do - local operator = expr[i] - if operator == nil then + local op_str = expr[i] + if op_str == nil then return nil, 'missing expression operator' end - local op2, err = eval_ast(expr[i + 1], obj) + local op2, eval_err = eval_ast(expr[i + 1], obj) if is_nil(op2) then - return nil, err + return nil, eval_err end - if operator == '+' then - if is_str_or_int(op1) and is_str_or_int(op2) then - local num1, num2 = tonumber(op1), tonumber(op2) - if not (num1 and num2) then - return nil, "Cannot perform arithmetic on non-numeric strings" - end - op1 = num1 + num2 - else - return nil, "Only operations on strings and numbers are allowed." - end - elseif operator == '-' then - if is_str_or_int(op1) and is_str_or_int(op2) then - local num1, num2 = tonumber(op1), tonumber(op2) - if not (num1 and num2) then - return nil, "Cannot perform arithmetic on non-numeric strings" - end - op1 = num1 - num2 - else - return nil, "Only operations on strings and numbers are allowed." - end - elseif operator == '*' then - if is_str_or_int(op1) and is_str_or_int(op2) then - local num1, num2 = tonumber(op1), tonumber(op2) - if not (num1 and num2) then - return nil, "Cannot perform arithmetic on non-numeric strings" - end - op1 = num1 * num2 - else - return nil, "Only operations on strings and numbers are allowed." - end - elseif operator == '/' then - if is_str_or_int(op1) and is_str_or_int(op2) then - local num1, num2 = tonumber(op1), tonumber(op2) - if not (num1 and num2) then - return nil, "Cannot perform arithmetic on non-numeric strings" - end - op1 = num1 / num2 - else - return nil, "Only operations on strings and numbers are allowed." - end - elseif operator == '%' then - if is_str_or_int(op1) and is_str_or_int(op2) then - local num1, num2 = tonumber(op1), tonumber(op2) - if not (num1 and num2) then - return nil, "Cannot perform arithmetic on non-numeric strings" - end - op1 = num1 % num2 - else - return nil, "Only operations on strings and numbers are allowed." - end - elseif operator:upper() == 'AND' or operator == '&&' then - op1 = notempty(op1) and notempty(op2) - elseif operator:upper() == 'OR' or operator == '||' then - op1 = notempty(op1) or notempty(op2) - elseif operator == '=' or operator == '==' then - local op2, err = match_cmp_type(op1, op2, false) - if err then - return nil, err - end - op1 = op1 == op2 - elseif operator == '<>' or operator == '!=' then - local op2, err = match_cmp_type(op1, op2, false) - if err then - return nil, err - end - op1 = op1 ~= op2 - elseif operator == '>' then - local op2, err = match_cmp_type(op1, op2, true) - if err then - return nil, err - end - op1 = op1 > op2 - elseif operator == '>=' then - local op2, err = match_cmp_type(op1, op2, true) - if err then - return nil, err - end - op1 = op1 >= op2 - elseif operator == '<' then - local op2, err = match_cmp_type(op1, op2, true) - if err then - return nil, err - end - op1 = op1 < op2 - elseif operator == '<=' then - local op2, err = match_cmp_type(op1, op2, true) - if err then - return nil, err - end - op1 = op1 <= op2 - else - return nil, 'unknown expression operator "' .. operator .. '"' + local op = parse_operator(op_str) + if op == 0 then + return nil, "unknown operator" + end + --- @cast op Operator + local result, cast_err = exec_binary_op(op, op1, op2, op_str) + if cast_err ~= nil then + return nil, cast_err end + op1 = result end return op1 end diff --git a/test/test.lua b/test/test.lua index 05aa4f2..84416a0 100755 --- a/test/test.lua +++ b/test/test.lua @@ -989,15 +989,15 @@ testQuery = { local result, err = jp.query(array, '$[?(@.value>=1)]') lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertItemsEquals(result, { array[1] }) local result, err = jp.query(array, '$[?(@.value<1)]') lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertItemsEquals(result, { array[2] }) local result, err = jp.query(array, '$[?(@.value<=1)]') lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertItemsEquals(result, { array[1], array[2] }) end, testFilterBoolStrComparison = function () From 18a75dc25e74a880f07d57499f37d8243af959c5 Mon Sep 17 00:00:00 2001 From: "d.belincev" Date: Thu, 29 Jan 2026 09:09:04 +0300 Subject: [PATCH 6/8] fix: convert errors, return error from match_cmp_type --- jsonpath.lua | 120 +++++++++++++++++++++++++++++++++++++++----------- test/test.lua | 46 +++++++++---------- 2 files changed, 118 insertions(+), 48 deletions(-) diff --git a/jsonpath.lua b/jsonpath.lua index 6ef01d8..a0e56a3 100755 --- a/jsonpath.lua +++ b/jsonpath.lua @@ -100,6 +100,18 @@ ]]-- local M = {} +local codes = { + SUCCESS = 200, + BAD_REQUEST = 400, + NOT_FOUND = 404, + INTERNAL_ERR = 500, +} + +local errors = require('errors') + +local JsonPathError = errors.new_class("JsonPathError") +local JsonPathNotFoundError = errors.new_class("JsonPathNotFoundError") + local ffi = require('ffi') -- Use Roberto Ierusalimschy's fabulous LulPeg pattern-matching library @@ -554,10 +566,12 @@ local function eval_ast(ast, obj) -- Helper helper: evaluate variable expression inside abstract syntax tree local function eval_var(expr, obj) if obj == nil then - return nil, 'object is not set' + return nil, Bad_request_error('object is not set') end if type(obj) ~= "table" then - return nil, 'object is primitive' + local err = JsonPathNotFoundError:new('object is primitive') + err.rc = codes.NOT_FOUND + return nil, err end for i = 2, #expr do -- [1] is "var" @@ -568,7 +582,9 @@ local function eval_ast(ast, obj) member = type(member) == 'number' and member + 1 or member obj = obj[member] if is_nil(obj) then - return nil, 'object doesn\'t contain an object or attribute "' .. member .. '"' + local err = JsonPathNotFoundError:new('object doesn\'t contain an object or attribute "'.. member ..'"') + err.rc = codes.NOT_FOUND + return nil, err end end return obj @@ -585,7 +601,10 @@ local function eval_ast(ast, obj) local function eval_union(expr, obj) local matches = {} -- [1] is "union" for i = 2, #expr do - local result = eval_ast(expr[i], obj) + local result, err = eval_ast(expr[i], obj) + if err then + return nil, err + end if type(result) == 'table' then for _, j in ipairs(result) do table.insert(matches, j) @@ -599,16 +618,31 @@ local function eval_ast(ast, obj) -- Helper helper: evaluate 'filter' expression inside abstract syntax tree local function eval_filter(expr, obj) - return eval_ast(expr[2], obj) and true or false + local result, err = eval_ast(expr[2], obj) + if err then + if err.rc == codes.NOT_FOUND then + return false + end + return nil, err + end + return result and true or false end -- Helper helper: evaluate 'slice' expression inside abstract syntax tree local function eval_slice(expr, obj) local matches = {} -- [1] is "slice" if #expr == 4 then - local from = tonumber(eval_ast(expr[2], obj)) - local to = tonumber(eval_ast(expr[3], obj)) - local step = tonumber(eval_ast(expr[4], obj)) + local from_result, err = eval_ast(expr[2], obj) + if err then return nil, err end + local to_result, err = eval_ast(expr[3], obj) + if err then return nil, err end + local step_result, err = eval_ast(expr[4], obj) + if err then return nil, err end + + local from = tonumber(from_result) + local to = tonumber(to_result) + local step = tonumber(step_result) + if (from == nil) or (from < 0) or (to == nil) or (to < 0) then local len = eval_var_length(obj) if from == nil then @@ -672,8 +706,7 @@ local function eval_ast(ast, obj) elseif ast[1] == 'filter' then return eval_filter(ast, obj) elseif ast[1] == 'slice' then - local result = eval_slice(ast, obj) - return result + return eval_slice(ast, obj) end return 0 @@ -705,7 +738,10 @@ local function match_path(ast, path, parent, obj) end elseif ast_spec[1] == 'union' or ast_spec[1] == 'slice' then -- match union or slice expression (on parent object) - local matches = eval_ast(ast_spec, parent) + local matches, err = eval_ast(ast_spec, parent) + if err then + return nil, err + end --- @cast matches table[] for _, i in pairs(matches) do match_component = tostring(i) == tostring(component) @@ -715,7 +751,16 @@ local function match_path(ast, path, parent, obj) end elseif ast_spec[1] == 'filter' then -- match filter expression - match_component = eval_ast(ast_spec, obj) and true or false + local filter_result, err = eval_ast(ast_spec, obj) + if err then + if err.rc == codes.NOT_FOUND then + match_component = false + else + return nil, err + end + else + match_component = filter_result and true or false + end end else if ast_spec == '*' then @@ -734,7 +779,16 @@ local function match_path(ast, path, parent, obj) if path_index == #path and ast_spec ~= "array" and match_component then local _, next_ast_spec = next(ast, ast_key) if next_ast_spec ~= nil and next_ast_spec[1] == 'filter' then - match_component = eval_ast(next_ast_spec, obj) and true or false + local filter_result, err = eval_ast(next_ast_spec, obj) + if err then + if err.rc == codes.NOT_FOUND then + match_component = false + else + return nil, err + end + else + match_component = filter_result and true or false + end ast_key, ast_spec = ast_iter(ast, ast_key) end end @@ -769,7 +823,10 @@ end local function match_tree(nodes, ast, path, parent, obj, count) -- Try to match every node against AST - local match = match_path(ast, path, parent, obj) + local match, err = match_path(ast, path, parent, obj) + if err then + return err + end if match == MATCH_ONE or match == MATCH_DESCENDANTS then -- This node matches. Add path and value to result -- (if max result count not yet reached) @@ -792,7 +849,10 @@ local function match_tree(nodes, ast, path, parent, obj, count) table.insert(path1, p) end table.insert(path1, type(key) == 'string' and key or (key - 1)) - match_tree(nodes, ast, path1, obj, child, count) + local err = match_tree(nodes, ast, path1, obj, child, count) + if err then + return err + end end end end @@ -818,15 +878,15 @@ end -- function M.parse(expr) if expr == nil or type(expr) ~= 'string' then - return nil, "missing or invalid 'expr' argument" + return nil, Bad_request_error("missing or invalid 'expr' argument") end local ast = Ct(jsonpath_grammer * Cp()):match(expr) if ast == nil or #ast ~= 2 then - return nil, 'invalid expression "' .. expr .. '"' + return nil, Bad_request_error('invalid expression "' .. expr .. '"') end if ast[2] ~= #expr + 1 then - return nil, 'invalid expression "' .. expr .. '" near "' .. expr:sub(ast[2]) .. '"' + return nil, Bad_request_error('invalid expression "' .. expr .. '" near "' .. expr:sub(ast[2]) .. '"') end return ast[1] end @@ -850,13 +910,16 @@ end -- function M.nodes(obj, expr, count) if obj == nil or type(obj) ~= 'table' then - return nil, "missing or invalid 'obj' argument" + local err = Bad_request_error("missing or invalid 'obj' argument") + return nil, err end if expr == nil or (type(expr) ~= 'string' and type(expr) ~= 'table') then - return nil, "missing or invalid 'expr' argument" + local err = Bad_request_error("missing or invalid 'expr' argument") + return nil, err end if count ~= nil and type(count) ~= 'number' then - return nil, "invalid 'count' argument" + local err = Bad_request_error("invalid 'count' argument") + return nil, err end local ast, err @@ -868,7 +931,11 @@ function M.nodes(obj, expr, count) ast = expr end if ast == nil then - return nil, err or 'internal error' + if not err then + local err = JsonPathError:new("internal error") + err.rc = codes.INTERNAL_ERR + end + return nil, err end if count ~= nil and count == 0 then @@ -885,8 +952,10 @@ function M.nodes(obj, expr, count) end local matches = {} - match_tree(matches, ast, { '$' }, {}, obj, count) - + local err = match_tree(matches, ast, { '$' }, {}, obj, count) + if err then + return nil, err + end -- Sort results by path local sorted = {} for p, v in pairs(matches) do @@ -938,7 +1007,8 @@ function M.value(obj, expr, count) return nodes[1].value end - return nil, 'no element matching expression' + local err = Bad_request_error('no element matching expression') + return nil, err end diff --git a/test/test.lua b/test/test.lua index 84416a0..422d9fe 100755 --- a/test/test.lua +++ b/test/test.lua @@ -826,8 +826,8 @@ testQuery = { } local result, err = jp.query(data, "$..photo[?(@.size>'400')]") - lu.assertItemsEquals(result, {}) lu.assertNil(err) + lu.assertItemsEquals(result, {}) end, testFilterNull = function() @@ -984,8 +984,8 @@ testQuery = { lu.assertItemsEquals(result, { array[1] }) local result, err = jp.query(array, '$[?(@.value>1)]') - lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertError(err) + lu.assertItemsEquals(result, nil) local result, err = jp.query(array, '$[?(@.value>=1)]') lu.assertNil(err) @@ -1006,24 +1006,24 @@ testQuery = { { id = 2, value = false }, } local result, err = jp.query(array, '$[?(@.value=="1")]') - lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertError(err) + lu.assertItemsEquals(result, nil) local result, err = jp.query(array, '$[?(@.value>"1")]') - lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertError(err) + lu.assertItemsEquals(result, nil) local result, err = jp.query(array, '$[?(@.value>="1")]') - lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertError(err) + lu.assertItemsEquals(result, nil) local result, err = jp.query(array, '$[?(@.value<"1")]') - lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertError(err) + lu.assertItemsEquals(result, nil) local result, err = jp.query(array, '$[?(@.value<="1")]') - lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertError(err) + lu.assertItemsEquals(result, nil) end, testFilterArithmeticOpOnBool = function () @@ -1033,24 +1033,24 @@ testQuery = { { id = 2, value = 2 }, } local result, err = jp.query(array, '$[?(@.value==true+1)]') - lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertError(err) + lu.assertItemsEquals(result, nil) local result, err = jp.query(array, '$[?(@.value==true*1)]') - lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertError(err) + lu.assertItemsEquals(result, nil) local result, err = jp.query(array, '$[?(@.value==true/1)]') - lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertError(err) + lu.assertItemsEquals(result, nil) local result, err = jp.query(array, '$[?(@.value==true%1)]') - lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertError(err) + lu.assertItemsEquals(result, nil) local result, err = jp.query(array, '$[?(@.value<>false+1)]') - lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertError(err) + lu.assertItemsEquals(result, nil) end, testFilterArithmeticOp = function () From 43c747dd2219fbab194cba6a88d54f312d7af532 Mon Sep 17 00:00:00 2001 From: "d.belincev" Date: Mon, 2 Feb 2026 15:30:21 +0300 Subject: [PATCH 7/8] fix: naming, add helper for not_found, delete code 200 --- jsonpath.lua | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/jsonpath.lua b/jsonpath.lua index a0e56a3..1af5f3d 100755 --- a/jsonpath.lua +++ b/jsonpath.lua @@ -101,7 +101,6 @@ local M = {} local codes = { - SUCCESS = 200, BAD_REQUEST = 400, NOT_FOUND = 404, INTERNAL_ERR = 500, @@ -566,12 +565,10 @@ local function eval_ast(ast, obj) -- Helper helper: evaluate variable expression inside abstract syntax tree local function eval_var(expr, obj) if obj == nil then - return nil, Bad_request_error('object is not set') + return nil, bad_request_error('object is not set') end if type(obj) ~= "table" then - local err = JsonPathNotFoundError:new('object is primitive') - err.rc = codes.NOT_FOUND - return nil, err + return nil, not_found_error('object is primitive') end for i = 2, #expr do -- [1] is "var" @@ -582,9 +579,7 @@ local function eval_ast(ast, obj) member = type(member) == 'number' and member + 1 or member obj = obj[member] if is_nil(obj) then - local err = JsonPathNotFoundError:new('object doesn\'t contain an object or attribute "'.. member ..'"') - err.rc = codes.NOT_FOUND - return nil, err + return nil, not_found_error('object doesn\'t contain an object or attribute "'.. member ..'"') end end return obj @@ -878,15 +873,15 @@ end -- function M.parse(expr) if expr == nil or type(expr) ~= 'string' then - return nil, Bad_request_error("missing or invalid 'expr' argument") + return nil, bad_request_error("missing or invalid 'expr' argument") end local ast = Ct(jsonpath_grammer * Cp()):match(expr) if ast == nil or #ast ~= 2 then - return nil, Bad_request_error('invalid expression "' .. expr .. '"') + return nil, bad_request_error('invalid expression "' .. expr .. '"') end if ast[2] ~= #expr + 1 then - return nil, Bad_request_error('invalid expression "' .. expr .. '" near "' .. expr:sub(ast[2]) .. '"') + return nil, bad_request_error('invalid expression "' .. expr .. '" near "' .. expr:sub(ast[2]) .. '"') end return ast[1] end @@ -910,15 +905,15 @@ end -- function M.nodes(obj, expr, count) if obj == nil or type(obj) ~= 'table' then - local err = Bad_request_error("missing or invalid 'obj' argument") + local err = bad_request_error("missing or invalid 'obj' argument") return nil, err end if expr == nil or (type(expr) ~= 'string' and type(expr) ~= 'table') then - local err = Bad_request_error("missing or invalid 'expr' argument") + local err = bad_request_error("missing or invalid 'expr' argument") return nil, err end if count ~= nil and type(count) ~= 'number' then - local err = Bad_request_error("invalid 'count' argument") + local err = bad_request_error("invalid 'count' argument") return nil, err end @@ -1007,7 +1002,7 @@ function M.value(obj, expr, count) return nodes[1].value end - local err = Bad_request_error('no element matching expression') + local err = bad_request_error('no element matching expression') return nil, err end From 2b47ca543cc368e66c739ce0058ecedb9b9fb9f0 Mon Sep 17 00:00:00 2001 From: "d.belincev" Date: Mon, 2 Feb 2026 17:27:13 +0300 Subject: [PATCH 8/8] fix: convert error --- jsonpath.lua | 50 ++++++++++++++++++++++++++++++++------------------ test/test.lua | 23 ++++++++++++----------- 2 files changed, 44 insertions(+), 29 deletions(-) diff --git a/jsonpath.lua b/jsonpath.lua index 1af5f3d..2953de4 100755 --- a/jsonpath.lua +++ b/jsonpath.lua @@ -297,6 +297,24 @@ local jsonpath_grammer = (function() return jsonpath end)() +local function bad_request_error(err) + local err = JsonPathError:new(err) + err.rc = codes.BAD_REQUEST + return err +end + +local function not_found_error(err) + local err = JsonPathNotFoundError:new(err) + err.rc = codes.NOT_FOUND + return err +end + +local function internal_error(err) + local err = JsonPathNotFoundError:new(err) + err.rc = codes.INTERNAL_ERR + return err +end + --- @alias Operator 1|2|3|4|5|6|7|8|9|10|11|12|13 --- @alias OperatorType 1|2|3|4 @@ -451,18 +469,19 @@ local function exec_binary_op(op, lval, rval, op_str) if l_type == "string" then lval = tonumber(lval) if lval == nil then - return nil, ("can not parse string lvalue as number for operation %s"):format(op_str) + return nil, bad_request_error(("can not parse string lvalue as number for operation %s"):format(op_str)) end elseif l_type ~= "number" then - return nil, ("lvalue is not a number for operation %s"):format(op_str) + return nil, bad_request_error(("lvalue is not a number for operation %s"):format(op_str)) end if r_type == "string" then rval = tonumber(rval) if rval == nil then - return nil, ("can not parse string rvalue as number for operation %s"):format(op_str) + return nil, + bad_request_error(("can not parse string rvalue as number for operation %s"):format(op_str)) end elseif r_type ~= "number" then - return nil, ("rvalue is not a number for operation %s"):format(op_str) + return nil, bad_request_error(("rvalue is not a number for operation %s"):format(op_str)) end elseif op_type == OPERATOR_TYPES.LOGICAL then -- everything which is not null is a true boolean @@ -550,10 +569,10 @@ local function exec_binary_op(op, lval, rval, op_str) -- must be the same type if l_type ~= r_type then - return nil, ("can not apply %s on types %s and %s"):format(op_str, l_type, r_type) + return nil, bad_request_error(("can not apply %s on types %s and %s"):format(op_str, l_type, r_type)) end else - return nil, ("unknown operator %s"):format(op_str) + return nil, bad_request_error(("unknown operator %s"):format(op_str)) end return OPERATORS_FN[op](lval, rval), nil @@ -667,7 +686,7 @@ local function eval_ast(ast, obj) for i = 3, #expr, 2 do local op_str = expr[i] if op_str == nil then - return nil, 'missing expression operator' + return nil, bad_request_error('missing expression operator') end local op2, eval_err = eval_ast(expr[i + 1], obj) if is_nil(op2) then @@ -675,7 +694,7 @@ local function eval_ast(ast, obj) end local op = parse_operator(op_str) if op == 0 then - return nil, "unknown operator" + return nil, bad_request_error("unknown operator") end --- @cast op Operator local result, cast_err = exec_binary_op(op, op1, op2, op_str) @@ -905,16 +924,13 @@ end -- function M.nodes(obj, expr, count) if obj == nil or type(obj) ~= 'table' then - local err = bad_request_error("missing or invalid 'obj' argument") - return nil, err + return nil, bad_request_error("missing or invalid 'obj' argument") end if expr == nil or (type(expr) ~= 'string' and type(expr) ~= 'table') then - local err = bad_request_error("missing or invalid 'expr' argument") - return nil, err + return nil, bad_request_error("missing or invalid 'expr' argument") end if count ~= nil and type(count) ~= 'number' then - local err = bad_request_error("invalid 'count' argument") - return nil, err + return nil, bad_request_error("invalid 'count' argument") end local ast, err @@ -927,8 +943,7 @@ function M.nodes(obj, expr, count) end if ast == nil then if not err then - local err = JsonPathError:new("internal error") - err.rc = codes.INTERNAL_ERR + err = internal_error("internal error") end return nil, err end @@ -1002,8 +1017,7 @@ function M.value(obj, expr, count) return nodes[1].value end - local err = bad_request_error('no element matching expression') - return nil, err + return nil, bad_request_error('no element matching expression') end diff --git a/test/test.lua b/test/test.lua index 422d9fe..c9f3016 100755 --- a/test/test.lua +++ b/test/test.lua @@ -985,7 +985,7 @@ testQuery = { local result, err = jp.query(array, '$[?(@.value>1)]') lu.assertError(err) - lu.assertItemsEquals(result, nil) + lu.assertItemsEquals(result, {}) local result, err = jp.query(array, '$[?(@.value>=1)]') lu.assertNil(err) @@ -999,15 +999,16 @@ testQuery = { lu.assertNil(err) lu.assertItemsEquals(result, { array[1], array[2] }) end, - + testFilterBoolStrComparison = function () local array = { { id = 1, value = true }, { id = 2, value = false }, } local result, err = jp.query(array, '$[?(@.value=="1")]') - lu.assertError(err) - lu.assertItemsEquals(result, nil) + -- lu.assertError(err) + lu.assertNil(err) + lu.assertItemsEquals(result, {}) local result, err = jp.query(array, '$[?(@.value>"1")]') lu.assertError(err) @@ -1053,22 +1054,22 @@ testQuery = { lu.assertItemsEquals(result, nil) end, - testFilterArithmeticOp = function () + testFilterArithmeticOponStr = function () local array = { { id = 1, value = 0 }, { id = 1, value = "a" }, } local result, err = jp.query(array, '$[?(@.value=="a"+"b")]') - lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertError(err) + lu.assertItemsEquals(result, nil) local result, err = jp.query(array, '$[?(@.value=="a"+null)]') - lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertError(err) + lu.assertItemsEquals(result, nil) local result, err = jp.query(array, '$[?(@.value=="a"+1)]') - lu.assertNil(err) - lu.assertItemsEquals(result, {}) + lu.assertError(err) + lu.assertItemsEquals(result, nil) end, }