Skip to content

Commit

Permalink
fix: make scan interface backwards-compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
uvlad7 committed Dec 26, 2024
1 parent eb00854 commit 0e467cb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
8 changes: 5 additions & 3 deletions ext/json_scanner/json_scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ static yajl_callbacks scan_callbacks = {
// allow_comments, dont_validate_strings, allow_trailing_garbage, allow_multiple_values, allow_partial_values
VALUE scan(int argc, VALUE *argv, VALUE self)
{
VALUE json_str, path_ary, kwargs;
VALUE json_str, path_ary, with_path_flag, kwargs;
VALUE kwargs_values[7];

int with_path = false, verbose_error = false;
Expand All @@ -508,10 +508,12 @@ VALUE scan(int argc, VALUE *argv, VALUE self)
// Turned out callbacks can't raise exceptions
// VALUE callback_err;
#if RUBY_API_VERSION_MAJOR > 2 || (RUBY_API_VERSION_MAJOR == 2 && RUBY_API_VERSION_MINOR >= 7)
rb_scan_args_kw(RB_SCAN_ARGS_LAST_HASH_KEYWORDS, argc, argv, "2:", &json_str, &path_ary, &kwargs);
rb_scan_args_kw(RB_SCAN_ARGS_LAST_HASH_KEYWORDS, argc, argv, "21:", &json_str, &path_ary, &with_path_flag, &kwargs);
#else
rb_scan_args(argc, argv, "2:", &json_str, &path_ary, &kwargs);
rb_scan_args(argc, argv, "21:", &json_str, &path_ary, &with_path_flag, &kwargs);
#endif
// rb_io_write(rb_stderr, rb_sprintf("with_path_flag: %" PRIsVALUE " \n", with_path_flag));
with_path = RTEST(with_path_flag);
if (kwargs != Qnil)
{
rb_get_kwargs(kwargs, scan_kwargs_table, 0, 7, kwargs_values);
Expand Down
35 changes: 29 additions & 6 deletions spec/json_scanner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,46 @@
end

it "allows to return an actual path to the element" do
with_path_expected_res = [
# result for first mathcer, each element array of two items:
# array of path elements and 3-element array start,end,type
[[[0], [1, 6, :array]], [[1], [7, 12, :array]]],
[
[[0, 0], [2, 3, :number]], [[0, 1], [4, 5, :number]],
[[1, 0], [8, 9, :number]], [[1, 1], [10, 11, :number]],
],
]
params = [
"[[1,2],[3,4]]",
[
[described_class::ANY_INDEX],
[described_class::ANY_INDEX, described_class::ANY_INDEX],
],
]
expect(described_class.scan(*params, with_path: true)).to eq(with_path_expected_res)
expect(described_class.scan(*params, true)).to eq(with_path_expected_res)
expect(
described_class.scan(*params, false, with_path: true),
).to eq(with_path_expected_res)
end

it "ignores reqular flag if kwarg is given" do
expect(
described_class.scan(
"[[1,2],[3,4]]",
[
[described_class::ANY_INDEX],
[described_class::ANY_INDEX, described_class::ANY_INDEX],
],
with_path: true,
true, with_path: false,
),
).to eq(
[
# result for first mathcer, each element array of two items:
# array of path elements and 3-element array start,end,type
[[[0], [1, 6, :array]], [[1], [7, 12, :array]]],
# result for first mathcer, each element 3-element array start,end,type
[[1, 6, :array], [7, 12, :array]],
[
[[0, 0], [2, 3, :number]], [[0, 1], [4, 5, :number]],
[[1, 0], [8, 9, :number]], [[1, 1], [10, 11, :number]],
[2, 3, :number], [4, 5, :number],
[8, 9, :number], [10, 11, :number],
],
],
)
Expand Down

0 comments on commit 0e467cb

Please sign in to comment.