Conformance/Schema.pm

filter: Statementstmt 42.1% 27 / 64 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 42.1% 8 / 19 Podpod 100% 10 / 10 totaltotal 33.0% 45 / 136 CC 16 SCAR 22.4
CC
16
Cov
28%
parse_yaml
20.0
parse_json
20.0
relative_path
12.0
CRAP 9.4
« Conformance/Check/ClaudeMd.pm ↑ Summary Conformance/Check/Workflows.pm »
1 package Dist::Conformance::Schema 0.004;
2  
3 # ABSTRACT: Parse YAML, JSON and INI, and validate the result against a schema
4  
5 19 ▶use 5.040;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use re '/aasx';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 19 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use Cpanel::JSON::XS();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 19 ▶use JSON::Validator::Schema::Troglodyne();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use YAML::XS();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 19 our $NAME = { type => 'string', pattern => '\A[A-Za-z0-9][A-Za-z0-9._-]*\z' };
16 19 our $PATH = { type => 'string', pattern => '\A(?!/)(?!(?:.*/)?[.][.](?:/|\z)).+\z' };
17 19 our $STATUS = { type => 'string', enum => [qw{pass info fail}] };
18  
19  
20 0 ▶sub parse_yaml ( $text, $where ) {
Subroutine parse_yaml: not called CC=4 SCAR=30.0
Pod documented
21 0 my $data = eval { YAML::XS::Load($text) };
22 0 ▶ die "$where does not parse as YAML: $@" if !defined $data && $@;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $data and $@ 0 0 0
Condition0%0/3
not defined $data and $@
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $data and $@
not defined $data $@
Truth table0%0/3
not(A) and B
A= not defined $dataB= $@
ABresult
0X0
100
111
23 0 ▶ die "$where does not hold a mapping at its top level\n" if ref $data ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data ne "HASH" 0 0 0
24 0 return $data;
25 }
26  
27  
28 0 ▶sub parse_json ( $bytes, $where ) {
Subroutine parse_json: not called CC=4 SCAR=30.0
Pod documented
29 0 my $data = eval { Cpanel::JSON::XS->new->utf8->decode($bytes) };
30 0 ▶ die( "$where does not parse as JSON: " . first_line($@), "\n" ) if !defined $data && $@;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $data and $@ 0 0 0
Condition0%0/3
not defined $data and $@
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $data and $@
not defined $data $@
Truth table0%0/3
not(A) and B
A= not defined $dataB= $@
ABresult
0X0
100
111
31 0 ▶ die "$where does not hold an object at its top level\n" if ref $data ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data ne "HASH" 0 0 0
32 0 return $data;
33 }
34  
35  
36 0 ▶sub encode_json ($data) {
Subroutine encode_json: not called CC=1 SCAR=6.9
Pod documented
37 0 return Cpanel::JSON::XS->new->utf8->pretty->space_before(0)->canonical->indent_length(2)->encode($data);
38 }
39  
40  
41 0 ▶sub validate ( $data, $schema, $where ) {
Subroutine validate: not called CC=2 SCAR=17.9
Pod documented
42 0 my $copy = Clone::clone($data);
43 0 my @errors = errors( $copy, $schema );
44 0 ▶ refuse( $where, @errors ) if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 0 0 0
45 0 return $copy;
46 }
47  
48  
49 0 ▶sub refuse ( $where, @errors ) {
Subroutine refuse: not called CC=1 SCAR=6.9
Pod documented
50 0 die join( "\n", "$where is not valid:", map { " $_" } @errors ), "\n";
51 }
52  
53  
54 0 ▶sub errors ( $data, $schema ) {
Subroutine errors: not called CC=1 SCAR=6.9
Pod documented
55 0 my $validator = JSON::Validator::Schema::Troglodyne->new;
56 0 $validator->coerce( { %{ $validator->coerce }, defaults => 1, booleans => 1 } );
57 0 my @errors = map { "$_" } $validator->validate( $data, $schema );
58 0 return @errors;
59 }
60  
61  
62 0 ▶sub matches ( $fragment, $value ) {
Subroutine matches: not called CC=2 SCAR=17.9
Pod documented
63  
64 # (?^:) gives the pattern the default flags that JSON::Validator matches it with, not the /s of this file.
65 0 ▶ return defined $value && $value =~ m/(?^:$fragment->{pattern})/;
Condition0%0/3
defined $value && $value =~ /(?^:$fragment->{'pattern'})/aasx
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value && $value =~ /(?^:$fragment->{'pattern'})/aasx
defined $value $value =~ /(?^:$fragment->{'pattern'})/aasx
Truth table0%0/3
A && B
A= defined $valueB= $value =~ /(?^:$fragment->{'pattern'})/aasx
ABresult
0X0
100
111
66 }
67  
68  
69 0 ▶sub relative_path ( $path, $inside ) {
Subroutine relative_path: not called CC=3 SCAR=24.8
Pod documented
70  
71 0 ▶ die( ( $path // 'undef' ) . " is not a relative path inside $inside\n" ) if !matches( $PATH, $path );
Branch: 1 branch
BranchTrueFalseTotal
unless matches($Dist::Conformance::Schema::PATH, $path) 0 0 0
Condition0%0/2
$path // "undef"
l!l
00
MC/DC0%0/1
$path // "undef"
$path
Truth table0%0/2
A
A= $path
Aresult
11
00
72 0 return $path;
73 }
74  
75  
76 0 ▶sub parse_ini ( $text, $where ) {
Subroutine parse_ini: not called CC=2 SCAR=17.9
Pod documented
77 0 my $data = eval { Dist::Conformance::Schema::INIReader->read_string($text) };
78 0 ▶ die( "$where does not parse as INI: " . first_line($@), "\n" ) if !defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
79 0 return $data;
80 }
81  
82  
83 0 ▶sub first_line ($text) {
Subroutine first_line: not called CC=3 SCAR=24.8
Pod documented
84 0 ▶ my ($line) = split /\n/, $text // q{};
Condition0%0/2
$text // ""
l!l
00
MC/DC0%0/1
$text // ""
$text
Truth table0%0/2
A
A= $text
Aresult
11
00
85 0 ▶ $line //= q{};
Condition0%0/2
$line //= ""
l!l
00
MC/DC0%0/1
$line //= ""
$line
Truth table0%0/2
A
A= $line
Aresult
11
00
86 0 $line =~ s/\ at\ \S+\ line\ \d+[.]?\z//;
87 0 return $line;
88 }
89  
90 # Config::INI::Reader keeps the last value of a repeated key, so it collects each repeat into a list.
91 19 package Dist::Conformance::Schema::INIReader 0.004 {
92 19 ▶ use parent 'Config::INI::Reader';
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 0 ▶ sub set_value ( $self, $name, $value ) {
Subroutine set_value: not called CC=3 SCAR=24.8
95 0 ▶ my $values = $self->{ __PACKAGE__ . '/values' }{ $self->current_section }{$name} //= [];
Condition0%0/2
$self->{"Dist::Conformance::Schema::INIReader/values"}{$self->current_section}{$name} //= []
l!l
00
MC/DC0%0/1
$self->{"Dist::Conformance::Schema::INIReader/values"}{$self->current_section}{$name} //= []
$self->{"Dist::Conformance::Schema::INIReader/values"}{$self->current_section}{$name}
Truth table0%0/2
A
A= $self->{"Dist::Conformance::Schema::INIReader/values"}{$self->current_section}{$name}
Aresult
11
00
96 0 push @$values, $value;
97 0 ▶ return $self->SUPER::set_value( $name, @$values == 1 ? $value : [@$values] );
Branch: 1 branch
BranchTrueFalseTotal
@$values == 1 ? : 0 0 0
98 }
99 }
100  
101 __END__
« Conformance/Check/ClaudeMd.pm ↑ Summary Conformance/Check/Workflows.pm »