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   14083404 use strict;
  176         390  
  176         7195  
4 176     176   893 use warnings;
  176         374  
  176         10916  
5              
6 176     176   76677 use JSON::PP ();
  176         2141071  
  176         5769  
7              
8 176     176   164048 use JQ::Lite::Filters;
  176         1048  
  176         13785  
9 176     176   111633 use JQ::Lite::Parser;
  176         741  
  176         8255  
10 176     176   1298 use JQ::Lite::Util ();
  176         348  
  176         90670  
11              
12             our $VERSION = '2.42';
13              
14             sub new {
15 157     157 1 19683005 my ($class, %opts) = @_;
16 157         462 my $base_vars = {};
17 157 100 66     1232 if (exists $opts{vars} && ref $opts{vars} eq 'HASH') {
18 45         186 $base_vars = { %{ $opts{vars} } };
  45         247  
19             }
20              
21             my $self = {
22             raw => $opts{raw} || 0,
23             _base_vars => $base_vars,
24 157   50     1553 _vars => { %{$base_vars} },
  157         1177  
25             };
26              
27 157         832 return bless $self, $class;
28             }
29              
30             sub run_query {
31 5945     5945 1 647025 my ($self, $json_text, $query) = @_;
32              
33 5945   100     26306 my $depth = $self->{_query_depth} || 0;
34 5945         16881 local $self->{_query_depth} = $depth + 1;
35 5945 100       14638 if ($depth == 0) {
36 5759 50       9395 local $self->{_vars} = { %{ $self->{_base_vars} || {} } };
  5759         24354  
37 5759         14797 return _run_query_internal($self, $json_text, $query);
38             }
39              
40 186         690 return _run_query_internal($self, $json_text, $query);
41             }
42              
43             sub _run_query_internal {
44 5945     5945   12167 my ($self, $json_text, $query) = @_;
45              
46 5945         16481 my $data = JQ::Lite::Util::_decode_json($json_text);
47              
48 5945 100 66     1800792 return ($data) if !defined $query || $query =~ /^\s*\.\s*$/;
49              
50 933         4717 my @parts = JQ::Lite::Parser::parse_query($query);
51              
52 933         2261 my @results = ($data);
53 933         1899 for my $part (@parts) {
54 1390         2061 my @next_results;
55              
56 1390 100       10101 if (JQ::Lite::Filters::apply($self, $part, \@results, \@next_results)) {
57 854         1913 @results = @next_results;
58 854         2358 next;
59             }
60              
61 476         1358 for my $item (@results) {
62 492         2453 push @next_results, JQ::Lite::Util::_traverse($item, $part);
63             }
64 476         1311 @results = @next_results;
65             }
66              
67 873         8844 return @results;
68             }
69              
70             1;
71             __END__