Skip to content

Commit bd225c7

Browse files
committed
Add optional args parameter to shortcut functions
compile() supports optional args parameter. Add it to shortcut functions as well.
1 parent b345d2c commit bd225c7

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ Convenience functions are available to get the output for a program and input in
195195
196196
assert jq.first(".[] + 1", [1, 2, 3]) == 2
197197
assert jq.first(".[] + 1", text="[1, 2, 3]") == 2
198+
assert jq.first(".[] + $addend", [1, 2, 3], args={"addend": 1}) == 2
198199
assert jq.text(".[] + 1", [1, 2, 3]) == "2\n3\n4"
199200
assert jq.all(".[] + 1", [1, 2, 3]) == [2, 3, 4]
200201
assert list(jq.iter(".[] + 1", [1, 2, 3])) == [2, 3, 4]

jq.pyx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,23 +394,23 @@ cdef class _ResultIterator(object):
394394
raise StopIteration()
395395

396396

397-
def all(program, value=_NO_VALUE, text=_NO_VALUE):
398-
return compile(program).input(value, text=text).all()
397+
def all(program, value=_NO_VALUE, text=_NO_VALUE, args=None):
398+
return compile(program, args=args).input(value, text=text).all()
399399

400400

401-
def first(program, value=_NO_VALUE, text=_NO_VALUE):
402-
return compile(program).input(value, text=text).first()
401+
def first(program, value=_NO_VALUE, text=_NO_VALUE, args=None):
402+
return compile(program, args=args).input(value, text=text).first()
403403

404404

405405
_iter = iter
406406

407407

408-
def iter(program, value=_NO_VALUE, text=_NO_VALUE):
409-
return _iter(compile(program).input(value, text=text))
408+
def iter(program, value=_NO_VALUE, text=_NO_VALUE, args=None):
409+
return _iter(compile(program, args=args).input(value, text=text))
410410

411411

412-
def text(program, value=_NO_VALUE, text=_NO_VALUE):
413-
return compile(program).input(value, text=text).text()
412+
def text(program, value=_NO_VALUE, text=_NO_VALUE, args=None):
413+
return compile(program, args=args).input(value, text=text).text()
414414

415415

416416
# Support the 0.1.x API for backwards compatibility

tests/jq_tests.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,21 @@ def test_iter_function_with_json_text_input_returns_all_output_element_in_iterat
431431
assert_equal(3, next(iterator))
432432
assert_equal(4, next(iterator))
433433
assert_equal("end", next(iterator, "end"))
434+
435+
def test_all_function_with_args_should_set_predefined_variables(self):
436+
output = jq.first("$a + $b + .", 3, args={"a": 100, "b": 20})
437+
438+
assert_equal(123, output)
439+
440+
def test_first_function_with_args_should_set_predefined_variables(self):
441+
output = jq.first('.m = "a\\($x)c"', {"n": 1}, args=dict(x="b", m="z"))
442+
443+
assert_equal({"m": "abc", "n": 1}, output)
444+
445+
def test_iter_function_with_args_should_set_predefined_variables(self):
446+
iterator = jq.iter(". * $add | .[]", {"a": 1, "b": 2}, args={"add": {"b": 3, "c": 4}})
447+
448+
assert_equal(1, next(iterator))
449+
assert_equal(3, next(iterator))
450+
assert_equal(4, next(iterator))
451+
assert_equal("end", next(iterator, "end"))

0 commit comments

Comments
 (0)