Skip to content

Commit 9192050

Browse files
committed
impl inspect, ref
1 parent 23d35df commit 9192050

File tree

5 files changed

+72
-6
lines changed

5 files changed

+72
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ You can also create a config and reuse it
6262
require "json_scanner"
6363

6464
config = JsonScanner::Config.new([[], ["key"], [(0..-1)]])
65+
# => #<JsonScanner::Config [[], ['key'], [(0..9223372036854775807)]]>
6566
JsonScanner.scan('{"key": "42"}', config)
6667
# => [[[0, 13, :object]], [[8, 12, :string]], []]
6768
JsonScanner.scan('{"key": "42"}', config, with_path: true)

bin/console

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
#!/usr/bin/env ruby
22
# frozen_string_literal: true
33

4+
# Disable 'extensions are not built' warning
5+
module AnnoyingGemWarningSilencer
6+
def contains_requirable_file?(file)
7+
return super unless source.is_a?(Bundler::Source::Gemspec)
8+
return false if respond_to?(:ignored?) && ignored?
9+
return false if missing_extensions?
10+
11+
super
12+
end
13+
end
14+
15+
Gem::BasicSpecification.prepend(AnnoyingGemWarningSilencer)
16+
417
require "bundler/setup"
518
require "json_scanner"
619

ext/json_scanner/json_scanner.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,43 @@ VALUE config_m_initialize(VALUE self, VALUE path_ary)
651651
return self;
652652
}
653653

654+
VALUE config_m_inspect(VALUE self)
655+
{
656+
scan_ctx *ctx;
657+
VALUE res;
658+
TypedData_Get_Struct(self, scan_ctx, &config_type, ctx);
659+
res = rb_sprintf("#<%" PRIsVALUE " [", rb_class_name(CLASS_OF(self)));
660+
for (int i = 0; ctx->paths && i < ctx->paths_len; i++)
661+
{
662+
rb_str_cat_cstr(res, "[");
663+
for (int j = 0; j < ctx->paths[i].len; j++)
664+
{
665+
switch (ctx->paths[i].elems[j].type)
666+
{
667+
case MATCHER_KEY:
668+
rb_str_catf(res, "'%.*s'", (int)ctx->paths[i].elems[j].value.key.len, ctx->paths[i].elems[j].value.key.val);
669+
break;
670+
case MATCHER_INDEX:
671+
rb_str_catf(res, "%ld", ctx->paths[i].elems[j].value.index);
672+
break;
673+
case MATCHER_INDEX_RANGE:
674+
rb_str_catf(res, "(%ld..%ld)", ctx->paths[i].elems[j].value.range.start, ctx->paths[i].elems[j].value.range.end);
675+
break;
676+
case MATCHER_ANY_KEY:
677+
rb_str_cat_cstr(res, "('*'..'*')");
678+
break;
679+
}
680+
if (j < ctx->paths[i].len - 1)
681+
rb_str_cat_cstr(res, ", ");
682+
}
683+
rb_str_cat_cstr(res, "]");
684+
if (i < ctx->paths_len - 1)
685+
rb_str_cat_cstr(res, ", ");
686+
}
687+
rb_str_cat_cstr(res, "]>");
688+
return res;
689+
}
690+
654691
static yajl_callbacks scan_callbacks = {
655692
scan_on_null,
656693
scan_on_boolean,
@@ -785,6 +822,7 @@ Init_json_scanner(void)
785822
rb_cJsonScannerConfig = rb_define_class_under(rb_mJsonScanner, "Config", rb_cObject);
786823
rb_define_alloc_func(rb_cJsonScannerConfig, config_alloc);
787824
rb_define_method(rb_cJsonScannerConfig, "initialize", config_m_initialize, 1);
825+
rb_define_method(rb_cJsonScannerConfig, "inspect", config_m_inspect, 0);
788826
rb_define_const(rb_mJsonScanner, "ANY_INDEX", rb_range_new(INT2FIX(0), INT2FIX(-1), false));
789827
any_key_sym = rb_id2sym(rb_intern("*"));
790828
rb_define_const(rb_mJsonScanner, "ANY_KEY", rb_range_new(any_key_sym, any_key_sym, false));

spec/extensiontesttask.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ def includes
5151
"../../#{@ext_dir}",
5252
"/usr/include/ruby-#{RUBY_VERSION}",
5353
"/usr/include/ruby-#{RUBY_VERSION}/#{RUBY_PLATFORM}",
54-
]).map { |l| "-I" + l }.join(" ")
54+
]).map { |l| "-I#{l}" }.join(" ")
5555
end
5656

5757
def libraries
58-
@libraries ||= (@test_libraries + %w[ruby pthread crypto]).map { |l| "-l" + l }.join(" ")
58+
@libraries ||= (@test_libraries + %w[ruby pthread crypto]).map { |l| "-l#{l}" }.join(" ")
5959
end
6060

6161
def lib_folders
62-
@lib_folders ||= (@test_lib_folders + %w[/usr/lib .]).map { |l| "-L" + l }.join(" ")
62+
@lib_folders ||= (@test_lib_folders + %w[/usr/lib .]).map { |l| "-L#{l}" }.join(" ")
6363
end
6464

6565
def compile_tests

spec/json_scanner_spec.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,18 @@
234234
key = "abracadabra".dup
235235
conf = described_class.new [[], [key]]
236236
key["cad"] = 0.chr
237-
key = nil
237+
key = nil # rubocop:disable Lint/UselessAssignment
238238
GC.start
239-
expect(10.times.map { JsonScanner.scan '{"abracadabra": 10}', conf, with_path: true }.uniq).to eq([[[[[], [0, 19, :object]]], [[["abracadabra"], [16, 18, :number]]]]])
240-
expect(10.times.map { JsonScanner.scan '{"abracadabra": 10}', conf }.uniq).to eq([[[[0, 19, :object]], [[16, 18, :number]]]])
239+
expect(
240+
10.times.map do
241+
JsonScanner.scan '{"abracadabra": 10}', conf, with_path: true
242+
end.uniq,
243+
).to eq([[[[[], [0, 19, :object]]], [[["abracadabra"], [16, 18, :number]]]]])
244+
expect(
245+
10.times.map do
246+
JsonScanner.scan '{"abracadabra": 10}', conf
247+
end.uniq,
248+
).to eq([[[[0, 19, :object]], [[16, 18, :number]]]])
241249
end
242250

243251
it "re-raises exceptions" do
@@ -251,5 +259,11 @@
251259
described_class.new [[(-42..1)]]
252260
end.to raise_error ArgumentError
253261
end
262+
263+
it "supports inspect" do
264+
expect(
265+
described_class.new([[], ["abracadabra", JsonScanner::ANY_INDEX], [42, JsonScanner::ANY_KEY]]).inspect,
266+
).to eq("#<JsonScanner::Config [[], ['abracadabra', (0..9223372036854775807)], [42, ('*'..'*')]]>")
267+
end
254268
end
255269
end

0 commit comments

Comments
 (0)