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   14187978 use strict;
  176         362  
  176         7164  
4 176     176   2705 use warnings;
  176         337  
  176         10740  
5              
6 176     176   82639 use JSON::PP ();
  176         2229569  
  176         11571  
7              
8 176     176   147514 use JQ::Lite::Filters;
  176         1141  
  176         13544  
9 176     176   112614 use JQ::Lite::Parser;
  176         664  
  176         8698  
10 176     176   1324 use JQ::Lite::Util ();
  176         419  
  176         93659  
11              
12             our $VERSION = '2.46';
13              
14             sub new {
15 157     157 1 20459889 my ($class, %opts) = @_;
16 157         475 my $base_vars = {};
17 157 100 66     1429 if (exists $opts{vars} && ref $opts{vars} eq 'HASH') {
18 45         109 $base_vars = { %{ $opts{vars} } };
  45         298  
19             }
20              
21             my $self = {
22             raw => $opts{raw} || 0,
23             _base_vars => $base_vars,
24 157   50     1553 _vars => { %{$base_vars} },
  157         1130  
25             };
26              
27 157         824 return bless $self, $class;
28             }
29              
30             sub run_query {
31 5950     5950 1 671115 my ($self, $json_text, $query) = @_;
32              
33 5950   100     23261 my $depth = $self->{_query_depth} || 0;
34 5950         14533 local $self->{_query_depth} = $depth + 1;
35 5950 100       12889 if ($depth == 0) {
36 5760 50       8602 local $self->{_vars} = { %{ $self->{_base_vars} || {} } };
  5760         20328  
37 5760         12700 return _run_query_internal($self, $json_text, $query);
38             }
39              
40 190         746 return _run_query_internal($self, $json_text, $query);
41             }
42              
43             sub _run_query_internal {
44 5950     5950   10800 my ($self, $json_text, $query) = @_;
45              
46 5950         14453 my $data = JQ::Lite::Util::_decode_json($json_text);
47              
48 5950 100 66     1697975 return ($data) if !defined $query || $query =~ /^\s*\.\s*$/;
49              
50 938         4762 my @parts = JQ::Lite::Parser::parse_query($query);
51              
52 938         2198 my @results = ($data);
53 938         1867 for my $part (@parts) {
54 1395         2133 my @next_results;
55              
56 1395 100       10669 if (JQ::Lite::Filters::apply($self, $part, \@results, \@next_results)) {
57 859         1876 @results = @next_results;
58 859         2416 next;
59             }
60              
61 476         1351 for my $item (@results) {
62 492         2484 push @next_results, JQ::Lite::Util::_traverse($item, $part);
63             }
64 476         1493 @results = @next_results;
65             }
66              
67 878         8830 return @results;
68             }
69              
70             1;
71             __END__