FreeIPA/API/Convert.pm

filter: Statementstmt 100% 95 / 95 Branchbran 94.7% 36 / 38 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 98.8% 166 / 168 SCAR 33.3
CC
28
Cov
99%
check_command
13.0
process_args
9.0
convert
7.0
CRAP 28.0
« FreeIPA/Error.pm ↑ Summary FreeIPA/API.pm »
1 package Net::FreeIPA::API::Convert;
2 1 $Net::FreeIPA::API::Convert::VERSION = '3.0.0';
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 1 ▶use Types::Serialiser; # is used by JSON::XS
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our @EXPORT_OK = qw(process_args);
14  
15 # Convert dispatch table
16 Readonly::Hash my %CONVERT_DISPATCH => {
17 3 ▶ str => sub {my $val = shift; return "$val";}, # stringify
Subroutine __ANON__: called CC=1 SCAR=0.0
18 7 ▶ int => sub {my $val = shift; return 0 + $val;}, # Force internal conversion to int
Subroutine __ANON__: called CC=1 SCAR=0.0
19 2 ▶ float => sub {my $val = shift; return 1.0 * $val;}, # Force internal conversion to float
Subroutine __ANON__: called CC=1 SCAR=0.0
20 14 ▶ bool => sub {my $val = shift; return $val ? Types::Serialiser::true : Types::Serialiser::false;},
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 9 5 14
21 1 };
22  
23 # Aliases for each dispatch
24 1 Readonly::Hash my %CONVERT_ALIAS => {
25 str => [qw(unicode DNSName)],
26 };
27  
28 1 Readonly my $API_RPC_OPTION_PATTERN => '^__';
29  
30  
31 =head1 NAME
32  
33 Net::FreeIPA::Convert provides type conversion for Net::FreeIPA
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 tabel, 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 {
53 21 ▶ my ($value, $type) = @_;
Subroutine convert: called CC=7 SCAR=19.5
Pod documented
54  
55 21 my $funcref = $CONVERT_DISPATCH{$type};
56  
57 21 ▶ if(!defined($funcref)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $funcref) 3 18 21
58 # is it an alias?
59 3 foreach my $tmpref (sort keys %CONVERT_ALIAS) {
60 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
61 }
62 };
63  
64 21 ▶ if (defined($funcref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $funcref) { } 20 1 21
65 20 my $vref = ref($value);
66 20 ▶ if ($vref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($vref eq 'ARRAY') { } 3 17 20
elsif ($vref eq 'HASH') { } 1 16 17
67 3 return [map {$funcref->($_)} @$value];
68 } elsif ($vref eq 'HASH') {
69 1 return {map {$_ => $funcref->($value->{$_})} sort keys %$value};
70 } else {
71 16 return $funcref->($value);
72 };
73 } else {
74 1 return $value;
75 }
76 }
77  
78 =item check_command
79  
80 Given the (single) command hashref C<cmd> 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 intermdiadate varaibles
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_command
91 {
92 23 ▶ my ($cmd, $value, $where) = @_;
Subroutine check_command: called CC=13 SCAR=25.6
Pod documented
93  
94 23 my $errmsg;
95  
96 23 my $ref = ref($value);
97 23 my $name = $cmd->{name};
98  
99 # Check mandatory / undef
100 # only mandatory if required and no autofill/default
101 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
102  
103 # Check multivalue
104 23 ▶ my $multi = $cmd->{multivalue} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$cmd->{'multivalue'} ? : 4 19 23
105  
106 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
107 6 ▶ if ($mandatory) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mandatory) 3 3 6
108 3 $errmsg = "name $name mandatory with undefined value";
109 };
110 ▶ } 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
111 (($ref eq 'ARRAY') && $multi) ) {
112 # Convert and add to where
113 11 my $wref = ref($where);
114 11 local $@;
115 11 eval {
116 11 ▶ if ($wref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($wref eq 'ARRAY') { } 7 4 11
elsif ($wref eq 'HASH') { } 3 1 4
117 7 push(@$where, convert($value, $cmd->{type}));
118 } elsif ($wref eq 'HASH') {
119 3 $where->{$name} = convert($value, $cmd->{type});
120 } else {
121 1 $errmsg = "name $name unknown where ref $wref";
122 };
123 };
124 11 ▶ $errmsg = "name $name where ref $wref died $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 10 11
125 } else {
126 6 $errmsg = "name $name wrong multivalue (multi $multi, ref $ref)";
127 };
128  
129 23 return $errmsg;
130 }
131  
132 =item process_args
133  
134 Given the command hasref C<cmds> and the arguments passed, return
135  
136 =over
137  
138 =item errmsg: an error message in case of failure
139  
140 =item posarg: arrayref with positional arguments
141  
142 =item opts: hasref with options
143  
144 =item rpc: hashref with options for the RPC call
145  
146 (All options starting with C<__> are passed as options to
147 C<Net::FreeIPA::RPC::rpc>, with C<__> prefix removed).
148  
149 =back
150  
151 Positional argument and option values are converted
152 using C<convert> function.
153  
154 =cut
155  
156 sub process_args
157 {
158 6 ▶ my ($cmds, @args) = @_;
Subroutine process_args: called CC=9 SCAR=22.0
Pod documented
159  
160 6 my $cmdname = $cmds->{name};
161  
162 6 my $posargs = [];
163 6 my $opts = {};
164 6 my $rpc = {};
165 6 my $errmsg;
166  
167 my $err_req = sub {
168 5 ▶ $errmsg = join(" ", "$cmdname:", shift, $errmsg);
Subroutine __ANON__: called CC=1 SCAR=0.0
169 5 return mkrequest($cmdname, error => $errmsg);
170 6 };
171  
172 # Check posargs
173 6 my $aidx = 0;
174 6 ▶ foreach my $cmd (@{$cmds->{takes_args} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$cmds->{"takes_args"} or [] 0 6 6
175 6 $aidx += 1;
176 6 $errmsg = check_command($cmd, shift(@args), $posargs);
177 6 ▶ return &$err_req("$aidx-th argument") if $errmsg;
Branch: 1 branch
BranchTrueFalseTotal
if $errmsg 2 4 6
178 }
179  
180 # Check options
181 4 my %origopts = @args;
182  
183 # Process all options
184 # The processed options are removed from %origopts
185 4 ▶ foreach my $cmd (@{$cmds->{takes_options} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$cmds->{"takes_options"} or [] 0 4 4
186 4 my $name = $cmd->{name};
187 4 $errmsg = check_command($cmd, delete $origopts{$name}, $opts);
188 4 ▶ return &$err_req("option") if $errmsg;
Branch: 1 branch
BranchTrueFalseTotal
if $errmsg 2 2 4
189 }
190  
191 # Filter out any RPC options
192 # Any remaing key is invalid
193 2 foreach my $name (sort keys %origopts) {
194 2 ▶ if ($name =~ m/$API_RPC_OPTION_PATTERN/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /$API_RPC_OPTION_PATTERN/) { } 1 1 2
195 1 my $val = $origopts{$name};
196 1 $name =~ s/$API_RPC_OPTION_PATTERN//;
197 1 $rpc->{$name} = $val;
198 } else {
199 1 return &$err_req("option invalid name $name");
200 };
201 }
202  
203 # No error
204 1 return mkrequest($cmdname, args => $posargs, opts => $opts, rpc => $rpc);
205 }
206  
207 =pod
208  
209 =back
210  
211 =cut
212  
213 1 1;
« FreeIPA/Error.pm ↑ Summary FreeIPA/API.pm »