XrmDatabase.pm

filter: Statementstmt 94.9% 170 / 179 Branchbran 83.3% 45 / 54 Conditioncond 77.7% 14 / 18 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 97.0% 33 / 34 Podpod 90.9% 10 / 11 totaltotal 90.2% 277 / 307 CC 47 SCAR 19.2
CC
47
Cov
91%
_query
14.0
_to_kv_xx
13.8
query
8.0
CRAP 6.8
↑ Summary XrmDatabase/Util.pm »
1 package Config::XrmDatabase;
2  
3 # ABSTRACT: Pure Perl X Resource Manager Database
4  
5 9 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 our $VERSION = '0.09';
9  
10 9 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use Config::XrmDatabase::Failure ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Config::XrmDatabase::Util ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Config::XrmDatabase::Types -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Types::Standard qw( Object Str Optional HashRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Type::Params qw( compile_named );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Ref::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 9 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 9 ▶use MooX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 9 ▶use experimental qw( signatures postderef declared_refs refaliasing );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 9 ▶use if $] >= 5.034, 'experimental', 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 has _db => (
29 is => 'rwp',
30 init_arg => undef,
31 41 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 9 );
33  
34 9 has _query_return_value => (
35 is => 'ro',
36 isa => QueryReturnValue,
37 init_arg => 'query_return_value',
38 coerce => 1,
39 default => 'value',
40 );
41  
42 9 has _query_on_failure => (
43 is => 'ro',
44 isa => OnQueryFailure,
45 init_arg => 'query_on_failure',
46 coerce => 1,
47 default => 'undef',
48 );
49  
50 # fake attribute so we can use MooX::StrictConstructor
51 9 has _insert => (
52 is => 'ro',
53 isa => HashRef,
54 init_arg => 'insert',
55 predicate => 1,
56 clearer => 1,
57 );
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92 41 ▶sub BUILD ( $self, $ ) {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
93 41 ▶ if ( $self->_has_insert ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_insert) 1 40 41
94 1 my $kv = $self->_insert;
95 1 $self->insert( $_, $kv->{$_} ) for keys %$kv;
96 1 $self->_clear_insert;
97 }
98 }
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114 1610 ▶sub insert ( $self, $name, $value ) {
Subroutine insert: called CC=3 SCAR=11.0
Pod documented
115  
116 1610 $name = parse_resource_name( $name );
117 1610 my $db = $self->_db;
118 1610 ▶ $db = $db->{$_} //= {} for $name->@*;
Condition100%2/2
$db->{$_} //= {}
l!l
66834316
MC/DC100%1/1
$db->{$_} //= {}
$db->{$_}
Truth table100%2/2
A
A= $db->{$_}
Aresult
11
00
119 1610 $db->{ +VALUE } = $value;
120 1610 $db->{ +MATCH_COUNT } = 0;
121 }
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222 9 ▶no namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
223 9 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
224 9 QUERY_RETURN_VALUE => 'value',
225 QUERY_RETURN_REFERENCE => 'reference',
226 QUERY_RETURN_ALL => 'all',
227 QUERY_ON_FAILURE_THROW => 'throw',
228 QUERY_ON_FAILURE_UNDEF => 'undef',
229 9 };
230 9 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
231  
232 # recursive query helper. The query method is a front-end.
233 227 ▶my sub _query ( $db, $idx, $class, $name, $match, $return_all ) {
Subroutine _query: called CC=14 SCAR=26.4
234  
235 227 my $is_last_level = $idx + 1 == @$name;
236  
237 # this level's component may be bound directly ('.') or via a '*'
238 # which matches zero components.
239 227 ▶ my @parents = ( [$db], $db->{ +LOOSE } ? [ $db->{ +LOOSE }, LOOSE ] : () );
Branch: 1 branch
BranchTrueFalseTotal
$db->{'*'} ? : 77 150 227
240  
241 # name·. > name·* > class·. > class·* > ?·. > ?·*
242 # ('?' can't be the last component)
243 227 ▶ for my $component ( $name->[$idx], $class->[$idx], $is_last_level ? () : SINGLE ) {
Branch: 1 branch
BranchTrueFalseTotal
$is_last_level ? : 69 158 227
244 503 for my $parent ( @parents ) {
245 626 my ( $subdb, @binding ) = @$parent;
246 626 ▶ my $entry = $subdb->{$component} // next;
Condition100%2/2
$subdb->{$component} // (next)
l!l
133493
MC/DC100%1/1
$subdb->{$component} // (next)
$subdb->{$component}
Truth table100%2/2
A
A= $subdb->{$component}
Aresult
11
00
247  
248 133 push $match->@*, @binding, $component;
249  
250 133 ▶ if ( !$is_last_level ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $is_last_level) { } 105 28 133
elsif (exists $entry->{'!!VALUE'}) { } 23 5 28
251 105 my $res = __SUB__->( $entry, $idx + 1, $class, $name, $match, $return_all );
252 105 ▶ return $res if defined $res;
Branch: 1 branch
BranchTrueFalseTotal
if defined $res 50 55 105
253 }
254 elsif ( exists $entry->{ +VALUE } ) {
255 23 ++$entry->{ +MATCH_COUNT };
256 23 my $value = $entry->{ +VALUE };
257 return $return_all
258 ? {
259 value => $value,
260 23 ▶ match_count => $entry->{ +MATCH_COUNT },
Branch: 1 branch
BranchTrueFalseTotal
$return_all ? : 8 15 23
261 key => $match,
262 }
263 : \$value;
264 }
265  
266 60 splice $match->@*, -( 1 + @binding );
267 }
268 }
269  
270 # '*' which matches one or more components
271 154 ▶ if ( !$is_last_level and my $subdb = $db->{ +LOOSE } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_last_level and my $subdb = $db->{"*"}) 36 118 154
Condition100%3/3
not $is_last_level and my $subdb = $db->{"*"}
!ll&&!rl&&r
467236
MC/DC100%2/2
not $is_last_level and my $subdb = $db->{"*"}
not $is_last_level my $subdb = $db->{"*"}
Truth table100%3/3
not(A) and B
A= not $is_last_levelB= my $subdb = $db->{"*"}
ABresult
0X0
100
111
272 36 push $match->@*, LOOSE;
273 36 for my $next ( $idx + 1 .. $#$name ) {
274 85 my $res = __SUB__->( $subdb, $next, $class, $name, $match, $return_all );
275 85 ▶ return $res if defined $res;
Branch: 1 branch
BranchTrueFalseTotal
if defined $res 12 73 85
276 }
277 24 pop $match->@*;
278 }
279  
280 142 return undef;
281 }
282  
283 37 ▶sub query ( $self, $class, $name, %iopt ) {
Subroutine query: called CC=8 SCAR=20.9
Pod documented
284  
285 37 state $check = compile_named(
286 { head => [ Str, Str ] },
287 return_value => Optional [QueryReturnValue],
288 on_failure => Optional [OnQueryFailure],
289 );
290  
291 37 ( $class, $name, my \%opt ) = $check->( $class, $name, %iopt );
292  
293 37 ▶ $opt{on_failure} //= $self->_query_on_failure;
Condition66%2/3
$opt{"on_failure"} //= $self->_query_on_failure
l!l&&r!l&&!r
4330
MC/DC0%0/2
$opt{"on_failure"} //= $self->_query_on_failure
$opt{"on_failure"} $self->_query_on_failure
Truth table66%2/3
A //= B
A= $opt{"on_failure"}B= $self->_query_on_failure
ABresult
1X1
011
000
294 37 ▶ $opt{return_value} //= $self->_query_return_value;
Condition66%2/3
$opt{"return_value"} //= $self->_query_return_value
l!l&&r!l&&!r
11260
MC/DC0%0/2
$opt{"return_value"} //= $self->_query_return_value
$opt{"return_value"} $self->_query_return_value
Truth table66%2/3
A //= B
A= $opt{"return_value"}B= $self->_query_return_value
ABresult
1X1
011
000
295  
296 37 ( $class, $name ) = map { parse_fq_resource_name( $_ ) } $class, $name;
297  
298 37 ▶ components_failure->throw( 'class and name must have the same number of components' )
Branch: 1 branch
BranchTrueFalseTotal
if @$class != @$name 0 37 37
299 if @$class != @$name;
300  
301 37 my $return_all = $opt{return_value} eq QUERY_RETURN_ALL;
302  
303 37 my $match = [];
304 37 my $retval = _query( $self->_db, 0, $class, $name, $match, $return_all );
305  
306 37 ▶ if ( !defined $retval ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $retval) 14 23 37
307 return $opt{on_failure}->( $name, $class )
308 14 ▶ if Ref::Util::is_coderef( $opt{on_failure} );
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_coderef($opt{"on_failure"}) 2 12 14
309  
310 query_failure->throw(
311 3 "unable to match name: '@{[ name_arr_to_str($name) ]} '; class : '@{[ name_arr_to_str($class) ]}'" )
312 12 ▶ if $opt{on_failure} eq QUERY_ON_FAILURE_THROW;
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"on_failure"} eq "throw" 3 9 12
313  
314 9 return undef;
315 }
316  
317 23 ▶ return $opt{return_value} eq QUERY_RETURN_VALUE ? $$retval : $retval;
Branch: 1 branch
BranchTrueFalseTotal
$opt{'return_value'} eq 'value' ? : 13 10 23
318 }
319  
320  
321  
322  
323  
324  
325  
326  
327  
328  
329  
330  
331  
332  
333  
334  
335  
336  
337  
338  
339 1 ▶sub read_file ( $class, $file, %opts ) {
Subroutine read_file: called CC=4 SCAR=14.3
Pod documented
340  
341 1 my $self = $class->new( %opts );
342  
343 1 require File::Slurper;
344  
345 1 my @lines;
346  
347 1 try {
348 1 @lines = File::Slurper::read_lines( $file );
349 }
350 catch ( $e ) {
351 0 file_failure->throw( "$e: error opening $file: $!" );
352 }
353  
354 1 my $idx = 0;
355 1 for my $line ( @lines ) {
356 750 ++$idx;
357 750 my ( $var, $value ) = $line =~ /^\s*([^:]+?)\s*:\s*(.*?)\s*$/;
358 750 ▶ file_failure->throw( sprintf( '%s:%d: unable to parse line', $file, $idx ) )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $var and defined $value 0 750 750
Condition33%1/3
defined $var and defined $value
!ll&&!rl&&r
00750
MC/DC0%0/2
defined $var and defined $value
defined $var defined $value
Truth table33%1/3
A and B
A= defined $varB= defined $value
ABresult
0X0
100
111
359 unless defined $var and defined $value;
360 750 $self->insert( $var, $value );
361 }
362  
363 1 return $self;
364 }
365  
366  
367  
368  
369  
370  
371  
372  
373  
374  
375 1 ▶sub write_file ( $self, $file ) {
Subroutine write_file: called CC=1 SCAR=0.0
Pod documented
376 1 require File::Slurper;
377 1 File::Slurper::write_text( $file, $self->to_string );
378 }
379  
380  
381  
382  
383  
384  
385  
386  
387  
388  
389  
390  
391  
392  
393  
394  
395 1 ▶sub to_string ( $self ) {
Subroutine to_string: called CC=5 SCAR=16.2
Pod documented
396  
397 1 my $folded = $self->_folded;
398 1 my @records;
399  
400 1 for my $key ( keys $folded->%* ) {
401 1500 my $value = $folded->{$key};
402  
403 1500 ▶ if ( $key =~ /^(?<key>.*)[.](?<component>${META_QR})$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(?<key>.*)[.](?<component>$Config::XrmDatabase::META_QR)$/u) 1500 0 1500
404 1500 ▶ next unless $+{component} eq VALUE;
Branch: 1 branch
BranchTrueFalseTotal
unless $+{"component"} eq "!!VALUE" 750 750 1500
405 750 $key = $+{key};
406 }
407  
408 750 push @records, "$key : $value";
409 }
410  
411 1 ▶ return @records ? join( "\n", @records, q{} ) : q{};
Branch: 1 branch
BranchTrueFalseTotal
@records ? : 1 0 1
412 }
413  
414  
415  
416  
417  
418  
419  
420  
421  
422  
423  
424  
425 1 ▶sub merge ( $self, $other ) {
Subroutine merge: called CC=1 SCAR=0.0
Pod documented
426  
427 1 require Hash::Merge;
428 1 my $merger = Hash::Merge->new( 'RIGHT_PRECEDENT' );
429  
430 1 $self->_db->%* = $merger->merge( $self->TO_HASH->{db}, $other->TO_HASH->{db} )->%*;
431  
432 1 return $self;
433 }
434  
435  
436  
437  
438  
439  
440  
441  
442  
443  
444 0 ▶sub clone ( $self ) {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
445 0 require Scalar::Util;
446  
447 0 my \%args = $self->TO_HASH;
448 0 my $db = delete $args{db}; # this isn't a constructor argument.
449 0 my $clone = Scalar::Util::blessed( $self )->new( \%args );
450 0 $clone->_set__db( $db );
451 0 return $clone;
452 }
453  
454  
455  
456  
457  
458  
459  
460  
461  
462  
463  
464  
465  
466  
467  
468  
469  
470  
471  
472  
473  
474  
475  
476  
477  
478  
479  
480  
481  
482  
483  
484  
485  
486  
487  
488  
489  
490  
491  
492  
493  
494  
495  
496  
497  
498  
499  
500  
501  
502  
503  
504  
505  
506  
507  
508  
509  
510  
511  
512  
513 9 my %KV_CONSTANTS;
514 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
515 9 %KV_CONSTANTS = ( map { uc( "KV_$_" ) => $_ } qw( all string array value match_count ) );
516  
517 }
518 9 ▶use constant \%KV_CONSTANTS;
Subroutine BEGIN: called CC=1 SCAR=0.0
519  
520  
521 4 ▶sub _to_kv_xx ( $self, %iopt ) {
Subroutine _to_kv_xx: called CC=13 SCAR=26.3
522 4 %iopt = ( key => KV_STRING, value => KV_VALUE, %iopt );
523  
524 4 state $match = {
525 ## no critic (ComplexRegexes)
526 1 value => qr/^(?<match> @{[ join '|', KV_VALUE, KV_MATCH_COUNT, KV_ALL ]} )$/xi,
527 1 key => qr/^(?<match> @{[ join '|', KV_STRING, KV_ARRAY ]} )$/xi,
528 };
529  
530 ## no critic (ComplexMappings)
531 my %opt = map {
532 4 parameter_failure->throw( "illegal value for '$_' option: $iopt{$_}" )
533 8 ▶ unless $iopt{$_} =~ $match->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $iopt{$_} =~ /$match->{$_}/u 0 8 8
534 8 $_ => $+{match};
535 } qw( key value );
536  
537 4 parameter_failure->throw( "illegal option: $_" ) for grep !defined $opt{$_}, keys %iopt;
538  
539 # don't clean out excess TIGHT characters if we'll need to later
540 # split it into components. otherwise we'd have to run
541 # parse_resource_name all over again.
542 4 my $normalize_keys = $opt{key} eq KV_STRING;
543 4 my $folded = $self->_folded( $normalize_keys );
544  
545 # first get values
546 # return single requested value
547 4 ▶ if ( my $component = $RMETA{ $opt{value} } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $component = $Config::XrmDatabase::RMETA{$opt{'value'}}) { } 3 1 4
elsif ($opt{'value'} eq 'all') { } 1 0 1
548  
549 3 for my $key ( keys $folded->%* ) {
550 30 ▶ if ( $key =~ /^(?<key>.*)[.](?<component>${META_QR})$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(?<key>.*)[.](?<component>$Config::XrmDatabase::META_QR)$/u) 30 0 30
551 # only allow the requested data out
552 30 my $value = delete $folded->{$key};
553 $folded->{ $+{key} } = $value
554 30 ▶ if $+{component} eq $component;
Branch: 1 branch
BranchTrueFalseTotal
if $+{"component"} eq $component 15 15 30
555 }
556 }
557 }
558  
559 elsif ( $opt{value} eq KV_ALL ) {
560  
561 1 for my $key ( keys $folded->%* ) {
562 10 ▶ if ( $key =~ /^(?<key>.*)[.](?<component>${META_QR})$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(?<key>.*)[.](?<component>$Config::XrmDatabase::META_QR)$/u) 10 0 10
563 ( $folded->{ $+{key} } //= {} )->{ $META{ $+{component} } }
564 10 ▶ = delete $folded->{$key};
Condition100%2/2
$$folded{$+{"key"}} //= {}
l!l
55
MC/DC100%1/1
$$folded{$+{"key"}} //= {}
$$folded{$+{"key"}}
Truth table100%2/2
A
A= $$folded{$+{"key"}}
Aresult
11
00
565 }
566 }
567 }
568  
569 # shouldn't get here
570 else {
571 0 internal_failure->throw( "internal error: unexpected value for 'value': $iopt{value}" );
572 }
573  
574 return $folded
575 4 ▶ if $opt{key} eq KV_STRING;
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"key"} eq "string" 3 1 4
576  
577 5 return [ map { [ [ split( /[.]/ ) ], $folded->{$_} ] } keys $folded->%* ]
578 1 ▶ if $opt{key} eq KV_ARRAY;
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"key"} eq "array" 1 0 1
579  
580 0 internal_failure->throw( "internal error: unexpected value for 'key': $iopt{key}" );
581 }
582  
583  
584  
585  
586  
587  
588  
589  
590  
591  
592  
593  
594  
595  
596  
597  
598  
599  
600  
601  
602  
603  
604  
605  
606  
607  
608  
609  
610  
611  
612  
613  
614  
615  
616  
617  
618  
619  
620  
621  
622  
623 3 ▶sub to_kv ( $self, %opt ) {
Subroutine to_kv: called CC=1 SCAR=0.0
Pod documented
624 3 $self->_to_kv_xx( %opt, key => 'string' );
625 }
626  
627  
628  
629  
630  
631  
632  
633  
634  
635  
636  
637  
638  
639  
640  
641  
642  
643  
644  
645  
646  
647  
648  
649  
650  
651  
652  
653  
654  
655  
656  
657  
658  
659  
660  
661  
662  
663  
664  
665  
666  
667  
668  
669  
670  
671 1 ▶sub to_kv_arr ( $self, %opt ) {
Subroutine to_kv_arr: called CC=1 SCAR=0.0
Pod documented
672 1 $self->_to_kv_xx( %opt, key => 'array' );
673 }
674  
675  
676  
677  
678  
679  
680  
681  
682  
683  
684  
685  
686  
687  
688 12 ▶sub TO_HASH ( $self ) {
Subroutine TO_HASH: called CC=1 SCAR=0.0
Pod documented
689 12 require Storable;
690  
691 {
692 12 query_return_value => $self->_query_return_value,
693 query_on_failure => $self->_query_on_failure,
694 db => Storable::dclone( $self->_db ),
695 };
696 }
697  
698  
699  
700  
701  
702  
703  
704  
705  
706 5 ▶sub _folded ( $self, $normalize_names = 1 ) {
Subroutine _folded: called CC=4 SCAR=13.9
707  
708 # Hash::Fold is overkill
709 5 require Hash::Fold;
710 5 my $folded = Hash::Fold->new( delimiter => q{.} )->fold( $self->TO_HASH->{db} );
711  
712 5 ▶ return $folded unless $normalize_names;
Branch: 1 branch
BranchTrueFalseTotal
unless $normalize_names 1 4 5
713  
714 4 for my $key ( keys %$folded ) {
715 1530 my $nkey = normalize_key( $key );
716 1530 $folded->{$nkey} = delete $folded->{$key};
717 }
718  
719 4 return $folded;
720 }
721  
722  
723  
724  
725  
726  
727 9 1;
728  
729 #
730 # This file is part of Config-XrmDatabase
731 #
732 # This software is Copyright (c) 2021 by Smithsonian Astrophysical Observatory.
733 #
734 # This is free software, licensed under:
735 #
736 # The GNU General Public License, Version 3, June 2007
737 #
738  
739 __END__
↑ Summary XrmDatabase/Util.pm »