Pad/Spec/Option.pm

filter: Statementstmt 97.6% 327 / 335 Branchbran 100% 78 / 78 Conditioncond 88.5% 31 / 35 MC/DCmcdc 80.9% 17 / 21 Subroutinesub 93.4% 57 / 61 Podpod 22.0% 11 / 50 totaltotal 89.8% 521 / 580 CC 56 SCAR 11.6
CC
56
Cov
97%
checkValue
8.0
settledValue
7.0
objectsFromPairs
6.0
CRAP 3.2
« Pad/Config/Format/Json.pm ↑ Summary Pad/Type/Int.pm »
1 12 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 12 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 12 ▶use Getopt::Pad::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Getopt::Pad::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Getopt::Pad::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 class Getopt::Pad::Spec::Option :strict(params) {
9 12 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶ use Getopt::Pad::Util qw(camelize specError isValidName optionSpelling processedWith);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 our $VERSION = '0.06';
13  
14 # The Value sources a parse hands over, in order of precedence, each with
15 # the wording of its user errors, which name the option as typed on the
16 # command line and by its Primary name in a config file; the spec
17 # default follows them. The command line gives a multiple option one
18 # word per occurrence, a config file a lone value or a list.
19 12 my @valueSources = (
20 { key => 'commandLine', problemFormat => "option '%s': %s", givesWords => 1, namesAsTyped => 1 },
21 { key => 'config', problemFormat => "config value for '%s': %s", givesWords => 0, namesAsTyped => 0 },
22 );
23 12 my %isValueSource = map { $_->{key} => 1 } @valueSources;
24  
25 12 field $key :param;
26 12 field $raw :param;
27 0 ▶ field $auto :param :reader = 0;
Subroutine auto: not called CC=1 SCAR=0.8
Pod undocumented
28 0 ▶ field $optionalValue :param :reader = 0;
Subroutine auto: called CC=1 SCAR=0.6
Subroutine optionalValue: not called CC=1 SCAR=0.5
Pod undocumented
Pod undocumented
29 0 ▶ field $trigger :param :reader = undef;
Subroutine optionalValue: called CC=1 SCAR=0.4
Pod undocumented
30 # Where the option sits in the spec, as the start of a spec error, e.g.
31 # "command 'image resize': ".
32 1105 ▶ field $where :param = '';
Subroutine trigger: called CC=1 SCAR=0.0
Pod undocumented
33  
34 4756 ▶ field $name :reader;
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
35 3202 ▶ field @aliases :reader;
Subroutine aliases: called CC=1 SCAR=0.0
Pod undocumented
36 1338 ▶ field $reader :reader;
Subroutine reader: called CC=1 SCAR=0.0
Pod undocumented
37 395 ▶ field $type :reader;
Subroutine type: called CC=1 SCAR=0.0
Pod undocumented
38 1 ▶ field $typeName :reader;
Subroutine typeName: called CC=1 SCAR=0.0
Pod undocumented
39 96 field $required :reader = 0;
40 96 ▶ field $hasDefault :reader = 0;
Subroutine required: called CC=1 SCAR=0.0
Pod undocumented
41 1 ▶ field $default :reader;
Subroutine default: called CC=1 SCAR=0.0
Subroutine hasDefault: called CC=1 SCAR=0.0
Pod undocumented
Pod undocumented
42 12 field $specDefault;
43 123 ▶ field $valid :reader;
Subroutine valid: called CC=1 SCAR=0.0
Pod undocumented
44 12 field $lazyValid;
45 168 ▶ field $processValue :reader;
Subroutine processValue: called CC=1 SCAR=0.0
Pod undocumented
46 208 ▶ field $group :reader;
Subroutine group: called CC=1 SCAR=0.0
Pod undocumented
47 160 field $help :reader = '';
48 0 ▶ field $multiple :reader = 0;
Subroutine help: called CC=1 SCAR=0.4
Pod undocumented
49 0 ▶ field $hash :reader = 0;
Subroutine multiple: not called CC=1 SCAR=0.4
Pod undocumented
50 184 ▶ field $csv :reader = 0;
Subroutine hash: called CC=1 SCAR=0.0
Pod undocumented
51 179 ▶ field $objectlist :reader = 0;
Subroutine csv: called CC=1 SCAR=0.0
Pod undocumented
52 170 ▶ field $hidden :reader = 0;
Subroutine objectlist: called CC=1 SCAR=0.0
Pod undocumented
53 319 ▶ field $inherit :reader = 0;
Subroutine hidden: called CC=1 SCAR=0.0
Pod undocumented
54 0 ▶ field $typehint :reader;
Subroutine inherit: called CC=1 SCAR=0.6
Subroutine typehint: not called CC=1 SCAR=0.6
Pod undocumented
Pod undocumented
55  
56 ADJUST {
57 specError("%soption key must be a non-empty string", $where) if !defined $key || $key eq '';
58 specError("%soption '%s': spec must be a hash reference", $where, $key) if ref $raw ne 'HASH';
59 specError("%soption '%s': a trigger is only allowed on auto options", $where, $key) if defined $trigger && !$auto;
60  
61 ($name, @aliases) = split /\|/, $key, -1;
62 foreach my $candidate ($name, @aliases) {
63 specError("%soption '%s': invalid name '%s'", $where, $key, $candidate // '') if !isValidName($candidate);
64 }
65 $reader = camelize($name);
66 specError("%soption '%s': reader '%s' collides with a built-in result method", $where, $name, $reader) if !$auto && Getopt::Pad::Result->reservesReader($reader);
67  
68 my %spec = $raw->%*;
69 ($type, $typeName) = Getopt::Pad::Type::takeFromSpec(\%spec, 'flag', sprintf("%soption '%s'", $where, $name));
70  
71 $required = delete $spec{required} ? 1 : 0;
72 if (exists $spec{default}) {
73 $hasDefault = 1;
74 $default = delete $spec{default};
75 $specDefault = $default;
76 }
77 $valid = delete $spec{valid};
78 $lazyValid = delete $spec{lazyValid};
79 $processValue = delete $spec{processValue};
80 $group = delete $spec{group} // 'Options';
81 $help = delete $spec{help} // '';
82 $multiple = delete $spec{multiple} ? 1 : 0;
83 $hash = delete $spec{hash} ? 1 : 0;
84 $csv = delete $spec{csv} ? 1 : 0;
85 $objectlist = delete $spec{objectlist} ? 1 : 0;
86 $hidden = delete $spec{hidden} ? 1 : 0;
87 $inherit = delete $spec{inherit} ? 1 : 0;
88 $typehint = delete $spec{typehint};
89  
90 specError("%soption '%s': unknown key(s): %s", $where, $name, join(', ', sort keys %spec)) if %spec;
91 specError("%soption '%s': required and default are mutually exclusive", $where, $name) if $required && $hasDefault;
92 my %shapeFlags = (multiple => $multiple, hash => $hash, objectlist => $objectlist);
93 my @shapes = grep { $shapeFlags{$_} } qw(multiple hash objectlist);
94 specError("%soption '%s': %s requires a value-taking type, not '%s'", $where, $name, $shapes[0], $typeName) if @shapes && !$type->takesValue;
95 specError("%soption '%s': %s are mutually exclusive", $where, $name, join(' and ', @shapes)) if @shapes > 1;
96 specError("%soption '%s': csv requires multiple", $where, $name) if $csv && !$multiple;
97 specError("%soption '%s': valid must be an array or code reference", $where, $name) if defined $valid && ref $valid ne 'ARRAY' && ref $valid ne 'CODE';
98 specError("%soption '%s': lazyValid must be a code reference", $where, $name) if defined $lazyValid && ref $lazyValid ne 'CODE';
99 specError("%soption '%s': processValue must be a code reference", $where, $name) if defined $processValue && ref $processValue ne 'CODE';
100 specError("%soption '%s': typehint must be a non-empty string", $where, $name) if defined $typehint && (ref $typehint || $typehint eq '');
101  
102 $default = $self->checkedDefault($default) if $hasDefault;
103 }
104  
105 # The spec default in the option's shape, every value checked and
106 # coerced. Only a scalar default may be undef: it means "no default"
107 # to the help output.
108 126 ▶ method checkedDefault($value) {
Subroutine checkedDefault: called CC=5 SCAR=16.3
Pod undocumented
109 126 ▶ return undef if !defined $value && !$multiple && !$hash && !$objectlist;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value or $multiple or $hash or $objectlist 2 124 126
Condition66%6/9
defined $value or $multiple or $hash or $objectlist
l!l&&r!l&&!r
12402
defined $value or $multiple or $hash
l!l&&r!l&&!r
12402
defined $value or $multiple
l!l&&r!l&&!r
12402
MC/DC25%1/4
defined $value or $multiple or $hash or $objectlist
defined $value $multiple $hash $objectlist
Truth table40%2/5
A or B or C or D
A= defined $valueB= $multipleC= $hashD= $objectlist
ABCDresult
1XXX1
01XX1
001X1
00011
00000
110 124 ▶ return $self->shapedValue($value, sub ($problem) { specError("%soption '%s': default value: %s", $where, $name, $problem) });
Subroutine __ANON__: called CC=1 SCAR=0.0
111 12 }
112  
113 # The default as help output and config files show it: as the spec
114 # wrote it. A converted value may not read back (a duration coerced to
115 # seconds), or be an object that cannot be shown at all.
116 59 ▶ method presentedDefault() {
Subroutine presentedDefault: called CC=1 SCAR=0.0
Pod documented
117 59 return $specDefault;
118 12 }
119  
120 # The Primary name as it is typed on the command line.
121 79 ▶ method spelling() {
Subroutine spelling: called CC=1 SCAR=0.0
Pod documented
122 79 return optionSpelling($name);
123 12 }
124  
125 # What the reader gets when no value source set the option and the spec
126 # has no default: an empty list or mapping, or undef.
127 76 ▶ method emptyValue() {
Subroutine emptyValue: called CC=4 SCAR=13.9
Pod undocumented
128 76 ▶ return [] if $multiple || $objectlist;
Branch: 1 branch
BranchTrueFalseTotal
if $multiple or $objectlist 21 55 76
Condition100%3/3
$multiple or $objectlist
l!l&&r!l&&!r
20155
MC/DC100%2/2
$multiple or $objectlist
$multiple $objectlist
Truth table100%3/3
A or B
A= $multipleB= $objectlist
ABresult
1X1
011
000
129 55 ▶ return {} if $hash;
Branch: 1 branch
BranchTrueFalseTotal
if $hash 21 34 55
130 34 return undef;
131 12 }
132  
133 # Each parse gets its own copy of a list, mapping or objectlist default.
134 38 ▶ method copiedDefault() {
Subroutine copiedDefault: called CC=4 SCAR=13.9
Pod undocumented
135 38 ▶ return [map { ref $_ eq 'HASH' ? { $_->%* } : $_ } $default->@*] if ref $default eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
if ref $default eq "ARRAY" 4 34 38
ref $_ eq 'HASH' ? : 2 2 4
136 34 ▶ return { $default->%* } if ref $default eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $default eq "HASH" 2 32 34
137 32 return $default;
138 12 }
139  
140 # Whether the option collects key=value words: a hash option, or an
141 # objectlist option with its INDEX.FIELD keys.
142 1131 ▶ method takesPairs() {
Subroutine takesPairs: called CC=3 SCAR=11.0
Pod documented
143 1131 ▶ return $hash || $objectlist ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$hash || $objectlist ? : 60 1071 1131
Condition100%3/3
$hash or $objectlist
l!l&&r!l&&!r
48121071
MC/DC100%2/2
$hash or $objectlist
$hash $objectlist
Truth table100%3/3
A or B
A= $hashB= $objectlist
ABresult
1X1
011
000
144 12 }
145  
146 # The tag the help output renders after the help text: the spec's
147 # typehint, or what the type calls itself.
148 109 ▶ method typeLabel() {
Subroutine typeLabel: called CC=2 SCAR=6.9
Pod documented
149 109 ▶ return $typehint // $type->label;
Condition100%3/3
$typehint // $type->label
l!l&&r!l&&!r
62281
MC/DC100%2/2
$typehint // $type->label
$typehint $type->label
Truth table100%3/3
A // B
A= $typehintB= $type->label
ABresult
1X1
011
000
150 12 }
151  
152 # The values the valid constraint allows right now: the static list, or
153 # what the valid coderef produces when asked. Empty without a constraint.
154 103 ▶ method validValues() {
Subroutine validValues: called CC=4 SCAR=13.9
Pod documented
155 103 ▶ return () if !defined $valid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $valid 1 102 103
156 102 ▶ return $valid->@* if ref $valid eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $valid eq "ARRAY" 95 7 102
157  
158 7 my $values = $valid->();
159 7 ▶ specError("%soption '%s': the valid coderef must return an array reference", $where, $name) if ref $values ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $values ne "ARRAY" 1 6 7
160 6 return $values->@*;
161 12 }
162  
163 # Check $value's shape, then the type, the valid list and the lazyValid
164 # predicate. Returns the problem description (undef when the value is
165 # acceptable) and the coerced value; the caller decides how to report
166 # the problem.
167 260 ▶ method checkValue($value) {
Subroutine checkValue: called CC=8 SCAR=20.8
Pod documented
168 260 ▶ return ('expected a single value, not a list or mapping', $value) if ref $value eq 'ARRAY' || ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" or ref $value eq "HASH" 4 256 260
Condition100%3/3
ref $value eq "ARRAY" or ref $value eq "HASH"
l!l&&r!l&&!r
22256
MC/DC100%2/2
ref $value eq "ARRAY" or ref $value eq "HASH"
ref $value eq "ARRAY" ref $value eq "HASH"
Truth table100%3/3
A or B
A= ref $value eq "ARRAY"B= ref $value eq "HASH"
ABresult
1X1
011
000
169  
170 256 my $problem = $type->check($value);
171 256 ▶ return ($problem, $value) if defined $problem;
Branch: 1 branch
BranchTrueFalseTotal
if defined $problem 10 246 256
172  
173 246 $value = $type->coerce($value);
174 246 ▶ if (defined $valid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valid) 88 158 246
175 88 my @allowed = $self->validValues;
176 87 ▶ return (sprintf("'%s' is not one of: %s", $value, join(', ', @allowed)), $value) if !grep { $_ eq $value } @allowed;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $value;} @allowed 8 79 87
177 }
178 237 ▶ return (sprintf("'%s' is not a valid value", $value), $value) if defined $lazyValid && !$lazyValid->($value);
Branch: 1 branch
BranchTrueFalseTotal
if defined $lazyValid and not &$lazyValid($value) 1 236 237
Condition66%2/3
defined $lazyValid and not &$lazyValid($value)
!ll&&!rl&&r
23601
MC/DC50%1/2
defined $lazyValid and not &$lazyValid($value)
defined $lazyValid not &$lazyValid($value)
Truth table66%2/3
A and not(B)
A= defined $lazyValidB= not &$lazyValid($value)
ABresult
0X0
100
111
179 236 return (undef, $value);
180 12 }
181  
182 # The checked value one parse settles on, and the reporter for the
183 # problems its type's verify and prepare find in it later, worded for
184 # where the value came from. The empty value of an option no source
185 # sets leaves nothing to verify: its reporter is undef. %sources maps
186 # each Value source to the raw values it gave, keyed by Primary name; a
187 # name missing from a map means that source did not set the option.
188 254 ▶ method settledValue(%sources) {
Subroutine settledValue: called CC=7 SCAR=19.5
Pod documented
189 254 my @unknown = grep { !$isValueSource{$_} } sort keys %sources;
190 254 ▶ croak(sprintf("Getopt::Pad: unknown value source(s): %s", join(', ', @unknown))) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 253 254
191  
192 253 foreach my $source (@valueSources) {
193 432 ▶ my $given = $sources{$source->{key}} // {};
Condition100%2/2
$sources{$source->{"key"}} // {}
l!l
39735
MC/DC100%1/1
$sources{$source->{"key"}} // {}
$sources{$source->{"key"}}
Truth table100%2/2
A
A= $sources{$source->{"key"}}
Aresult
11
00
194 432 ▶ next if !exists $given->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $given->{$name} 297 135 432
195  
196 135 my $report = $self->reporterFor($source);
197 135 return ($self->validatedValue($given->{$name}, $source, $report), $report);
198 }
199  
200 118 ▶ return ($self->copiedDefault, $self->defaultReporter) if $hasDefault;
Branch: 1 branch
BranchTrueFalseTotal
if $hasDefault 38 80 118
201 80 ▶ Getopt::Pad::Error->throw("missing required option '%s'", $self->spelling) if $required;
Branch: 1 branch
BranchTrueFalseTotal
if $required 4 76 80
202 76 return ($self->emptyValue, undef);
203 12 }
204  
205 # The checked value one parse settles on, see settledValue.
206 52 ▶ method readerValue(%sources) {
Subroutine readerValue: called CC=1 SCAR=0.0
Pod documented
207 52 my ($value) = $self->settledValue(%sources);
208 35 return $value;
209 12 }
210  
211 7 ▶ method processedValue($result, $value) {
Subroutine processedValue: called CC=1 SCAR=0.0
Pod documented
212 7 return processedWith($processValue, $result, $value);
213 12 }
214  
215 # A reporter that throws a problem in the wording of $source: the
216 # option named as typed for the command line, by its Primary name for
217 # a config file.
218 135 ▶ method reporterFor($source) {
Subroutine reporterFor: called CC=2 SCAR=6.9
Pod undocumented
219 135 ▶ my $named = $source->{namesAsTyped} ? $self->spelling : $name;
Branch: 1 branch
BranchTrueFalseTotal
$source->{'namesAsTyped'} ? : 74 61 135
220 135 ▶ return sub ($problem) { Getopt::Pad::Error->throw($source->{problemFormat}, $named, $problem) };
Subroutine __ANON__: called CC=1 SCAR=0.0
221 12 }
222  
223 38 ▶ method defaultReporter() {
Subroutine defaultReporter: called CC=1 SCAR=0.0
Pod undocumented
224 38 ▶ return sub ($problem) { Getopt::Pad::Error->throw("option '%s': default value: %s", $self->spelling, $problem) };
Subroutine __ANON__: called CC=1 SCAR=0.0
225 12 }
226  
227 # The value one source gave, in the option's shape, with problems told
228 # to $report. The command line gives a multiple option its words and a
229 # pair-taking option a flat mapping of its key=value pairs; a config
230 # file gives the shape directly.
231 135 ▶ method validatedValue($value, $source, $report) {
Subroutine validatedValue: called CC=4 SCAR=13.9
Pod undocumented
232 135 ▶ return [map { $self->checkedScalar($_, $report) } $self->listItems($value, $source, $report)] if $multiple;
Branch: 1 branch
BranchTrueFalseTotal
if $multiple 16 119 135
233 119 ▶ $value = $self->objectsFromPairs($value, $report) if $objectlist && $source->{givesWords};
Branch: 1 branch
BranchTrueFalseTotal
if $objectlist and $source->{"givesWords"} 9 110 119
Condition100%3/3
$objectlist and $source->{"givesWords"}
!ll&&!rl&&r
10739
MC/DC100%2/2
$objectlist and $source->{"givesWords"}
$objectlist $source->{"givesWords"}
Truth table100%3/3
A and B
A= $objectlistB= $source->{"givesWords"}
ABresult
0X0
100
111
234 114 return $self->shapedValue($value, $report);
235 12 }
236  
237 # The items a multiple option received from $source. A csv option splits
238 # every word and every lone config value at commas; a config list is
239 # taken as given.
240 16 ▶ method listItems($value, $source, $report) {
Subroutine listItems: called CC=5 SCAR=16.1
Pod undocumented
241 16 ▶ my $isConfigList = ref $value eq 'ARRAY' && !$source->{givesWords};
Condition100%3/3
ref $value eq 'ARRAY' && !$source->{'givesWords'}
!ll&&!rl&&r
484
MC/DC100%2/2
ref $value eq 'ARRAY' && !$source->{'givesWords'}
ref $value eq 'ARRAY' !$source->{'givesWords'}
Truth table100%3/3
A && not(B)
A= ref $value eq 'ARRAY'B= !$source->{'givesWords'}
ABresult
0X0
100
111
242 16 ▶ return $value->@* if $isConfigList;
Branch: 1 branch
BranchTrueFalseTotal
if $isConfigList 4 12 16
243  
244 12 ▶ my @scalars = ref $value eq 'ARRAY' ? $value->@* : ($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 8 4 12
245 12 ▶ return @scalars if !$csv;
Branch: 1 branch
BranchTrueFalseTotal
unless $csv 5 7 12
246 7 return map { $self->csvItems($_, $report) } @scalars;
247 12 }
248  
249 # Items are trimmed; one trailing comma is tolerated, an empty item is
250 # not. An undefined value passes through to be reported as missing.
251 8 ▶ method csvItems($word, $report) {
Subroutine csvItems: called CC=4 SCAR=13.9
Pod undocumented
252 8 ▶ return ($word) if !defined $word;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $word 1 7 8
253  
254 7 my $trimmed = $word =~ s/\A\s+|\s+\z//gr =~ s/,\z//r;
255 7 my @items = map { s/\A\s+|\s+\z//gr } split /,/, $trimmed, -1;
256 7 ▶ $report->(sprintf("'%s' contains an empty item", $word)) if !@items || grep { $_ eq '' } @items;
Branch: 1 branch
BranchTrueFalseTotal
if not @items or grep {$_ eq "";} @items 2 5 7
Condition100%3/3
not @items or grep {$_ eq "";} @items
l!l&&r!l&&!r
115
MC/DC100%2/2
not @items or grep {$_ eq "";} @items
not @items grep {$_ eq "";} @items
Truth table100%3/3
not(A) or B
A= not @itemsB= grep {$_ eq "";} @items
ABresult
1X1
011
000
257 5 return @items;
258 12 }
259  
260 # The objects an objectlist option collects from its INDEX.FIELD=VALUE
261 # words, handed over as a flat mapping. The indices must form 0..n-1,
262 # each written one way only: a leading zero would make 0 and 00 two
263 # keys for one object.
264 9 ▶ method objectsFromPairs($pairs, $report) {
Subroutine objectsFromPairs: called CC=6 SCAR=17.9
Pod undocumented
265 9 my %objectAt;
266 9 foreach my $key (sort keys $pairs->%*) {
267 13 ▶ $report->(sprintf("invalid key '%s', the index must not have leading zeros", $key)) if $key =~ /\A0[0-9]+\./;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\A0[0-9]+\./u 1 12 13
268 12 ▶ my ($index, $field) = $key =~ /\A([0-9]+)\.([\w-]+)\z/ or $report->(sprintf("invalid key '%s', expected INDEX.FIELD=VALUE", $key));
Branch: 1 branch
BranchTrueFalseTotal
() = $key =~ /\A([0-9]+)\.([\w-]+)\z/u or &$report(sprintf(q[invalid key '%s', expected INDEX.FIELD=VALUE], $key)) 10 2 12
269 10 $objectAt{$index}{$field} = $pairs->{$key};
270 }
271  
272 # The sorted indices match their positions unless one is missing;
273 # comparing them needs no array as large as the largest index.
274 6 my @indices = sort { $a <=> $b } keys %objectAt;
275 6 foreach my $position (0 .. $#indices) {
276 8 ▶ $report->(sprintf('missing index %d', $position)) if $indices[$position] != $position;
Branch: 1 branch
BranchTrueFalseTotal
if $indices[$position] != $position 2 6 8
277 }
278 4 return [map { $objectAt{$_} } @indices];
279 12 }
280  
281 # $value in the option's shape with every scalar checked and coerced: a
282 # list for multiple, a mapping for hash, a list of mappings for
283 # objectlist, else one scalar. $report is told about a problem, worded
284 # without the option name, and does not return.
285 238 ▶ method shapedValue($value, $report) {
Subroutine shapedValue: called CC=4 SCAR=13.9
Pod undocumented
286 238 ▶ return $self->shapedList($value, $report) if $multiple;
Branch: 1 branch
BranchTrueFalseTotal
if $multiple 3 235 238
287 235 ▶ return $self->shapedMapping($value, $report) if $hash;
Branch: 1 branch
BranchTrueFalseTotal
if $hash 18 217 235
288 217 ▶ return $self->shapedObjects($value, $report) if $objectlist;
Branch: 1 branch
BranchTrueFalseTotal
if $objectlist 12 205 217
289 205 return $self->checkedScalar($value, $report);
290 12 }
291  
292 3 ▶ method shapedList($value, $report) {
Subroutine shapedList: called CC=2 SCAR=6.9
Pod undocumented
293 3 ▶ $report->('expected a list of values') if ref $value ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value ne "ARRAY" 1 2 3
294 2 return [map { $self->checkedScalar($_, $report) } $value->@*];
295 12 }
296  
297 32 ▶ method shapedMapping($value, $report) {
Subroutine shapedMapping: called CC=4 SCAR=13.9
Pod undocumented
298 32 ▶ $report->('expected a mapping of keys to values') if ref $value ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value ne "HASH" 5 27 32
299  
300 27 my %checked;
301 27 foreach my $key (sort keys $value->%*) {
302 32 ▶ $report->('empty key') if $key eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "" 2 30 32
303 30 $checked{$key} = $self->checkedScalar($value->{$key}, $self->reportUnder($report, sprintf("key '%s'", $key)));
304 }
305 21 return \%checked;
306 12 }
307  
308 12 ▶ method shapedObjects($value, $report) {
Subroutine shapedObjects: called CC=2 SCAR=6.9
Pod undocumented
309 12 ▶ $report->('expected a list of mappings') if ref $value ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value ne "ARRAY" 2 10 12
310 10 return [map { $self->shapedMapping($value->[$_], $self->reportUnder($report, sprintf('entry %d', $_))) } 0 .. $#$value];
311 12 }
312  
313 # A reporter that prefixes where the problem sits, e.g. "key 'os': ...".
314 44 ▶ method reportUnder($report, $where) {
Subroutine reportUnder: called CC=1 SCAR=0.0
Pod undocumented
315 44 ▶ return sub ($problem) { $report->(sprintf('%s: %s', $where, $problem)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
316 12 }
317  
318 263 ▶ method checkedScalar($value, $report) {
Subroutine checkedScalar: called CC=3 SCAR=11.0
Pod undocumented
319 263 ▶ $report->('no value given') if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 3 260 263
320  
321 260 my ($problem, $coerced) = $self->checkValue($value);
322 259 ▶ $report->($problem) if defined $problem;
Branch: 1 branch
BranchTrueFalseTotal
if defined $problem 23 236 259
323 236 return $coerced;
324 12 }
325  
326 1115 ▶ method glSpec() {
Subroutine glSpec: called CC=1 SCAR=0.0
Pod documented
327 1115 return $type->glSpec(join('|', $name, @aliases), optionalValue => $optionalValue, multiple => $multiple, hash => $self->takesPairs);
328 12 }
329  
330 235 ▶ method negatable() {
Subroutine negatable: called CC=1 SCAR=0.0
Pod documented
331 235 return $type->negatable;
332 12 }
333 }
334  
335 12 1;
336  
337 __END__
« Pad/Config/Format/Json.pm ↑ Summary Pad/Type/Int.pm »