API/EndpointContext/Compiler.pm

filter: Statementstmt 96.5% 85 / 88 Branchbran 87.5% 35 / 40 Conditioncond 61.1% 11 / 18 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 3 totaltotal 84.9% 147 / 173 CC 32 SCAR 21.3
CC
32
Cov
90%
compile
14.8
evaluate
11.0
_compile_expression
5.0
CRAP 8.4
« API/Signature4.pm ↑ Summary API/Template.pm »
1 package Amazon::API::EndpointContext::Compiler;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util qw(reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 ########################################################################
12 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
13 ########################################################################
14 1 my ($class) = @_;
15  
16 1 return bless {}, $class;
17 }
18  
19 ########################################################################
20 5 ▶sub compile {
Subroutine compile: called CC=13 SCAR=26.9
Pod undocumented
21 ########################################################################
22 5 my ( $self, $operations ) = @_;
23  
24 5 ▶ croak 'operations must be a hash reference'
Branch: 1 branch
BranchTrueFalseTotal
if not ref $operations or builtin::reftype($operations) ne "HASH" 1 4 5
Condition66%2/3
not ref $operations or builtin::reftype($operations) ne "HASH"
l!l&&r!l&&!r
014
MC/DC50%1/2
not ref $operations or builtin::reftype($operations) ne "HASH"
not ref $operations builtin::reftype($operations) ne "HASH"
Truth table66%2/3
not(A) or B
A= not ref $operationsB= builtin::reftype($operations) ne "HASH"
ABresult
1X1
011
000
25 if !ref $operations
26 || reftype($operations) ne 'HASH';
27  
28 4 my %compiled;
29  
30 4 foreach my $action ( sort keys %{$operations} ) {
31 4 my $operation_context = $operations->{$action}->{operationContextParams};
32  
33 next
34 4 ▶ if !$operation_context;
Branch: 1 branch
BranchTrueFalseTotal
unless $operation_context 0 4 4
35  
36 4 ▶ croak "operationContextParams for $action must be a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
if not ref $operation_context or builtin::reftype($operation_context) ne "HASH" 1 3 4
Condition66%2/3
not ref $operation_context or builtin::reftype($operation_context) ne "HASH"
l!l&&r!l&&!r
013
MC/DC50%1/2
not ref $operation_context or builtin::reftype($operation_context) ne "HASH"
not ref $operation_context builtin::reftype($operation_context) ne "HASH"
Truth table66%2/3
not(A) or B
A= not ref $operation_contextB= builtin::reftype($operation_context) ne "HASH"
ABresult
1X1
011
000
37 if !ref $operation_context
38 || reftype($operation_context) ne 'HASH';
39  
40 3 foreach my $name ( sort keys %{$operation_context} ) {
41 3 my $definition = $operation_context->{$name};
42  
43 3 ▶ croak "operationContextParams $name for $action must be a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
if not ref $definition or builtin::reftype($definition) ne "HASH" 1 2 3
Condition66%2/3
not ref $definition or builtin::reftype($definition) ne "HASH"
l!l&&r!l&&!r
012
MC/DC50%1/2
not ref $definition or builtin::reftype($definition) ne "HASH"
not ref $definition builtin::reftype($definition) ne "HASH"
Truth table66%2/3
not(A) or B
A= not ref $definitionB= builtin::reftype($definition) ne "HASH"
ABresult
1X1
011
000
44 if !ref $definition
45 || reftype($definition) ne 'HASH';
46  
47 2 my $path = $definition->{path};
48  
49 2 ▶ croak "operationContextParams $name for $action has no path"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $path or ref $path or $path eq "" 2 0 2
Condition66%4/6
not defined $path or ref $path or $path eq ""
l!l&&r!l&&!r
110
not defined $path or ref $path
l!l&&r!l&&!r
101
MC/DC0%0/3
not defined $path or ref $path or $path eq ""
not defined $path ref $path $path eq ""
Truth table50%2/4
not(A) or B or C
A= not defined $pathB= ref $pathC= $path eq ""
ABCresult
1XX1
01X1
0011
0000
50 if !defined $path
51 || ref $path
52 || $path eq q{};
53  
54 0 $compiled{$action}->{$name}
55 = $self->_compile_expression( $action, $name, $path );
56 }
57 }
58  
59 0 return \%compiled;
60 }
61  
62 ########################################################################
63 8 ▶sub _compile_expression {
Subroutine _compile_expression: called CC=5 SCAR=16.1
64 ########################################################################
65 8 my ( $self, $action, $name, $expression ) = @_;
66  
67 8 ▶ if ( $expression =~ /^keys\(([^.()\[\]\s]+)\)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expression =~ /^keys\(([^.()\[\]\s]+)\)$/) 1 7 8
68 1 return { $1 => { _function => 'keys', }, };
69 }
70  
71 7 ▶ if ( $expression =~ /^([^.()\[\]\s]+)\[\*\]\.(.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expression =~ /^([^.()\[\]\s]+)\[\*\]\.(.+)$/) 2 5 7
72 2 my $source = $1;
73 2 my $rest = $2;
74  
75 2 $rest =~ s/\[\]$//;
76  
77 2 my @selectors;
78  
79 2 ▶ if ( $rest =~ /^\[(.+)\]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /^\[(.+)\]$/) { } 1 1 2
80 1 foreach my $selector ( split /\s*,\s*/, $1 ) {
81 4 push @selectors, $self->_compile_path( $action, $name, $selector );
82 }
83 }
84 else {
85 1 push @selectors, $self->_compile_path( $action, $name, $rest );
86 }
87  
88 2 return { $source => \@selectors, };
89 }
90  
91 5 return $self->_compile_path( $action, $name, $expression );
92 }
93  
94 ########################################################################
95 10 ▶sub _compile_path {
Subroutine _compile_path: called CC=3 SCAR=11.0
96 ########################################################################
97 10 my ( $self, $action, $name, $path ) = @_;
98  
99 10 my @parts = split /[.]/, $path, -1;
100  
101 croak "unsupported operation context path for $action $name: $path"
102 10 ▶ if any { $_ eq q{} || /[\[\](),\s]/ } @parts;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
if &any(sub { $_ eq "" or /[\[\](),\s]/; } , @parts) 4 6 10
$_ eq "" or /[\[\](),\s]/ 1 16 17
103  
104 6 my $selector = pop @parts;
105  
106 6 ▶ while (@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 6 6 12
107 6 $selector = { pop(@parts) => $selector, };
108 }
109  
110 6 return $selector;
111 }
112  
113 ########################################################################
114 31 ▶sub _value {
Subroutine _value: called CC=3 SCAR=12.5
115 ########################################################################
116 31 my ($value) = @_;
117  
118 31 ▶ return $value->get__value
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) and $value->can("get__value") 0 31 31
Condition33%1/3
builtin::blessed($value) and $value->can("get__value")
!ll&&!rl&&r
3100
MC/DC0%0/2
builtin::blessed($value) and $value->can("get__value")
builtin::blessed($value) $value->can("get__value")
Truth table33%1/3
A and B
A= builtin::blessed($value)B= $value->can("get__value")
ABresult
0X0
100
111
119 if blessed($value)
120 && $value->can('get__value');
121  
122 31 return $value;
123 }
124  
125 ########################################################################
126 18 ▶sub evaluate {
Subroutine evaluate: called CC=11 SCAR=24.0
Pod undocumented
127 ########################################################################
128 18 my ( $self, $expression, $parameters ) = @_;
129  
130 return
131 18 ▶ if !defined $parameters;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parameters 0 18 18
132  
133 18 $parameters = _value($parameters);
134  
135 18 my ($key) = keys %{$expression};
136  
137 return
138 18 ▶ if !exists $parameters->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $parameters->{$key} 5 13 18
139  
140 13 my $value = _value( $parameters->{$key} );
141 13 my $selector = $expression->{$key};
142  
143 13 ▶ if ( !ref $selector ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $selector) 8 5 13
144 8 return $value->{$selector};
145 }
146  
147 5 ▶ if ( reftype($selector) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($selector) eq "HASH") 2 3 5
148 2 ▶ if ( $selector->{_function} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selector->{"_function"}) 2 0 2
149 1 return [ keys %{$value} ]
150 2 ▶ if $selector->{_function} eq 'keys';
Branch: 1 branch
BranchTrueFalseTotal
if $selector->{"_function"} eq "keys" 1 1 2
151  
152 1 croak "unsupported endpoint context function: $selector->{_function}";
153 }
154  
155 0 return $self->evaluate( $selector, $value );
156 }
157  
158 3 ▶ if ( reftype($selector) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($selector) eq "ARRAY") 2 1 3
159 2 my @results;
160  
161 2 foreach my $item ( @{$value} ) {
162 4 foreach my $expansion ( @{$selector} ) {
163 10 my $result = $self->evaluate( $expansion, $item );
164  
165 10 ▶ push @results, $result
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 6 4 10
166 if defined $result;
167 }
168 }
169  
170 2 return \@results;
171 }
172  
173 1 croak 'unsupported compiled endpoint context expression';
174 }
175  
176 1 1;
« API/Signature4.pm ↑ Summary API/Template.pm »