Declare/ZDCF/Validator.pm

filter: Statementstmt 98.6% 72 / 73 Branchbran 85.0% 17 / 20 Conditioncond 85.7% 6 / 7 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 94.9% 112 / 118 CC 17 SCAR 16.0
CC
17
Cov
95%
upgrade_structure
9.0
_get_validator
5.0
find_spec_version
3.0
CRAP 4.9
« Declare/Device.pm ↑ Summary Declare/Device/Runtime.pm »
1 package ZMQ::Declare::ZDCF::Validator;
2 {
3 1 $ZMQ::Declare::ZDCF::Validator::VERSION = '0.03';
4 }
5 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Data::Rx;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Scope for schema snippets
12 SCOPE: {
13 # The following spec snippets are shared between ZDCF 0.1 and ZDCF 1.0
14 1 my $context_schema = { # the top level context obj/hash
15 type => '//rec',
16 optional => { # can have these properties
17 iothreads => { type => '//int', range => {min => 1} },
18 verbose => '//bool',
19 },
20 };
21 1 my $option_schema = {
22 type => '//rec',
23 optional => {
24 "hwm" => { type => '//int' },
25 "swap" => { type => '//int' },
26 "affinity" => { type => '//int' },
27 "identity" => { type => '//str' },
28 "subscribe" => { type => '//str' },
29 "rate" => { type => '//int' },
30 "recovery_ivl" => { type => '//int' },
31 "mcast_loop" => { type => '//bool' },
32 "sndbuf" => { type => '//int' },
33 "rcvbuf" => { type => '//int' },
34 },
35 };
36 1 my $string_or_value_ary_schema = {
37 type => '//any',
38 of => [
39 { type => '//str' },
40 { type => '//arr', length => {min => 1}, contents => "//str" },
41 ]
42 };
43 my $socket_type_schema = {
44 type => '//any',
45 of => [
46 map {
47 1 { type => '//str', value => $_ },
48 { type => '//str', value => uc($_) }
49 } qw(sub pub req rep xreq xrep push pull pair router dealer)
50 ]
51 };
52 1 my $socket_schema = {
53 type => '//any',
54 of => [
55 {
56 type => '//rec',
57 required => {
58 type => $socket_type_schema,
59 bind => $string_or_value_ary_schema,
60 },
61 optional => {
62 connect => $string_or_value_ary_schema,
63 option => $option_schema,
64 },
65 },
66 {
67 type => '//rec',
68 required => {
69 type => $socket_type_schema,
70 connect => $string_or_value_ary_schema,
71 },
72 optional => {
73 bind => $string_or_value_ary_schema,
74 option => $option_schema,
75 },
76 }
77 ]
78 };
79  
80 # The following are versioned
81 # First for ZDCF 0.1
82 1 my $device_schema_0 = {
83 type => '//rec',
84 # device must have property called 'type'
85 required => { 'type' => {type => '//str'} },
86 rest => {type => '//map', values => $socket_schema}, # anything else is a socket (sigh)
87 };
88 1 my $base_zdcf_schema_0 = {
89 type => '//rec',
90 optional => {
91 context => $context_schema,
92 version => { type => '//num', range => {min => 0} },
93 },
94 rest => {type => '//map', values => $device_schema_0}, # anything but the context is a device
95 };
96  
97 # Now ZDCF 1.0
98 1 my $device_schema_1 = {
99 type => '//rec',
100 optional => {
101 # device CAN have property called 'type' (no longer required)
102 'type' => {type => '//str'},
103 'sockets' => {
104 type => '//map',
105 values => $socket_schema
106 },
107 },
108 };
109 1 my $app_schema_1 = {
110 type => '//rec',
111 optional => {
112 context => $context_schema,
113 devices => { type => '//map', values => $device_schema_1 },
114 },
115 };
116 1 my $base_zdcf_schema_1 = {
117 type => '//rec',
118 required => {
119 version => { type => '//num', range => {min => 0} },
120 },
121 optional => {
122 apps => { type => '//map', values => $app_schema_1 },
123 },
124 };
125  
126 # A single Rx object is enough
127 1 my $rx = Data::Rx->new;
128  
129 1 my %validator_schemata; # schema cache
130 19 ▶ sub _get_validator {
Subroutine _get_validator: called CC=5 SCAR=16.2
131 19 my $version = shift;
132  
133 # normalize version
134 19 ▶ my $major_version = int($version||0);
Condition100%2/2
$version || 0
l!l
514
MC/DC100%1/1
$version || 0
$version
Truth table100%2/2
A
A= $version
Aresult
11
00
135  
136 19 ▶ if (not exists $validator_schemata{$major_version}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $validator_schemata{$major_version}) 2 17 19
137 2 ▶ if ($major_version == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($major_version == 0) { } 1 1 2
elsif ($major_version == 1) { } 1 0 1
138 1 my $validator_schema = $rx->make_schema($base_zdcf_schema_0);
139 1 $validator_schemata{$major_version} = $validator_schema;
140 }
141 elsif ($major_version == 1) {
142 1 my $validator_schema = $rx->make_schema($base_zdcf_schema_1);
143 1 $validator_schemata{$major_version} = $validator_schema;
144 }
145 else {
146 0 die __PACKAGE__ . " does not support ZDCF specification version $version";
147 }
148 }
149  
150 19 return $validator_schemata{$major_version};
151 }
152 } # end SCOPE
153  
154 1 ▶sub validate {
Subroutine validate: called CC=2 SCAR=6.9
Pod documented
155 19 my ($self, $structure, $force_version) = @_;
156  
157 # Just extract the spec version so we use the right validation code
158 19 ▶ my $version = defined $force_version ? $force_version : $self->find_spec_version($structure);
Branch: 1 branch
BranchTrueFalseTotal
defined $force_version ? : 2 17 19
159  
160 19 return _get_validator($version)->check($structure);
161 }
162  
163 7 ▶sub upgrade_structure {
Subroutine upgrade_structure: called CC=9 SCAR=22.0
Pod documented
164 7 my ($self, $structure) = @_;
165 7 my $major_version = int( $self->find_spec_version($structure) );
166  
167 7 ▶ if ($major_version == 0 and keys %$structure) {
Branch: 1 branch
BranchTrueFalseTotal
if ($major_version == 0 and keys %$structure) 6 1 7
Condition66%2/3
$major_version == 0 and keys %$structure
!ll&&!rl&&r
016
MC/DC50%1/2
$major_version == 0 and keys %$structure
$major_version == 0 keys %$structure
Truth table66%2/3
A and B
A= $major_version == 0B= keys %$structure
ABresult
0X0
100
111
168 # introduce "apps", "devices", and "sockets" intermediate layers
169  
170 # add "apps" layer
171 6 my $app = {};
172 6 foreach my $key (keys %$structure) {
173 10 ▶ next if $key eq 'version';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "version" 0 10 10
174 10 $app->{$key} = delete $structure->{$key};
175 }
176 6 $structure->{apps} = {"" => $app};
177  
178 # add "devices" layer
179 6 my $devices = {};
180 6 foreach my $key (keys %$app) {
181 10 ▶ next if $key eq 'context';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "context" 6 4 10
182 4 my $device = $devices->{$key} = delete $app->{$key};
183 4 $devices->{$key} = $device;
184  
185 # add "sockets" layer
186 4 my $sockets = {};
187 4 foreach my $key (keys %$device) {
188 6 ▶ next if $key eq 'type';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "type" 4 2 6
189 2 $sockets->{$key} = delete $device->{$key};
190 }
191 4 $device->{sockets} = $sockets;
192  
193 } # end foreach key in application
194 6 $app->{devices} = $devices;
195  
196 6 $structure->{version} = 1.0;
197 } # end if have to upgrade from v0
198 }
199  
200 26 ▶sub find_spec_version {
Subroutine find_spec_version: called CC=3 SCAR=11.0
Pod documented
201 26 my ($self, $structure) = @_;
202  
203 26 ▶ return undef if not ref($structure) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $structure eq "HASH" 1 25 26
204 25 ▶ my $spec_version = $structure->{version} || '0'; # 0 == pre-versioned spec
Condition100%2/2
$structure->{'version'} || '0'
l!l
520
MC/DC100%1/1
$structure->{'version'} || '0'
$structure->{'version'}
Truth table100%2/2
A
A= $structure->{'version'}
Aresult
11
00
205 25 return $spec_version;
206 }
207  
208 4 ▶sub validate_and_upgrade {
Subroutine validate_and_upgrade: called CC=2 SCAR=7.0
Pod documented
209 4 my ($self, $structure) = @_;
210 4 ▶ return undef if not $self->validate($structure);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validate($structure) 0 4 4
211 4 my $copy = Clone::clone($structure);
212 4 $self->upgrade_structure($copy);
213 4 return $copy;
214 }
215  
216 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
217 1 __PACKAGE__->meta->make_immutable;
218  
219 __END__
« Declare/Device.pm ↑ Summary Declare/Device/Runtime.pm »