| 1 | package JSON::Conditional; | ||||||||||||||||||||||||||||
| 2 | 9 | ▶ | use 5.006; use strict; use warnings; our $VERSION = '0.01'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use JSON; use Clone qw/clone/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 9 | our $JSON; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | BEGIN { | ||||||||||||||||||||||||||||
| 8 | 9 | ▶ | $JSON = JSON->new->allow_blessed->convert_blessed; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | } | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub new { | ||||||||||||||||||||||||||||
| 12 | 8 | ▶ | bless ($_[1] || {}), $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.6
Pod
undocumented
Condition50%1/2
$_[1] || {}
MC/DC0%0/1
$_[1] || {}
$_[1]
Truth table50%1/2
A
A= $_[1]
| |||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub encode { | ||||||||||||||||||||||||||||
| 16 | 0 | ▶ | $JSON->encode($_[1]); | ||||||||||||||||||||||||||
|
Subroutine
encode: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub decode { | ||||||||||||||||||||||||||||
| 20 | 0 | ▶ | $JSON->decode($_[1]); | ||||||||||||||||||||||||||
|
Subroutine
decode: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | sub compile { | ||||||||||||||||||||||||||||
| 24 | 8 | ▶ | my ($self, $json, $params, $return_struct) = @_; | ||||||||||||||||||||||||||
|
Subroutine
compile: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 8 | ▶ | $json = $JSON->decode($json) unless ref $json; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 8 | $json = $self->itterate($json, $params); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 8 | ▶ | return $return_struct ? $json : $JSON->encode($json); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | sub itterate { | ||||||||||||||||||||||||||||
| 34 | 77 | ▶ | my ($self, $json, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
itterate: called CC=8 SCAR=21.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 77 | my $ref = ref $json; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 77 | ▶ | if ($ref eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 39 | 36 | $json = $self->loops( | |||||||||||||||||||||||||||
| 40 | $self->conditionals($json, $params), | ||||||||||||||||||||||||||||
| 41 | $params | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 36 | for my $key ( keys %{$json} ) { | |||||||||||||||||||||||||||
| 45 | 55 | my $value = $self->itterate($json->{$key}, $params); | |||||||||||||||||||||||||||
| 46 | $value eq 'compiled_null' | ||||||||||||||||||||||||||||
| 47 | ? delete $json->{$key} | ||||||||||||||||||||||||||||
| 48 | 55 | ▶ | : do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 55 | $json->{$key} = $value; | |||||||||||||||||||||||||||
| 50 | }; | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 36 | ▶ | return keys %{$json} ? $json : 'compiled_null'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | } elsif ($ref eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 55 | 5 | my $i = 0; | |||||||||||||||||||||||||||
| 56 | 5 | for my $item (@{ $json }) { | |||||||||||||||||||||||||||
| 57 | 14 | my $value = $self->itterate($item, $params); | |||||||||||||||||||||||||||
| 58 | $value eq 'compiled_null' | ||||||||||||||||||||||||||||
| 59 | 14 | ▶ | ? do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | splice @{$json}, $i, 1; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | : $i++; | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 41 | return $json; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | sub loops { | ||||||||||||||||||||||||||||
| 70 | 36 | ▶ | my ($self, $json, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
loops: called CC=17 SCAR=29.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | my %loops = map { | ||||||||||||||||||||||||||||
| 73 | 36 | ($_ => delete $json->{$_}) | |||||||||||||||||||||||||||
| 74 | } qw/for/; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 36 | ▶ | if ($loops{for}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 5 | my $key = delete $loops{for}{key}; | |||||||||||||||||||||||||||
| 78 | 5 | ▶ | die "no key defined for loop" unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 5 | ▶ | if ($loops{for}{each}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 3 | my @each = (); | |||||||||||||||||||||||||||
| 81 | 3 | my $map = delete $loops{for}{each}; | |||||||||||||||||||||||||||
| 82 | die "param $key must be an arrayref" | ||||||||||||||||||||||||||||
| 83 | 3 | ▶ | unless (ref($params->{$key}) || "") eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $params->{$key} || ''
MC/DC0%0/1
ref $params->{$key} || ''
ref $params->{$key}
Truth table50%1/2
A
A= ref $params->{$key}
| |||||||||||||||||||||||||||||
| 84 | 3 | for (@{$params->{$key}}) { | |||||||||||||||||||||||||||
| 85 | 12 | my $jsn = $self->conditionals(clone($loops{for}), $_); | |||||||||||||||||||||||||||
| 86 | 12 | ▶ | push @each, $jsn if scalar keys %{$jsn}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 3 | ▶ | $json->{$map} = \@each if scalar @each; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 5 | ▶ | if ($loops{for}{keys}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 2 | my %keys = (); | |||||||||||||||||||||||||||
| 92 | 2 | my $map = delete $loops{for}{keys}; | |||||||||||||||||||||||||||
| 93 | die "param $key muse be an hashref" | ||||||||||||||||||||||||||||
| 94 | 2 | ▶ | unless (ref($params->{$key}) || "") eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $params->{$key} || ''
MC/DC0%0/1
ref $params->{$key} || ''
ref $params->{$key}
Truth table50%1/2
A
A= ref $params->{$key}
| |||||||||||||||||||||||||||||
| 95 | 2 | for my $k (keys %{$params->{$key}}) { | |||||||||||||||||||||||||||
| 96 | my $jsn = $self->conditionals( | ||||||||||||||||||||||||||||
| 97 | clone($loops{for}), | ||||||||||||||||||||||||||||
| 98 | 8 | $params->{$key}->{$k} | |||||||||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||||||||||
| 100 | 8 | ▶ | $keys{$k} = $jsn if scalar keys %{$jsn}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 2 | ▶ | if (scalar %keys) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | $map =~ m/^1$/ ? do { | ||||||||||||||||||||||||||||
| 104 | 1 | for my $k (keys %keys) { | |||||||||||||||||||||||||||
| 105 | 4 | $json->{$k} = $keys{$k}; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 2 | ▶ | } : do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 1 | $json->{$map} = \%keys; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 36 | return $json; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | sub conditionals { | ||||||||||||||||||||||||||||
| 118 | 56 | ▶ | my ($self, $json, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
conditionals: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | my %keywords = map { | ||||||||||||||||||||||||||||
| 121 | 56 | ($_ => delete $json->{$_}) | |||||||||||||||||||||||||||
| 122 | } qw/if elsif else/; | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 56 | ▶ | if ($keywords{if}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 23 | my ($expression) = $self->expressions($keywords{if}, $params); | |||||||||||||||||||||||||||
| 126 | 23 | ▶ | unless ($expression) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 15 | ▶ | if ($keywords{elsif}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 15 | ($expression) = $self->expressions($keywords{elsif}, $params); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 15 | ▶ | unless ($expression) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 6 | ▶ | if ($keywords{else}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 6 | ($expression) = $keywords{else}->{then}; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 23 | ▶ | if ($expression) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 23 | $json->{$_} = $expression->{$_} for ( keys %{$expression} ); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 56 | return $json; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | sub expressions { | ||||||||||||||||||||||||||||
| 145 | 42 | ▶ | my ($self, $keyword, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
expressions: called CC=24 SCAR=35.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 42 | my $success = 0; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | $success = exists $params->{$keyword->{key}} | ||||||||||||||||||||||||||||
| 150 | 42 | ▶ | if defined $keyword->{exists}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 42 | my $key = $params->{$keyword->{key}}; | |||||||||||||||||||||||||||
| 153 | 42 | ▶ | if (defined $key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | $success = $key =~ m/$keyword->{m}/ | ||||||||||||||||||||||||||||
| 155 | 42 | ▶ | if !$success && defined $keyword->{m}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $success and defined $keyword->{"m"}
MC/DC50%1/2
not $success and defined $keyword->{"m"}
not $success
defined $keyword->{"m"}
Truth table66%2/3
not(A) and B
A= not $successB= defined $keyword->{"m"}
| |||||||||||||||||||||||||||||
| 156 | $success = $key !~ m/$keyword->{nm}/ | ||||||||||||||||||||||||||||
| 157 | 42 | ▶ | if !$success && defined $keyword->{nm}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $success and defined $keyword->{"nm"}
MC/DC0%0/2
not $success and defined $keyword->{"nm"}
not $success
defined $keyword->{"nm"}
Truth table66%2/3
not(A) and B
A= not $successB= defined $keyword->{"nm"}
| |||||||||||||||||||||||||||||
| 158 | $success = $key eq $keyword->{eq} | ||||||||||||||||||||||||||||
| 159 | 42 | ▶ | if !$success && defined $keyword->{eq}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $success and defined $keyword->{"eq"}
MC/DC100%2/2
not $success and defined $keyword->{"eq"}
not $success
defined $keyword->{"eq"}
Truth table100%3/3
not(A) and B
A= not $successB= defined $keyword->{"eq"}
| |||||||||||||||||||||||||||||
| 160 | $success = $key ne $keyword->{ne} | ||||||||||||||||||||||||||||
| 161 | 42 | ▶ | if !$success && defined $keyword->{ne}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $success and defined $keyword->{"ne"}
MC/DC100%2/2
not $success and defined $keyword->{"ne"}
not $success
defined $keyword->{"ne"}
Truth table100%3/3
not(A) and B
A= not $successB= defined $keyword->{"ne"}
| |||||||||||||||||||||||||||||
| 162 | $success = $key > $keyword->{gt} | ||||||||||||||||||||||||||||
| 163 | 42 | ▶ | if !$success && defined $keyword->{gt}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $success and defined $keyword->{"gt"}
MC/DC0%0/2
not $success and defined $keyword->{"gt"}
not $success
defined $keyword->{"gt"}
Truth table66%2/3
not(A) and B
A= not $successB= defined $keyword->{"gt"}
| |||||||||||||||||||||||||||||
| 164 | $success = $key < $keyword->{lt} | ||||||||||||||||||||||||||||
| 165 | if !$success && defined $keyword->{lt} | ||||||||||||||||||||||||||||
| 166 | 42 | ▶ | } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $success and defined $keyword->{"lt"}
MC/DC0%0/2
not $success and defined $keyword->{"lt"}
not $success
defined $keyword->{"lt"}
Truth table66%2/3
not(A) and B
A= not $successB= defined $keyword->{"lt"}
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | ($success, $keyword) = $self->expressions($keyword->{or}, $params) | ||||||||||||||||||||||||||||
| 169 | 42 | ▶ | if ($keyword->{or} && !$success); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$keyword->{"or"} and not $success
MC/DC0%0/2
$keyword->{"or"} and not $success
$keyword->{"or"}
not $success
Truth table33%1/3
A and not(B)
A= $keyword->{"or"}B= not $success
| |||||||||||||||||||||||||||||
| 170 | ($success, $keyword) = $self->expressions($keyword->{and}, $params) | ||||||||||||||||||||||||||||
| 171 | 42 | ▶ | if ($keyword->{and} && $success); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$keyword->{"and"} and $success
MC/DC0%0/2
$keyword->{"and"} and $success
$keyword->{"and"}
$success
Truth table33%1/3
A and B
A= $keyword->{"and"}B= $success
| |||||||||||||||||||||||||||||
| 172 | 42 | ▶ | if ($keyword->{elsif} && !$success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$keyword->{"elsif"} and not $success
MC/DC50%1/2
$keyword->{"elsif"} and not $success
$keyword->{"elsif"}
not $success
Truth table66%2/3
A and not(B)
A= $keyword->{"elsif"}B= not $success
| |||||||||||||||||||||||||||||
| 173 | 4 | $keyword = $keyword->{elsif}; | |||||||||||||||||||||||||||
| 174 | 4 | ($success, $keyword) = $self->expressions($keyword, $params); | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | ($success, $keyword) = ($keyword->{else}->{then}, $keyword->{else}) | ||||||||||||||||||||||||||||
| 177 | 42 | ▶ | if ($keyword->{else} && !$success); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$keyword->{"else"} and not $success
MC/DC0%0/2
$keyword->{"else"} and not $success
$keyword->{"else"}
not $success
Truth table33%1/3
A and not(B)
A= $keyword->{"else"}B= not $success
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 42 | ▶ | return (($success ? $keyword->{then} : 0), $keyword); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 9 | 1; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | __END__ | ||||||||||||||||||||||||||||