JSONObject/Util.pm

filter: Statementstmt 93.0% 148 / 159 Branchbran 52.3% 44 / 84 Conditioncond 56.8% 29 / 51 MC/DCmcdc 11.7% 4 / 34 Subroutinesub 95.8% 23 / 24 Podpod n/a totaltotal 70.4% 248 / 352 CC 62 SCAR 26.2
CC
62
Cov
75%
identify_type
28.5
meta_from_json
15.8
meta_to_json
14.7
CRAP 13.7
↑ Summary JSONObject.pm »
1 #!/usr/bin/perl
2  
3 package MooseX::Role::JSONObject::Util;
4  
5 4 ▶use v5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use version; our $VERSION = version->declare("v0.1.0");
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use List::Util qw/pairfirst/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 func identify_type(Any $val, Moose::Meta::Attribute $attr)
15 4 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine identify_type: called CC=16 SCAR=33.5
Branch: 5 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 93 93
unless ($Method::Signatures::mutc{"cache"}{"Any"} ||= "Method::Signatures"->_make_constraint("Any"))->check($val) 0 93 93
unless scalar @_ > 1 0 93 93
unless ($Method::Signatures::mutc{"cache"}{"Moose::Meta::Attribute"} ||= "Method::Signatures"->_make_constraint("Moose::Meta::Attribute"))->check($attr) 0 93 93
if scalar @_ > 2 0 93 93
Condition33%2/6
$Method::Signatures::mutc{"cache"}{"Any"} ||= "Method::Signatures"->_make_constraint("Any")
l!l&&r!l&&!r
9300
$Method::Signatures::mutc{"cache"}{"Moose::Meta::Attribute"} ||= "Method::Signatures"->_make_constraint("Moose::Meta::Attribute")
l!l&&r!l&&!r
9300
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Any"} ||= "Method::Signatures"->_make_constraint("Any")
$Method::Signatures::mutc{"cache"}{"Any"} "Method::Signatures"->_make_constraint("Any")
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Moose::Meta::Attribute"} ||= "Method::Signatures"->_make_constraint("Moose::Meta::Attribute")
$Method::Signatures::mutc{"cache"}{"Moose::Meta::Attribute"} "Method::Signatures"->_make_constraint("Moose::Meta::Attribute")
Truth table33%1/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Any"}B= "Method::Signatures"->_make_constraint("Any")
ABresult
1X1
011
000
Truth table33%1/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Moose::Meta::Attribute"}B= "Method::Signatures"->_make_constraint("Moose::Meta::Attribute")
ABresult
1X1
011
000
16 93 ▶ if (!$attr->has_type_constraint) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attr->has_type_constraint) 0 93 93
17 0 ▶ if (!defined $val) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $val) { } 0 0 0
elsif (ref $val eq 'ARRAY') { } 0 0 0
elsif (ref $val eq 'HASH') { } 0 0 0
18 0 die "MooseX::Role::JSONObject::SKIP\n";
19 } elsif (ref $val eq 'ARRAY') {
20 0 return ('array', '*');
21 } elsif (ref $val eq 'HASH') {
22 0 return ('hash', '*');
23 }
24 0 return ['*'];
25 }
26  
27 93 ▶ if (!defined $val && !$attr->is_required) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val or $attr->is_required) 1 92 93
Condition100%3/3
defined $val or $attr->is_required
l!l&&r!l&&!r
78141
MC/DC100%2/2
defined $val or $attr->is_required
defined $val $attr->is_required
Truth table100%3/3
A or B
A= defined $valB= $attr->is_required
ABresult
1X1
011
000
28 1 die "MooseX::Role::JSONObject::SKIP\n";
29 }
30 92 my $type = $attr->type_constraint;
31 92 my @res;
32 124 again:
33 my @handlers = (
34 'Object' => ['obj', $type->name],
35 'Num' => ['num'],
36 'Str' => ['str'],
37 'Bool' => ['bool'],
38 'Maybe' => ['maybe', '#PARAM'],
39 'ArrayRef' => ['array', '#PARAM'],
40 'HashRef' => ['hash', '#PARAM'],
41 );
42 124 ▶ my (undef, $list) = pairfirst { $type->is_a_type_of($a) } @handlers;
Subroutine __ANON__: called CC=1 SCAR=0.0
43 124 ▶ if (!defined $list) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $list) 0 124 124
44 0 die "FIXME: handle the type constraint for ".$attr->name;
45 }
46 124 push @res, @{$list};
47  
48 124 ▶ if ($res[$#res] eq '#PARAM') {
Branch: 1 branch
BranchTrueFalseTotal
if ($res[$#res] eq "#PARAM") 32 92 124
49 32 pop @res;
50 32 $type = $type->{type_parameter};
51 32 goto again;
52 }
53  
54 # Apparently we're done looping over parameterized type constraints
55 92 return @res;
56 }
57  
58 4 func get_value(Any $val, Moose::Meta::Attribute $attr, CodeRef $objfunc)
59 4 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine get_value: called CC=11 SCAR=26.1
Branch: 7 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 93 93
unless ($Method::Signatures::mutc{"cache"}{"Any"} ||= "Method::Signatures"->_make_constraint("Any"))->check($val) 0 93 93
unless scalar @_ > 1 0 93 93
unless ($Method::Signatures::mutc{"cache"}{"Moose::Meta::Attribute"} ||= "Method::Signatures"->_make_constraint("Moose::Meta::Attribute"))->check($attr) 0 93 93
unless scalar @_ > 2 0 93 93
unless ($Method::Signatures::mutc{"cache"}{"CodeRef"} ||= "Method::Signatures"->_make_constraint("CodeRef"))->check($objfunc) 0 93 93
if scalar @_ > 3 0 93 93
Condition66%6/9
$Method::Signatures::mutc{"cache"}{"Any"} ||= "Method::Signatures"->_make_constraint("Any")
l!l&&r!l&&!r
9030
$Method::Signatures::mutc{"cache"}{"Moose::Meta::Attribute"} ||= "Method::Signatures"->_make_constraint("Moose::Meta::Attribute")
l!l&&r!l&&!r
9030
$Method::Signatures::mutc{"cache"}{"CodeRef"} ||= "Method::Signatures"->_make_constraint("CodeRef")
l!l&&r!l&&!r
9030
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Any"} ||= "Method::Signatures"->_make_constraint("Any")
$Method::Signatures::mutc{"cache"}{"Any"} "Method::Signatures"->_make_constraint("Any")
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Moose::Meta::Attribute"} ||= "Method::Signatures"->_make_constraint("Moose::Meta::Attribute")
$Method::Signatures::mutc{"cache"}{"Moose::Meta::Attribute"} "Method::Signatures"->_make_constraint("Moose::Meta::Attribute")
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"CodeRef"} ||= "Method::Signatures"->_make_constraint("CodeRef")
$Method::Signatures::mutc{"cache"}{"CodeRef"} "Method::Signatures"->_make_constraint("CodeRef")
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Any"}B= "Method::Signatures"->_make_constraint("Any")
ABresult
1X1
011
000
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Moose::Meta::Attribute"}B= "Method::Signatures"->_make_constraint("Moose::Meta::Attribute")
ABresult
1X1
011
000
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"CodeRef"}B= "Method::Signatures"->_make_constraint("CodeRef")
ABresult
1X1
011
000
60 93 my @type = identify_type($val, $attr);
61 92 return get_type_value($val, \@type, $objfunc);
62 }
63  
64 4 func get_type_value(Any $val, ArrayRef[Str] $type, CodeRef $objfunc)
65 4 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine get_type_value: called CC=11 SCAR=25.3
Branch: 7 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 113 113
unless ($Method::Signatures::mutc{"cache"}{"Any"} ||= "Method::Signatures"->_make_constraint("Any"))->check($val) 0 113 113
unless scalar @_ > 1 0 113 113
unless ($Method::Signatures::mutc{"cache"}{"ArrayRef[Str]"} ||= "Method::Signatures"->_make_constraint("ArrayRef[Str]"))->check($type) 0 113 113
unless scalar @_ > 2 0 113 113
unless ($Method::Signatures::mutc{"cache"}{"CodeRef"} ||= "Method::Signatures"->_make_constraint("CodeRef"))->check($objfunc) 0 113 113
if scalar @_ > 3 0 113 113
Condition44%4/9
$Method::Signatures::mutc{"cache"}{"Any"} ||= "Method::Signatures"->_make_constraint("Any")
l!l&&r!l&&!r
11300
$Method::Signatures::mutc{"cache"}{"ArrayRef[Str]"} ||= "Method::Signatures"->_make_constraint("ArrayRef[Str]")
l!l&&r!l&&!r
11030
$Method::Signatures::mutc{"cache"}{"CodeRef"} ||= "Method::Signatures"->_make_constraint("CodeRef")
l!l&&r!l&&!r
11300
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Any"} ||= "Method::Signatures"->_make_constraint("Any")
$Method::Signatures::mutc{"cache"}{"Any"} "Method::Signatures"->_make_constraint("Any")
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"ArrayRef[Str]"} ||= "Method::Signatures"->_make_constraint("ArrayRef[Str]")
$Method::Signatures::mutc{"cache"}{"ArrayRef[Str]"} "Method::Signatures"->_make_constraint("ArrayRef[Str]")
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"CodeRef"} ||= "Method::Signatures"->_make_constraint("CodeRef")
$Method::Signatures::mutc{"cache"}{"CodeRef"} "Method::Signatures"->_make_constraint("CodeRef")
Truth table33%1/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Any"}B= "Method::Signatures"->_make_constraint("Any")
ABresult
1X1
011
000
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"ArrayRef[Str]"}B= "Method::Signatures"->_make_constraint("ArrayRef[Str]")
ABresult
1X1
011
000
Truth table33%1/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"CodeRef"}B= "Method::Signatures"->_make_constraint("CodeRef")
ABresult
1X1
011
000
66 113 my %handlers;
67 %handlers = (
68 21 ▶ 'obj' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.5
69 21 my ($t, $v) = @_;
70  
71 21 my $tname = shift @{$t};
72 21 ▶ if (@{$t}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$t;}) 0 21 21
73 0 die "Internal error: ".
74 "identify_type() returned ".
75 0 "extra data after 'obj': @{$t}\n";
76 }
77 21 return $objfunc->($v,
78 Class::MOP::Class->initialize($tname));
79 },
80 54 ▶ 'num' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
81 54 return $val + 0;
82 },
83 15 ▶ 'str' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
84 15 return $val."";
85 },
86 0 ▶ 'bool' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 return !!$val;
88 },
89 27 ▶ 'maybe' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
90 27 my ($t, $v) = @_;
91  
92 27 ▶ return undef unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 14 13 27
93 13 my $f = shift @{$t};
94 13 return $handlers{$f}->($t, $v, $a);
95 },
96 7 ▶ 'array' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
97 7 my ($t, $v) = @_;
98  
99 return [ map {
100 15 get_type_value($_, [@{$t}], $objfunc)
101 7 } @{$v} ];
102 },
103 2 ▶ 'hash' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
104 2 my ($t, $v) = @_;
105  
106 return { map {
107 6 ($_, get_type_value($v->{$_}, [@{$t}], $objfunc))
108 2 } keys %{$v} };
109 },
110 113 );
111  
112 113 my $type_first = shift @{$type};
113 113 return $handlers{$type_first}->($type, $val);
114 }
115  
116 4 func meta_to_json(Object $obj, Moose::Meta::Class $meta)
117 4 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine meta_to_json: called CC=13 SCAR=26.9
Branch: 5 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 28 28
unless ($Method::Signatures::mutc{"cache"}{"Object"} ||= "Method::Signatures"->_make_constraint("Object"))->check($obj) 0 28 28
unless scalar @_ > 1 0 28 28
unless ($Method::Signatures::mutc{"cache"}{"Moose::Meta::Class"} ||= "Method::Signatures"->_make_constraint("Moose::Meta::Class"))->check($meta) 0 28 28
if scalar @_ > 2 0 28 28
Condition66%4/6
$Method::Signatures::mutc{"cache"}{"Object"} ||= "Method::Signatures"->_make_constraint("Object")
l!l&&r!l&&!r
2620
$Method::Signatures::mutc{"cache"}{"Moose::Meta::Class"} ||= "Method::Signatures"->_make_constraint("Moose::Meta::Class")
l!l&&r!l&&!r
2710
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Object"} ||= "Method::Signatures"->_make_constraint("Object")
$Method::Signatures::mutc{"cache"}{"Object"} "Method::Signatures"->_make_constraint("Object")
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Moose::Meta::Class"} ||= "Method::Signatures"->_make_constraint("Moose::Meta::Class")
$Method::Signatures::mutc{"cache"}{"Moose::Meta::Class"} "Method::Signatures"->_make_constraint("Moose::Meta::Class")
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Object"}B= "Method::Signatures"->_make_constraint("Object")
ABresult
1X1
011
000
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Moose::Meta::Class"}B= "Method::Signatures"->_make_constraint("Moose::Meta::Class")
ABresult
1X1
011
000
118 28 my @attrs = $meta->get_all_attributes;
119 28 my $res = {};
120 28 for my $attr (@attrs) {
121 78 my $name = $attr->name;
122 78 my $hname = $name;
123 78 ▶ if ($attr->has_applied_traits &&
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->has_applied_traits and grep(($_ eq "MooseX::Role::JSONObject::Meta::Trait"), @{$attr->applied_traits;})) 2 76 78
Condition66%2/3
$attr->has_applied_traits and grep(($_ eq "MooseX::Role::JSONObject::Meta::Trait"), @{$attr->applied_traits;})
!ll&&!rl&&r
7602
MC/DC50%1/2
$attr->has_applied_traits and grep(($_ eq "MooseX::Role::JSONObject::Meta::Trait"), @{$attr->applied_traits;})
$attr->has_applied_traits grep(($_ eq "MooseX::Role::JSONObject::Meta::Trait"), @{$attr->applied_traits;})
Truth table66%2/3
A and B
A= $attr->has_applied_traitsB= grep(($_ eq "MooseX::Role::JSONObject::Meta::Trait"), @{$attr->applied_traits;})
ABresult
0X0
100
111
124 2 grep $_ eq 'MooseX::Role::JSONObject::Meta::Trait', @{$attr->applied_traits}) {
125 2 $hname = $attr->json_attr;
126 }
127 78 my $v = $obj->{$name};
128  
129 78 my $ok;
130 78 eval {
131 78 $res->{$hname} = get_value($v, $attr, \&meta_to_json);
132 77 $ok = 1;
133 };
134 78 my $msg = $@;
135 78 ▶ if (!$ok && $msg ne "MooseX::Role::JSONObject::SKIP\n") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok and $msg ne "MooseX::Role::JSONObject::SKIP\n") 0 78 78
Condition66%2/3
not $ok and $msg ne "MooseX::Role::JSONObject::SKIP\n"
!ll&&!rl&&r
7710
MC/DC0%0/2
not $ok and $msg ne "MooseX::Role::JSONObject::SKIP\n"
not $ok $msg ne "MooseX::Role::JSONObject::SKIP\n"
Truth table66%2/3
not(A) and B
A= not $okB= $msg ne "MooseX::Role::JSONObject::SKIP\n"
ABresult
0X0
100
111
136 0 die "$msg";
137 }
138 }
139 28 return $res;
140 }
141  
142 4 func meta_from_json(HashRef $data, Moose::Meta::Class $meta)
143 4 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.4
Subroutine meta_from_json: called CC=13 SCAR=27.6
Branch: 5 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 5 5
unless ($Method::Signatures::mutc{"cache"}{"HashRef"} ||= "Method::Signatures"->_make_constraint("HashRef"))->check($data) 0 5 5
unless scalar @_ > 1 0 5 5
unless ($Method::Signatures::mutc{"cache"}{"Moose::Meta::Class"} ||= "Method::Signatures"->_make_constraint("Moose::Meta::Class"))->check($meta) 0 5 5
if scalar @_ > 2 0 5 5
Condition50%3/6
$Method::Signatures::mutc{"cache"}{"HashRef"} ||= "Method::Signatures"->_make_constraint("HashRef")
l!l&&r!l&&!r
500
$Method::Signatures::mutc{"cache"}{"Moose::Meta::Class"} ||= "Method::Signatures"->_make_constraint("Moose::Meta::Class")
l!l&&r!l&&!r
320
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"HashRef"} ||= "Method::Signatures"->_make_constraint("HashRef")
$Method::Signatures::mutc{"cache"}{"HashRef"} "Method::Signatures"->_make_constraint("HashRef")
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Moose::Meta::Class"} ||= "Method::Signatures"->_make_constraint("Moose::Meta::Class")
$Method::Signatures::mutc{"cache"}{"Moose::Meta::Class"} "Method::Signatures"->_make_constraint("Moose::Meta::Class")
Truth table33%1/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"HashRef"}B= "Method::Signatures"->_make_constraint("HashRef")
ABresult
1X1
011
000
Truth table66%2/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Moose::Meta::Class"}B= "Method::Signatures"->_make_constraint("Moose::Meta::Class")
ABresult
1X1
011
000
144 5 my @attrs = $meta->get_all_attributes;
145 5 my %res;
146 5 for my $attr (@attrs) {
147 15 my $name = $attr->name;
148 15 my $hname = $name;
149 15 ▶ if ($attr->has_applied_traits &&
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->has_applied_traits and grep(($_ eq "MooseX::Role::JSONObject::Meta::Trait"), @{$attr->applied_traits;})) 2 13 15
Condition66%2/3
$attr->has_applied_traits and grep(($_ eq "MooseX::Role::JSONObject::Meta::Trait"), @{$attr->applied_traits;})
!ll&&!rl&&r
1302
MC/DC50%1/2
$attr->has_applied_traits and grep(($_ eq "MooseX::Role::JSONObject::Meta::Trait"), @{$attr->applied_traits;})
$attr->has_applied_traits grep(($_ eq "MooseX::Role::JSONObject::Meta::Trait"), @{$attr->applied_traits;})
Truth table66%2/3
A and B
A= $attr->has_applied_traitsB= grep(($_ eq "MooseX::Role::JSONObject::Meta::Trait"), @{$attr->applied_traits;})
ABresult
0X0
100
111
150 2 grep $_ eq 'MooseX::Role::JSONObject::Meta::Trait', @{$attr->applied_traits}) {
151 2 $hname = $attr->json_attr;
152 }
153 15 my $v = $data->{$hname};
154  
155 15 my $ok;
156 15 eval {
157 15 $res{$name} = get_value($v, $attr, \&meta_from_json);
158 15 $ok = 1;
159 };
160 15 my $msg = $@;
161 15 ▶ if (!$ok && $msg ne "MooseX::Role::JSONObject::SKIP\n") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok and $msg ne "MooseX::Role::JSONObject::SKIP\n") 0 15 15
Condition33%1/3
not $ok and $msg ne "MooseX::Role::JSONObject::SKIP\n"
!ll&&!rl&&r
1500
MC/DC0%0/2
not $ok and $msg ne "MooseX::Role::JSONObject::SKIP\n"
not $ok $msg ne "MooseX::Role::JSONObject::SKIP\n"
Truth table33%1/3
not(A) and B
A= not $okB= $msg ne "MooseX::Role::JSONObject::SKIP\n"
ABresult
0X0
100
111
162 0 die "$msg";
163 }
164 }
165 5 return $meta->new_object(%res);
166 }
167  
168 4 1;
169 __END__
↑ Summary JSONObject.pm »