Resolver.pm

filter: Statementstmt 80.8% 190 / 235 Branchbran 68.7% 55 / 80 Conditioncond 43.1% 19 / 44 MC/DCmcdc 24.0% 6 / 25 Subroutinesub 65.1% 28 / 43 Podpod 84.6% 11 / 13 totaltotal 70.2% 309 / 440 CC 113 SCAR 25.2
CC
113
Cov
74%
resolve_value
35.7
resolve_value
35.7
get_value
9.1
CRAP 12.4
« Resolver/CLI.pm ↑ Summary Resolver/Utils.pm »
1 package Config::Resolver;
2  
3 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 29 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 29 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 29 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 29 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶use Scalar::Util qw(looks_like_number reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 29 ▶use Config::Resolver::Utils qw(is_hash is_array slurp_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 29 local $SIG{__WARN__} = 'DEFAULT';
16 29 local $SIG{__DIE__} = 'DEFAULT';
17  
18 29 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 29 Readonly::Scalar our $TRUE => 1;
21 29 Readonly::Scalar our $FALSE => 0;
22 29 Readonly::Scalar our $EMPTY => q{};
23  
24 29 ▶use parent qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 29 __PACKAGE__->follow_best_practice;
27 29 __PACKAGE__->mk_accessors(
28 qw(
29 debug
30 function_map
31 functions
32 parameters
33 backends
34 handler_map
35 plugins
36 warning_level
37 logger
38 )
39 );
40  
41 29 our $VERSION = '1.0.12';
42  
43 ########################################################################
44 26 ▶sub new {
Subroutine new: called CC=4 SCAR=19.4
Pod documented
45 ########################################################################
46 0 ▶ my ( $class, @args ) = @_;
Subroutine new: not called CC=4 SCAR=19.4
Pod documented
47  
48 0 ▶ my $options = ref $args[0] ? $args[0] : {@args};
Branch: 2 branches
BranchTrueFalseTotal
ref $args[0] ? : 2 24 26
ref $args[0] ? : 0 0 0
49  
50 0 ▶ $options->{warning_level} //= 'error';
Condition50%2/4
$options->{"warning_level"} //= "error"
l!l
224
$options->{"warning_level"} //= "error"
l!l
00
MC/DC100%1/1
$options->{"warning_level"} //= "error"
$options->{"warning_level"}
MC/DC0%0/1
$options->{"warning_level"} //= "error"
$options->{"warning_level"}
Truth table100%2/2
A
A= $options->{"warning_level"}
Aresult
11
00
Truth table0%0/2
A
A= $options->{"warning_level"}
Aresult
11
00
51  
52 0 my $self = $class->SUPER::new($options);
53  
54 0 ▶ if ( $self->get_logger ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->get_logger) 0 26 26
if ($self->get_logger) 0 0 0
55 0 $self->get_logger->debug('initializing resolver...');
56 }
57  
58 0 $self->init($options);
59  
60 0 return $self;
61 }
62  
63 ########################################################################
64 11 ▶sub resolve {
Subroutine resolve: called CC=1 SCAR=1.2
Pod documented
65 ########################################################################
66 0 ▶ my ( $self, @args ) = @_;
Subroutine resolve: not called CC=1 SCAR=1.2
Pod documented
67  
68 0 my ($resolved) = $self->finalize_parameters(@args);
69  
70 0 return $resolved;
71 }
72  
73 ########################################################################
74 26 ▶sub init {
Subroutine init: called CC=3 SCAR=15.8
Pod documented
75 ########################################################################
76 0 ▶ my ( $self, $options ) = @_;
Subroutine init: not called CC=3 SCAR=15.8
Pod documented
77  
78 # 1. Define our "base" whitelist. These are always safe.
79 my %base_functions = (
80 3 ▶ uc => sub { return uc( $_[0] // $EMPTY ) }, # (Safer with // '')
Subroutine __ANON__: called CC=2 SCAR=9.2
Condition33%1/3
$_[0] // $Config::Resolver::EMPTY
l!l&&r!l&&!r
300
MC/DC0%0/2
$_[0] // $Config::Resolver::EMPTY
$_[0] $Config::Resolver::EMPTY
Truth table33%1/3
A // B
A= $_[0]B= $Config::Resolver::EMPTY
ABresult
1X1
011
000
81 0 ▶ lc => sub { return lc( $_[0] // $EMPTY ) },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
$_[0] // $Config::Resolver::EMPTY
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0] // $Config::Resolver::EMPTY
$_[0] $Config::Resolver::EMPTY
Truth table0%0/3
A // B
A= $_[0]B= $Config::Resolver::EMPTY
ABresult
1X1
011
000
82 0 );
83  
84 # 2. Get the user's custom functions from the 'new' options.
85 # Default to an empty hash if none are provided.
86 0 ▶ my $user_functions = $self->get_functions // {};
Condition50%2/4
$self->get_functions // {}
l!l
323
$self->get_functions // {}
l!l
00
MC/DC100%1/1
$self->get_functions // {}
$self->get_functions
MC/DC0%0/1
$self->get_functions // {}
$self->get_functions
Truth table100%2/2
A
A= $self->get_functions
Aresult
11
00
Truth table0%0/2
A
A= $self->get_functions
Aresult
11
00
87  
88 0 ▶ croak "ERROR: 'allowed_functions' must be a HASH reference\n"
Branch: 2 branches
BranchTrueFalseTotal
if builtin::reftype($user_functions) ne "HASH" 0 26 26
if builtin::reftype($user_functions) ne "HASH" 0 0 0
89 if reftype($user_functions) ne 'HASH';
90  
91 # 3. Merge them. The user's functions override the base.
92 # This is the "append" you were talking about.
93 0 my $final_function_map = { %base_functions, %{$user_functions}, };
94  
95 # 4. Store the final, merged map in our new accessor.
96 0 $self->set_function_map($final_function_map);
97  
98 0 $self->init_plugins($options);
99  
100 0 return $self;
101 }
102  
103 ########################################################################
104 26 ▶sub init_plugins {
Subroutine init_plugins: called CC=9 SCAR=22.1
Pod undocumented
105 ########################################################################
106 26 my ( $self, $options ) = @_;
107  
108 # 1. Define our "base" handlers. These are always included.
109 my %base_handlers = (
110 1 ▶ 'env' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
111 1 my ( $path, $params ) = @_;
112 1 return $ENV{$path};
113 },
114 1 ▶ 'file' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
115 1 my ( $path, $params ) = @_;
116 # We can use slurp_file now that we've imported it
117 1 return slurp_file($path);
118 },
119 26 );
120  
121 26 ▶ my $plugin_list = $self->get_plugins // [];
Condition100%2/2
$self->get_plugins // []
l!l
323
MC/DC100%1/1
$self->get_plugins // []
$self->get_plugins
Truth table100%2/2
A
A= $self->get_plugins
Aresult
11
00
122 26 ▶ my $user_handlers = $self->get_backends // {};
Condition100%2/2
$self->get_backends // {}
l!l
323
MC/DC100%1/1
$self->get_backends // {}
$self->get_backends
Truth table100%2/2
A
A= $self->get_backends
Aresult
11
00
123  
124 # This is the master config: { ssm => { region => ... }, vault => { ... } }
125 26 ▶ my $plugin_config_all = $options->{plugin_config} // {};
Condition100%2/2
$options->{"plugin_config"} // {}
l!l
125
MC/DC100%1/1
$options->{"plugin_config"} // {}
$options->{"plugin_config"}
Truth table100%2/2
A
A= $options->{"plugin_config"}
Aresult
11
00
126  
127 26 my %plugin_handlers;
128  
129 26 foreach my $plugin_name ( @{$plugin_list} ) {
130 2 my $class = "Config::Resolver::Plugin::${plugin_name}";
131 2 load $class;
132  
133 # 1. Get the config key from the new package variable
134 # This is the "correct" way to check for the package var
135 2 my $protocol;
136  
137 2 my $stash_name = $class . q{::}; # e.g., 'Config::Resolver::Plugin::SSM::'
138  
139 {
140 29 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
141  
142 2 $protocol = ${ $stash_name . 'PROTOCOL' };
143  
144 2 ▶ croak "Plugin $class must define a package variable '\$PROTOCOL'."
Branch: 1 branch
BranchTrueFalseTotal
unless $protocol 1 1 2
145 if !$protocol;
146 }
147  
148 # 2. Get the config for this plugin
149 1 ▶ my $plugin_specific_config = $plugin_config_all->{$protocol} // {};
Condition50%1/2
$plugin_config_all->{$protocol} // {}
l!l
10
MC/DC0%0/1
$plugin_config_all->{$protocol} // {}
$plugin_config_all->{$protocol}
Truth table50%1/2
A
A= $plugin_config_all->{$protocol}
Aresult
11
00
150  
151 1 my $logger = $self->get_logger;
152 1 my $log_level;
153  
154 1 ▶ if ($logger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logger) 0 1 1
155 0 $log_level = $logger->level;
156 }
157  
158 # 3. Create the object
159 my $plugin_obj = $class->new(
160 { debug => $options->{debug},
161 warning_level => $options->{warning_level},
162 logger => $logger,
163 1 %{$plugin_specific_config},
164 }
165 );
166  
167 1 ▶ if ($logger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logger) 0 1 1
168 0 $logger->level($log_level);
169 }
170  
171 1 $plugin_handlers{$protocol} = $plugin_obj;
172 }
173  
174 25 my $final_handler_map = { %base_handlers, %plugin_handlers, %{$user_handlers}, };
175  
176 25 $self->set_handler_map($final_handler_map);
177  
178 25 return $self;
179 }
180  
181 ########################################################################
182 19 ▶sub resolve_value {
Subroutine resolve_value: called CC=19 SCAR=35.7
Pod documented
183 ########################################################################
184 19 my ( $self, $value, $parameters ) = @_;
185  
186 19 my $handlers = $self->get_handler_map;
187  
188 # It checks for the "protocol" pattern first.
189 19 ▶ if ( $value =~ /^(\w+):\/\/(.+)$/xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ m[^(\w+)://(.+)$]msx) 5 14 19
190 5 my ( $prefix, $path ) = ( $1, $2 ); # $prefix is the 'xxx'
191  
192 5 ▶ if ( my $handler = $handlers->{$prefix} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handler = $handlers->{$prefix}) 5 0 5
193  
194 5 my $resolved_val;
195  
196 5 ▶ if ( reftype($handler) eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($handler) eq 'CODE') { } 5 0 5
elsif (builtin::blessed($handler) and $handler->can('resolve')) { } 0 0 0
197 5 $resolved_val = $handler->( $path, $parameters );
198 }
199 ▶ elsif ( blessed($handler) && $handler->can('resolve') ) {
Condition0%0/3
builtin::blessed($handler) and $handler->can("resolve")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($handler) and $handler->can("resolve")
builtin::blessed($handler) $handler->can("resolve")
Truth table0%0/3
A and B
A= builtin::blessed($handler)B= $handler->can("resolve")
ABresult
0X0
100
111
200 0 $resolved_val = $handler->resolve( $path, $parameters );
201 }
202 else {
203 0 croak "Invalid handler for protocol '$prefix': "
204 . 'handler must be a coderef or a blessed object '
205 . q{that implements a 'resolve' method.};
206 }
207  
208 # Handle errors/undefs
209 5 ▶ die "could not resolve [$value]\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $resolved_val and $self->get_warning_level eq "error" 0 5 5
Condition33%1/3
not defined $resolved_val and $self->get_warning_level eq "error"
!ll&&!rl&&r
500
MC/DC0%0/2
not defined $resolved_val and $self->get_warning_level eq "error"
not defined $resolved_val $self->get_warning_level eq "error"
Truth table33%1/3
not(A) and B
A= not defined $resolved_valB= $self->get_warning_level eq "error"
ABresult
0X0
100
111
210 if !defined $resolved_val && $self->get_warning_level eq 'error';
211  
212 # Return the resolved value, defaulting undef to empty string
213 5 ▶ return ( $resolved_val // $EMPTY, $parameters );
Condition33%1/3
$resolved_val // $Config::Resolver::EMPTY
l!l&&r!l&&!r
500
MC/DC0%0/2
$resolved_val // $Config::Resolver::EMPTY
$resolved_val $Config::Resolver::EMPTY
Truth table33%1/3
A // B
A= $resolved_valB= $Config::Resolver::EMPTY
ABresult
1X1
011
000
214 }
215  
216 # If no backend plugin exists, we can either 'croak' or just
217 # fall through and treat it as a literal string.
218 }
219  
220 14 ▶ if ( $value =~ /\$[{]([\S]+?)[}]/xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\$[{]([\S]+?)[}]/msx) 6 8 14
221 6 my %vars;
222  
223 6 ▶ while ( $value =~ /\$[{]([\S]+?)[}]/xsmg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\$[{]([\S]+?)[}]/gmsx) 6 5 11
224 6 my $p = $1;
225  
226 6 my $v = $self->get_parameter( $parameters, $p );
227  
228 {
229 5 local $SIG{__DIE__} = 'DEFAULT';
230  
231 5 ▶ die "could not resolve [$p]\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $v and $self->get_warning_level eq "error" 0 5 5
Condition33%1/3
not defined $v and $self->get_warning_level eq "error"
!ll&&!rl&&r
500
MC/DC0%0/2
not defined $v and $self->get_warning_level eq "error"
not defined $v $self->get_warning_level eq "error"
Truth table33%1/3
not(A) and B
A= not defined $vB= $self->get_warning_level eq "error"
ABresult
0X0
100
111
232 if !defined $v && $self->get_warning_level eq 'error';
233 }
234  
235 5 $vars{$p} = $v;
236 }
237  
238 5 foreach my $k ( keys %vars ) {
239 5 ▶ if ( !exists $vars{$k} || !defined $vars{$k} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $vars{$k} and defined $vars{$k}) 0 5 5
Condition33%1/3
exists $vars{$k} and defined $vars{$k}
!ll&&!rl&&r
005
MC/DC0%0/2
exists $vars{$k} and defined $vars{$k}
exists $vars{$k} defined $vars{$k}
Truth table33%1/3
A and B
A= exists $vars{$k}B= defined $vars{$k}
ABresult
0X0
100
111
240 0 $vars{$k} = q{};
241 0 print {*STDERR} "WARNING: $k is undefined!\n";
242 }
243  
244 5 $value =~ s/\$[{]\Q$k\E[}]/$vars{$k}/xsmg;
245 }
246 5 return ( $value, $parameters );
247 }
248  
249 8 ▶ if ( $value =~ /\$[{]([\S]+?)\s+([\w!=><]+)\s+(.+?)\s+\?\s+(.+?)\s+\:\s+(.+?)\s*[}]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\$[{]([\S]+?)\s+([\w!=><]+)\s+(.+?)\s+\?\s+(.+?)\s+\:\s+(.+?)\s*[}]/) 7 1 8
250 my %dispatch_ops = (
251 # Numeric
252 1 ▶ q{==} => sub { $_[0] == $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
253 0 ▶ q{!=} => sub { $_[0] != $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
254 0 ▶ q{>} => sub { $_[0] > $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
255 0 ▶ q{<} => sub { $_[0] < $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
256 0 ▶ q{>=} => sub { $_[0] >= $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
257 0 ▶ q{<=} => sub { $_[0] <= $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
258  
259 # String
260 5 ▶ q{eq} => sub { $_[0] eq $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
261 0 ▶ q{ne} => sub { $_[0] ne $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
262 0 ▶ q{gt} => sub { $_[0] gt $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
263 0 ▶ q{lt} => sub { $_[0] lt $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
264 0 ▶ q{ge} => sub { $_[0] ge $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
265 0 ▶ q{le} => sub { $_[0] le $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
266 7 );
267  
268 # ternary
269 7 my $lhs = $self->get_parameter( $parameters, $1 );
270 7 my $rhs = $self->eval_arg( $3, $parameters );
271 7 my $op = $2;
272 7 my $alt_true = $self->eval_arg( $4, $parameters );
273 7 my $alt_false = $self->eval_arg( $5, $parameters );
274  
275 croak "Invalid operator $op in ternary"
276 7 ▶ if !exists $dispatch_ops{$op};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $dispatch_ops{$op} 1 6 7
277  
278 6 my $is_true = $dispatch_ops{$op}->( $lhs, $rhs );
279 6 ▶ my $val = $is_true ? $alt_true : $alt_false;
Branch: 1 branch
BranchTrueFalseTotal
$is_true ? : 3 3 6
280  
281 6 $value =~ s/\$[{]([\S]+?)\s+([\w!=><]+)\s+(.+?)\s+\?\s+(.+?)\s+\:\s+(.+?)\s*[}]/$val/;
282  
283 6 return ( $value, $parameters );
284 }
285  
286 1 return ( $value, $parameters );
287 }
288  
289 ########################################################################
290 27 ▶sub eval_arg {
Subroutine eval_arg: called CC=3 SCAR=11.0
Pod documented
291 ########################################################################
292 27 my ( $self, $arg, $parameters ) = @_;
293  
294 # 1. Is it a number? (e.g., 123)
295 27 ▶ if ( looks_like_number($arg) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($arg)) 2 25 27
296 2 return $arg;
297 }
298  
299 # 2. Is it a quoted string? (e.g., "prod-db" or 'dev-db')
300 25 ▶ if ( $arg =~ / ^ (["']) (.*?) \1 $ /xsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ / ^ (["']) (.*?) \1 $ /msx) 22 3 25
301 22 my $val = $2; # Get the captured content
302  
303 # Un-escape any backslashed quotes
304 22 $val =~ s/ \\ (["']) /$1/gx;
305  
306 22 return $val;
307 }
308  
309 # 3. If it's not a number or quoted string, it must be a
310 # variable path (e.g., database.dev).
311 3 return $self->get_parameter( $parameters, $arg );
312 }
313  
314 ########################################################################
315 0 ▶sub _resolve_array {
Subroutine _resolve_array: not called CC=2 SCAR=17.9
316 ########################################################################
317 0 my ( $self, $obj, $parameters ) = @_;
318  
319 0 foreach my $val ( @{$obj} ) {
320 0 ( $val, $parameters ) = $self->finalize_parameters( $val, $parameters );
321 }
322  
323 0 return ( $obj, $parameters );
324 }
325  
326 ########################################################################
327 3 ▶sub _resolve_hash {
Subroutine _resolve_hash: called CC=2 SCAR=6.9
328 ########################################################################
329 3 my ( $self, $obj, $parameters ) = @_;
330  
331 3 foreach my $key ( keys %{$obj} ) {
332 3 my $val;
333  
334 3 ( $val, $parameters ) = $self->finalize_parameters( $obj->{$key}, $parameters );
335  
336 3 $obj->{$key} = $val;
337 }
338  
339 3 return ( $obj, $parameters );
340 }
341  
342 ########################################################################
343 14 ▶sub finalize_parameters {
Subroutine finalize_parameters: called CC=6 SCAR=20.7
Pod documented
344 ########################################################################
345 14 my ( $self, $obj, $parameters ) = @_;
346  
347 14 ▶ $parameters //= $self->get_parameters // {};
Condition25%1/4
$parameters //= $self->get_parameters // {}
l!l
140
$self->get_parameters // {}
l!l
00
MC/DC0%0/1
$parameters //= $self->get_parameters // {}
$parameters
Truth table50%1/2
A
A= $parameters
Aresult
11
00
348  
349 14 ▶ return $self->resolve_value( $obj, $parameters )
Branch: 1 branch
BranchTrueFalseTotal
unless is_hash($obj) or is_array($obj) 11 3 14
Condition66%2/3
is_hash($obj) or is_array($obj)
l!l&&r!l&&!r
3011
MC/DC50%1/2
is_hash($obj) or is_array($obj)
is_hash($obj) is_array($obj)
Truth table66%2/3
A or B
A= is_hash($obj)B= is_array($obj)
ABresult
1X1
011
000
350 if !is_hash($obj) && !is_array($obj);
351  
352 3 ▶ return $self->_resolve_hash( $obj, $parameters )
Branch: 1 branch
BranchTrueFalseTotal
if is_hash($obj) 3 0 3
353 if is_hash($obj);
354  
355 0 return $self->_resolve_array( $obj, $parameters );
356 }
357  
358 ########################################################################
359 28 ▶sub get_value {
Subroutine get_value: called CC=9 SCAR=22.1
Pod documented
360 ########################################################################
361 28 my ( $self, $obj, $path ) = @_;
362  
363 28 my @parts = split /[.]/xsm, $path;
364  
365 28 ▶ croak "error: invalid path [$path]\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 0 28 28
366 if !@parts;
367  
368 28 ▶ while ( @parts > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts > 1) 11 28 39
369 11 ▶ last if !ref $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj 0 11 11
370  
371 11 my ( $key, $idx ) = is_key_or_idx( shift @parts );
372  
373 11 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 11 0 11
374 11 $obj = $obj->{$key};
375 }
376  
377 11 ▶ if ( defined $idx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $idx) 3 8 11
378 3 $obj = $obj->[$idx];
379 }
380 }
381  
382 28 my ( $key, $idx ) = is_key_or_idx( shift @parts );
383  
384 return
385 28 ▶ if !ref $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj 1 27 28
386  
387 27 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 27 0 27
388 27 $obj = $obj->{$key};
389 }
390  
391 27 ▶ if ( defined $idx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $idx) 4 23 27
392 4 $obj = $obj->[$idx];
393 }
394  
395 27 return $obj;
396 }
397  
398 ########################################################################
399 23 ▶sub get_parameter {
Subroutine get_parameter: called CC=6 SCAR=18.0
Pod documented
400 ########################################################################
401 23 my ( $self, $obj, $path ) = @_;
402  
403 23 ▶ die "error: no path\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 23 23
404 if !$path;
405  
406 23 ▶ die "error: no parameter store defined\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $obj 0 23 23
407 if !$obj;
408  
409 # --- FETCH THE PRE-BUILT MAP ---
410 # It no longer defines its own hash.
411 23 my $func_map = $self->get_function_map;
412  
413 23 my $function;
414  
415 23 ▶ if ( $path =~ /^([[:alpha:]_:]+)[(](.*?)[)]$/ixsm ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /^([[:alpha:]_:]+)[(](.*?)[)]$/imsx) 8 15 23
416 8 ( $function, $path ) = ( $1, $2 );
417 }
418  
419 23 ▶ return $self->get_value( $obj, $path )
Branch: 1 branch
BranchTrueFalseTotal
unless $function 15 8 23
420 if !$function;
421  
422 # 1. Check if the requested function exists in our map
423 croak "ERROR: function '$function' is not permitted in resolver.\n"
424 8 ▶ if !exists $func_map->{$function};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $func_map->{$function} 2 6 8
425  
426 # 2. Get the argument's value
427 6 my $arg = $self->get_value( $obj, $path );
428  
429 # 3. Safely call the function via its code reference
430 6 my $result = $func_map->{$function}->($arg);
431  
432 6 return $result;
433 }
434  
435 ########################################################################
436 39 ▶sub is_key_or_idx {
Subroutine is_key_or_idx: called CC=3 SCAR=11.0
Pod undocumented
437 ########################################################################
438 39 my ($p) = @_;
439  
440 39 my ( $key, $idx );
441  
442 # array?
443 39 ▶ if ( $p =~ /^([ _[:alpha:][:digit:]]+)\[(\d+)\]$/ixsm ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p =~ /^([ _[:alpha:][:digit:]]+)\[(\d+)\]$/imsx) { } 7 32 39
elsif ($p =~ /^([ _[:alpha:][:digit:]]+)/imsx) { } 32 0 32
444 7 $key = $1;
445 7 $idx = $2;
446 }
447 # hash key?
448 elsif ( $p =~ /^([ _[:alpha:][:digit:]]+)/ixsm ) {
449 32 $key = $1;
450 }
451  
452 39 return ( $key, $idx );
453 }
454  
455 29 1;
456  
457 __END__
« Resolver/CLI.pm ↑ Summary Resolver/Utils.pm »