File Coverage

blib/lib/JQ/Lite.pm
Criterion Covered Total %
statement 48 48 100.0
branch 9 10 90.0
condition 7 10 70.0
subroutine 9 9 100.0
pod 2 2 100.0
total 75 79 94.9


line stmt bran cond sub pod time code
1             package JQ::Lite;
2              
3 176     176   10165650 use strict;
  176         267  
  176         5583  
4 176     176   687 use warnings;
  176         3236  
  176         7970  
5              
6 176     176   61010 use JSON::PP ();
  176         1541329  
  176         9149  
7              
8 176     176   115822 use JQ::Lite::Filters;
  176         868  
  176         10419  
9 176     176   85311 use JQ::Lite::Parser;
  176         493  
  176         6403  
10 176     176   1030 use JQ::Lite::Util ();
  176         279  
  176         69589  
11              
12             our $VERSION = '2.44';
13              
14             sub new {
15 157     157 1 13046323 my ($class, %opts) = @_;
16 157         352 my $base_vars = {};
17 157 100 66     915 if (exists $opts{vars} && ref $opts{vars} eq 'HASH') {
18 45         107 $base_vars = { %{ $opts{vars} } };
  45         211  
19             }
20              
21             my $self = {
22             raw => $opts{raw} || 0,
23             _base_vars => $base_vars,
24 157   50     1131 _vars => { %{$base_vars} },
  157         831  
25             };
26              
27 157         600 return bless $self, $class;
28             }
29              
30             sub run_query {
31 5945     5945 1 422759 my ($self, $json_text, $query) = @_;
32              
33 5945   100     14547 my $depth = $self->{_query_depth} || 0;
34 5945         9025 local $self->{_query_depth} = $depth + 1;
35 5945 100       8417 if ($depth == 0) {
36 5759 50       5195 local $self->{_vars} = { %{ $self->{_base_vars} || {} } };
  5759         11934  
37 5759         8346 return _run_query_internal($self, $json_text, $query);
38             }
39              
40 186         391 return _run_query_internal($self, $json_text, $query);
41             }
42              
43             sub _run_query_internal {
44 5945     5945   7332 my ($self, $json_text, $query) = @_;
45              
46 5945         8831 my $data = JQ::Lite::Util::_decode_json($json_text);
47              
48 5945 100 66     1022694 return ($data) if !defined $query || $query =~ /^\s*\.\s*$/;
49              
50 933         3216 my @parts = JQ::Lite::Parser::parse_query($query);
51              
52 933         1491 my @results = ($data);
53 933         1350 for my $part (@parts) {
54 1390         1493 my @next_results;
55              
56 1390 100       7119 if (JQ::Lite::Filters::apply($self, $part, \@results, \@next_results)) {
57 854         1346 @results = @next_results;
58 854         1641 next;
59             }
60              
61 476         896 for my $item (@results) {
62 492         1601 push @next_results, JQ::Lite::Util::_traverse($item, $part);
63             }
64 476         870 @results = @next_results;
65             }
66              
67 873         5938 return @results;
68             }
69              
70             1;
71             __END__