lib/Config/Neat/Schema.pm

filter: Statementstmt 93.6% 88 / 94 Branchbran 83.9% 47 / 56 Conditioncond 80.9% 17 / 21 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 6 totaltotal 84.8% 173 / 204 CC 37 SCAR 31.6
CC
37
Cov
89%
validate_node
33.8
get_node_type
5.5
validate
2.0
CRAP 23.5
« lib/Config/Neat.pm ↑ Summary blib/lib/Config/Neat/Util.pm »
1 =head1 NAME
2  
3 Config::Neat::Schema - Validate Config::Neat files against schema
4  
5 =head1 SYNOPSIS
6  
7 File 01.nconf:
8  
9 foo {
10 bar baz etc
11  
12 etc {
13 pwd 1 2
14 }
15 }
16 abc def
17  
18 File schema.nconf:
19  
20 foo
21 {
22 bar ARRAY
23 etc
24 {
25 * ARRAY
26 pwd STRING
27 }
28 }
29 data DATA
30  
31 if file 01.nconf is validated against schema.nconf, it will:
32 1) convert arrays to strings for the known nodes with 'STRING' type
33 2) die or warn (depending on the settings) when an unknown node is found
34 (in the example above, 'abc').
35  
36 '*' as the name of the node means 'node with any name'. If such catch-all rule
37 is not specified, all possible node values need to be specified explicitly.
38  
39 Possible type specifiers are: HASH (this is default if not specified),
40 ARRAY, STRING, ARRAY_OR_HASH, STRING_OR_HASH, or DATA. 'DATA' nodes may contain
41 any arbitrary data structure and are not validated.
42  
43 =head1 COPYRIGHT
44  
45 Copyright (C) 2012-2015 Igor Afanasyev <igor.afanasyev@gmail.com>
46  
47 =head1 SEE ALSO
48  
49 L<https://github.com/iafan/Config-Neat>
50  
51 =cut
52  
53 package Config::Neat::Schema;
54  
55 1 our $VERSION = '1.4';
56  
57 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 1 ▶use Config::Neat::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use Config::Neat::Inheritable;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use Config::Neat::Util qw(new_ixhash is_hash is_any_hash is_any_array is_simple_array is_neat_array hash_has_sequential_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use File::Spec::Functions qw(rel2abs);
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 #
67 # Initialize object
68 #
69 sub new {
70 1 ▶ my ($class, $data) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
71  
72 1 my $self = {
73 schema => $data
74 };
75  
76 1 bless $self, $class;
77 1 return $self;
78 }
79  
80 # Given file name, will read and store the schema file
81 sub load {
82 1 ▶ my ($self, $filename, $binmode) = @_;
Subroutine load: called CC=1 SCAR=0.0
Pod undocumented
83 1 my $c = Config::Neat::Inheritable->new();
84 1 return $self->{schema} = $c->parse_file($filename, $binmode);
85 }
86  
87 # Store loaded data as current schema
88 sub set {
89 0 ▶ my ($self, $data) = @_;
Subroutine set: not called CC=1 SCAR=6.9
Pod undocumented
90 0 $self->{schema} = $data;
91 }
92  
93 # Validates provided data structure (parsed config file) against the previously loaded schema
94 # with expanded '@inherit' blocks
95 sub validate {
96 20 ▶ my ($self, $data) = @_;
Subroutine validate: called CC=2 SCAR=7.1
Pod undocumented
97 20 ▶ die "Schema should be loaded prior to validation" unless defined $self->{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"schema"} 0 20 20
98 20 return $self->validate_node($self->{schema}, $data, undef, undef, []);
99 }
100  
101 sub validate_node {
102 120 ▶ my ($self, $schema_node, $data_node, $parent_data, $parent_data_key, $path) = @_;
Subroutine validate_node: called CC=33 SCAR=35.2
Pod undocumented
103  
104 120 my $pathstr = '/'.join('/', @$path);
105  
106 120 ▶ if (!$schema_node) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema_node) 1 119 120
107 1 die "Node '$pathstr' is not defined in the schema";
108 }
109  
110 119 my $schema_type = $self->get_node_type($schema_node);
111 119 my $data_type = $self->get_node_type($data_node);
112  
113 #print "::[$pathstr] schema_type=[$schema_type], data_type=[$data_type]\n";
114 #use Data::Dumper; print Dumper($data_node);
115  
116 119 ▶ if ($schema_type eq 'STRING') {
Branch: 3 branches
BranchTrueFalseTotal
if ($schema_type eq 'STRING') { } 0 119 119
elsif ($schema_type eq 'ARRAY') { } 46 73 119
elsif ($schema_type eq 'HASH' and defined $schema_node->{''}) { } 38 35 73
117 # the node itself is already a scalar and contains the type definition
118 0 $schema_type = $schema_node;
119 } elsif ($schema_type eq 'ARRAY') {
120 # the string representation of the node contains the type definition
121 46 $schema_type = $schema_node->as_string;
122 ▶ } elsif ($schema_type eq 'HASH' and defined $schema_node->{''}) {
Condition66%2/3
$schema_type eq "HASH" and defined $schema_node->{""}
!ll&&!rl&&r
03538
MC/DC50%1/2
$schema_type eq "HASH" and defined $schema_node->{""}
$schema_type eq "HASH" defined $schema_node->{""}
Truth table66%2/3
A and B
A= $schema_type eq "HASH"B= defined $schema_node->{""}
ABresult
0X0
100
111
123 # if it's a hash, the the string representation of the node's default parameter
124 # may contain the type definition override
125 38 my $val = $schema_node->{''};
126 38 ▶ $schema_type = $schema_node->{''}->as_string if is_neat_array($val);
Branch: 1 branch
BranchTrueFalseTotal
if is_neat_array($val) 38 0 38
127 38 ▶ $schema_type = $schema_node->{''} if ref(\$val) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref \$val eq "SCALAR" 0 38 38
128 }
129  
130 # disambiguate fuzzy node schema types
131 119 ▶ if ($schema_type eq 'ARRAY_OR_HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_type eq "ARRAY_OR_HASH") 17 102 119
132 17 ▶ $schema_type = ($data_type eq 'HASH') ? 'HASH' : 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
$data_type eq 'HASH' ? : 2 15 17
133 }
134  
135 119 ▶ if ($schema_type eq 'STRING_OR_HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_type eq "STRING_OR_HASH") 2 117 119
136 2 ▶ $schema_type = ($data_type eq 'HASH') ? 'HASH' : 'STRING';
Branch: 1 branch
BranchTrueFalseTotal
$data_type eq 'HASH' ? : 1 1 2
137 }
138  
139 # automatic casting from ARRAY to STRING
140 119 ▶ if ($schema_type eq 'STRING' and $data_type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_type eq "STRING" and $data_type eq "ARRAY") 14 105 119
Condition66%2/3
$schema_type eq "STRING" and $data_type eq "ARRAY"
!ll&&!rl&&r
105014
MC/DC50%1/2
$schema_type eq "STRING" and $data_type eq "ARRAY"
$schema_type eq "STRING" $data_type eq "ARRAY"
Truth table66%2/3
A and B
A= $schema_type eq "STRING"B= $data_type eq "ARRAY"
ABresult
0X0
100
111
141 14 $parent_data->{$parent_data_key} = $data_node = $data_node->as_string;
142 14 $data_type = $schema_type;
143 }
144  
145 # automatic casting from ARRAY to BOOLEAN
146 119 ▶ if ($schema_type eq 'BOOLEAN' and $data_type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_type eq "BOOLEAN" and $data_type eq "ARRAY") 11 108 119
Condition66%2/3
$schema_type eq "BOOLEAN" and $data_type eq "ARRAY"
!ll&&!rl&&r
108011
MC/DC50%1/2
$schema_type eq "BOOLEAN" and $data_type eq "ARRAY"
$schema_type eq "BOOLEAN" $data_type eq "ARRAY"
Truth table66%2/3
A and B
A= $schema_type eq "BOOLEAN"B= $data_type eq "ARRAY"
ABresult
0X0
100
111
147 11 ▶ die "'".$data_node->as_string."' is not a valid boolean number\n" unless $data_node->is_boolean;
Branch: 1 branch
BranchTrueFalseTotal
unless $data_node->is_boolean 1 10 11
148 10 $parent_data->{$parent_data_key} = $data_node = $data_node->as_boolean;
149 10 $data_type = $schema_type;
150 }
151  
152 # skip (don't validate) DATA nodes
153 118 ▶ return 1 if ($schema_type eq 'DATA');
Branch: 1 branch
BranchTrueFalseTotal
if $schema_type eq "DATA" 10 108 118
154  
155 108 ▶ if ($schema_type eq 'LIST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_type eq "LIST") 8 100 108
156 # if this is not a simple array of scalars, wrap as an array
157 8 ▶ if (is_simple_array($data_node) or !is_any_array($data_node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_simple_array($data_node) or not is_any_array($data_node)) 2 6 8
Condition100%3/3
is_simple_array($data_node) or not is_any_array($data_node)
l!l&&r!l&&!r
116
MC/DC100%2/2
is_simple_array($data_node) or not is_any_array($data_node)
is_simple_array($data_node) not is_any_array($data_node)
Truth table100%3/3
A or not(B)
A= is_simple_array($data_node)B= not is_any_array($data_node)
ABresult
1X1
011
000
158 2 $data_node = [$data_node];
159 }
160 # then, convert an array to an ixhash with sequential keys
161 8 my $h = new_ixhash;
162 8 my $i = 0;
163 8 map { $h->{$i++} = $_ } @$data_node;
164 8 $parent_data->{$parent_data_key} = $data_node = $h;
165  
166 8 $data_type = 'HASH';
167 8 $schema_type = 'ARRAY';
168 }
169  
170 # see if automatic casting from HASH to ARRAY is possible
171 108 my $cast_to_array;
172  
173 108 ▶ if ($schema_type eq 'ARRAY' and $data_type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_type eq "ARRAY" and $data_type eq "HASH") 15 93 108
Condition100%3/3
$schema_type eq "ARRAY" and $data_type eq "HASH"
!ll&&!rl&&r
692415
MC/DC100%2/2
$schema_type eq "ARRAY" and $data_type eq "HASH"
$schema_type eq "ARRAY" $data_type eq "HASH"
Truth table100%3/3
A and B
A= $schema_type eq "ARRAY"B= $data_type eq "HASH"
ABresult
0X0
100
111
174 15 ▶ die "Can't cast '$pathstr' to ARRAY, since it is a HASH containing non-sequential keys" unless hash_has_sequential_keys($data_node);
Branch: 1 branch
BranchTrueFalseTotal
unless hash_has_sequential_keys($data_node) 1 14 15
175 14 $cast_to_array = 1;
176 }
177  
178 107 ▶ if ($schema_type ne $data_type && !$cast_to_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_type ne $data_type and not $cast_to_array) 0 107 107
Condition66%2/3
$schema_type ne $data_type and not $cast_to_array
!ll&&!rl&&r
93140
MC/DC0%0/2
$schema_type ne $data_type and not $cast_to_array
$schema_type ne $data_type not $cast_to_array
Truth table66%2/3
A and not(B)
A= $schema_type ne $data_typeB= not $cast_to_array
ABresult
0X0
100
111
179 0 die "'$pathstr' is $data_type, while it is expected to be $schema_type";
180 }
181  
182 107 ▶ if ($data_type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type eq "ARRAY") 24 83 107
183 # flatten the array
184 24 $parent_data->{$parent_data_key} = $data_node->as_flat_array;
185 }
186  
187 107 ▶ if ($data_type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type eq "HASH") 59 48 107
188 59 foreach my $key (keys %$data_node) {
189 101 my @a = @$path;
190 101 push @a, $key;
191 101 ▶ if ($key eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq '') { } 0 101 101
192 # TODO: check if the default parameter for the hash is allowed, and if it is a string or array
193 } else {
194 101 ▶ die "Can't validate '/", join('/', @a), "', because schema contains no definition for it" if !is_hash($schema_node);
Branch: 1 branch
BranchTrueFalseTotal
unless is_hash($schema_node) 1 100 101
195 100 ▶ my $schema_subnode = $schema_node->{$key} || $schema_node->{'*'};
Condition100%3/3
$schema_node->{$key} || $schema_node->{'*'}
l!l&&r!l&&!r
41581
MC/DC100%2/2
$schema_node->{$key} || $schema_node->{'*'}
$schema_node->{$key} $schema_node->{'*'}
Truth table100%3/3
A || B
A= $schema_node->{$key}B= $schema_node->{'*'}
ABresult
1X1
011
000
196 100 $self->validate_node($schema_subnode, $data_node->{$key}, $data_node, $key, \@a);
197 }
198 }
199 }
200  
201 99 ▶ if ($cast_to_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cast_to_array) 13 86 99
202 13 my @a = values %$data_node;
203 13 $parent_data->{$parent_data_key} = Config::Neat::Array->new(\@a);
204 }
205  
206 99 return 1;
207 }
208  
209 sub get_node_type {
210 238 ▶ my ($self, $node) = @_;
Subroutine get_node_type: called CC=4 SCAR=17.1
Pod undocumented
211 238 ▶ return 'HASH' if ref($node) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $node eq "HASH" 132 106 238
212 106 ▶ return 'ARRAY' if is_any_array($node);
Branch: 1 branch
BranchTrueFalseTotal
if is_any_array($node) 106 0 106
213 0 ▶ return 'STRING' if ref(\$node) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref \$node eq "SCALAR" 0 0 0
214 0 return 'UNKNOWN';
215 }
216  
217 1 1;
« lib/Config/Neat.pm ↑ Summary blib/lib/Config/Neat/Util.pm »