Lim/RPC.pm

filter: Statementstmt 70.4% 43 / 61 Branchbran 38.1% 16 / 42 Conditioncond 25.9% 7 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 48.0% 75 / 156 CC 34 SCAR 43.0
CC
34
Cov
51%
V
103.3
R
48.3
BEGIN
1.0
CRAP 74.0
« Lim/RPC/Transport/RabbitMQ.pm ↑ Summary Lim/RPC/Value/Collection.pm »
1 package Lim::RPC;
2  
3 10 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Lim::Error ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =encoding utf8
12  
13 =head1 NAME
14  
15 Lim::RPC - Utilities for Lim's RPC
16  
17 =head1 VERSION
18  
19 See L<Lim> for version.
20  
21 =cut
22  
23 10 our $VERSION = $Lim::VERSION;
24  
25 =head1 SYNOPSIS
26  
27 =over 4
28  
29 use Lim::RPC;
30  
31 =back
32  
33 =head1 NOTE
34  
35 These functions are mainly used internaly, you should not have any reason to
36 call them.
37  
38 =head1 METHODS
39  
40 =over 4
41  
42 =item Lim::RPC::V($q, $def)
43  
44 V is for Verify, it will verify the content of the hash ref C<$q> against the
45 RPC definition in C<$def>. On an error it will L<confess>.
46  
47 =cut
48  
49 4 ▶sub V {
Subroutine V: called CC=23 SCAR=46.4
Pod documented
50 4 my ($q, $def) = @_;
51  
52 4 ▶ if (defined $q and defined $def) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $q and defined $def) 4 0 4
Condition33%1/3
defined $q and defined $def
!ll&&!rl&&r
004
MC/DC0%0/2
defined $q and defined $def
defined $q defined $def
Truth table33%1/3
A and B
A= defined $qB= defined $def
ABresult
0X0
100
111
53 4 ▶ unless (ref($q) eq 'HASH' and ref($def) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $q eq "HASH" and ref $def eq "HASH") 0 4 4
Condition33%1/3
ref $q eq "HASH" and ref $def eq "HASH"
!ll&&!rl&&r
004
MC/DC0%0/2
ref $q eq "HASH" and ref $def eq "HASH"
ref $q eq "HASH" ref $def eq "HASH"
Truth table33%1/3
A and B
A= ref $q eq "HASH"B= ref $def eq "HASH"
ABresult
0X0
100
111
54 0 confess __PACKAGE__, ': Can not verify data, invalid parameters given';
55 }
56  
57 4 my @v = ([$q, $def]);
58 4 ▶ while (defined (my $v = shift(@v))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = shift @v)) 4 4 8
59 4 ($q, $def) = @$v;
60 4 my $a;
61  
62 4 ▶ if (ref($q) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $q eq 'ARRAY') { } 0 4 4
63 0 $a = $q;
64 }
65 else {
66 4 $a = [$q];
67 }
68  
69 4 foreach $q (@{$a}) {
70 4 ▶ unless (ref($q) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $q eq "HASH") 0 4 4
71 0 confess __PACKAGE__, ': Can not verify data, invalid data given';
72 }
73  
74 # check required
75 4 foreach my $k (keys %$def) {
76 4 ▶ if (blessed($def->{$k}) and $def->{$k}->required and !exists $q->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($def->{$k}) and $def->{$k}->required and not exists $q->{$k}) 0 4 4
Condition33%2/6
builtin::blessed($def->{$k}) and $def->{$k}->required and not exists $q->{$k}
!ll&&!rl&&r
040
builtin::blessed($def->{$k}) and $def->{$k}->required
!ll&&!rl&&r
004
MC/DC0%0/3
builtin::blessed($def->{$k}) and $def->{$k}->required and not exists $q->{$k}
builtin::blessed($def->{$k}) $def->{$k}->required not exists $q->{$k}
Truth table25%1/4
A and B and not(C)
A= builtin::blessed($def->{$k})B= $def->{$k}->requiredC= not exists $q->{$k}
ABCresult
0XX0
10X0
1100
1111
77 0 confess __PACKAGE__, ': required data (', $k, ') missing, does not match definition';
78 }
79 }
80  
81 # check data
82 4 foreach my $k (keys %$q) {
83 4 ▶ unless (exists $def->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $def->{$k}) 0 4 4
84 0 confess __PACKAGE__, ': invalid data (', $k, '), no definition exists';
85 }
86  
87 4 ▶ if (blessed($def->{$k}) and !$def->{$k}->comform($q->{$k})) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($def->{$k}) and not $def->{$k}->comform($q->{$k})) 0 4 4
Condition33%1/3
builtin::blessed($def->{$k}) and not $def->{$k}->comform($q->{$k})
!ll&&!rl&&r
040
MC/DC0%0/2
builtin::blessed($def->{$k}) and not $def->{$k}->comform($q->{$k})
builtin::blessed($def->{$k}) not $def->{$k}->comform($q->{$k})
Truth table33%1/3
A and not(B)
A= builtin::blessed($def->{$k})B= not $def->{$k}->comform($q->{$k})
ABresult
0X0
100
111
88 0 confess __PACKAGE__, ': invalid data (', $k, '), validation failed';
89 }
90  
91 4 ▶ if (ref($q->{$k}) eq 'HASH' or ref($q->{$k}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $q->{$k} eq "HASH" or ref $q->{$k} eq "ARRAY") 0 4 4
Condition33%1/3
ref $q->{$k} eq "HASH" or ref $q->{$k} eq "ARRAY"
l!l&&r!l&&!r
004
MC/DC0%0/2
ref $q->{$k} eq "HASH" or ref $q->{$k} eq "ARRAY"
ref $q->{$k} eq "HASH" ref $q->{$k} eq "ARRAY"
Truth table33%1/3
A or B
A= ref $q->{$k} eq "HASH"B= ref $q->{$k} eq "ARRAY"
ABresult
1X1
011
000
92 0 ▶ if (ref($def->{$k}) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $def->{$k} eq 'HASH') { } 0 0 0
elsif (builtin::blessed($def->{$k}) and $def->{$k}->isa('Lim::RPC::Value::Collection')) { } 0 0 0
93 0 push(@v, [$q->{$k}, $def->{$k}]);
94 }
95 ▶ elsif (blessed $def->{$k} and $def->{$k}->isa('Lim::RPC::Value::Collection')) {
Condition0%0/3
builtin::blessed($def->{$k}) and $def->{$k}->isa("Lim::RPC::Value::Collection")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($def->{$k}) and $def->{$k}->isa("Lim::RPC::Value::Collection")
builtin::blessed($def->{$k}) $def->{$k}->isa("Lim::RPC::Value::Collection")
Truth table0%0/3
A and B
A= builtin::blessed($def->{$k})B= $def->{$k}->isa("Lim::RPC::Value::Collection")
ABresult
0X0
100
111
96 0 ▶ unless ($def->{$k}->swallow) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($def->{$k}->swallow) 0 0 0
97 0 push(@v, [$q->{$k}, $def->{$k}->children]);
98 }
99 }
100 else {
101 0 confess __PACKAGE__, ': invalid definition (', $k, '), can not validate data';
102 }
103 }
104 }
105 }
106 }
107 }
108 4 return;
109 }
110  
111 =item Lim::RPC::R($cb, $data)
112  
113 R is for Result, called when a RPC call finish and convert the given C<$data> to
114 the corresponding protocol.
115  
116 =cut
117  
118 4 ▶sub R {
Subroutine R: called CC=12 SCAR=38.8
Pod documented
119 4 my ($cb, $data) = @_;
120  
121 4 ▶ unless (blessed($cb)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($cb)) 0 4 4
122 0 confess __PACKAGE__, ': cb not blessed';
123 }
124  
125 4 ▶ if (blessed($data)) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($data)) { } 0 4 4
elsif (defined $data) { } 4 0 4
126 0 ▶ if ($data->isa('Lim::Error')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->isa("Lim::Error")) 0 0 0
127 0 return $cb->cb->($data);
128 }
129 }
130 elsif (defined $data) {
131 4 ▶ unless (ref($data) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data eq "HASH") 0 4 4
132 0 confess __PACKAGE__, ': data not a hash';
133 }
134  
135 4 ▶ if ($cb->call_def and exists $cb->call_def->{out}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cb->call_def and exists $cb->call_def->{'out'}) { } 4 0 4
elsif (%$data) { } 0 0 0
Condition33%1/3
$cb->call_def and exists $cb->call_def->{"out"}
!ll&&!rl&&r
004
MC/DC0%0/2
$cb->call_def and exists $cb->call_def->{"out"}
$cb->call_def exists $cb->call_def->{"out"}
Truth table33%1/3
A and B
A= $cb->call_defB= exists $cb->call_def->{"out"}
ABresult
0X0
100
111
136 4 Lim::RPC::V($data, $cb->call_def->{out});
137 }
138 elsif (%$data) {
139 0 confess __PACKAGE__, ': data given without definition';
140 }
141 }
142 else {
143 0 ▶ if ($cb->call_def and exists $cb->call_def->{out}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb->call_def and exists $cb->call_def->{"out"}) 0 0 0
Condition0%0/3
$cb->call_def and exists $cb->call_def->{"out"}
!ll&&!rl&&r
000
MC/DC0%0/2
$cb->call_def and exists $cb->call_def->{"out"}
$cb->call_def exists $cb->call_def->{"out"}
Truth table0%0/3
A and B
A= $cb->call_defB= exists $cb->call_def->{"out"}
ABresult
0X0
100
111
144 0 Lim::RPC::V({}, $cb->call_def->{out});
145 }
146 }
147  
148 4 ▶ return $cb->cb->(defined $data ? $data : {});
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 4 0 4
149 }
150  
151 =back
152  
153 =head1 AUTHOR
154  
155 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
156  
157 =head1 BUGS
158  
159 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
160  
161 =head1 SUPPORT
162  
163 You can find documentation for this module with the perldoc command.
164  
165 perldoc Lim::RPC
166  
167 You can also look for information at:
168  
169 =over 4
170  
171 =item * Lim issue tracker (report bugs here)
172  
173 L<https://github.com/jelu/lim/issues>
174  
175 =back
176  
177 =head1 ACKNOWLEDGEMENTS
178  
179 =head1 LICENSE AND COPYRIGHT
180  
181 Copyright 2012-2013 Jerry Lundström.
182  
183 This program is free software; you can redistribute it and/or modify it
184 under the terms of either: the GNU General Public License as published
185 by the Free Software Foundation; or the Artistic License.
186  
187 See http://dev.perl.org/licenses/ for more information.
188  
189  
190 =cut
191  
192 10 1; # End of Lim::RPC
« Lim/RPC/Transport/RabbitMQ.pm ↑ Summary Lim/RPC/Value/Collection.pm »