blib/lib/YAML/PP/Schema/Env.pm

filter: Statementstmt 94.5% 35 / 37 Branchbran 83.3% 10 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 90.9% 50 / 55 SCAR 20.8
CC
8
Cov
92%
__ANON__
5.0
register
4.0
BEGIN
1.0
CRAP 8.0
↑ Summary
1 # -*- perl -*-
2  
3 #
4 # Author: Slaven Rezic
5 #
6 # Copyright (C) 2023 Slaven Rezic. All rights reserved.
7 # This package is free software; you can redistribute it and/or
8 # modify it under the same terms as Perl itself.
9 #
10  
11 package YAML::PP::Schema::Env;
12  
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 our $VERSION = '0.01';
19  
20 sub register {
21 5 ▶ my ($self, %args) = @_;
Subroutine register: called CC=4 SCAR=13.9
Pod undocumented
22 5 my $schema = $args{schema};
23
24 5 my $options = $args{options};
25 5 my $default_value;
26 5 my $default_separator = ':';
27 5 for my $opt (@$options) {
28 6 ▶ if ($opt =~ m{^defval=(.*)$}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt =~ /^defval=(.*)$/) { } 4 2 6
elsif ($opt =~ /defsep=(.*)$/) { } 2 0 2
29 4 $default_value = $1;
30 } elsif ($opt =~ m{defsep=(.*)$}) {
31 2 $default_separator = $1;
32 } else {
33 0 croak "Invalid option for ENV Schema: '$opt'";
34 }
35 }
36
37 $schema->add_resolver(
38 tag => '!ENV',
39 match => [ all => sub {
40 28 ▶ my ($constructor, $event) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.1
41 28 (my $val = $event->{value}) =~ s{\$(\{.*?\})}{
42 37 my $capture = $1;
43 37 ▶ if ($capture =~ m{\{(.*?)(?:\Q$default_separator\E(.*))?\}$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($capture =~ /{(.*?)(?:\Q$default_separator\E(.*))?}$/) { } 37 0 37
44 37 my($this_env, $this_default_value) = ($1, $2);
45 37 my $this_value;
46 37 ▶ if (!exists $ENV{$this_env}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $ENV{$this_env}) { } 23 14 37
47 23 ▶ if (defined $this_default_value) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $this_default_value) { } 9 14 23
elsif (defined $default_value) { } 12 2 14
48 9 $this_value = $this_default_value;
49 } elsif (defined $default_value) {
50 12 $this_value = $default_value;
51 } else {
52 2 croak "There's no environment variable '$this_env', and no global or local default value was configured";
53 }
54 } else {
55 14 $this_value = $ENV{$this_env};
56 }
57 35 $this_value;
58 } else {
59 0 croak "Unexpected error: cannot parse '$capture'";
60 }
61 }ge;
62 26 return $val;
63 5 }],
64 implicit => 0,
65 );
66 }
67  
68 1 1;
69  
70 __END__
↑ Summary