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

filter: Statementstmt 97.8% 91 / 93 Branchbran 92.1% 59 / 64 Conditioncond 81.4% 35 / 43 MC/DCmcdc 63.6% 14 / 22 Subroutinesub 100% 15 / 15 Podpod 100% 2 / 2 totaltotal 90.3% 216 / 239 CC 50 SCAR 20.1
CC
50
Cov
92%
_val_type
12.0
_unmarshal_attr_val
9.0
_marshal_val
8.0
CRAP 7.5
↑ Summary Build »
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.05';
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 sub dynamodb_marshal {
15 18 ▶ my ($attrs, %args) = @_;
Subroutine dynamodb_marshal: called CC=6 SCAR=20.3
Pod documented
16 18 ▶ my $force_type = $args{force_type} || {};
Condition100%2/2
$args{'force_type'} || {}
l!l
315
MC/DC100%1/1
$args{'force_type'} || {}
$args{'force_type'}
Truth table100%2/2
A
A= $args{'force_type'}
Aresult
11
00
17 18 ▶ die __PACKAGE__.'::dynamodb_marshal(): argument must be a hashref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attrs and ref $attrs eq "HASH" 0 18 18
Condition33%1/3
ref $attrs and ref $attrs eq "HASH"
!ll&&!rl&&r
0018
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 18 ▶ 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 18 18
Condition33%1/3
ref $force_type and ref $force_type eq "HASH"
!ll&&!rl&&r
0018
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 18 return _marshal_hashref($attrs, $force_type);
28 }
29  
30 sub dynamodb_unmarshal {
31 9 ▶ my ($attrs) = @_;
Subroutine dynamodb_unmarshal: called CC=3 SCAR=12.5
Pod documented
32 9 ▶ die __PACKAGE__.'::dynamodb_unmarshal(): argument must be a hashref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attrs and ref $attrs eq "HASH" 0 9 9
Condition33%1/3
ref $attrs and ref $attrs eq "HASH"
!ll&&!rl&&r
009
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
33 unless (
34 ref $attrs
35 && ref $attrs eq 'HASH'
36 );
37 9 return _unmarshal_hashref($attrs);
38 }
39  
40 sub _marshal_hashref {
41 23 ▶ my ($attrs, $force_type) = @_;
Subroutine _marshal_hashref: called CC=5 SCAR=16.1
42 23 ▶ $force_type ||= {};
Condition100%2/2
$force_type ||= {}
l!l
185
MC/DC100%1/1
$force_type ||= {}
$force_type
Truth table100%2/2
A
A= $force_type
Aresult
11
00
43 23 my %marshalled;
44 23 for my $key (keys %$attrs) {
45 68 my $val = $attrs->{$key};
46 68 my $new_val;
47 68 ▶ if (my $type = $force_type->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $force_type->{$key}) { } 9 59 68
48 9 $new_val = _marshal_val_force_type($val, $type);
49 } else {
50 59 $new_val = _marshal_val($val);
51 }
52 65 ▶ if ($new_val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_val) 60 5 65
53 60 $marshalled{$key} = $new_val;
54 }
55 }
56 20 return \%marshalled;
57 }
58  
59 sub _unmarshal_hashref {
60 14 ▶ my ($attrs) = @_;
Subroutine _unmarshal_hashref: called CC=1 SCAR=0.0
61 14 return { map { $_ => _unmarshal_attr_val($attrs->{$_}) } keys %$attrs };
62 }
63  
64 sub _marshal_val {
65 75 ▶ my ($val) = @_;
Subroutine _marshal_val: called CC=8 SCAR=20.8
66 75 my $type = _val_type($val);
67  
68 73 ▶ return { $type => $val } if $type =~ /^(N|S)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^(N|S)$/ 45 28 73
69 28 ▶ return { $type => 1 } if $type eq 'NULL';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "NULL" 6 22 28
70 22 ▶ return { $type => $val ? 1 : 0 } if $type eq 'BOOL';
Branch: 2 branches
BranchTrueFalseTotal
if $type eq "BOOL" 4 18 22
$val ? : 2 2 4
71 18 ▶ return { $type => [ $val->members ] } if $type =~ /^(NS|SS)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^(NS|SS)$/ 4 14 18
72 14 ▶ return { $type => [ map { _marshal_val($_) } @$val ] } if ($type eq 'L');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "L" 9 5 14
73 5 ▶ return { $type => _marshal_hashref($val) } if ($type eq 'M');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "M" 5 0 5
74  
75 0 die "don't know how to marshal type of $type";
76 }
77  
78 sub _marshal_val_force_type {
79 9 ▶ my ($val, $type) = @_;
Subroutine _marshal_val_force_type: called CC=6 SCAR=17.9
80  
81 9 ▶ if ($type eq 'N') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "N") 4 5 9
82 4 ▶ return undef unless StrictNum->check($val);
Branch: 1 branch
BranchTrueFalseTotal
unless &StrictNum()->check($val) 3 1 4
83 1 return { N => $val };
84 }
85  
86 5 ▶ if ($type eq 'S') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "S") 4 1 5
87 4 ▶ return undef unless (defined $val && length($val));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and length $val 2 2 4
Condition100%3/3
defined $val and length $val
!ll&&!rl&&r
112
MC/DC100%2/2
defined $val and length $val
defined $val length $val
Truth table100%3/3
A and B
A= defined $valB= length $val
ABresult
0X0
100
111
88 2 return { S => "$val" };
89 }
90  
91 1 die __PACKAGE__.'::dynamodb_marshal(): force_type only supports "S" and "N" types';
92 }
93  
94 sub _unmarshal_attr_val {
95 47 ▶ my ($attr_val) = @_;
Subroutine _unmarshal_attr_val: called CC=9 SCAR=22.0
96 47 my ($type, $val) = %$attr_val;
97  
98 47 ▶ return undef if $type eq 'NULL';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "NULL" 2 45 47
99 45 ▶ return $val if $type =~ /^(S|N)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^(S|N)$/ 25 20 45
100 20 ▶ return true if $type eq 'BOOL' && $val;
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "BOOL" and $val 2 18 20
Condition100%3/3
$type eq "BOOL" and $val
!ll&&!rl&&r
1622
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
101 18 ▶ return false if $type eq 'BOOL';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "BOOL" 2 16 18
102 16 ▶ return Set::Object->new(@$val) if $type =~ /^(NS|SS)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^(NS|SS)$/ 4 12 16
103 12 ▶ return [ map { _unmarshal_attr_val($_) } @$val ] if $type eq 'L';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "L" 7 5 12
104 5 ▶ return _unmarshal_hashref($val) if $type eq 'M';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "M" 5 0 5
105  
106 0 die "don't know how to unmarshal $type";
107 }
108  
109 sub _val_type {
110 88 ▶ my ($val) = @_;
Subroutine _val_type: called CC=12 SCAR=24.9
111  
112 88 ▶ return 'NULL' if ! defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 4 84 88
113 84 ▶ return 'NULL' if $val eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "" 2 82 84
114 82 ▶ return 'N' if _is_number($val);
Branch: 1 branch
BranchTrueFalseTotal
if _is_number($val) 28 54 82
115 54 ▶ return 'S' if !ref $val;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val 29 25 54
116  
117 25 ▶ return 'BOOL' if isBoolean($val);
Branch: 1 branch
BranchTrueFalseTotal
if &isBoolean($val) 4 21 25
118  
119 21 my $ref = ref $val;
120 21 ▶ return 'L' if $ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "ARRAY" 9 12 21
121 12 ▶ return 'M' if $ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "HASH" 6 6 12
122  
123 6 ▶ if (blessed($val) and $val->isa('Set::Object')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($val) and $val->isa("Set::Object")) 5 1 6
Condition66%2/3
builtin::blessed($val) and $val->isa("Set::Object")
!ll&&!rl&&r
015
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
124 5 my @types = map { _val_type($_) } $val->members;
125 die "Sets can only contain strings and numbers, found $_"
126 5 for grep { !/^(S|N)$/ } @types;
127 4 ▶ if (grep { /^S$/ } @types) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/^S$/;} @types) { } 2 2 4
128 2 return 'SS';
129 } else {
130 2 return 'NS';
131 }
132 }
133  
134 1 die __PACKAGE__.": unable to marshal value: $val";
135 }
136  
137 sub _is_number {
138 82 ▶ my ($val) = @_;
Subroutine _is_number: called CC=8 SCAR=20.8
139 return (
140 82 ▶ (!ref $val)
Condition95%20/21
!ref($val) && &StrictNum()->check($val) && ($val == 0 || $val < 1e+126 && $val > 1e-130 || $val < -1e-130 && $val > -1e+126) && length $val <= 38
!ll&&!rl&&r
52228
not ref $val and &StrictNum()->check($val) and $val == 0 || $val < "1E+126" && $val > "1E-130" || $val < "-1E-130" && $val > "-1E+126"
!ll&&!rl&&r
48232
not ref $val and &StrictNum()->check($val)
!ll&&!rl&&r
252334
$val == 0 or $val < "1E+126" and $val > "1E-130" or $val < "-1E-130" and $val > "-1E+126"
l!l&&r!l&&!r
2842
$val == 0 or $val < "1E+126" and $val > "1E-130"
l!l&&r!l&&!r
0286
$val < "1E+126" and $val > "1E-130"
!ll&&!rl&&r
1528
$val < "-1E-130" and $val > "-1E+126"
!ll&&!rl&&r
312
MC/DC87%7/8
!ref($val) && &StrictNum()->check($val) && ($val == 0 || $val < 1e+126 && $val > 1e-130 || $val < -1e-130 && $val > -1e+126) && length $val <= 38
not ref $val &StrictNum()->check($val) $val == 0 $val < "1E+126" $val > "1E-130" $val < "-1E-130" $val > "-1E+126" length $val <= 38
Truth table57%8/14
not(A) and B and C or D and E or F and G && H
A= not ref $valB= &StrictNum()->check($val)C= $val == 0D= $val < "1E+126"E= $val > "1E-130"F= $val < "-1E-130"G= $val > "-1E+126"H= length $val <= 38
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
1100X0XX0
1100X10X0
110100XX0
1101010X0
111XXXX00
11011XX00
1100X1100
110101100
111XXXX11
11011XX11
1100X1111
110101111
141 && StrictNum->check($val)
142 && (
143 $val == 0
144 || (
145 $val < '1E+126'
146 && $val > '1E-130'
147 )
148 || (
149 $val < '-1E-130'
150 && $val > '-1E+126'
151 )
152 )
153 && length($val) <= 38
154 );
155 }
156  
157  
158 1 1;
159 __END__
↑ Summary Build »