FreeIPA/API/Convert.pm

filter: Statementstmt 100% 95 / 95 Branchbran 100% 34 / 34 Conditioncond 87.5% 14 / 16 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 97.6% 166 / 170 CC 28 SCAR 20.3
CC
28
Cov
99%
check_command
13.0
process_args
9.0
convert
7.0
CRAP 7.6
« FreeIPA/Error.pm ↑ Summary FreeIPA/API/Function.pm »
1 package Net::FreeIPA::API::Convert;
2 1 $Net::FreeIPA::API::Convert::VERSION = '3.0.3';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings qw(FATAL numeric);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Net::FreeIPA::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # cannout use 'use Types::Serialiser'; it is incompatible with JSON::XS 2.X (eg on EL6)
9 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our @EXPORT_OK = qw(process_args);
15  
16 # Convert dispatch table
17 Readonly::Hash my %CONVERT_DISPATCH => {
18 3 ▶ str => sub {my $val = shift; return "$val";}, # stringify
Subroutine __ANON__: called CC=1 SCAR=0.0
19 7 ▶ int => sub {my $val = shift; return 0 + $val;}, # Force internal conversion to int
Subroutine __ANON__: called CC=1 SCAR=0.0
20 2 ▶ float => sub {my $val = shift; return 1.0 * $val;}, # Force internal conversion to float
Subroutine __ANON__: called CC=1 SCAR=0.0
21 14 ▶ bool => 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 ? : 9 5 14
22 1 };
23  
24 # Aliases for each dispatch
25 1 Readonly::Hash my %CONVERT_ALIAS => {
26 str => [qw(unicode DNSName)],
27 };
28  
29 1 Readonly my $API_RPC_OPTION_PATTERN => '^__';
30  
31  
32 =head1 NAME
33  
34 Net::FreeIPA::Convert provides type conversion for Net::FreeIPA
35  
36 =head2 Public functions
37  
38 =over
39  
40 =item convert
41  
42 Convert/cast value to type.
43  
44 If a type is not found in the dispatch tabel, log a warning and return the value as-is.
45  
46 Always returns value, dies when dealing with non-convertable type (using 'FATAL numeric').
47  
48 =cut
49  
50 # Do not use intermediate variables for the result
51  
52 sub convert
53 21 ▶{
Subroutine convert: called CC=7 SCAR=19.5
Pod documented
54 21 my ($value, $type) = @_;
55  
56 21 my $funcref = $CONVERT_DISPATCH{$type};
57  
58 21 ▶ if(!defined($funcref)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $funcref) 3 18 21
59 # is it an alias?
60 3 foreach my $tmpref (sort keys %CONVERT_ALIAS) {
61 3 ▶ $funcref = $CONVERT_DISPATCH{$tmpref} if (grep {$_ eq $type} @{$CONVERT_ALIAS{$tmpref}});
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $type;} @{$CONVERT_ALIAS{$tmpref};} 2 1 3
62 }
63 };
64  
65 21 ▶ if (defined($funcref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $funcref) { } 20 1 21
66 20 my $vref = ref($value);
67 20 ▶ if ($vref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($vref eq 'ARRAY') { } 3 17 20
elsif ($vref eq 'HASH') { } 1 16 17
68 3 return [map {$funcref->($_)} @$value];
69 } elsif ($vref eq 'HASH') {
70 1 return {map {$_ => $funcref->($value->{$_})} sort keys %$value};
71 } else {
72 16 return $funcref->($value);
73 };
74 } else {
75 1 return $value;
76 }
77 }
78  
79 =item check_command
80  
81 Given the (single) command hashref C<cmd> and C<value>,
82 verify the value, convert it and add it to C<where>.
83  
84 (Adding to where is required to avoid using intermdiadate varaibles
85 which can cause problems for the internal types).
86  
87 Returns errormessage (which is undef on success).
88  
89 =cut
90  
91 sub check_command
92 23 ▶{
Subroutine check_command: called CC=13 SCAR=25.6
Pod documented
93 23 my ($cmd, $value, $where) = @_;
94  
95 23 my $errmsg;
96  
97 23 my $ref = ref($value);
98 23 my $name = $cmd->{name};
99  
100 # Check mandatory / undef
101 # only mandatory if required and no autofill/default
102 23 ▶ my $mandatory = ($cmd->{required} && (! $cmd->{autofill})) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cmd->{'required'} && !$cmd->{'autofill'} ? : 8 15 23
Condition100%3/3
$cmd->{"required"} and not $cmd->{"autofill"}
!ll&&!rl&&r
4118
MC/DC100%2/2
$cmd->{"required"} and not $cmd->{"autofill"}
$cmd->{"required"} not $cmd->{"autofill"}
Truth table100%3/3
A and not(B)
A= $cmd->{"required"}B= not $cmd->{"autofill"}
ABresult
0X0
100
111
103  
104 # Check multivalue
105 23 ▶ my $multi = $cmd->{multivalue} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cmd->{'multivalue'} ? : 4 19 23
106  
107 23 ▶ if (! defined($value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 6 17 23
elsif (not $ref || $multi or $ref eq 'ARRAY' and $multi) { } 11 6 17
108 6 ▶ if ($mandatory) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mandatory) 3 3 6
109 3 $errmsg = "name $name mandatory with undefined value";
110 };
111 ▶ } elsif((! $ref && ! $multi) ||
Condition100%9/9
not $ref || $multi or $ref eq "ARRAY" and $multi
l!l&&r!l&&!r
926
$ref || $multi
l!l&&r!l&&!r
719
$ref eq "ARRAY" and $multi
!ll&&!rl&&r
332
MC/DC100%4/4
not $ref || $multi or $ref eq "ARRAY" and $multi
$ref $multi $ref eq "ARRAY" $multi
Truth table71%5/7
not(A || B) or C and D
A= $refB= $multiC= $ref eq "ARRAY"D= $multi
ABCDresult
00XX1
1X111
01111
1X0X0
1X100
010X0
01100
112 (($ref eq 'ARRAY') && $multi) ) {
113 # Convert and add to where
114 11 my $wref = ref($where);
115 11 local $@;
116 11 eval {
117 11 ▶ if ($wref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($wref eq 'ARRAY') { } 7 4 11
elsif ($wref eq 'HASH') { } 3 1 4
118 7 push(@$where, convert($value, $cmd->{type}));
119 } elsif ($wref eq 'HASH') {
120 3 $where->{$name} = convert($value, $cmd->{type});
121 } else {
122 1 $errmsg = "name $name unknown where ref $wref";
123 };
124 };
125 11 ▶ $errmsg = "name $name where ref $wref died $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 10 11
126 } else {
127 6 $errmsg = "name $name wrong multivalue (multi $multi, ref $ref)";
128 };
129  
130 23 return $errmsg;
131 }
132  
133 =item process_args
134  
135 Given the command hasref C<cmds> and the arguments passed, return
136  
137 =over
138  
139 =item errmsg: an error message in case of failure
140  
141 =item posarg: arrayref with positional arguments
142  
143 =item opts: hasref with options
144  
145 =item rpc: hashref with options for the RPC call
146  
147 (All options starting with C<__> are passed as options to
148 C<Net::FreeIPA::RPC::rpc>, with C<__> prefix removed).
149  
150 =back
151  
152 Positional argument and option values are converted
153 using C<convert> function.
154  
155 =cut
156  
157 sub process_args
158 6 ▶{
Subroutine process_args: called CC=9 SCAR=22.0
Pod documented
159 6 my ($cmds, @args) = @_;
160  
161 6 my $cmdname = $cmds->{name};
162  
163 6 my $posargs = [];
164 6 my $opts = {};
165 6 my $rpc = {};
166 6 my $errmsg;
167  
168 5 ▶ my $err_req = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
169 5 $errmsg = join(" ", "$cmdname:", shift, $errmsg);
170 5 return mkrequest($cmdname, error => $errmsg);
171 6 };
172  
173 # Check posargs
174 6 my $aidx = 0;
175 6 ▶ foreach my $cmd (@{$cmds->{takes_args} || []}) {
Condition50%1/2
$cmds->{'takes_args'} || []
l!l
60
MC/DC0%0/1
$cmds->{'takes_args'} || []
$cmds->{'takes_args'}
Truth table50%1/2
A
A= $cmds->{'takes_args'}
Aresult
11
00
176 6 $aidx += 1;
177 6 $errmsg = check_command($cmd, shift(@args), $posargs);
178 6 ▶ return &$err_req("$aidx-th argument") if $errmsg;
Branch: 1 branch
BranchTrueFalseTotal
if $errmsg 2 4 6
179 }
180  
181 # Check options
182 4 my %origopts = @args;
183  
184 # Process all options
185 # The processed options are removed from %origopts
186 4 ▶ foreach my $cmd (@{$cmds->{takes_options} || []}) {
Condition50%1/2
$cmds->{'takes_options'} || []
l!l
40
MC/DC0%0/1
$cmds->{'takes_options'} || []
$cmds->{'takes_options'}
Truth table50%1/2
A
A= $cmds->{'takes_options'}
Aresult
11
00
187 4 my $name = $cmd->{name};
188 4 $errmsg = check_command($cmd, delete $origopts{$name}, $opts);
189 4 ▶ return &$err_req("option") if $errmsg;
Branch: 1 branch
BranchTrueFalseTotal
if $errmsg 2 2 4
190 }
191  
192 # Filter out any RPC options
193 # Any remaing key is invalid
194 2 foreach my $name (sort keys %origopts) {
195 2 ▶ if ($name =~ m/$API_RPC_OPTION_PATTERN/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /$API_RPC_OPTION_PATTERN/) { } 1 1 2
196 1 my $val = $origopts{$name};
197 1 $name =~ s/$API_RPC_OPTION_PATTERN//;
198 1 $rpc->{$name} = $val;
199 } else {
200 1 return &$err_req("option invalid name $name");
201 };
202 }
203  
204 # No error
205 1 return mkrequest($cmdname, args => $posargs, opts => $opts, rpc => $rpc);
206 }
207  
208 =pod
209  
210 =back
211  
212 =cut
213  
214 1 1;
« FreeIPA/Error.pm ↑ Summary FreeIPA/API/Function.pm »