Client/API/Convert.pm

filter: Statementstmt 95.2% 100 / 105 Branchbran 85.0% 34 / 40 Conditioncond 83.3% 10 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 90.6% 164 / 181 CC 31 SCAR 21.9
CC
31
Cov
92%
process_args
15.1
check_option
10.1
convert
7.3
CRAP 8.9
« Client/Error.pm ↑ Summary Client/Auth.pm »
1 package Net::OpenStack::Client::API::Convert;
2 10 $Net::OpenStack::Client::API::Convert::VERSION = '0.1.4';
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings qw(FATAL numeric);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Net::OpenStack::Client::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # cannot use 'use Types::Serialiser'; it is incompatible with JSON::XS 2.X (eg on EL6)
9 10 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 10 our @EXPORT_OK = qw(convert process_args);
15  
16 # Convert dispatch table
17 Readonly::Hash my %CONVERT_DISPATCH => {
18 26 ▶ string => sub {my $val = shift; return "$val";}, # stringify
Subroutine __ANON__: called CC=1 SCAR=0.0
19 11 ▶ long => sub {my $val = shift; return 0 + $val;}, # Force internal conversion to int/long
Subroutine __ANON__: called CC=1 SCAR=0.0
20 2 ▶ double => sub {my $val = shift; return 1.0 * $val;}, # Force internal conversion to float/double
Subroutine __ANON__: called CC=1 SCAR=0.0
21 22 ▶ boolean => sub {my $val = shift; return $val ? JSON::XS::true : JSON::XS::false;},
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 16 6 22
22 10 };
23  
24 # Aliases for each dispatch
25 10 Readonly::Hash my %CONVERT_ALIAS => {
26 };
27  
28 10 Readonly my $API_REST_OPTION_PATTERN => '^__';
29  
30  
31 =head1 NAME
32  
33 Net::OpenStack::Client::API::Convert provides type conversion for Net::OpenStack
34  
35 =head2 Public functions
36  
37 =over
38  
39 =item convert
40  
41 Convert/cast value to type.
42  
43 If a type is not found in the dispatch table, log a warning and return the value as-is.
44  
45 Always returns value, dies when dealing with non-convertable type (using 'FATAL numeric').
46  
47 =cut
48  
49 # Do not use intermediate variables for the result
50  
51 sub convert
52 103 ▶{
Subroutine convert: called CC=7 SCAR=19.9
Pod documented
53 103 my ($value, $type) = @_;
54  
55 103 my $funcref = $CONVERT_DISPATCH{$type};
56  
57 103 ▶ if (!defined($funcref)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $funcref) 46 57 103
58 # is it an alias?
59 46 foreach my $tmpref (sort keys %CONVERT_ALIAS) {
60 0 ▶ $funcref = $CONVERT_DISPATCH{$tmpref} if (grep {$_ eq $type} @{$CONVERT_ALIAS{$tmpref}});
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $type;} @{$CONVERT_ALIAS{$tmpref};} 0 0 0
61 }
62 };
63  
64 103 ▶ if (defined($funcref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $funcref) { } 57 46 103
65 57 my $vref = ref($value);
66 57 ▶ if ($vref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($vref eq 'ARRAY') { } 4 53 57
elsif ($vref eq 'HASH') { } 1 52 53
67 4 return [map {$funcref->($_)} @$value];
68 } elsif ($vref eq 'HASH') {
69 1 return {map {$_ => $funcref->($value->{$_})} sort keys %$value};
70 } else {
71 52 return $funcref->($value);
72 };
73 } else {
74 46 return $value;
75 }
76 }
77  
78 =item check_option
79  
80 Given the (single) option hashref C<option> and C<value>,
81 verify the value, convert it and add it to C<where>.
82  
83 (Adding to where is required to avoid using intermediadate variables
84 which can cause problems for the internal types).
85  
86 Returns errormessage (which is undef on success).
87  
88 =cut
89  
90 sub check_option
91 120 ▶{
Subroutine check_option: called CC=10 SCAR=23.1
Pod documented
92 120 my ($opt, $value, $where, $attr) = @_;
93  
94 120 my $errmsg;
95  
96 120 my $ref = ref($value);
97 120 my $name = $opt->{name};
98  
99 120 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 0 120 120
100 # insert value attribute if needed. Reset where to this attribute
101 };
102  
103 # Check mandatory / undef
104 120 ▶ my $mandatory = $opt->{required} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'required'} ? : 52 68 120
105  
106 120 ▶ if (! defined($value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 38 82 120
elsif (not $ref or $ref eq 'ARRAY') { } 82 0 82
107 38 ▶ if ($mandatory) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mandatory) 5 33 38
108 5 $errmsg = "name $name mandatory with undefined value";
109 };
110 ▶ } elsif (!$ref || $ref eq 'ARRAY') {
Condition66%2/3
not $ref or $ref eq "ARRAY"
l!l&&r!l&&!r
7930
MC/DC0%0/2
not $ref or $ref eq "ARRAY"
not $ref $ref eq "ARRAY"
Truth table66%2/3
not(A) or B
A= not $refB= $ref eq "ARRAY"
ABresult
1X1
011
000
111 # Convert and add to where
112 82 my $wref = ref($where);
113 82 local $@;
114 82 eval {
115 82 ▶ if ($wref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($wref eq 'ARRAY') { } 2 80 82
elsif ($wref eq 'HASH') { } 79 1 80
116 2 push(@$where, convert($value, $opt->{type}));
117 } elsif ($wref eq 'HASH') {
118 79 $where->{$name} = convert($value, $opt->{type});
119 } else {
120 1 $errmsg = "name $name unknown where ref $wref";
121 };
122 };
123 82 ▶ $errmsg = "name $name where ref $wref died $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 80 82
124 } else {
125 0 $errmsg = "name $name wrong multivalue (ref $ref)";
126 };
127  
128 120 return $errmsg;
129 }
130  
131 =item process_args
132  
133 Given the command hashref C<cmdhs> and the arguments passed, return
134 Request instance.
135  
136 Command hashref
137  
138 =over
139  
140 =item endpoint
141  
142 =item method
143  
144 =item templates (optional)
145  
146 =item parameters (optional)
147  
148 =item options (optional)
149  
150 (All options starting with C<__> are passed as options to
151 C<Net::OpenStack::Client::REST::rest>, with C<__> prefix removed).
152  
153 =back
154  
155 Request instance:
156  
157 =over
158  
159 =item error: an error message in case of failure
160  
161 =item tpls: hashref with templates for endpoint
162  
163 =item params: hashref with parameters for endpoint
164  
165 =item opts: hashref with options
166  
167 =item rest: hashref with options for the REST call
168  
169 =back
170  
171 Values are converted using C<convert> function.
172  
173 =cut
174  
175 sub process_args
176 38 ▶{
Subroutine process_args: called CC=15 SCAR=27.1
Pod documented
177 38 my ($cmdhs, @args) = @_;
178  
179 # template name and value
180 38 my $templates = {};
181 # parameters name and value
182 38 my $parameters = {};
183 # option name and value
184 38 my $options = {};
185 # option name and path (separate from option values)
186 38 my $paths = {};
187 # rest options
188 38 my $rest = {};
189  
190 38 my $errmsg;
191  
192 38 my $endpoint = $cmdhs->{endpoint};
193 38 my $method = $cmdhs->{method};
194  
195 6 ▶ my $err_req = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
196 6 $errmsg = join(" ", "$endpoint $method:", shift, $errmsg);
197 6 return mkrequest($endpoint, $method, error => $errmsg);
198 38 };
199  
200 38 my %origopts = @args;
201  
202 38 my $raw = delete $origopts{raw};
203 38 ▶ if ($raw && ref($raw) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw and ref $raw ne "HASH") 0 38 38
Condition66%2/3
$raw and ref $raw ne "HASH"
!ll&&!rl&&r
3710
MC/DC0%0/2
$raw and ref $raw ne "HASH"
$raw ref $raw ne "HASH"
Truth table66%2/3
A and B
A= $rawB= ref $raw ne "HASH"
ABresult
0X0
100
111
204 0 return &$err_req("raw option must be a hashref, got ".ref($raw));
205 }
206  
207 # Check endpoint template values; sort of mandatory special named options
208 # The processed options are removed from %origopts
209 # TODO: naming conflict between JSON key, parameter and template name? (handled in gen.pl)
210 38 ▶ foreach my $name (@{$cmdhs->{templates} || []}) {
Condition100%2/2
$cmdhs->{'templates'} || []
l!l
2612
MC/DC100%1/1
$cmdhs->{'templates'} || []
$cmdhs->{'templates'}
Truth table100%2/2
A
A= $cmdhs->{'templates'}
Aresult
11
00
211 # all strings, used for templating
212 40 $errmsg = check_option({name => $name, required => 1, type => 'str'}, delete $origopts{$name}, $templates);
213 40 ▶ return &$err_req("endpoint template $name") if $errmsg;
Branch: 1 branch
BranchTrueFalseTotal
if $errmsg 2 38 40
214 }
215  
216 # Check parameters
217 36 ▶ foreach my $name (@{$cmdhs->{parameters} || []}) {
Condition100%2/2
$cmdhs->{'parameters'} || []
l!l
1224
MC/DC100%1/1
$cmdhs->{'parameters'} || []
$cmdhs->{'parameters'}
Truth table100%2/2
A
A= $cmdhs->{'parameters'}
Aresult
11
00
218 # all strings, used for url buildup
219 30 $errmsg = check_option({name => $name, type => 'str'}, delete $origopts{$name}, $parameters);
220 30 ▶ return &$err_req("endpoint parameter $name") if $errmsg;
Branch: 1 branch
BranchTrueFalseTotal
if $errmsg 0 30 30
221 }
222  
223 # Check options
224 # Process all options (for JSON data)
225 # The processed options are removed from %origopts
226 36 ▶ foreach my $name (sort keys %{$cmdhs->{options} || {}}) {
Condition100%2/2
$cmdhs->{'options'} || {}
l!l
333
MC/DC100%1/1
$cmdhs->{'options'} || {}
$cmdhs->{'options'}
Truth table100%2/2
A
A= $cmdhs->{'options'}
Aresult
11
00
227 44 my $opt = $cmdhs->{options}->{$name};
228 44 ▶ $opt->{name} = $name if ! exists($opt->{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opt->{$name} 42 2 44
229  
230 # Need both value (added via check_option) and path
231 44 $paths->{$name} = $opt->{path};
232 44 $errmsg = check_option($opt, delete $origopts{$name}, $options);
233 44 ▶ return &$err_req("option $name") if $errmsg;
Branch: 1 branch
BranchTrueFalseTotal
if $errmsg 3 41 44
234 }
235  
236 # Filter out any REST options
237 # Any remaining key is invalid
238 33 foreach my $name (sort keys %origopts) {
239 2 ▶ if ($name =~ m/$API_REST_OPTION_PATTERN/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /$API_REST_OPTION_PATTERN/) { } 1 1 2
240 1 my $val = $origopts{$name};
241 1 $name =~ s/$API_REST_OPTION_PATTERN//;
242 1 $rest->{$name} = $val;
243 } else {
244 1 return &$err_req("option invalid name $name");
245 };
246 }
247  
248 # No error
249 return mkrequest(
250 $endpoint,
251 $method,
252 tpls => $templates,
253 params => $parameters,
254 opts => $options,
255 paths => $paths,
256 rest => $rest,
257 raw => $raw,
258 service => $cmdhs->{service},
259 version => $cmdhs->{version},
260 result => $cmdhs->{result},
261 32 );
262 }
263  
264 =pod
265  
266 =back
267  
268 =cut
269  
270 10 1;
« Client/Error.pm ↑ Summary Client/Auth.pm »