blib/lib/Net/Amazon/DynamoDB/Marshaler.pm

filter: Statementstmt 98.5% 131 / 133 Branchbran 92.5% 87 / 94 Conditioncond 70.3% 19 / 27 MC/DCmcdc 52.9% 9 / 17 Subroutinesub 100% 17 / 17 Podpod 100% 2 / 2 totaltotal 91.3% 265 / 290 CC 61 SCAR 23.8
CC
61
Cov
93%
_marshal_val
19.0
_is_valid_number
12.0
_val_type
12.0
CRAP 10.8
↑ Summary
1 package Net::Amazon::DynamoDB::Marshaler;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '0.07';
6  
7 1 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 our @EXPORT = qw(dynamodb_marshal dynamodb_unmarshal);
9  
10 1 ▶use boolean qw(true false isBoolean);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Types::Standard qw(StrictNum);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 25 ▶sub dynamodb_marshal {
Subroutine dynamodb_marshal: called CC=7 SCAR=21.5
Pod documented
15 25 my ($attrs, %args) = @_;
16 25 ▶ my $force_type = $args{force_type} || {};
Condition100%2/2
$args{'force_type'} || {}
l!l
1015
MC/DC100%1/1
$args{'force_type'} || {}
$args{'force_type'}
Truth table100%2/2
A
A= $args{'force_type'}
Aresult
11
00
17 25 ▶ die __PACKAGE__.'::dynamodb_marshal(): argument must be a hashref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attrs and ref $attrs eq "HASH" 0 25 25
Condition33%1/3
ref $attrs and ref $attrs eq "HASH"
!ll&&!rl&&r
0025
MC/DC0%0/2
ref $attrs and ref $attrs eq "HASH"
ref $attrs ref $attrs eq "HASH"
Truth table33%1/3
A and B
A= ref $attrsB= ref $attrs eq "HASH"
ABresult
0X0
100
111
18 unless (
19 ref $attrs
20 && ref $attrs eq 'HASH'
21 );
22 25 ▶ die __PACKAGE__.'::dynamodb_marshal(): force_type must be a hashref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $force_type and ref $force_type eq "HASH" 0 25 25
Condition33%1/3
ref $force_type and ref $force_type eq "HASH"
!ll&&!rl&&r
0025
MC/DC0%0/2
ref $force_type and ref $force_type eq "HASH"
ref $force_type ref $force_type eq "HASH"
Truth table33%1/3
A and B
A= ref $force_typeB= ref $force_type eq "HASH"
ABresult
0X0
100
111
23 unless (
24 ref $force_type
25 && ref $force_type eq 'HASH'
26 );
27  
28 die __PACKAGE__.qq|::dynamodb_marshal(): invalid force_type value for "$_"|
29 25 for grep {
30 19 $force_type->{$_} !~ /^[SN]$/;
31 }
32 keys %$force_type;
33  
34 24 return _marshal_hashref($attrs, $force_type);
35 }
36  
37 10 ▶sub dynamodb_unmarshal {
Subroutine dynamodb_unmarshal: called CC=3 SCAR=12.5
Pod documented
38 10 my ($attrs) = @_;
39 10 ▶ die __PACKAGE__.'::dynamodb_unmarshal(): argument must be a hashref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attrs and ref $attrs eq "HASH" 0 10 10
Condition33%1/3
ref $attrs and ref $attrs eq "HASH"
!ll&&!rl&&r
0010
MC/DC0%0/2
ref $attrs and ref $attrs eq "HASH"
ref $attrs ref $attrs eq "HASH"
Truth table33%1/3
A and B
A= ref $attrsB= ref $attrs eq "HASH"
ABresult
0X0
100
111
40 unless (
41 ref $attrs
42 && ref $attrs eq 'HASH'
43 );
44 10 return _unmarshal_hashref($attrs);
45 }
46  
47 36 ▶sub _marshal_hashref {
Subroutine _marshal_hashref: called CC=4 SCAR=13.9
48 36 my ($attrs, $force_types) = @_;
49 36 ▶ $force_types ||= {};
Condition50%1/2
$force_types ||= {}
l!l
360
MC/DC0%0/1
$force_types ||= {}
$force_types
Truth table50%1/2
A
A= $force_types
Aresult
11
00
50 36 my %marshalled;
51 36 for my $key (keys %$attrs) {
52 111 my $val = $attrs->{$key};
53 111 my $force_type = $force_types->{$key};
54 111 my $new_val = _marshal_val($val, $force_type);
55 107 ▶ if ($new_val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_val) 100 7 107
56 100 $marshalled{$key} = $new_val;
57 }
58 }
59 32 return \%marshalled;
60 }
61  
62 15 ▶sub _unmarshal_hashref {
Subroutine _unmarshal_hashref: called CC=1 SCAR=0.0
63 15 my ($attrs) = @_;
64 15 return { map { $_ => _unmarshal_attr_val($attrs->{$_}) } keys %$attrs };
65 }
66  
67 144 ▶sub _marshal_val {
Subroutine _marshal_val: called CC=19 SCAR=29.4
68 144 my ($val, $force_type) = @_;
69 144 ▶ $force_type ||= '';
Condition100%2/2
$force_type ||= ""
l!l
5391
MC/DC100%1/1
$force_type ||= ""
$force_type
Truth table100%2/2
A
A= $force_type
Aresult
11
00
70  
71 # Calculate the type according to our rules.
72 144 my $type = _val_type($val);
73  
74 # Subref to build string value result
75 142 ▶ my $marshalled_string = sub {{ S => "$_[0]" }};
Subroutine __ANON__: called CC=1 SCAR=0.0
76  
77 # Subref to build number value result
78 142 ▶ my $marshalled_num = sub {{ N => $_[0] }};
Subroutine __ANON__: called CC=1 SCAR=0.0
79  
80 # Handle strings
81 142 ▶ if ($type eq 'S') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "S") 39 103 142
82 # Strip these out if we're force-typing to number
83 39 ▶ if ($force_type eq 'N') {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_type eq "N") 3 36 39
84 3 return undef;
85 }
86 36 return $marshalled_string->($val);
87 }
88  
89 # Handle numbers
90 103 ▶ if ($type eq 'N') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "N") 42 61 103
91 # Force-stringify if asked
92 42 ▶ if ($force_type eq 'S') {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_type eq "S") 9 33 42
93 9 return $marshalled_string->($val);
94 }
95 33 return $marshalled_num->($val);
96 }
97  
98 # Handle nulls
99 61 ▶ if ($type eq 'NULL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "NULL") 14 47 61
100 # Strip these out if we're force-typing
101 14 ▶ if ($force_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_type) 7 7 14
102 7 return undef;
103 }
104 7 return { NULL => 1 };
105 }
106  
107 # Handle booleans
108 47 ▶ if ($type eq 'BOOL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "BOOL") 12 35 47
109 # Force-stringify if asked
110 12 ▶ if ($force_type eq 'S') {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_type eq "S") 2 10 12
111 2 ▶ return $marshalled_string->($val ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 1 1 2
112 }
113 # Force-numerify if asked
114 10 ▶ if ($force_type eq 'N') {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_type eq "N") 2 8 10
115 2 ▶ return $marshalled_num->($val ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 1 1 2
116 }
117 8 ▶ return { BOOL => $val ? 1 : 0 };
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 4 4 8
118 }
119  
120 # Handle sets
121 35 ▶ if ($type =~ /^(NS|SS)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^(NS|SS)$/) 11 24 35
122 # Blow up trying to force-type a set
123 11 ▶ die __PACKAGE__.'force_type not supported for sets yet'
Branch: 1 branch
BranchTrueFalseTotal
if $force_type 2 9 11
124 if $force_type;
125 9 return { $type => [ $val->members ] };
126 }
127  
128 # Handle lists
129 24 ▶ if ($type eq 'L') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "L") 12 12 24
130 12 my @items = map { _marshal_val($_, $force_type) } @$val;
131 # Strip out any values that failed force-typing
132 12 @items = grep { defined } @items;
133 12 return { L => \@items };
134 }
135  
136 # Handle maps
137 12 ▶ if ($type eq 'M') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "M") 12 0 12
138 12 my $force_types = {};
139 12 ▶ if ($force_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_type) 7 5 12
140 # Recursively apply our force type to all values.
141 7 $force_types = { map { $_ => $force_type } keys %$val };
142 }
143 12 return { M => _marshal_hashref($val, $force_types) };
144 }
145  
146 0 die "don't know how to marshal type of $type";
147 }
148  
149 50 ▶sub _unmarshal_attr_val {
Subroutine _unmarshal_attr_val: called CC=9 SCAR=22.0
150 50 my ($attr_val) = @_;
151 50 my ($type, $val) = _get_canonical_attr_val($attr_val);
152  
153 50 ▶ return undef if $type eq 'NULL';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "NULL" 2 48 50
154 48 ▶ return $val if $type =~ /^(S|N)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^(S|N)$/ 27 21 48
155 21 ▶ return true if $type eq 'BOOL' && $val;
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "BOOL" and $val 3 18 21
Condition100%3/3
$type eq "BOOL" and $val
!ll&&!rl&&r
1623
MC/DC100%2/2
$type eq "BOOL" and $val
$type eq "BOOL" $val
Truth table100%3/3
A and B
A= $type eq "BOOL"B= $val
ABresult
0X0
100
111
156 18 ▶ return false if $type eq 'BOOL';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "BOOL" 2 16 18
157 16 ▶ return Set::Object->new(@$val) if $type =~ /^(NS|SS)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^(NS|SS)$/ 4 12 16
158 12 ▶ return [ map { _unmarshal_attr_val($_) } @$val ] if $type eq 'L';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "L" 7 5 12
159 5 ▶ return _unmarshal_hashref($val) if $type eq 'M';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "M" 5 0 5
160  
161 0 die "don't know how to unmarshal $type";
162 }
163  
164 # Sometimes Paws includes an empty L value in its AttributeValue object, see
165 # https://github.com/pplu/aws-sdk-perl/issues/79
166 50 ▶sub _get_canonical_attr_val {
Subroutine _get_canonical_attr_val: called CC=2 SCAR=6.9
167 50 my ($attr_val) = @_;
168 50 my @types = keys %$attr_val;
169 50 my $canonical_type;
170 50 ▶ if (@types == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@types == 1) { } 47 3 50
171 47 $canonical_type = $types[0];
172 } else {
173 3 ($canonical_type) = grep { $_ ne 'L' } @types;
174 }
175  
176 50 return ($canonical_type, $attr_val->{$canonical_type});
177 }
178  
179 178 ▶sub _val_type {
Subroutine _val_type: called CC=12 SCAR=24.9
180 178 my ($val) = @_;
181  
182 178 ▶ return 'NULL' if ! defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 10 168 178
183 168 ▶ return 'NULL' if $val eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "" 4 164 168
184 164 ▶ return 'N' if _is_valid_number($val);
Branch: 1 branch
BranchTrueFalseTotal
if _is_valid_number($val) 58 106 164
185 106 ▶ return 'S' if !ref $val;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val 56 50 106
186  
187 50 ▶ return 'BOOL' if isBoolean($val);
Branch: 1 branch
BranchTrueFalseTotal
if &isBoolean($val) 12 38 50
188  
189 38 my $ref = ref $val;
190 38 ▶ return 'L' if $ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "ARRAY" 12 26 38
191 26 ▶ return 'M' if $ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "HASH" 13 13 26
192  
193 13 ▶ if (blessed($val) and $val->isa('Set::Object')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($val) and $val->isa("Set::Object")) 12 1 13
Condition66%2/3
builtin::blessed($val) and $val->isa("Set::Object")
!ll&&!rl&&r
0112
MC/DC50%1/2
builtin::blessed($val) and $val->isa("Set::Object")
builtin::blessed($val) $val->isa("Set::Object")
Truth table66%2/3
A and B
A= builtin::blessed($val)B= $val->isa("Set::Object")
ABresult
0X0
100
111
194 12 my @types = map { _val_type($_) } $val->members;
195 die "Sets can only contain strings and numbers, found $_"
196 12 for grep { !/^(S|N)$/ } @types;
197 11 ▶ if (grep { /^S$/ } @types) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/^S$/;} @types) { } 6 5 11
198 6 return 'SS';
199 } else {
200 5 return 'NS';
201 }
202 }
203  
204 1 die __PACKAGE__.": unable to marshal value: $val";
205 }
206  
207 164 ▶sub _is_valid_number {
Subroutine _is_valid_number: called CC=12 SCAR=24.9
208 164 my ($val) = @_;
209 164 ▶ return 0 if ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 50 114 164
210 114 ▶ return 0 unless StrictNum->check($val);
Branch: 1 branch
BranchTrueFalseTotal
unless &StrictNum()->check($val) 49 65 114
211  
212 # Some very high numbers are equal to 0, keep those as strings
213 65 ▶ return 1 if ("$val" eq '0');
Branch: 1 branch
BranchTrueFalseTotal
if "$val" eq 0 0 65 65
214 65 ▶ return 0 if ($val == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $val == 0 0 65 65
215  
216 65 ▶ return 0 if ($val > 0 && $val <= 1e-130);
Branch: 1 branch
BranchTrueFalseTotal
if $val > 0 and $val <= "1e-130" 1 64 65
Condition100%3/3
$val > 0 and $val <= "1e-130"
!ll&&!rl&&r
4601
MC/DC100%2/2
$val > 0 and $val <= "1e-130"
$val > 0 $val <= "1e-130"
Truth table100%3/3
A and B
A= $val > 0B= $val <= "1e-130"
ABresult
0X0
100
111
217 64 ▶ return 0 if ($val < 0 && $val >= -1e-130);
Branch: 1 branch
BranchTrueFalseTotal
if $val < 0 and $val >= "-1e-130" 1 63 64
Condition100%3/3
$val < 0 and $val >= "-1e-130"
!ll&&!rl&&r
6031
MC/DC100%2/2
$val < 0 and $val >= "-1e-130"
$val < 0 $val >= "-1e-130"
Truth table100%3/3
A and B
A= $val < 0B= $val >= "-1e-130"
ABresult
0X0
100
111
218  
219 63 ▶ return 0 if ($val >= 1e126);
Branch: 1 branch
BranchTrueFalseTotal
if $val >= "1e+126" 2 61 63
220 61 ▶ return 0 if ($val <= -1e126);
Branch: 1 branch
BranchTrueFalseTotal
if $val <= "-1e+126" 1 60 61
221  
222 60 ▶ return 0 if length($val) > 38;
Branch: 1 branch
BranchTrueFalseTotal
if length $val > 38 2 58 60
223  
224 58 return 1;
225 }
226  
227  
228 1 1;
229 __END__
↑ Summary