blib/lib/IO/K8s/Role/SpecBuilder.pm

filter: Statementstmt 98.7% 235 / 238 Branchbran 90.8% 158 / 174 Conditioncond 63.0% 46 / 73 MC/DCmcdc 34.7% 16 / 46 Subroutinesub 100% 37 / 37 Podpod 100% 7 / 7 totaltotal 86.7% 499 / 575 CC 118 SCAR 20.1
CC
118
Cov
91%
spec_delete
18.6
_sb_walk_vivify
9.5
_sb_inflate
9.2
CRAP 7.5
« blib/lib/IO/K8s.pm ↑ Summary lib/IO/K8s/ApiextensionsApiserver/Pkg/Apis/Apiextensions/V1/JSONSchemaPropsOrBool.pm »
1 package IO::K8s::Role::SpecBuilder;
2 # ABSTRACT: Role for deep-path spec manipulation on CRD objects
3 143 our $VERSION = '1.112';
4 143 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 143 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 143 ▶use Module::Runtime qw(use_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 # Imports above `use Moo::Role` on purpose: Role::Tiny treats subs already in
8 # the package as not-methods, so their names stay off every consumer. A `use`
9 # below that line composes its exports onto all shipped classes (k118).
10 143 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Carp treats IO::K8s as part of this role (k175), the rule IO::K8s::CRD
13 # (k165) and IO::K8s::List (k170) follow: a write that hands a hash to a
14 # typed field inflates it through IO::K8s, and a shape croak raised there --
15 # or a constructor error, which IO::K8s moves to Carp's answer (k164) --
16 # names the line that called spec_set or spec_merge, not the inflation call
17 # in this file. A caller in any other package still sees its own line.
18 143 our @CARP_NOT = ('IO::K8s');
19  
20 # ---------------------------------------------------------------------------
21 # A node on a spec path is one of: a plain hashref, a plain arrayref, or an
22 # IO::K8s object (anything composing IO::K8s::Role::Resource). Segments are
23 # dot-separated. An integer segment indexes an array; -1 is the last element,
24 # or the one created on an empty array. Anything else is a hash key or, on an
25 # object, a JSON field name mapped to its attribute through the registry (so
26 # '$ref' reaches _ref). A field an object does not declare lives in its
27 # _unknown_fields bag (D1) and is reachable like any other key.
28 #
29 # A union object -- V1::JSON, JSONSchemaPropsOr{Array,Bool,StringArray} --
30 # is not a node of its own: it serializes as the bare value it holds, so a
31 # path walks through it into that value (k172), the node its
32 # _spec_path_node names. Before, the walk treated it as an ordinary object
33 # with no fields and wrote every key into its _unknown_fields bag, which a
34 # union's TO_JSON never reads.
35 #
36 # Before 1.108 every method assumed spec was a hashref: on a modeled spec
37 # spec_set replaced the struct with {} and wrote into an orphan (k90).
38 # ---------------------------------------------------------------------------
39  
40 # Run a Moo constructor or accessor call from inside a walk and re-raise
41 # its failure with the spec path in front: Moo and Type::Tiny name the
42 # attribute, never the path the caller wrote, and a "Missing required
43 # arguments" from a class the walk tried to vivify would otherwise be
44 # reported against this file's line number (k101). Type::Tiny's multi-line
45 # validation errors put "at FILE line N" on the *first* line and then
46 # append several explanation lines after it, so the strip below is
47 # unanchored and global -- every embedded "at FILE line N" fragment goes,
48 # wherever it sits, while the explanation lines stay. croak still appends
49 # its own single trailing "at CALLER line N." to the re-raised message;
50 # that one is correct (it points at the caller of the spec_* method) and
51 # is left alone.
52 #
53 # Detecting failure from eval's own return value, not from $@'s truthiness,
54 # matters here: $code->() can legitimately `die 0` or `die ""`, and either
55 # one sets $@ to a value that is itself FALSE, so a bare `unless $@` check
56 # would read that as success and hand back an undef $result as if nothing
57 # had gone wrong. Stringifying $@ only inside this block, once eval's own
58 # return value has confirmed a failure, also avoids coercing an exception
59 # object to a string before it is known there is one to report.
60 451 ▶sub _sb_guard {
Subroutine _sb_guard: called CC=2 SCAR=6.9
61 451 my ($path, $what, $code) = @_;
62 451 my $result;
63 451 ▶ unless (eval { $result = $code->(); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $result = &$code(); 1 } }) 15 436 451
64 15 my $err = "$@";
65 15 $err =~ s/ at \S+ line \d+\.?//g;
66 15 $err =~ s/\s+\z//;
67 15 croak "spec path '$path': $what: $err";
68 }
69 436 return $result;
70 }
71  
72 1499 ▶sub _sb_is_obj { blessed($_[0]) && $_[0]->can('_k8s_attr_info') }
Subroutine _sb_is_obj: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and $_[0]->can("_k8s_attr_info") 1273 226 1499
73  
74 103 ▶sub _sb_is_index { defined $_[0] && $_[0] =~ /\A-?\d+\z/ }
Subroutine _sb_is_index: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and $_[0] =~ /\A-?\d+\z/ 101 2 103
75  
76 # A union object (k172): one that names the node a path walks into. Its
77 # _spec_path_node returns that node -- a container, an object, a scalar, or
78 # undef when the union holds nothing -- and, when the node is a typed
79 # array the union keeps in an attribute of its own, that attribute's name.
80 964 ▶sub _sb_is_union { blessed($_[0]) && $_[0]->can('_spec_path_node') }
Subroutine _sb_is_union: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and $_[0]->can("_spec_path_node") 723 241 964
81  
82 # JSON key -> (attribute name, registry info) on an object; empty list when
83 # the object declares no such field.
84 1213 ▶sub _sb_attr {
Subroutine _sb_attr: called CC=4 SCAR=13.9
85 1213 my ($node, $key) = @_;
86 1213 my $info = $node->_k8s_attr_info;
87 1213 for my $attr (keys %$info) {
88 5039 ▶ return ($attr, $info->{$attr}) if ($info->{$attr}{json_key} // $attr) eq $key;
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{$attr}{"json_key"} // $attr) eq $key 1172 3867 5039
Condition66%2/3
$info->{$attr}{"json_key"} // $attr
l!l&&r!l&&!r
15848810
MC/DC0%0/2
$info->{$attr}{"json_key"} // $attr
$info->{$attr}{"json_key"} $attr
Truth table66%2/3
A // B
A= $info->{$attr}{"json_key"}B= $attr
ABresult
1X1
011
000
89 }
90 41 return;
91 }
92  
93 # Resolve an index against an array for writing. -1 on an empty array is the
94 # slot a new element goes into; any other index outside the array croaks
95 # rather than letting Perl autovivify a hole or die on a negative subscript.
96 25 ▶sub _sb_index {
Subroutine _sb_index: called CC=9 SCAR=22.1
97 25 my ($array, $seg, $path) = @_;
98 25 ▶ croak "spec path '$path': '$seg' is not an array index" unless _sb_is_index($seg);
Branch: 1 branch
BranchTrueFalseTotal
unless _sb_is_index($seg) 1 24 25
99 24 ▶ return $seg if $seg >= 0 && $seg <= @$array;
Branch: 1 branch
BranchTrueFalseTotal
if $seg >= 0 and $seg <= @$array 5 19 24
Condition66%2/3
$seg >= 0 and $seg <= @$array
!ll&&!rl&&r
1905
MC/DC50%1/2
$seg >= 0 and $seg <= @$array
$seg >= 0 $seg <= @$array
Truth table66%2/3
A and B
A= $seg >= 0B= $seg <= @$array
ABresult
0X0
100
111
100 19 ▶ return 0 if $seg == -1 && !@$array;
Branch: 1 branch
BranchTrueFalseTotal
if $seg == -1 and not @$array 15 4 19
Condition100%3/3
$seg == -1 and not @$array
!ll&&!rl&&r
1315
MC/DC100%2/2
$seg == -1 and not @$array
$seg == -1 not @$array
Truth table100%3/3
A and not(B)
A= $seg == -1B= not @$array
ABresult
0X0
100
111
101 4 my $resolved = @$array + $seg;
102 4 ▶ croak "spec path '$path': index $seg is out of range for an array of " . scalar(@$array)
Branch: 1 branch
BranchTrueFalseTotal
if $seg < 0 and $resolved < 0 1 3 4
Condition66%2/3
$seg < 0 and $resolved < 0
!ll&&!rl&&r
031
MC/DC50%1/2
$seg < 0 and $resolved < 0
$seg < 0 $resolved < 0
Truth table66%2/3
A and B
A= $seg < 0B= $resolved < 0
ABresult
0X0
100
111
103 if $seg < 0 && $resolved < 0;
104 3 ▶ croak "spec path '$path': index $seg is beyond the end of an array of " . scalar(@$array)
Branch: 1 branch
BranchTrueFalseTotal
if $seg > @$array 0 3 3
105 if $seg > @$array;
106 3 return $resolved;
107 }
108  
109 # Read one segment. Returns the child, or undef when it is not there. Never
110 # creates anything. Reading from a union reads from the node it holds; a
111 # union reached as the child itself is returned as it is.
112 417 ▶sub _sb_child {
Subroutine _sb_child: called CC=9 SCAR=22.0
113 417 my ($node, $seg) = @_;
114 417 ▶ ($node) = $node->_spec_path_node if _sb_is_union($node);
Branch: 1 branch
BranchTrueFalseTotal
if _sb_is_union($node) 14 403 417
115 417 ▶ if (_sb_is_obj($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_sb_is_obj($node)) 327 90 417
116 327 my ($attr) = _sb_attr($node, $seg);
117 327 ▶ return $node->$attr if defined $attr;
Branch: 1 branch
BranchTrueFalseTotal
if defined $attr 305 22 327
118 22 return $node->_unknown_fields->{$seg};
119 }
120 90 ▶ if (ref $node eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node eq "ARRAY") 53 37 90
121 53 ▶ return undef unless _sb_is_index($seg);
Branch: 1 branch
BranchTrueFalseTotal
unless _sb_is_index($seg) 0 53 53
122 53 ▶ return undef if $seg < -@$node || $seg > $#$node;
Branch: 1 branch
BranchTrueFalseTotal
if $seg < -@$node or $seg > $node->$#* 11 42 53
Condition66%2/3
$seg < -@$node or $seg > $node->$#*
l!l&&r!l&&!r
11042
MC/DC50%1/2
$seg < -@$node or $seg > $node->$#*
$seg < -@$node $seg > $node->$#*
Truth table66%2/3
A or B
A= $seg < -@$nodeB= $seg > $node->$#*
ABresult
1X1
011
000
123 42 return $node->[$seg];
124 }
125 37 ▶ return $node->{$seg} if ref $node eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $node eq "HASH" 35 2 37
126 2 return undef;
127 }
128  
129 # Hand a plain hashref (or an array/hash of them) to a typed slot as objects.
130 # Uses the shared default IO::K8s instance the way FROM_HASH does: registry
131 # class names are already fully expanded, so no per-instance resolution is
132 # involved. Scalars and already-blessed values pass through.
133 218 ▶sub _sb_inflate {
Subroutine _sb_inflate: called CC=9 SCAR=22.2
134 218 my ($info, $value) = @_;
135 218 ▶ return $value unless $info && ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $info and ref $value 49 169 218
Condition66%2/3
$info and ref $value
!ll&&!rl&&r
049169
MC/DC50%1/2
$info and ref $value
$info ref $value
Truth table66%2/3
A and B
A= $infoB= ref $value
ABresult
0X0
100
111
136 169 my $k8s = IO::K8s::Role::Resource::_default_k8s();
137 169 ▶ if ($info->{is_object}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"is_object"}) 73 96 169
138 return ref $value eq 'HASH'
139 73 ▶ ? $k8s->_struct_to_object_expanded($info->{class}, $value)
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' ? : 40 33 73
140 : $value;
141 }
142 96 ▶ if ($info->{is_array_of_objects} && ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"is_array_of_objects"} and ref $value eq "ARRAY") 57 39 96
Condition66%2/3
$info->{"is_array_of_objects"} and ref $value eq "ARRAY"
!ll&&!rl&&r
39057
MC/DC50%1/2
$info->{"is_array_of_objects"} and ref $value eq "ARRAY"
$info->{"is_array_of_objects"} ref $value eq "ARRAY"
Truth table66%2/3
A and B
A= $info->{"is_array_of_objects"}B= ref $value eq "ARRAY"
ABresult
0X0
100
111
143 57 return [ map { _sb_elem($info->{class}, $_) } @$value ];
144 }
145 39 ▶ if ($info->{is_hash_of_objects} && ref $value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"is_hash_of_objects"} and ref $value eq "HASH") 1 38 39
Condition66%2/3
$info->{"is_hash_of_objects"} and ref $value eq "HASH"
!ll&&!rl&&r
3801
MC/DC50%1/2
$info->{"is_hash_of_objects"} and ref $value eq "HASH"
$info->{"is_hash_of_objects"} ref $value eq "HASH"
Truth table66%2/3
A and B
A= $info->{"is_hash_of_objects"}B= ref $value eq "HASH"
ABresult
0X0
100
111
146 0 return { map { $_ => _sb_elem($info->{class}, $value->{$_}) } keys %$value };
147 }
148 38 return $value;
149 }
150  
151 # One element of an array/hash of objects: a hashref becomes $elem_class.
152 6 ▶sub _sb_elem {
Subroutine _sb_elem: called CC=3 SCAR=12.5
153 6 my ($elem_class, $value) = @_;
154 6 ▶ return $value unless $elem_class && ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $elem_class and ref $value eq "HASH" 0 6 6
Condition33%1/3
$elem_class and ref $value eq "HASH"
!ll&&!rl&&r
006
MC/DC0%0/2
$elem_class and ref $value eq "HASH"
$elem_class ref $value eq "HASH"
Truth table33%1/3
A and B
A= $elem_classB= ref $value eq "HASH"
ABresult
0X0
100
111
155 6 return IO::K8s::Role::Resource::_default_k8s()->_struct_to_object_expanded($elem_class, $value);
156 }
157  
158 # The collection an object's declared field holds, as a walk context: the
159 # owning object, the attribute, the field's JSON key and its registry entry.
160 # A walk that enters a container through such a field carries this along,
161 # so a write into one of the container's elements can answer to the field's
162 # own Moo coercion and type check (k147). undef when $node is not an object
163 # or does not declare $seg -- a container reached as an element of another
164 # container, or kept in the _unknown_fields bag, answers to no field and
165 # keeps the free JSON rules of a plain hash or array.
166 489 ▶sub _sb_coll_ctx {
Subroutine _sb_coll_ctx: called CC=3 SCAR=11.0
167 489 my ($node, $seg) = @_;
168 489 ▶ return undef unless _sb_is_obj($node);
Branch: 1 branch
BranchTrueFalseTotal
unless _sb_is_obj($node) 44 445 489
169 445 my ($attr, $info) = _sb_attr($node, $seg);
170 445 ▶ return undef unless defined $attr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr 4 441 445
171 441 return { owner => $node, attr => $attr, field => $seg, info => $info };
172 }
173  
174 # Run the elements about to be written into a typed collection through the
175 # field's own setter rules (k147). $new is a fresh container of the same
176 # kind as the collection holding only those elements -- [ @values ] for a
177 # push, [ $value ] for an index, { $key => $value } for a map key. The
178 # field's effective Moo coercion and type check (the spec in force for the
179 # owner's class, nearest first) run on it in the setter's order, coerce
180 # before isa, and the coerced container comes back for the caller to write
181 # into the collection in place -- so a reference spec_array or spec_hash
182 # handed out earlier stays the live one, and nothing is written unless
183 # every element passed.
184 #
185 # Checking the written elements alone is exact, not an approximation: every
186 # coercion a collection field gets (the [Bool] and array/hash-of-objects
187 # coercers in IO::K8s::Resource) maps element by element, and every isa it
188 # gets is ArrayRef[X]/HashRef[X] (Maybe-wrapped when optional) -- the DSL
189 # has no constraint on the collection as a whole. It also keeps the cost of
190 # a write independent of the collection's size, and leaves elements that
191 # arrived by direct mutation of the container alone (not checked, D1/D2).
192 # A failure's element index counts from the first written element.
193 97 ▶sub _sb_coll_check {
Subroutine _sb_coll_check: called CC=2 SCAR=7.0
194 97 my ($new, $ctx, $path, $what) = @_;
195 my $spec = IO::K8s::Role::Resource::_effective_attribute_specs(ref $ctx->{owner})
196 97 ▶ ->{ $ctx->{attr} } // {};
Condition50%1/2
IO::K8s::Role::Resource::_effective_attribute_specs(ref $ctx->{"owner"})->{$ctx->{"attr"}} // {}
l!l
970
MC/DC0%0/1
IO::K8s::Role::Resource::_effective_attribute_specs(ref $ctx->{"owner"})->{$ctx->{"attr"}} // {}
IO::K8s::Role::Resource::_effective_attribute_specs(ref $ctx->{"owner"})->{$ctx->{"attr"}}
Truth table50%1/2
A
A= IO::K8s::Role::Resource::_effective_attribute_specs(ref $ctx->{"owner"})->{$ctx->{"attr"}}
Aresult
11
00
197 97 ▶ return _sb_guard($path, $what, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
198 97 my $value = $new;
199 97 ▶ $value = $spec->{coerce}->($value) if $spec->{coerce};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"coerce"} 69 28 97
200 96 ▶ $spec->{isa}->($value) if $spec->{isa};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"isa"} 96 0 96
201 88 return $value;
202 97 });
203 }
204  
205 # Store $value under $seg of $node. A declared field on an object goes
206 # through its accessor (hashrefs inflated first, so the type constraint sees
207 # an object); an undeclared one goes into the _unknown_fields bag. Inside a
208 # collection a walk entered through a declared field ($ctx), the element
209 # passes _sb_coll_check first; anywhere else it is stored as given.
210 # Returns the value as stored -- after the setter's or the collection's
211 # coercion, not the value handed in.
212 279 ▶sub _sb_store {
Subroutine _sb_store: called CC=8 SCAR=20.9
213 279 my ($node, $seg, $value, $path, $ctx) = @_;
214 279 ▶ if (_sb_is_obj($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_sb_is_obj($node)) 228 51 279
215 228 my ($attr, $info) = _sb_attr($node, $seg);
216 228 ▶ if (defined $attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attr) 218 10 228
217 218 $value = _sb_inflate($info, $value);
218 213 ▶ _sb_guard($path, "cannot set '$seg'", sub { $node->$attr($value) });
Subroutine __ANON__: called CC=1 SCAR=0.0
219 210 return $node->$attr;
220 }
221 10 return $node->_unknown_fields->{$seg} = $value;
222 }
223 51 ▶ if (ref $node eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node eq "ARRAY") 25 26 51
224 25 my $i = _sb_index($node, $seg, $path);
225 23 ▶ ($value) = @{ _sb_coll_check([ $value ], $ctx, $path,
Branch: 1 branch
BranchTrueFalseTotal
if $ctx 19 4 23
226 "cannot set '$seg' in '$ctx->{field}'") } if $ctx;
227 21 return $node->[$i] = $value;
228 }
229 26 ▶ if (ref $node eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node eq "HASH") 26 0 26
230 $value = _sb_coll_check({ $seg => $value }, $ctx, $path,
231 26 ▶ "cannot set '$seg' in '$ctx->{field}'")->{$seg} if $ctx;
Branch: 1 branch
BranchTrueFalseTotal
if $ctx 9 17 26
232 25 return $node->{$seg} = $value;
233 }
234 0 ▶ croak "spec path '$path': cannot store '$seg' in a " . (ref($node) || 'scalar');
Condition0%0/2
ref $node || 'scalar'
l!l
00
MC/DC0%0/1
ref $node || 'scalar'
ref $node
Truth table0%0/2
A
A= ref $node
Aresult
11
00
235 }
236  
237 # The class a new element of the collection $ctx describes must be: the
238 # declared class of an array/hash-of-objects field; undef otherwise.
239 21 ▶sub _sb_elem_class {
Subroutine _sb_elem_class: called CC=4 SCAR=13.9
240 21 my ($ctx) = @_;
241 21 ▶ return undef unless $ctx;
Branch: 1 branch
BranchTrueFalseTotal
unless $ctx 7 14 21
242 14 my $info = $ctx->{info};
243 14 ▶ return $info->{class} if $info->{is_array_of_objects} || $info->{is_hash_of_objects};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_array_of_objects"} or $info->{"is_hash_of_objects"} 12 2 14
Condition100%3/3
$info->{"is_array_of_objects"} or $info->{"is_hash_of_objects"}
l!l&&r!l&&!r
1022
MC/DC100%2/2
$info->{"is_array_of_objects"} or $info->{"is_hash_of_objects"}
$info->{"is_array_of_objects"} $info->{"is_hash_of_objects"}
Truth table100%3/3
A or B
A= $info->{"is_array_of_objects"}B= $info->{"is_hash_of_objects"}
ABresult
1X1
011
000
244 2 return undef;
245 }
246  
247 # A new, empty object of $class for the slot $seg. A union class (k172) is
248 # built through its FROM_STRUCT holding the empty container $next asks for
249 # -- an array for an index, a hash (the schema arm) for anything else -- so
250 # the walk has a node to continue into; a union that cannot hold that
251 # (JSONSchemaPropsOrBool has no array arm) croaks here, before anything is
252 # stored.
253 44 ▶sub _sb_new_node {
Subroutine _sb_new_node: called CC=1 SCAR=0.0
254 44 my ($class, $next, $seg, $path) = @_;
255 44 ▶ return _sb_guard($path, "cannot create $class for '$seg'", sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
256 44 my $loaded = use_module($class);
257 44 ▶ return $loaded->new unless $loaded->can('_spec_path_node');
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded->can("_spec_path_node") 28 16 44
258 16 ▶ my $fresh = $loaded->FROM_STRUCT(_sb_is_index($next) ? [] : {},
Branch: 1 branch
BranchTrueFalseTotal
_sb_is_index($next) ? : 4 12 16
259 IO::K8s::Role::Resource::_default_k8s());
260 16 my ($node) = $fresh->_spec_path_node;
261 16 ▶ die 'it cannot hold ' . (_sb_is_index($next) ? 'an array' : 'a hash') . "\n"
Branch: 2 branches
BranchTrueFalseTotal
unless ref $node 1 15 16
_sb_is_index($next) ? : 1 0 1
262 unless ref $node;
263 15 return $fresh;
264 44 });
265 }
266  
267 # The node a walk continues into from the union $union, stored under $seg
268 # of $parent (k172), and the collection context that node answers to: the
269 # union's own attribute when it holds a typed array there (a write into
270 # the array is checked against that attribute, as k147 checks a declared
271 # collection), undef otherwise -- a V1::JSON value is free JSON. A union
272 # holding nothing is first replaced under $seg by a fresh one built for
273 # $next ($ctx is $parent's own context, for that store). The node comes
274 # back as it is, a scalar included; the caller decides what a scalar means.
275 36 ▶sub _sb_union_node {
Subroutine _sb_union_node: called CC=3 SCAR=11.0
276 36 my ($parent, $seg, $union, $next, $ctx, $path) = @_;
277 36 my ($node, $attr) = $union->_spec_path_node;
278 36 ▶ unless (defined $node) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node) 3 33 36
279 3 $union = _sb_store($parent, $seg, _sb_new_node(ref $union, $next, $seg, $path), $path, $ctx);
280 3 ($node, $attr) = $union->_spec_path_node;
281 }
282 36 ▶ return ($node, defined $attr ? { owner => $union, attr => $attr, field => $seg, info => {} } : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $attr ? : 4 32 36
283 }
284  
285 # The union class a value stored under $seg of $node has to be -- the
286 # declared class of an object field, or the element class of the typed
287 # collection $ctx describes -- or undef when that is not a union.
288 54 ▶sub _sb_union_slot {
Subroutine _sb_union_slot: called CC=7 SCAR=19.7
289 54 my ($node, $seg, $ctx) = @_;
290 54 my $class;
291 54 ▶ if (_sb_is_obj($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_sb_is_obj($node)) { } 50 4 54
292 50 my (undef, $info) = _sb_attr($node, $seg);
293 50 ▶ $class = $info->{class} if $info && $info->{is_object};
Branch: 1 branch
BranchTrueFalseTotal
if $info and $info->{"is_object"} 0 50 50
Condition66%2/3
$info and $info->{"is_object"}
!ll&&!rl&&r
2480
MC/DC0%0/2
$info and $info->{"is_object"}
$info $info->{"is_object"}
Truth table66%2/3
A and B
A= $infoB= $info->{"is_object"}
ABresult
0X0
100
111
294 } else {
295 4 $class = _sb_elem_class($ctx);
296 }
297 54 ▶ return undef unless $class && eval { use_module($class); 1 } && $class->can('_spec_path_node');
Branch: 1 branch
BranchTrueFalseTotal
unless $class and eval { do { &use_module($class); 1 } } and $class->can("_spec_path_node") 53 1 54
Condition66%4/6
$class and eval { do { &use_module($class); 1 } } and $class->can("_spec_path_node")
!ll&&!rl&&r
5301
$class and eval { do { &use_module($class); 1 } }
!ll&&!rl&&r
5301
MC/DC33%1/3
$class and eval { do { &use_module($class); 1 } } and $class->can("_spec_path_node")
$class eval { do { &use_module($class); 1 } } $class->can("_spec_path_node")
Truth table50%2/4
A and B and C
A= $classB= eval { do { &use_module($class); 1 } }C= $class->can("_spec_path_node")
ABCresult
0XX0
10X0
1100
1111
298 1 return $class;
299 }
300  
301 # What to create in an empty slot so a walk can continue. On an object the
302 # registry decides: the declared class for a struct/object field, [] or {}
303 # for the container forms, croak for a scalar. Inside an array or hash of
304 # objects ($ctx) the element class. Elsewhere the next segment decides: an
305 # index means an array, anything else a hash.
306 77 ▶sub _sb_fresh {
Subroutine _sb_fresh: called CC=9 SCAR=22.0
307 77 my ($node, $seg, $next, $ctx, $path) = @_;
308 77 ▶ my $elem_class = _sb_is_obj($node) ? undef : _sb_elem_class($ctx);
Branch: 1 branch
BranchTrueFalseTotal
_sb_is_obj($node) ? : 60 17 77
309 77 ▶ if (_sb_is_obj($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_sb_is_obj($node)) 60 17 77
310 60 my (undef, $info) = _sb_attr($node, $seg);
311 60 ▶ if ($info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info) 59 1 60
312 59 ▶ return _sb_new_node($info->{class}, $next, $seg, $path) if $info->{is_object};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_object"} 29 30 59
313 30 ▶ return [] if grep { $info->{$_} } qw(
Branch: 1 branch
BranchTrueFalseTotal
if grep {$info->{$_};} "is_array_of_objects", "is_array_of_str", "is_array_of_int", "is_array_of_bool", "is_array_of_hash", "is_array_of_array", "is_array_of_num", "is_array_of_quantity", "is_array_of_time", "is_array_of_int_or_string" 13 17 30
314 is_array_of_objects is_array_of_str is_array_of_int
315 is_array_of_bool is_array_of_hash is_array_of_array
316 is_array_of_num is_array_of_quantity is_array_of_time
317 is_array_of_int_or_string
318 );
319 17 ▶ return {} if grep { $info->{$_} } qw(
Branch: 1 branch
BranchTrueFalseTotal
if grep {$info->{$_};} "is_hash_of_str", "is_hash_of_objects", "is_hash_of_int", "is_hash_of_num", "is_hash_of_bool", "is_hash_of_quantity", "is_hash_of_time", "is_hash_of_int_or_string", "is_hash_opaque", "is_hash_of_str_arrays", "is_hash_of_object_arrays" 16 1 17
320 is_hash_of_str is_hash_of_objects is_hash_of_int is_hash_of_num
321 is_hash_of_bool is_hash_of_quantity is_hash_of_time
322 is_hash_of_int_or_string is_hash_opaque
323 is_hash_of_str_arrays is_hash_of_object_arrays
324 );
325 1 croak "spec path '$path': cannot descend through scalar field '$seg'";
326 }
327 }
328 18 ▶ return _sb_new_node($elem_class, $next, $seg, $path) if $elem_class;
Branch: 1 branch
BranchTrueFalseTotal
if $elem_class 11 7 18
329 7 ▶ return _sb_is_index($next) ? [] : {};
Branch: 1 branch
BranchTrueFalseTotal
_sb_is_index($next) ? : 1 6 7
330 }
331  
332 # The spec node. With $vivify, create it when missing: the declared class
333 # when spec is a typed field of this object, a plain hash otherwise.
334 #
335 # Every spec_* entry point comes through here, so this is also the one
336 # place that has to answer for a consumer with no `spec` field at all --
337 # 32 of the shipped Kinds carry none (ConfigMap, Secret, Endpoints, the
338 # four RBAC kinds, ...). Since the role is composed onto every APIObject
339 # rather than only onto CRD classes (k103) they all have the spec_*
340 # methods, and without this guard the first thing such a call hits is
341 # Moo's "Can't locate object method spec", reported against this file's
342 # line number instead of naming the class that has no spec.
343 #
344 # Checked per call rather than declared as `requires 'spec'`: the role is
345 # composed at `use IO::K8s::APIObject` time, before the class's own
346 # `k8s spec => ...` line has run, so a requires would reject every
347 # consumer including the ones that do declare a spec.
348 367 ▶sub _sb_root {
Subroutine _sb_root: called CC=8 SCAR=21.1
349 367 my ($self, $vivify, $path) = @_;
350 367 ▶ croak((ref($self) || $self)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("spec") 14 353 367
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
1400
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
351 . ' has no spec field: the spec_* methods need one to read or build')
352 unless $self->can('spec');
353 353 my $spec = $self->spec;
354 353 ▶ return $spec if ref $spec;
Branch: 1 branch
BranchTrueFalseTotal
if ref $spec 259 94 353
355 94 ▶ return undef unless $vivify;
Branch: 1 branch
BranchTrueFalseTotal
unless $vivify 2 92 94
356 92 ▶ my (undef, $info) = _sb_is_obj($self) ? _sb_attr($self, 'spec') : ();
Branch: 1 branch
BranchTrueFalseTotal
_sb_is_obj($self) ? : 92 0 92
357 $spec = $info && $info->{is_object}
358 90 ▶ ? _sb_guard($path, "cannot create $info->{class} for 'spec'", sub { use_module($info->{class})->new })
Subroutine __ANON__: called CC=1 SCAR=0.0
359 92 ▶ : {};
Branch: 1 branch
BranchTrueFalseTotal
$info && $info->{'is_object'} ? : 90 2 92
Condition66%2/3
$info and $info->{"is_object"}
!ll&&!rl&&r
0290
MC/DC50%1/2
$info and $info->{"is_object"}
$info $info->{"is_object"}
Truth table66%2/3
A and B
A= $infoB= $info->{"is_object"}
ABresult
0X0
100
111
360 92 $self->spec($spec);
361 92 return $self->spec;
362 }
363  
364 # Walk to the parent of the last segment, creating what is missing. Returns
365 # ($parent, $last_segment, $ctx): $ctx is the collection context (see
366 # _sb_coll_ctx) when $parent is a container held by a declared field --
367 # spec itself included -- and undef otherwise.
368 264 ▶sub _sb_walk_vivify {
Subroutine _sb_walk_vivify: called CC=9 SCAR=22.5
369 264 my ($self, $path) = @_;
370 264 my @segs = split /\./, $path;
371 264 my $last = pop @segs;
372 264 ▶ croak "spec path '$path' is empty" unless defined $last && length $last;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $last and length $last 0 264 264
Condition33%1/3
defined $last and length $last
!ll&&!rl&&r
00264
MC/DC0%0/2
defined $last and length $last
defined $last length $last
Truth table33%1/3
A and B
A= defined $lastB= length $last
ABresult
0X0
100
111
373 264 ▶ my ($node, $ctx) = _sb_enter($self, 'spec', $self->_sb_root(1, $path),
Branch: 1 branch
BranchTrueFalseTotal
@segs ? : 98 158 256
374 @segs ? $segs[0] : $last, undef, $path);
375 256 for my $i (0 .. $#segs) {
376 151 my $seg = $segs[$i];
377 151 ▶ my $next = $i < $#segs ? $segs[$i + 1] : $last;
Branch: 1 branch
BranchTrueFalseTotal
$i < $#segs ? : 53 98 151
378 151 my $child = _sb_child($node, $seg);
379 151 ▶ if (defined $child && !ref $child) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $child and not ref $child) 0 151 151
Condition66%2/3
defined $child and not ref $child
!ll&&!rl&&r
64870
MC/DC0%0/2
defined $child and not ref $child
defined $child not ref $child
Truth table66%2/3
A and not(B)
A= defined $childB= not ref $child
ABresult
0X0
100
111
380 0 croak "spec path '$path': cannot descend through scalar field '$seg'";
381 }
382 151 ▶ unless (ref $child) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $child) 64 87 151
383 64 $child = _sb_fresh($node, $seg, $next, $ctx, $path);
384 61 $child = _sb_store($node, $seg, $child, $path, $ctx);
385 }
386 147 ($node, $ctx) = _sb_enter($node, $seg, $child, $next, $ctx, $path);
387 }
388 250 return ($node, $last, $ctx);
389 }
390  
391 # Step from $parent through $seg onto its child $child: the node the walk
392 # continues from and the collection context it answers to. A container we
393 # just entered answers to a field only when the object we came from
394 # declares it; an element of a container does not. A union is stepped
395 # through into the node it holds (k172), which must not be a scalar.
396 417 ▶sub _sb_enter {
Subroutine _sb_enter: called CC=3 SCAR=11.0
397 417 my ($parent, $seg, $child, $next, $ctx, $path) = @_;
398 417 ▶ return ($child, _sb_coll_ctx($parent, $seg)) unless _sb_is_union($child);
Branch: 1 branch
BranchTrueFalseTotal
unless _sb_is_union($child) 393 24 417
399 24 my ($node, $node_ctx) = _sb_union_node($parent, $seg, $child, $next, $ctx, $path);
400 24 ▶ croak "spec path '$path': cannot descend through scalar field '$seg'" unless ref $node;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 2 22 24
401 22 return ($node, $node_ctx);
402 }
403  
404  
405 68 ▶sub spec_get {
Subroutine spec_get: called CC=5 SCAR=16.1
Pod documented
406 68 my ($self, $path) = @_;
407 68 my $node = $self->_sb_root(0);
408 66 ▶ return undef unless ref $node;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 2 64 66
409 64 for my $seg (split /\./, $path) {
410 139 ▶ return undef unless ref $node;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 2 137 139
411 137 $node = _sb_child($node, $seg);
412 137 ▶ return undef unless defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 9 128 137
413 }
414 53 return $node;
415 }
416  
417  
418 142 ▶sub spec_set {
Subroutine spec_set: called CC=1 SCAR=0.0
Pod documented
419 142 my ($self, $path, $value) = @_;
420 142 my ($parent, $last, $ctx) = $self->_sb_walk_vivify($path);
421 134 _sb_store($parent, $last, $value, $path, $ctx);
422 124 return $self;
423 }
424  
425  
426 22 ▶sub spec_array {
Subroutine spec_array: called CC=1 SCAR=0.0
Pod documented
427 22 my ($self, $path) = @_;
428 22 my ($array) = _sb_array($self->_sb_walk_vivify($path), $path);
429 17 return $array;
430 }
431  
432 # The array under $last of $parent, stored there first when the slot is
433 # empty, and the collection context its elements answer to; spec_array and
434 # spec_push share it so a push walks the path once. A union under $last
435 # hands out the array it holds (k172) -- built holding an empty one when
436 # the slot is empty or the union holds nothing.
437 96 ▶sub _sb_array {
Subroutine _sb_array: called CC=6 SCAR=17.9
438 96 my ($parent, $last, $ctx, $path) = @_;
439 96 my $array = _sb_child($parent, $last);
440 # The array's own field, not its parent's: the values become its elements.
441 96 my $array_ctx = _sb_coll_ctx($parent, $last);
442 96 ▶ if (!defined $array and my $union = _sb_union_slot($parent, $last, $ctx)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $array and my $union = _sb_union_slot($parent, $last, $ctx)) 1 95 96
Condition100%3/3
not defined $array and my $union = _sb_union_slot($parent, $last, $ctx)
!ll&&!rl&&r
42531
MC/DC100%2/2
not defined $array and my $union = _sb_union_slot($parent, $last, $ctx)
not defined $array my $union = _sb_union_slot($parent, $last, $ctx)
Truth table100%3/3
not(A) and B
A= not defined $arrayB= my $union = _sb_union_slot($parent, $last, $ctx)
ABresult
0X0
100
111
443 1 $array = _sb_store($parent, $last, _sb_new_node($union, 0, $last, $path), $path, $ctx);
444 }
445 96 ▶ ($array, $array_ctx) = _sb_union_node($parent, $last, $array, 0, $ctx, $path)
Branch: 1 branch
BranchTrueFalseTotal
if _sb_is_union($array) 7 89 96
446 if _sb_is_union($array);
447 96 ▶ return ($array, $array_ctx) if ref $array eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $array eq "ARRAY" 41 55 96
448 55 ▶ croak "spec path '$path': '$last' holds a non-array value" if defined $array;
Branch: 1 branch
BranchTrueFalseTotal
if defined $array 2 53 55
449 53 return (_sb_store($parent, $last, [], $path, $ctx), $array_ctx);
450 }
451  
452  
453 22 ▶sub spec_hash {
Subroutine spec_hash: called CC=6 SCAR=18.0
Pod documented
454 22 my ($self, $path) = @_;
455 22 my ($parent, $last, $ctx) = $self->_sb_walk_vivify($path);
456 20 my $node = _sb_child($parent, $last);
457 20 ▶ croak "spec path '$path': '$last' holds a scalar" if defined $node && !ref $node;
Branch: 1 branch
BranchTrueFalseTotal
if defined $node and not ref $node 0 20 20
Condition66%2/3
defined $node and not ref $node
!ll&&!rl&&r
1370
MC/DC0%0/2
defined $node and not ref $node
defined $node not ref $node
Truth table66%2/3
A and not(B)
A= defined $nodeB= not ref $node
ABresult
0X0
100
111
458 20 ▶ $node = _sb_store($parent, $last, _sb_fresh($parent, $last, undef, $ctx, $path), $path, $ctx)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 13 7 20
459 unless defined $node;
460 # A union hands out the container it holds, not itself (k172): a key
461 # written into the union object would never reach its TO_JSON.
462 20 ▶ ($node) = _sb_union_node($parent, $last, $node, undef, $ctx, $path) if _sb_is_union($node);
Branch: 1 branch
BranchTrueFalseTotal
if _sb_is_union($node) 5 15 20
463 20 ▶ croak "spec path '$path': '$last' holds a scalar" unless ref $node;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 1 19 20
464 19 return $node;
465 }
466  
467  
468 78 ▶sub spec_push {
Subroutine spec_push: called CC=2 SCAR=6.9
Pod documented
469 78 my ($self, $path, @values) = @_;
470 78 my ($parent, $last, $ctx) = $self->_sb_walk_vivify($path);
471 76 my ($array, $array_ctx) = _sb_array($parent, $last, $ctx, $path);
472 76 ▶ @values = @{ _sb_coll_check([ @values ], $array_ctx, $path,
Branch: 1 branch
BranchTrueFalseTotal
if $array_ctx 69 7 76
473 "cannot push onto '$last'") } if $array_ctx;
474 70 push @$array, @values;
475 70 return $self;
476 }
477  
478  
479 11 ▶sub spec_merge {
Subroutine spec_merge: called CC=2 SCAR=6.9
Pod documented
480 11 my ($self, %data) = @_;
481 11 for my $key (keys %data) {
482 16 my ($root, $ctx) = _sb_enter($self, 'spec', $self->_sb_root(1, $key), $key, undef, $key);
483 14 _sb_store($root, $key, $data{$key}, $key, $ctx);
484 }
485 8 return $self;
486 }
487  
488  
489 19 ▶sub spec_delete {
Subroutine spec_delete: called CC=17 SCAR=29.2
Pod documented
490 19 my ($self, $path) = @_;
491 19 my $node = $self->_sb_root(0);
492 17 ▶ return $self unless ref $node;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 0 17 17
493 17 my @segs = split /\./, $path;
494 17 my $last = pop @segs;
495 17 for my $seg (@segs) {
496 13 $node = _sb_child($node, $seg);
497 13 ▶ return $self unless ref $node;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 3 10 13
498 }
499 # Deleting from a union deletes from the node it holds (k172).
500 14 ▶ ($node) = $node->_spec_path_node if _sb_is_union($node);
Branch: 1 branch
BranchTrueFalseTotal
if _sb_is_union($node) 2 12 14
501 14 ▶ return $self unless ref $node;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 0 14 14
502 14 ▶ if (_sb_is_obj($node)) {
Branch: 3 branches
BranchTrueFalseTotal
if (_sb_is_obj($node)) { } 11 3 14
elsif (ref $node eq 'ARRAY') { } 1 2 3
elsif (ref $node eq 'HASH') { } 2 0 2
503 11 my ($attr, $info) = _sb_attr($node, $last);
504 11 ▶ if (defined $attr && $info->{options} && $info->{options}{nullable}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $attr and $info->{'options'} and $info->{'options'}{'nullable'}) { } 2 9 11
elsif (defined $attr) { } 7 2 9
Condition83%5/6
defined $attr and $info->{"options"} and $info->{"options"}{"nullable"}
!ll&&!rl&&r
902
defined $attr and $info->{"options"}
!ll&&!rl&&r
272
MC/DC66%2/3
defined $attr and $info->{"options"} and $info->{"options"}{"nullable"}
defined $attr $info->{"options"} $info->{"options"}{"nullable"}
Truth table75%3/4
A and B and C
A= defined $attrB= $info->{"options"}C= $info->{"options"}{"nullable"}
ABCresult
0XX0
10X0
1100
1111
505 # Setting undef would leave a nullable field present with null
506 # (k158); its clearer removes the key.
507 2 my (undef, $clear) = IO::K8s::Resource::_nullable_methods($attr);
508 2 $node->$clear;
509 } elsif (defined $attr) {
510 7 ▶ _sb_guard($path, "cannot clear '$last'", sub { $node->$attr(undef) });
Subroutine __ANON__: called CC=1 SCAR=0.0
511 } else {
512 2 delete $node->_unknown_fields->{$last};
513 }
514 } elsif (ref $node eq 'ARRAY') {
515 1 ▶ return $self unless _sb_is_index($last) && @$node;
Branch: 1 branch
BranchTrueFalseTotal
unless _sb_is_index($last) and @$node 0 1 1
Condition33%1/3
_sb_is_index($last) and @$node
!ll&&!rl&&r
001
MC/DC0%0/2
_sb_is_index($last) and @$node
_sb_is_index($last) @$node
Truth table33%1/3
A and B
A= _sb_is_index($last)B= @$node
ABresult
0X0
100
111
516 1 ▶ return $self if $last < -@$node || $last > $#$node;
Branch: 1 branch
BranchTrueFalseTotal
if $last < -@$node or $last > $node->$#* 0 1 1
Condition33%1/3
$last < -@$node or $last > $node->$#*
l!l&&r!l&&!r
001
MC/DC0%0/2
$last < -@$node or $last > $node->$#*
$last < -@$node $last > $node->$#*
Truth table33%1/3
A or B
A= $last < -@$nodeB= $last > $node->$#*
ABresult
1X1
011
000
517 1 splice @$node, $last, 1;
518 } elsif (ref $node eq 'HASH') {
519 2 delete $node->{$last};
520 }
521 13 return $self;
522 }
523  
524 143 1;
525  
526 __END__
« blib/lib/IO/K8s.pm ↑ Summary lib/IO/K8s/ApiextensionsApiserver/Pkg/Apis/Apiextensions/V1/JSONSchemaPropsOrBool.pm »