blib/lib/IO/K8s/Resource.pm

filter: Statementstmt 97.5% 433 / 444 Branchbran 90.6% 261 / 288 Conditioncond 82.1% 133 / 162 MC/DCmcdc 66.3% 63 / 95 Subroutinesub 100% 58 / 58 Podpod 0.0% 0 / 1 totaltotal 90.4% 948 / 1048 CC 215 SCAR 38.5
CC
215
Cov
93%
_declare_field
110.9
_constrain
19.0
__ANON__
17.5
CRAP 46.8
« blib/lib/IO/K8s/Role/Resource.pm ↑ Summary blib/lib/IO/K8s/AutoGen.pm »
1 package IO::K8s::Resource;
2 # ABSTRACT: Base class for all Kubernetes resources
3 157 our $VERSION = '1.110';
4 157 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 157 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 157 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 157 ▶use Moo ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 157 ▶use Moo::Role ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 157 ▶use Import::Into;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 157 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 157 ▶use Types::Standard qw( ArrayRef Bool HashRef InstanceOf Int Maybe Num Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 157 ▶use IO::K8s::Types qw( IntOrStr Quantity Time );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 157 ▶use IO::K8s::Role::Resource ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 157 ▶use namespace::clean ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 157 ▶use Scalar::Util qw( blessed reftype looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 157 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 157 ▶use Sub::Util qw( subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Registry: class -> attr -> { type, class, is_array, is_hash, is_bool, is_int }
20 # Use 'our' to make it a proper package variable accessible via symbol table
21 157 our %_attr_registry;
22  
23 # Unknown-field policy (D1). Off: a constructor key no attribute claims is
24 # kept in the object's _unknown_fields bag and emitted again by TO_JSON, so
25 # a document from a newer upstream than the class still round-trips. On: it
26 # dies naming the class and the field. IO::K8s localizes this around its own
27 # entry points when built with strict => 1 (see IO::K8s/strict); nothing
28 # else writes it. A package variable rather than a constructor argument so
29 # that it reaches the inline-struct coercers, which call ->new directly and
30 # never pass through IO::K8s::_inflate_struct.
31 157 our $STRICT = 0;
32  
33 # Class name expansion map
34 157 my %_class_prefix = (
35 'Core' => 'IO::K8s::Api::Core',
36 'Apps' => 'IO::K8s::Api::Apps',
37 'Batch' => 'IO::K8s::Api::Batch',
38 'Networking' => 'IO::K8s::Api::Networking',
39 'Rbac' => 'IO::K8s::Api::Rbac',
40 'Storage' => 'IO::K8s::Api::Storage',
41 'Policy' => 'IO::K8s::Api::Policy',
42 'Autoscaling' => 'IO::K8s::Api::Autoscaling',
43 'Admissionregistration' => 'IO::K8s::Api::Admissionregistration',
44 'Coordination' => 'IO::K8s::Api::Coordination',
45 'Discovery' => 'IO::K8s::Api::Discovery',
46 'Events' => 'IO::K8s::Api::Events',
47 'Flowcontrol' => 'IO::K8s::Api::Flowcontrol',
48 'Node' => 'IO::K8s::Api::Node',
49 'Scheduling' => 'IO::K8s::Api::Scheduling',
50 'Certificates' => 'IO::K8s::Api::Certificates',
51 'Authentication' => 'IO::K8s::Api::Authentication',
52 'Authorization' => 'IO::K8s::Api::Authorization',
53 'Resource' => 'IO::K8s::Api::Resource',
54 'Storagemigration' => 'IO::K8s::Api::Storagemigration',
55 'Lifecycle' => 'IO::K8s::Api::Lifecycle',
56 'Meta' => 'IO::K8s::Apimachinery::Pkg::Apis::Meta',
57 'Apiextensions' => 'IO::K8s::ApiextensionsApiserver::Pkg::Apis::Apiextensions',
58 'KubeAggregator' => 'IO::K8s::KubeAggregator::Pkg::Apis::Apiregistration',
59 # Bundled CRD providers (step 4, k95/D6): a provider class is written
60 # the same short way a core class is ('Traefik::V1alpha1::Middleware'
61 # like 'Core::V1::Pod'), not '+IO::K8s::Traefik::V1alpha1::Middleware'.
62 # PrometheusOperator, VolumeSnapshot and ExternalSecrets have no shipped
63 # classes yet -- the prefixes are reserved ahead of their provider tasks
64 # so the emitter can start rendering short names for them immediately.
65 'Cilium' => 'IO::K8s::Cilium',
66 'Traefik' => 'IO::K8s::Traefik',
67 'CertManager' => 'IO::K8s::CertManager',
68 'GatewayAPI' => 'IO::K8s::GatewayAPI',
69 'K3s' => 'IO::K8s::K3s',
70 'AgentSandbox' => 'IO::K8s::AgentSandbox',
71 'PrometheusOperator' => 'IO::K8s::PrometheusOperator',
72 'VolumeSnapshot' => 'IO::K8s::VolumeSnapshot',
73 'ExternalSecrets' => 'IO::K8s::ExternalSecrets',
74 );
75  
76 # Type flag lookup table
77 157 my %TYPE_FLAGS = (
78 Str => { is_str => 1 },
79 Int => { is_int => 1 },
80 Num => { is_num => 1 },
81 Bool => { is_bool => 1 },
82 IntOrStr => { is_int_or_string => 1 },
83 Quantity => { is_quantity => 1 },
84 Time => { is_time => 1 },
85 );
86  
87 # For string path: map type name to base Type::Tiny constraint
88 # Custom K8s types (IntOrStr, Quantity, Time) fall back to Str
89 157 my %STR_ISA_MAP = (
90 Str => Str,
91 Int => Int,
92 Num => Num,
93 Bool => Bool,
94 );
95  
96 # The Type::Tiny base type for a scalar kind name -- Str, Int, Num, Bool
97 # keep their own Types::Standard constraint; IntOrStr, Quantity and Time
98 # fall back to Str, the same way the bareword type-spec branch of _k8s
99 # does via %STR_ISA_MAP above. Exposed so AutoGen's own default-vs-kind
100 # guard (_field_options) can build the same constrained type _k8s would
101 # without duplicating this map.
102 26 ▶sub _scalar_base_for {
Subroutine _scalar_base_for: called CC=2 SCAR=7.1
103 26 my ($kind) = @_;
104 26 ▶ return $STR_ISA_MAP{$kind} // Str;
Condition66%2/3
$STR_ISA_MAP{$kind} // &Str()
l!l&&r!l&&!r
2420
MC/DC0%0/2
$STR_ISA_MAP{$kind} // &Str()
$STR_ISA_MAP{$kind} &Str()
Truth table66%2/3
A // B
A= $STR_ISA_MAP{$kind}B= &Str()
ABresult
1X1
011
000
105 }
106  
107 # Value types for the hash-of-scalar-type DSL form { TypeName => 1 } (k63),
108 # and for HashRef[TypeName], which means the same (k191). Everything here
109 # constrains each VALUE against the scalar type, so a map upstream declares
110 # as map[X]Quantity finally rejects cpu => 'banana' at construction instead
111 # of at the API server.
112 #
113 # 'Str' is NOT here: since k191 it is the string map (is_hash_of_str, every
114 # scalar value a JSON string on the wire), and its two spellings validate
115 # differently. HashRef[Str] is strict, a reference value is refused.
116 # { Str => 1 } keeps the bare HashRef it always had and is marked
117 # is_hash_of_str_lenient: it was the opaque map until k191, so a reference
118 # value still passes and TO_JSON warns about it once per class and field.
119 # The free map is Opaque (or a bare HashRef), flag is_hash_opaque.
120 157 my %HASH_VALUE_TYPES = (
121 Int => { isa => Int, flag => 'is_hash_of_int' },
122 Num => { isa => Num, flag => 'is_hash_of_num' },
123 Bool => { isa => Bool, flag => 'is_hash_of_bool' },
124 Quantity => { isa => Quantity, flag => 'is_hash_of_quantity' },
125 Time => { isa => Time, flag => 'is_hash_of_time' },
126 IntOrStr => { isa => IntOrStr, flag => 'is_hash_of_int_or_string' },
127 );
128  
129 # Field options a declaration may carry (D3): the third argument
130 # (`k8s name => Type, { ... }`) or, inside an inline struct, the two-element
131 # form `name => [ Type, { ... } ]`. The legacy 'required' marker and the
132 # `Type!` suffix still work and mean { required => 1 }. Everything here is
133 # recorded in the registry for to_crd; enum, minimum, maximum and pattern
134 # are also enforced at construction, the way { Quantity => 1 } validates
135 # its values -- a bad value fails here instead of at the API server.
136 # nullable also keeps an explicit JSON null on the way in and out (k158).
137 # default is deliberately NOT applied client-side: defaulting is the API
138 # server's job, and a client default would change the wire output.
139 157 my %FIELD_OPTIONS = map { $_ => 1 } qw(
140 required default enum minimum maximum pattern description nullable
141 preserve_unknown
142 );
143 157 my %NUMERIC_KIND = (Int => 1, Num => 1);
144 157 my %STRING_KIND = (Str => 1, IntOrStr => 1, Quantity => 1, Time => 1);
145  
146 # The value constraints as child types of the field's base type, so a
147 # failure names the rule ("is not one of", "is below the minimum") rather
148 # than an anonymous intersection. $kind is the scalar type name the field
149 # is built on (Str, Int, Num, Bool, IntOrStr, Quantity, Time).
150 40614 ▶sub _constrain {
Subroutine _constrain: called CC=19 SCAR=29.5
151 40614 my ($base, $kind, $opts, $where) = @_;
152 40614 my $type = $base;
153  
154 40614 ▶ if (exists $opts->{enum}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"enum"}) 1987 38627 40614
155 croak "k8s: 'enum' for $where must be a non-empty arrayref"
156 1987 ▶ unless ref $opts->{enum} eq 'ARRAY' && @{ $opts->{enum} };
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts->{"enum"} eq "ARRAY" and @{$opts->{"enum"};} 1 1986 1987
Condition66%2/3
ref $opts->{"enum"} eq "ARRAY" and @{$opts->{"enum"};}
!ll&&!rl&&r
011986
MC/DC50%1/2
ref $opts->{"enum"} eq "ARRAY" and @{$opts->{"enum"};}
ref $opts->{"enum"} eq "ARRAY" @{$opts->{"enum"};}
Truth table66%2/3
A and B
A= ref $opts->{"enum"} eq "ARRAY"B= @{$opts->{"enum"};}
ABresult
0X0
100
111
157 1986 ▶ croak "k8s: 'enum' is not allowed on a Bool field ($where)" if $kind eq 'Bool';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "Bool" 1 1985 1986
158 1985 my %allowed = map { $_ => 1 } @{ $opts->{enum} };
159 croak "k8s: 'enum' for $where has duplicate entries"
160 1985 ▶ unless @{ $opts->{enum} } == keys %allowed;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$opts->{"enum"};} == keys %allowed 1 1984 1985
161 1984 my $list = join ', ', @{ $opts->{enum} };
162 $type = $type->create_child_type(
163 display_name => $type->display_name . '[enum]',
164 3029 ▶ constraint => sub { defined $_ && exists $allowed{$_} },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and exists $allowed{$_} 3029 0 3029
165 11 ▶ message => sub { "Value \"$_\" is not one of: $list" },
Subroutine __ANON__: called CC=1 SCAR=0.0
166 1984 );
167 }
168  
169 40611 ▶ if (exists $opts->{minimum} || exists $opts->{maximum}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"minimum"} or exists $opts->{"maximum"}) 1258 39353 40611
Condition100%3/3
exists $opts->{"minimum"} or exists $opts->{"maximum"}
l!l&&r!l&&!r
12391939353
MC/DC100%2/2
exists $opts->{"minimum"} or exists $opts->{"maximum"}
exists $opts->{"minimum"} exists $opts->{"maximum"}
Truth table100%3/3
A or B
A= exists $opts->{"minimum"}B= exists $opts->{"maximum"}
ABresult
1X1
011
000
170 croak "k8s: 'minimum' and 'maximum' need an Int or Num field, not $kind ($where)"
171 1258 ▶ unless $NUMERIC_KIND{$kind};
Branch: 1 branch
BranchTrueFalseTotal
unless $NUMERIC_KIND{$kind} 1 1257 1258
172 1257 my ($min, $max) = @{$opts}{qw(minimum maximum)};
173 1257 for my $bound ($min, $max) {
174 2513 ▶ croak "k8s: 'minimum' and 'maximum' for $where must be numbers"
Branch: 1 branch
BranchTrueFalseTotal
if defined $bound and not &looks_like_number($bound) 1 2512 2513
Condition100%3/3
defined $bound and not &looks_like_number($bound)
!ll&&!rl&&r
72117911
MC/DC100%2/2
defined $bound and not &looks_like_number($bound)
defined $bound not &looks_like_number($bound)
Truth table100%3/3
A and not(B)
A= defined $boundB= not &looks_like_number($bound)
ABresult
0X0
100
111
175 if defined $bound && !looks_like_number($bound);
176 }
177 1256 ▶ croak "k8s: 'minimum' ($min) must not exceed 'maximum' ($max) for $where"
Branch: 1 branch
BranchTrueFalseTotal
if defined $min and defined $max and $min > $max 1 1255 1256
Condition100%6/6
defined $min and defined $max and $min > $max
!ll&&!rl&&r
7215341
defined $min and defined $max
!ll&&!rl&&r
19702535
MC/DC100%3/3
defined $min and defined $max and $min > $max
defined $min defined $max $min > $max
Truth table100%4/4
A and B and C
A= defined $minB= defined $maxC= $min > $max
ABCresult
0XX0
10X0
1100
1111
178 if defined $min && defined $max && $min > $max;
179 $type = $type->create_child_type(
180 display_name => $type->display_name . '[range]',
181 1432 ▶ constraint => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
182 1432 ▶ (!defined $min || $_ >= $min) && (!defined $max || $_ <= $max)
Branch: 1 branch
BranchTrueFalseTotal
!defined($min) || $_ >= $min and !defined($max) || $_ <= $max 1423 9 1432
Condition100%9/9
(!defined($min) || $_ >= $min) && (!defined($max) || $_ <= $max)
!ll&&!rl&&r
9241399
not defined $min or $_ >= $min
l!l&&r!l&&!r
2713969
not defined $max or $_ <= $max
l!l&&r!l&&!r
64475524
MC/DC100%4/4
(!defined($min) || $_ >= $min) && (!defined($max) || $_ <= $max)
not defined $min $_ >= $min not defined $max $_ <= $max
Truth table85%6/7
not(A) or B && not(C) or D
A= not defined $minB= $_ >= $minC= not defined $maxD= $_ <= $max
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
183 },
184 9 ▶ message => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
185 9 ▶ defined $min && $_ < $min
Branch: 1 branch
BranchTrueFalseTotal
defined $min && $_ < $min ? : 3 6 9
Condition100%3/3
defined $min and $_ < $min
!ll&&!rl&&r
243
MC/DC100%2/2
defined $min and $_ < $min
defined $min $_ < $min
Truth table100%3/3
A and B
A= defined $minB= $_ < $min
ABresult
0X0
100
111
186 ? "Value \"$_\" is below the minimum $min"
187 : "Value \"$_\" is above the maximum $max";
188 },
189 1255 );
190 }
191  
192 40608 ▶ if (exists $opts->{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"pattern"}) 2315 38293 40608
193 croak "k8s: 'pattern' needs a string field, not $kind ($where)"
194 2315 ▶ unless $STRING_KIND{$kind};
Branch: 1 branch
BranchTrueFalseTotal
unless $STRING_KIND{$kind} 1 2314 2315
195 my $re = ref $opts->{pattern} eq 'Regexp'
196 ? $opts->{pattern}
197 2314 ▶ : eval { my $p = $opts->{pattern}; qr/$p/ };
Branch: 1 branch
BranchTrueFalseTotal
ref $opts->{'pattern'} eq 'Regexp' ? : 2006 308 2314
198 2314 ▶ croak "k8s: 'pattern' for $where does not compile: $@" unless $re;
Branch: 1 branch
BranchTrueFalseTotal
unless $re 1 2313 2314
199 $type = $type->create_child_type(
200 display_name => $type->display_name . '[pattern]',
201 5094 ▶ constraint => sub { defined $_ && $_ =~ $re },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $_ =~ /$re/ 5094 0 5094
202 14 ▶ message => sub { "Value \"$_\" does not match the pattern $re" },
Subroutine __ANON__: called CC=1 SCAR=0.0
203 2313 );
204 }
205  
206 40606 return $type;
207 }
208  
209 # Options that only make sense on a scalar-bearing field. Object, struct
210 # and opaque container fields reject them at class load.
211 29544 ▶sub _reject_value_options {
Subroutine _reject_value_options: called CC=5 SCAR=16.1
212 29544 my ($opts, $where) = @_;
213 29544 ▶ croak "k8s: 'enum' needs a scalar field ($where)" if exists $opts->{enum};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"enum"} 1 29543 29544
214 croak "k8s: 'minimum' and 'maximum' need a scalar field ($where)"
215 29543 ▶ if exists $opts->{minimum} || exists $opts->{maximum};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"minimum"} or exists $opts->{"maximum"} 1 29542 29543
Condition66%2/3
exists $opts->{"minimum"} or exists $opts->{"maximum"}
l!l&&r!l&&!r
1029542
MC/DC50%1/2
exists $opts->{"minimum"} or exists $opts->{"maximum"}
exists $opts->{"minimum"} exists $opts->{"maximum"}
Truth table66%2/3
A or B
A= exists $opts->{"minimum"}B= exists $opts->{"maximum"}
ABresult
1X1
011
000
216 29542 ▶ croak "k8s: 'pattern' needs a scalar field ($where)" if exists $opts->{pattern};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"pattern"} 1 29541 29542
217 }
218  
219 16634 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
220 16634 my $class = shift;
221 16634 my $caller = caller;
222 16634 $class->_setup_class($caller);
223 16634 $class->_import_map_types($caller);
224 }
225  
226 # HashRef and Opaque for map declarations (k191): `k8s data => HashRef[Str]`,
227 # `k8s raw => Opaque`. Only here, where a class is compiled from source --
228 # _setup_class also serves inline structs and IO::K8s::AutoGen, which build
229 # their fields at runtime and never name a type in code. Both names are
230 # swept from the package again once the scope that said `use` has finished
231 # compiling (namespace::clean, the explicit-list form): the declarations
232 # compiled against them keep working, but neither becomes a method of every
233 # shipped class, which is what k118 took HashRef off (t/27_no_import_leak.t).
234 16634 ▶sub _import_map_types {
Subroutine _import_map_types: called CC=1 SCAR=0.0
235 16634 my ($class, $target) = @_;
236 16634 Types::Standard->import::into($target, qw( HashRef ));
237 16634 IO::K8s::Types->import::into($target, qw( Opaque ));
238 16634 namespace::clean->import(-cleanee => $target, qw( HashRef Opaque ));
239 }
240  
241 17203 ▶sub _setup_class {
Subroutine _setup_class: called CC=1 SCAR=0.0
242 17203 my ($class, $target) = @_;
243 17203 Moo->import::into($target);
244 17203 Types::Standard->import::into($target, qw( Str Int Bool Num ));
245 17203 IO::K8s::Types->import::into($target, qw( IntOrStr Quantity Time ));
246 17203 Moo::Role->apply_roles_to_package($target, 'IO::K8s::Role::Resource');
247 17203 my $stash = Package::Stash->new($target);
248 17203 ▶ $stash->add_symbol('&k8s', sub { $class->_k8s($target, @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
249 }
250  
251 27676 ▶sub _expand_class {
Subroutine _expand_class: called CC=6 SCAR=17.9
252 27676 my ($short) = @_;
253  
254 # +FullClassName - strip + and use as-is
255 27676 ▶ return substr($short, 1) if $short =~ /^\+/;
Branch: 1 branch
BranchTrueFalseTotal
if $short =~ /^\+/ 11412 16264 27676
256  
257 # Already fully qualified?
258 16264 ▶ return $short if $short =~ /^IO::K8s::/;
Branch: 1 branch
BranchTrueFalseTotal
if $short =~ /^IO::K8s::/ 1 16263 16264
259  
260 # Prefix match against %_class_prefix: try the longest key first so that
261 # CamelCase prefixes like KubeAggregator win over a hypothetical shorter
262 # substring. Anything in the map is canonical — the lookup IS the source
263 # of truth, the regex is not. Unknown short names still fall through to
264 # the IO::K8s::Api default so this stays backwards compatible.
265 16263 ▶ if ($short =~ /^([A-Z]\w*)::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($short =~ /^([A-Z]\w*)::/) 16262 1 16263
266 16262 for my $prefix (sort { length($b) <=> length($a) } keys %_class_prefix) {
267 393866 ▶ next unless $short =~ /^\Q$prefix\E::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $short =~ /^\Q$prefix\E::/ 377647 16219 393866
268 16219 $short =~ s/^\Q$prefix\E:://;
269 16219 return $_class_prefix{$prefix} . '::' . $short;
270 }
271 }
272  
273 # Default: assume it's under IO::K8s::Api
274 44 return "IO::K8s::Api::$short";
275 }
276  
277 # A read-only view of %_class_prefix (short prefix -> full namespace),
278 # keyed the same as the map _expand_class walks above. This started as a
279 # lexical 'my' with a single reader inside this file; IO::K8s::CRD::Emitter
280 # is the first outside consumer, building the REVERSE map (full namespace
281 # -> short prefix) from it so a rendered class reference uses the short
282 # form a hand-written class would ('Traefik::V1alpha1::Middleware',
283 # 'Core::V1::PodTemplateSpec') instead of always spelling out '+Full::Name'.
284 8 ▶sub class_prefixes { \%_class_prefix }
Subroutine class_prefixes: called CC=1 SCAR=0.0
Pod undocumented
285  
286 101390 ▶sub _is_type_tiny {
Subroutine _is_type_tiny: called CC=2 SCAR=7.1
287 101390 my ($obj) = @_;
288 101390 ▶ return blessed($obj) && $obj->isa('Type::Tiny');
Condition66%2/3
builtin::blessed($obj) && $obj->isa('Type::Tiny')
!ll&&!rl&&r
60998040392
MC/DC50%1/2
builtin::blessed($obj) && $obj->isa('Type::Tiny')
builtin::blessed($obj) $obj->isa('Type::Tiny')
Truth table66%2/3
A && B
A= builtin::blessed($obj)B= $obj->isa('Type::Tiny')
ABresult
0X0
100
111
289 }
290  
291 # Sanitize JSON field names into valid Perl identifiers for Moo attributes:
292 # every character outside [A-Za-z0-9_] becomes '_' -- $ref -> _ref,
293 # $schema -> _schema, x-kubernetes-foo -> x_kubernetes_foo, x.y/z -> x_y_z,
294 # and the same for any other character a schema's property key happens to
295 # carry, not only the '-', '.' and '/' realistic CRD schemas mostly produce.
296 # This matters beyond cosmetics: `has` is always called with an `isa` type
297 # constraint here (see _k8s below), which routes through Sub::Quote's
298 # inlined accessor generation, and that dies naming the attribute as an
299 # invalid sub name for any of these characters -- the same failure
300 # IO::K8s::AutoGen::_class_segments exists to avoid for package segments,
301 # just one call site over (the attribute name here, not a class name; the
302 # two use the same blanket rule). A CRD schema is free to use any of these
303 # characters in a property key; the original is preserved on the wire via
304 # the caller's existing json_key/init_arg mapping below whenever this
305 # changes the name.
306 70155 ▶sub _sanitize_attr_name {
Subroutine _sanitize_attr_name: called CC=2 SCAR=6.9
307 70155 my ($name) = @_;
308 70155 ▶ return $name unless $name =~ /[^a-zA-Z0-9_]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /[^a-zA-Z0-9_]/ 69304 851 70155
309 851 (my $safe = $name) =~ s/[^A-Za-z0-9_]/_/g;
310 851 return $safe;
311 }
312  
313 # The one boolean normalization in the distribution: everything that can
314 # arrive on a Bool attribute, reduced to a plain 0/1 -- or undef, meaning
315 # "leave the field unset". Used by the Bool and [Bool] coercers below and,
316 # before the constructor even runs, by the is_bool branch of
317 # IO::K8s::_inflate_struct -- those two used to disagree (k37).
318 #
319 # Two traps, both of which silently flip false into true:
320 # * every reference is true in Perl, so \0 (the bare false idiom) and a
321 # JSON::PP::Boolean (a blessed ref to 0) must be dereferenced, not tested;
322 # * 'false' is a non-empty string and therefore true, so the strings have to
323 # be spelled out rather than left to truthiness.
324 #
325 # Anything that cannot mean true or false dies (k42): a non-scalar
326 # reference, and a scalar ref that dereferences to yet another reference
327 # (\\0 used to come out silently true). reftype, not ref, so that blessed
328 # scalar refs -- JSON::PP::Boolean, boolean.pm, Types::Serialiser, any
329 # bless \(my $x = 0) -- keep working. Messages end in \n deliberately:
330 # they are diagnostics, and the callers (Moo's coercion wrapper, the
331 # eval/rethrow in _inflate_struct) attach the attribute context.
332 18884 ▶sub _normalize_bool {
Subroutine _normalize_bool: called CC=8 SCAR=20.8
333 18884 my ($value) = @_;
334 18884 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 7902 10982 18884
335 7902 my $reftype = reftype($value);
336 7902 ▶ die "Bool value must be a scalar or scalar ref, got $reftype\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $reftype eq "SCALAR" or $reftype eq "REF" 12 7890 7902
Condition100%3/3
$reftype eq "SCALAR" or $reftype eq "REF"
l!l&&r!l&&!r
7883712
MC/DC100%2/2
$reftype eq "SCALAR" or $reftype eq "REF"
$reftype eq "SCALAR" $reftype eq "REF"
Truth table100%3/3
A or B
A= $reftype eq "SCALAR"B= $reftype eq "REF"
ABresult
1X1
011
000
337 unless $reftype eq 'SCALAR' || $reftype eq 'REF';
338 7890 $value = $$value;
339 7890 ▶ die 'Bool scalar ref dereferenced to another reference ('
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 7 7883 7890
340 . ref($value) . "), not a boolean\n" if ref $value;
341 }
342 # Explicit undef stays undef: the attribute is Maybe[Bool], TO_JSON
343 # omits undef, and "no value" must not turn into an explicit false on
344 # the wire (k48). `return undef`, not bare `return` -- in the
345 # [Bool] coercer's list context a bare return would drop the element.
346 18865 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2818 16047 18865
347 16047 ▶ return 0 if lc($value) eq 'false';
Branch: 1 branch
BranchTrueFalseTotal
if lc $value eq "false" 27 16020 16047
348 16020 ▶ return $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 9433 6587 16020
349 }
350  
351 # The coercion a single object-bearing field gets (k100): a plain hashref
352 # becomes an instance of $class_name, built by the very call FROM_HASH
353 # makes, so bool normalization and the D1 unknown-field policy are the ones
354 # the inflate path already uses instead of a second, subtly different way of
355 # building the same object. See the is_object branch of _k8s below for why
356 # one `ref eq 'HASH'` test is the whole guard.
357 #
358 # Named rather than written inline in that branch for the same reason
359 # _normalize_bool above is: a second caller needs exactly this, and the two
360 # must not drift. That caller is IO::K8s::Role::APIObject, whose `metadata`
361 # is a plain `has` -- the role composes before any k8s declaration runs, and
362 # metadata is then only registered through _k8s_adopt, which never calls
363 # has(), so a coercer installed here would never reach it (k115). The role
364 # therefore declares metadata with this coercion from the start.
365 #
366 # $class_name is captured; IO::K8s::Role::Resource::_default_k8s() is
367 # touched at COERCION time only, never while the attribute is installed --
368 # _k8s runs at compile time of every one of the ~850 classes, and
369 # _default_k8s requires IO::K8s, which loads this file.
370 18100 ▶sub _object_coercer {
Subroutine _object_coercer: called CC=1 SCAR=0.0
371 18100 my ($class_name) = @_;
372 18100 my $builds = _object_builds($class_name);
373 22932 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
374 22932 ▶ return $_[0] unless $builds->($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless &$builds($_[0]) 22785 147 22932
375 147 return IO::K8s::Role::Resource::_default_k8s()
376 ->_struct_to_object_expanded($class_name, $_[0]);
377 18100 };
378 }
379  
380 # Whether a value handed to an object-bearing field of $class_name is built
381 # into that class by the object coercers, as a predicate the three of them
382 # share -- the single field above and, element by element, the array and
383 # hash forms in _declare_field.
384 #
385 # A plain hashref always is. A class with FROM_STRUCT -- the apiextensions
386 # union classes, which serialize as the bare value they hold -- takes every
387 # defined value (k179): inflation hands it anything, so values => [1, 2]
388 # or enum => ['a'] inflated fine while ->new, the setter and spec_set died
389 # on the InstanceOf check for the very same values. An object already of
390 # the class is left alone, and undef stays "no value". Every other class
391 # keeps the k146 rule: anything but a hashref goes on to `isa`, which
392 # refuses it.
393 #
394 # FROM_STRUCT is only known once the class is loaded, and the coercer is
395 # installed long before (see _object_coercer). It is asked the first time
396 # a value is not a hashref, and the answer is kept; a class that does not
397 # load is asked again next time and meanwhile counts as an ordinary class,
398 # so such a value still meets the type check it met before.
399 25076 ▶sub _object_builds {
Subroutine _object_builds: called CC=1 SCAR=0.0
400 25076 my ($class_name) = @_;
401 25076 my $from_struct;
402 69405 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
403 69405 my ($value) = @_;
404 69405 ▶ return 0 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 18 69387 69405
405 69387 ▶ return 1 if ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 287 69100 69387
406 69100 ▶ $from_struct //= _takes_any_value($class_name);
Condition66%2/3
$from_struct //= _takes_any_value($class_name)
l!l&&r!l&&!r
6452945710
MC/DC0%0/2
$from_struct //= _takes_any_value($class_name)
$from_struct _takes_any_value($class_name)
Truth table66%2/3
A //= B
A= $from_structB= _takes_any_value($class_name)
ABresult
1X1
011
000
407 69100 ▶ return 0 unless $from_struct;
Branch: 1 branch
BranchTrueFalseTotal
unless $from_struct 58435 10665 69100
408 10665 ▶ return blessed($value) && $value->isa($class_name) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) && $value->isa($class_name) ? : 10637 28 10665
Condition100%3/3
builtin::blessed($value) and $value->isa($class_name)
!ll&&!rl&&r
26210637
MC/DC100%2/2
builtin::blessed($value) and $value->isa($class_name)
builtin::blessed($value) $value->isa($class_name)
Truth table100%3/3
A and B
A= builtin::blessed($value)B= $value->isa($class_name)
ABresult
0X0
100
111
409 25076 };
410 }
411  
412 # True when $class_name inflates through FROM_STRUCT (k179), false when it
413 # does not, nothing when it cannot be loaded.
414 4571 ▶sub _takes_any_value {
Subroutine _takes_any_value: called CC=3 SCAR=11.0
415 4571 my ($class_name) = @_;
416 4571 ▶ return unless eval {
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { IO::K8s::Role::Resource::_default_k8s()->load_class($class_name); 1 } } 0 4571 4571
417 4571 IO::K8s::Role::Resource::_default_k8s()->load_class($class_name);
418 4571 1;
419 };
420 4571 ▶ return $class_name->can('FROM_STRUCT') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$class_name->can('FROM_STRUCT') ? : 86 4485 4571
421 }
422  
423 # A map whose values have a type (k191): HashRef[X], and the { X => 1 } form
424 # that means the same for every X but Str. Sets the registry flag in $info
425 # and returns the field's type. X is a Type::Tiny value type: a scalar kind
426 # by its name -- Str (the strict string map), Int, Num, Bool, IntOrStr,
427 # Quantity, Time -- or InstanceOf[Class], the map of objects. The value
428 # constraint is X itself, so HashRef[Time] checks what { Time => 1 } checks
429 # and a lenient Str-based Time (IO::K8s::AutoGen's) stays lenient.
430 419 ▶sub _typed_map {
Subroutine _typed_map: called CC=4 SCAR=13.9
431 419 my ($info, $value_type, $opts, $where) = @_;
432 419 my $kind = $value_type->name;
433 419 ▶ if ($kind eq 'Str') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "Str") 12 407 419
434 12 $info->{is_hash_of_str} = 1;
435 12 return HashRef[ _constrain($value_type, 'Str', $opts, $where) ];
436 }
437 407 ▶ if (my $vt = $HASH_VALUE_TYPES{$kind}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $vt = $HASH_VALUE_TYPES{$kind}) 406 1 407
438 406 $info->{ $vt->{flag} } = 1;
439 406 return HashRef[ _constrain($value_type, $kind, $opts, $where) ];
440 }
441 1 ▶ return _object_map($info, $value_type->class, $opts, $where)
Branch: 1 branch
BranchTrueFalseTotal
if $value_type->isa("Type::Tiny::Class") 1 0 1
442 if $value_type->isa('Type::Tiny::Class');
443 0 croak "k8s: cannot interpret the value type of $where: HashRef["
444 . $value_type->display_name . '] (a scalar type, or InstanceOf[Class])';
445 }
446  
447 # The map of objects: { Class => 1 } and HashRef[InstanceOf[Class]].
448 347 ▶sub _object_map {
Subroutine _object_map: called CC=1 SCAR=0.0
449 347 my ($info, $full_class, $opts, $where) = @_;
450 347 $info->{is_hash_of_objects} = 1;
451 347 $info->{class} = $full_class;
452 347 _reject_value_options($opts, $where);
453 347 return HashRef[InstanceOf[$full_class]];
454 }
455  
456 142 ▶sub _generate_inline_struct {
Subroutine _generate_inline_struct: called CC=2 SCAR=6.9
457 142 my ($class_name, $fields) = @_;
458 142 __PACKAGE__->_setup_class($class_name);
459 142 for my $field_name (keys %$fields) {
460 430 __PACKAGE__->_k8s($class_name, $field_name, $fields->{$field_name});
461 }
462 }
463  
464 # The nearest registry entry for a Perl attribute name: $class's own, else
465 # the first one found walking @ISA depth-first, left to right -- the order
466 # IO::K8s::Role::Resource::_merged_attr_info resolves in, so this answers
467 # for the same view TO_JSON and FROM_HASH read. Returns the declaring class
468 # and its entry, or nothing. Uncached: it runs while classes are still
469 # being declared, and populating the role's merged-view cache from here
470 # would be a side effect of a declaration that may yet be rejected.
471 140266 ▶sub _nearest_registration {
Subroutine _nearest_registration: called CC=5 SCAR=16.1
472 140266 my ($class, $attr_name) = @_;
473 140266 my $own = $_attr_registry{$class};
474 140266 ▶ return ($class, $own->{$attr_name}) if $own && $own->{$attr_name};
Branch: 1 branch
BranchTrueFalseTotal
if $own and $own->{$attr_name} 53 140213 140266
Condition100%3/3
$own and $own->{$attr_name}
!ll&&!rl&&r
871135310053
MC/DC100%2/2
$own and $own->{$attr_name}
$own $own->{$attr_name}
Truth table100%3/3
A and B
A= $ownB= $own->{$attr_name}
ABresult
0X0
100
111
475 157 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
476 140213 for my $parent (@{"${class}::ISA"}) {
477 70111 my @found = _nearest_registration($parent, $attr_name);
478 70111 ▶ return @found if @found;
Branch: 1 branch
BranchTrueFalseTotal
if @found 7 70104 70111
479 }
480 140206 return;
481 }
482  
483 # The role that $class's method $attr_name comes from, when that role lists
484 # the name in its %YIELDS_TO_K8S_FIELD -- a role helper meant to give way to
485 # a declared wire field of the same name (IO::K8s::Role::APIObject's
486 # conditions, for ComponentStatus). Found through the method's own name
487 # rather than a list of classes, and checked against the role itself: the
488 # code must be the role's sub, and $class must do the role. Anything else --
489 # a method the class wrote, one a role does not declare as yielding, a
490 # modifier-wrapped helper -- returns nothing and stays a collision.
491 26 ▶sub _yielding_role_helper {
Subroutine _yielding_role_helper: called CC=10 SCAR=24.2
492 26 my ($class, $attr_name) = @_;
493 26 ▶ my $code = $class->can($attr_name) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $code = $class->can($attr_name) or return 26 0 26
494 26 ▶ my ($role, $sub) = subname($code) =~ /\A(.+)::([^:]+)\z/ or return;
Branch: 1 branch
BranchTrueFalseTotal
() = subname($code) =~ /\A(.+)::([^:]+)\z/ or return 26 0 26
495 26 ▶ return unless $sub eq $attr_name && Moo::Role->is_role($role);
Branch: 1 branch
BranchTrueFalseTotal
unless $sub eq $attr_name and "Moo::Role"->is_role($role) 3 23 26
Condition100%3/3
$sub eq $attr_name and "Moo::Role"->is_role($role)
!ll&&!rl&&r
2123
MC/DC100%2/2
$sub eq $attr_name and "Moo::Role"->is_role($role)
$sub eq $attr_name "Moo::Role"->is_role($role)
Truth table100%3/3
A and B
A= $sub eq $attr_nameB= "Moo::Role"->is_role($role)
ABresult
0X0
100
111
496 157 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
497 23 ▶ return unless ${"${role}::YIELDS_TO_K8S_FIELD"}{$attr_name};
Branch: 1 branch
BranchTrueFalseTotal
unless do { "${role}::YIELDS_TO_K8S_FIELD" }->{$attr_name} 11 12 23
498 12 return unless defined &{"${role}::${attr_name}"}
499 12 ▶ && \&{"${role}::${attr_name}"} == $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &{"${role}::$attr_name";} and \&{"${role}::$attr_name";} == $code 0 12 12
Condition33%1/3
defined &{"${role}::$attr_name";} and \&{"${role}::$attr_name";} == $code
!ll&&!rl&&r
0012
MC/DC0%0/2
defined &{"${role}::$attr_name";} and \&{"${role}::$attr_name";} == $code
defined &{"${role}::$attr_name";} \&{"${role}::$attr_name";} == $code
Truth table33%1/3
A and B
A= defined &{"${role}::$attr_name";}B= \&{"${role}::$attr_name";} == $code
ABresult
0X0
100
111
500 12 ▶ return unless $class->can('does') && $class->does($role);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("does") and $class->does($role) 0 12 12
Condition33%1/3
$class->can("does") and $class->does($role)
!ll&&!rl&&r
0012
MC/DC0%0/2
$class->can("does") and $class->does($role)
$class->can("does") $class->does($role)
Truth table33%1/3
A and B
A= $class->can("does")B= $class->does($role)
ABresult
0X0
100
111
501 12 return $role;
502 }
503  
504 # The `k8s` DSL entry point. A thin wrapper so that every argument a caller
505 # of `k8s` can pass has a meaning; the adopt switch below is not one of them.
506 67276 ▶sub _k8s {
Subroutine _k8s: called CC=1 SCAR=0.0
507 67276 my ($class, $caller, $name, $type_spec, $marker) = @_;
508 67276 return $class->_declare_field($caller, $name, $type_spec, $marker, 0);
509 }
510  
511 # Register a wire field for a Moo attribute the class already has, without
512 # calling has() -- the one field this exists for is metadata, which
513 # IO::K8s::Role::APIObject declares itself (with the ObjectMeta coercion)
514 # and which IO::K8s::APIObject::import and IO::K8s::AutoGen then register
515 # for the registry readers. Private on purpose: the public `k8s` never
516 # adopts, so a field can no longer end up registered over an attribute
517 # nobody declared for it (k144). Refused unless an attribute of that name
518 # is in effect and its init_arg is the field's JSON key.
519 2879 ▶sub _k8s_adopt {
Subroutine _k8s_adopt: called CC=1 SCAR=0.0
520 2879 my ($class, $caller, $name, $type_spec, $marker) = @_;
521 2879 return $class->_declare_field($caller, $name, $type_spec, $marker, 1);
522 }
523  
524 70155 ▶sub _declare_field {
Subroutine _declare_field: called CC=103 SCAR=47.1
525 70155 my ($class, $caller, $name, $type_spec, $marker, $adopt) = @_;
526  
527 70155 my $json_key = $name;
528 70155 my $attr_name = _sanitize_attr_name($name);
529 70155 my $where = "field '$name' of $caller";
530  
531 # Declaration preflight (k144). Every conflict is refused here, before
532 # anything below builds an inline-struct class, calls has() or writes
533 # the registry, the attribute list or the merged-view cache -- a
534 # rejected declaration leaves the class exactly as it was.
535 #
536 # Two JSON keys, one accessor: _sanitize_attr_name is not injective
537 # (x-value and x_value both become x_value), so a second key reaching
538 # an attribute name another key already holds -- in this class or,
539 # nearest wins, in an ancestor -- would silently retarget that field.
540 70155 my ($declarer, $registered) = _nearest_registration($caller, $attr_name);
541 70155 ▶ if ($registered) {
Branch: 1 branch
BranchTrueFalseTotal
if ($registered) 53 70102 70155
542 53 ▶ my $other_key = $registered->{json_key} // $attr_name;
Condition66%2/3
$registered->{"json_key"} // $attr_name
l!l&&r!l&&!r
4490
MC/DC0%0/2
$registered->{"json_key"} // $attr_name
$registered->{"json_key"} $attr_name
Truth table66%2/3
A // B
A= $registered->{"json_key"}B= $attr_name
ABresult
1X1
011
000
543 53 ▶ croak "k8s: $where collides with field '$other_key' of $declarer: "
Branch: 1 branch
BranchTrueFalseTotal
if $other_key ne $json_key 4 49 53
544 . "both map to the Perl attribute '$attr_name'"
545 if $other_key ne $json_key;
546 }
547 # The same JSON key declared a second time in this very class (k151).
548 # Moo keeps the first attribute, so a second declaration is only ever
549 # compared with the first one, below, once its type is interpreted --
550 # identical is a no-op, anything else is refused.
551 70151 ▶ my $redeclared = $registered && $declarer eq $caller;
Condition100%3/3
$registered && $declarer eq $caller
!ll&&!rl&&r
70102544
MC/DC100%2/2
$registered && $declarer eq $caller
$registered $declarer eq $caller
Truth table100%3/3
A && B
A= $registeredB= $declarer eq $caller
ABresult
0X0
100
111
552 # A method that is not a Moo attribute -- an IO::K8s::Role::APIObject
553 # helper such as get_condition, a Moo keyword -- used to make the old
554 # `return if $caller->can($attr_name)` register the field and skip
555 # has(), leaving the wire field served by that method. What counts is
556 # the effective Moo spec; a registry entry proves nothing. The one way
557 # past this is a role helper its role declares as yielding to a wire
558 # field of the same name (see _yielding_role_helper): the field is then
559 # installed over it.
560 70151 my ($spec, $local, $yielding);
561 70151 ▶ if ($caller->can($attr_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caller->can($attr_name)) 2954 67197 70151
562 2954 $spec = IO::K8s::Role::Resource::_effective_attribute_specs($caller)->{$attr_name};
563 2954 ▶ $yielding = _yielding_role_helper($caller, $attr_name) unless $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec 26 2928 2954
564 2954 ▶ croak "k8s: $where collides with the method '$attr_name' of $caller, "
Branch: 1 branch
BranchTrueFalseTotal
unless $spec or $yielding 14 2940 2954
Condition100%3/3
$spec or $yielding
l!l&&r!l&&!r
29281214
MC/DC100%2/2
$spec or $yielding
$spec $yielding
Truth table100%3/3
A or B
A= $specB= $yielding
ABresult
1X1
011
000
565 . 'which is not an attribute'
566 unless $spec || $yielding;
567 157 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
568 2940 $local = defined &{"${caller}::${attr_name}"};
569 }
570 # Moo refuses has() for an accessor this very package already defines,
571 # so a fresh declaration can replace an inherited attribute (nearest
572 # wins, below) but never one of the class's own. That leaves three
573 # cases where has() is not called at all:
574 # * adopt -- the explicit path above; the attribute must exist and
575 # take the JSON key as its constructor argument;
576 # * a field this class already declared (or adopted), declared again
577 # with the same JSON key -- compared with the first declaration
578 # further down, and a no-op when identical (k151). It used to
579 # overwrite the registry entry while Moo kept the first spec, so
580 # the two described different fields;
581 # * anything else the class defines itself (a role's attribute, a
582 # plain has) -- refused, since the field would be registered over
583 # an attribute that does not follow its declaration.
584 70137 my $install = 1;
585 70137 ▶ if ($adopt) {
Branch: 3 branches
BranchTrueFalseTotal
if ($adopt) { } 2879 67258 70137
elsif ($yielding) { } 12 67246 67258
elsif ($local) { } 44 67202 67246
586 2879 ▶ my $init = $spec && exists $spec->{init_arg} ? $spec->{init_arg} : $attr_name;
Branch: 1 branch
BranchTrueFalseTotal
$spec && exists $spec->{'init_arg'} ? : 0 2879 2879
Condition33%1/3
$spec and exists $spec->{"init_arg"}
!ll&&!rl&&r
028790
MC/DC0%0/2
$spec and exists $spec->{"init_arg"}
$spec exists $spec->{"init_arg"}
Truth table33%1/3
A and B
A= $specB= exists $spec->{"init_arg"}
ABresult
0X0
100
111
587 2879 ▶ croak "k8s: cannot adopt $where: $caller has no attribute '$attr_name' "
Branch: 1 branch
BranchTrueFalseTotal
unless $spec and defined $init and $init eq $json_key 0 2879 2879
Condition33%2/6
$spec and defined $init and $init eq $json_key
!ll&&!rl&&r
002879
$spec and defined $init
!ll&&!rl&&r
002879
MC/DC0%0/3
$spec and defined $init and $init eq $json_key
$spec defined $init $init eq $json_key
Truth table25%1/4
A and B and C
A= $specB= defined $initC= $init eq $json_key
ABCresult
0XX0
10X0
1100
1111
588 . "taking '$json_key' as its constructor argument"
589 unless $spec && defined $init && $init eq $json_key;
590 2879 $install = 0;
591 } elsif ($yielding) {
592 # Installed below; a helper composed into this very package is
593 # removed right before has(), which refuses to overwrite it.
594 } elsif ($local) {
595 44 ▶ croak "k8s: $where would take over the attribute '$attr_name' that "
Branch: 1 branch
BranchTrueFalseTotal
unless $redeclared 0 44 44
596 . "$caller defines outside the k8s DSL"
597 unless $redeclared;
598 44 $install = 0;
599 }
600  
601 # Inline-struct form: name => [ Type, { options } ]. Exactly two elements
602 # with a hashref second is unambiguous -- every array type spec ([Str],
603 # ['Core::V1::Container'], [ {} ], [ [] ]) has one element.
604 70137 ▶ if (ref $type_spec eq 'ARRAY' && @$type_spec == 2 && ref $type_spec->[1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type_spec eq "ARRAY" and @$type_spec == 2 and ref $type_spec->[1] eq "HASH") 10 70127 70137
Condition83%5/6
ref $type_spec eq "ARRAY" and @$type_spec == 2 and ref $type_spec->[1] eq "HASH"
!ll&&!rl&&r
70127010
ref $type_spec eq "ARRAY" and @$type_spec == 2
!ll&&!rl&&r
599581016910
MC/DC66%2/3
ref $type_spec eq "ARRAY" and @$type_spec == 2 and ref $type_spec->[1] eq "HASH"
ref $type_spec eq "ARRAY" @$type_spec == 2 ref $type_spec->[1] eq "HASH"
Truth table75%3/4
A and B and C
A= ref $type_spec eq "ARRAY"B= @$type_spec == 2C= ref $type_spec->[1] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
605 10 ($type_spec, $marker) = @$type_spec;
606 }
607  
608 70137 my %opts;
609 70137 ▶ if (ref $marker eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $marker eq 'HASH') { } 11548 58589 70137
elsif (defined $marker and $marker eq 'required') { } 7126 51463 58589
elsif (defined $marker) { } 1 51462 51463
610 11548 %opts = %$marker;
611 ▶ } elsif (defined $marker && $marker eq 'required') {
Condition100%3/3
defined $marker and $marker eq "required"
!ll&&!rl&&r
5146217126
MC/DC100%2/2
defined $marker and $marker eq "required"
defined $marker $marker eq "required"
Truth table100%3/3
A and B
A= defined $markerB= $marker eq "required"
ABresult
0X0
100
111
612 7126 $opts{required} = 1;
613 } elsif (defined $marker) {
614 1 croak "k8s: third argument for $where must be 'required' or a hashref of field options, got '$marker'";
615 }
616 70136 for my $key (sort keys %opts) {
617 croak "k8s: unknown field option '$key' for $where (known: "
618 . join(', ', sort keys %FIELD_OPTIONS) . ')'
619 21399 ▶ unless $FIELD_OPTIONS{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $FIELD_OPTIONS{$key} 1 21398 21399
620 }
621 # An option present with an undef value is a declaration error, not a
622 # no-op: `pattern => $schema->{pattern}` on an upstream schema with no
623 # pattern would otherwise compile to a match-everything regex instead
624 # of simply not declaring the option, and the same silent trap applies
625 # to every other option (an undef default is not a JSON null we model).
626 70135 for my $key (sort keys %opts) {
627 croak "k8s: field option '$key' for $where must not be undef"
628 21398 ▶ unless defined $opts{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{$key} 2 21396 21398
629 }
630 # A nullable field keeps an explicit JSON null (k158) and brings two
631 # methods of its own: has_<accessor>, true while the key exists (null
632 # included), and clear_<accessor>, which makes the field absent again.
633 # Both names are part of the declaration preflight: taken by anything
634 # but this very field's own predicate and clearer -- declared before in
635 # this class, or inherited from the field an ancestor declares -- they
636 # are refused like a colliding field name, before anything is built.
637 70133 ▶ my @nullable_methods = $opts{nullable} ? _nullable_methods($attr_name) : ();
Branch: 1 branch
BranchTrueFalseTotal
$opts{'nullable'} ? : 43 70090 70133
638 70133 ▶ if (@nullable_methods) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nullable_methods) 43 70090 70133
639 43 ▶ my $own = IO::K8s::Role::Resource::_effective_attribute_specs($caller)->{$attr_name} // {};
Condition100%2/2
IO::K8s::Role::Resource::_effective_attribute_specs($caller)->{$attr_name} // {}
l!l
241
MC/DC100%1/1
IO::K8s::Role::Resource::_effective_attribute_specs($caller)->{$attr_name} // {}
IO::K8s::Role::Resource::_effective_attribute_specs($caller)->{$attr_name}
Truth table100%2/2
A
A= IO::K8s::Role::Resource::_effective_attribute_specs($caller)->{$attr_name}
Aresult
11
00
640 43 ▶ my %own = map { defined $_ ? ($_ => 1) : () } @{$own}{qw( predicate clearer )};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 4 82 86
641 43 for my $method (@nullable_methods) {
642 croak "k8s: $where needs the method '$method' as a nullable field, "
643 . "but $caller already has a method of that name"
644 84 ▶ if $caller->can($method) && !$own{$method};
Branch: 1 branch
BranchTrueFalseTotal
if $caller->can($method) and not $own{$method} 3 81 84
Condition100%3/3
$caller->can($method) and not $own{$method}
!ll&&!rl&&r
7743
MC/DC100%2/2
$caller->can($method) and not $own{$method}
$caller->can($method) not $own{$method}
Truth table100%3/3
A and not(B)
A= $caller->can($method)B= not $own{$method}
ABresult
0X0
100
111
645 }
646 }
647 # required => 1 (or the legacy 'required' marker / '!' suffix) both
648 # enforces the field at construction and records required => 1 in the
649 # registry. required => 'schema' does the second half only: AutoGen
650 # uses it for an OpenAPI schema's required list, since a real cluster
651 # document can omit a field the schema requires -- a server-side
652 # default, or an object still short of that field (an empty status
653 # right after creation) -- and Moo enforcement would reject a
654 # perfectly valid document for it (Critical 1 of the k93 review). Any
655 # other true value is treated the same as 1.
656 70130 my $required_opt = delete $opts{required};
657 70130 ▶ my $required_recorded = $required_opt ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$required_opt ? : 13621 56509 70130
658 70130 ▶ my $required = ($required_opt && $required_opt ne 'schema') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$required_opt && $required_opt ne 'schema' ? : 7132 62998 70130
Condition100%3/3
$required_opt and $required_opt ne "schema"
!ll&&!rl&&r
5650964897132
MC/DC100%2/2
$required_opt and $required_opt ne "schema"
$required_opt $required_opt ne "schema"
Truth table100%3/3
A and B
A= $required_optB= $required_opt ne "schema"
ABresult
0X0
100
111
659  
660 # `!` suffix on strings (legacy/alternative required syntax)
661 70130 ▶ if (!ref $type_spec && !_is_type_tiny($type_spec) && $type_spec =~ s/!$//) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $type_spec || _is_type_tiny($type_spec) and $type_spec =~ s/!$//) { } 3 70127 70130
elsif (ref $type_spec eq 'ARRAY' and not ref $type_spec->[0] and $type_spec->[0] =~ s/!$//) { } 1 133620 133621
Condition83%5/6
not ref $type_spec || _is_type_tiny($type_spec) and $type_spec =~ s/!$//
!ll&&!rl&&r
49016211113
ref $type_spec || _is_type_tiny($type_spec)
l!l&&r!l&&!r
49016021114
MC/DC66%2/3
not ref $type_spec || _is_type_tiny($type_spec) and $type_spec =~ s/!$//
ref $type_spec _is_type_tiny($type_spec) $type_spec =~ s/!$//
Truth table75%3/4
not(A || B) and C
A= ref $type_specB= _is_type_tiny($type_spec)C= $type_spec =~ s/!$//
ABCresult
1XX0
01X0
0000
0011
662 3 $required = $required_recorded = 1;
663 ▶ } elsif (ref $type_spec eq 'ARRAY' && !ref($type_spec->[0]) && $type_spec->[0] =~ s/!$//) {
Condition100%6/6
ref $type_spec eq "ARRAY" and not ref $type_spec->[0] and $type_spec->[0] =~ s/!$//
!ll&&!rl&&r
6349466321
ref $type_spec eq "ARRAY" and not ref $type_spec->[0]
!ll&&!rl&&r
5995635386633
MC/DC100%3/3
ref $type_spec eq "ARRAY" and not ref $type_spec->[0] and $type_spec->[0] =~ s/!$//
ref $type_spec eq "ARRAY" not ref $type_spec->[0] $type_spec->[0] =~ s/!$//
Truth table100%4/4
A and not(B) and C
A= ref $type_spec eq "ARRAY"B= not ref $type_spec->[0]C= $type_spec->[0] =~ s/!$//
ABCresult
0XX0
10X0
1100
1111
664 1 $required = $required_recorded = 1;
665 }
666  
667 # Every branch below sets $inner, the type of a present value; the
668 # Maybe wrapping for an optional field happens once at the end.
669 70130 my %info;
670 my $inner;
671  
672 # Handle Type::Tiny objects directly (Str, Int, Bool, IntOrStr, Quantity,
673 # Time) -- and, since k191, the map types: Opaque or a bare HashRef is
674 # the free map, HashRef[X] a map whose values are X.
675 70130 ▶ if (_is_type_tiny($type_spec)) {
Branch: 4 branches
BranchTrueFalseTotal
if (_is_type_tiny($type_spec)) { } 36879 33251 70130
elsif (not ref $type_spec) { } 21114 12137 33251
elsif (ref $type_spec eq 'ARRAY') { } 10171 1966 12137
elsif (ref $type_spec eq 'HASH') { } 1966 0 1966
676 36879 my $kind = $type_spec->name;
677 36879 my $flags = $TYPE_FLAGS{$kind};
678 36879 ▶ if ($flags) {
Branch: 3 branches
BranchTrueFalseTotal
if ($flags) { } 36231 648 36879
elsif ($kind eq 'Opaque' or $kind eq 'HashRef') { } 484 164 648
elsif ($type_spec->is_parameterized and $type_spec->parameterized_from->name eq 'HashRef') { } 164 0 164
679 36231 %info = %$flags;
680 36231 $inner = _constrain($type_spec, $kind, \%opts, $where);
681 ▶ } elsif ($kind eq 'Opaque' || $kind eq 'HashRef') {
Condition100%3/3
$kind eq "Opaque" or $kind eq "HashRef"
l!l&&r!l&&!r
4822164
MC/DC100%2/2
$kind eq "Opaque" or $kind eq "HashRef"
$kind eq "Opaque" $kind eq "HashRef"
Truth table100%3/3
A or B
A= $kind eq "Opaque"B= $kind eq "HashRef"
ABresult
1X1
011
000
682 # Values untyped and copied through as they are; there is no
683 # scalar to put a value option on.
684 484 $info{is_hash_opaque} = 1;
685 484 _reject_value_options(\%opts, $where);
686 484 $inner = HashRef;
687 ▶ } elsif ($type_spec->is_parameterized
Condition33%1/3
$type_spec->is_parameterized and $type_spec->parameterized_from->name eq "HashRef"
!ll&&!rl&&r
00164
MC/DC0%0/2
$type_spec->is_parameterized and $type_spec->parameterized_from->name eq "HashRef"
$type_spec->is_parameterized $type_spec->parameterized_from->name eq "HashRef"
Truth table33%1/3
A and B
A= $type_spec->is_parameterizedB= $type_spec->parameterized_from->name eq "HashRef"
ABresult
0X0
100
111
688 && $type_spec->parameterized_from->name eq 'HashRef') {
689 164 $inner = _typed_map(\%info, $type_spec->type_parameter, \%opts, $where);
690 }
691 } elsif (!ref $type_spec) {
692 21114 ▶ if (my $flags = $TYPE_FLAGS{$type_spec}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $flags = $TYPE_FLAGS{$type_spec}) { } 413 20701 21114
693 413 %info = %$flags;
694 413 ▶ my $base = $STR_ISA_MAP{$type_spec} // Str;
Condition66%2/3
$STR_ISA_MAP{$type_spec} // &Str()
l!l&&r!l&&!r
378350
MC/DC0%0/2
$STR_ISA_MAP{$type_spec} // &Str()
$STR_ISA_MAP{$type_spec} &Str()
Truth table66%2/3
A // B
A= $STR_ISA_MAP{$type_spec}B= &Str()
ABresult
1X1
011
000
695 413 $inner = _constrain($base, $type_spec, \%opts, $where);
696 } else {
697 20701 my $full_class = _expand_class($type_spec);
698 20701 $info{is_object} = 1;
699 20701 $info{class} = $full_class;
700 20701 _reject_value_options(\%opts, $where);
701 20700 $inner = InstanceOf[$full_class];
702 }
703 } elsif (ref $type_spec eq 'ARRAY') {
704 10171 my $elem = $type_spec->[0];
705 # [ {} ] / [ [] ] -- an array of opaque hashes or opaque arrays, for a
706 # schema whose items are `type: object` / `type: array` with no further
707 # structure (k66). Validated as arrays of the right container
708 # shape; the contents pass through untyped, the same one-level-copy
709 # opaque handling a free-form HashRef gets in TO_JSON / _inflate_struct.
710 10171 ▶ if (ref $elem eq 'HASH') {
Branch: 5 branches
BranchTrueFalseTotal
if (ref $elem eq 'HASH') { } 21 10150 10171
elsif (ref $elem eq 'ARRAY') { } 4 10146 10150
elsif (_is_type_tiny($elem)) { } 3513 6633 10146
elsif ($elem eq 'Str') { } 4 6629 6633
elsif ($elem eq 'Int') { } 0 6629 6629
711 21 $info{is_array_of_hash} = 1;
712 21 _reject_value_options(\%opts, $where);
713 20 $inner = ArrayRef[HashRef];
714 } elsif (ref $elem eq 'ARRAY') {
715 4 $info{is_array_of_array} = 1;
716 4 _reject_value_options(\%opts, $where);
717 4 $inner = ArrayRef[ArrayRef];
718 # Handle [Str] with Type::Tiny object -- and, since 1.108's to_crd
719 # (D9), every other scalar kind the DSL knows too. Before, only
720 # Str/Int/Bool recorded an is_array_of_* flag here; [Num]/[Quantity]/
721 # [Time]/[IntOrStr] still built and validated a working ArrayRef
722 # attribute (Moo's own isa/coerce below is unaffected either way),
723 # but left the registry entry with NO classifying flag at all --
724 # invisible to anything that reads the registry instead of the Moo
725 # attribute, which is exactly what to_crd's _schema_for_class does
726 # (found via IO::K8s::Api::Resource::V1::ResourceSlice, whose
727 # DeviceCapacity.validValues is [Quantity]; k96 task-2 review).
728 # Purely additive when introduced (k96 task-2): TO_JSON and
729 # _inflate_struct had no branch keyed on any of these four flags, so
730 # they fell through to the same generic ArrayRef copy an unflagged
731 # entry already used. _inflate_struct still does today -- inflation
732 # is unchanged. TO_JSON no longer does: it now reads is_array_of_num
733 # for JSON-number elements (k155), is_array_of_quantity and
734 # is_array_of_time to stringify elements (k180), and
735 # is_array_of_int_or_string for the per-element int-or-string rule
736 # (k167, k181).
737 } elsif (_is_type_tiny($elem)) {
738 3513 my $kind = $elem->name;
739 3513 ▶ if ($kind eq 'Str') {
Branch: 7 branches
BranchTrueFalseTotal
if ($kind eq 'Str') { } 3314 199 3513
elsif ($kind eq 'Int') { } 89 110 199
elsif ($kind eq 'Bool') { } 36 74 110
elsif ($kind eq 'Num') { } 8 66 74
elsif ($kind eq 'IntOrStr') { } 13 53 66
elsif ($kind eq 'Quantity') { } 34 19 53
elsif ($kind eq 'Time') { } 19 0 19
740 3314 $info{is_array_of_str} = 1;
741 } elsif ($kind eq 'Int') {
742 89 $info{is_array_of_int} = 1;
743 } elsif ($kind eq 'Bool') {
744 36 $info{is_array_of_bool} = 1;
745 } elsif ($kind eq 'Num') {
746 8 $info{is_array_of_num} = 1;
747 } elsif ($kind eq 'IntOrStr') {
748 13 $info{is_array_of_int_or_string} = 1;
749 } elsif ($kind eq 'Quantity') {
750 34 $info{is_array_of_quantity} = 1;
751 } elsif ($kind eq 'Time') {
752 19 $info{is_array_of_time} = 1;
753 }
754 3513 $inner = ArrayRef[ _constrain($elem, $kind, \%opts, $where) ];
755 } elsif ($elem eq 'Str') {
756 4 $info{is_array_of_str} = 1;
757 4 $inner = ArrayRef[ _constrain(Str, 'Str', \%opts, $where) ];
758 } elsif ($elem eq 'Int') {
759 0 $info{is_array_of_int} = 1;
760 0 $inner = ArrayRef[ _constrain(Int, 'Int', \%opts, $where) ];
761 } else {
762 6629 my $full_class = _expand_class($elem);
763 6629 $info{is_array_of_objects} = 1;
764 6629 $info{class} = $full_class;
765 6629 _reject_value_options(\%opts, $where);
766 6629 $inner = ArrayRef[InstanceOf[$full_class]];
767 }
768 } elsif (ref $type_spec eq 'HASH') {
769 1966 my @keys = keys %$type_spec;
770 1966 ▶ if (@keys == 1 && !ref($type_spec->{$keys[0]}) && $type_spec->{$keys[0]} eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if (@keys == 1 and not ref $$type_spec{$keys[0]} and $$type_spec{$keys[0]} eq '1') { } 1813 153 1966
Condition100%6/6
@keys == 1 and not ref $$type_spec{$keys[0]} and $$type_spec{$keys[0]} eq 1
!ll&&!rl&&r
14671813
@keys == 1 and not ref $$type_spec{$keys[0]}
!ll&&!rl&&r
110361820
MC/DC100%3/3
@keys == 1 and not ref $$type_spec{$keys[0]} and $$type_spec{$keys[0]} eq 1
@keys == 1 not ref $$type_spec{$keys[0]} $$type_spec{$keys[0]} eq 1
Truth table100%4/4
A and not(B) and C
A= @keys == 1B= not ref $$type_spec{$keys[0]}C= $$type_spec{$keys[0]} eq 1
ABCresult
0XX0
10X0
1100
1111
771 # Hash-of-X pattern: { TypeName => 1 }
772 1813 my $vkind = $keys[0];
773 1813 ▶ if ($vkind eq 'Str') {
Branch: 2 branches
BranchTrueFalseTotal
if ($vkind eq 'Str') { } 1212 601 1813
elsif (my $vt = $HASH_VALUE_TYPES{$vkind}) { } 255 346 601
774 # The string map, lenient (k191): TO_JSON puts every scalar
775 # value out as a JSON string, but the check stays the bare
776 # HashRef it was while { Str => 1 } meant the opaque map, so
777 # a declaration still relying on that keeps loading and
778 # serializing -- its reference values pass through, with a
779 # one-time warning. HashRef[Str] is the strict spelling.
780 1212 $info{is_hash_of_str} = 1;
781 1212 $info{is_hash_of_str_lenient} = 1;
782 1212 _reject_value_options(\%opts, $where);
783 1211 $inner = HashRef;
784 } elsif (my $vt = $HASH_VALUE_TYPES{$vkind}) {
785 # { Quantity => 1 } and friends: a typed value map. Each value
786 # is validated against the scalar type (k63).
787 255 $inner = _typed_map(\%info, $vt->{isa}, \%opts, $where);
788 } else {
789 346 $inner = _object_map(\%info, _expand_class($vkind), \%opts, $where);
790 }
791 } else {
792 # Inline struct: { field => TypeSpec, ... }
793 153 my $inner_class = $caller . '::_' . ucfirst($attr_name);
794 153 ▶ if ($redeclared) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redeclared) { } 11 142 153
795 11 _redeclare_inline_struct($caller, $where, $registered, $inner_class, $type_spec);
796 } else {
797 142 _generate_inline_struct($inner_class, $type_spec);
798 }
799 146 $info{is_object} = 1;
800 146 $info{is_inline_struct} = 1;
801 146 $info{class} = $inner_class;
802 146 _reject_value_options(\%opts, $where);
803 146 $inner = InstanceOf[$inner_class];
804 }
805 }
806  
807 70112 ▶ croak "k8s: cannot interpret the type of $where" unless defined $inner;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inner 0 70112 70112
808  
809 # A default that the field's own type rejects is a declaration error,
810 # not something to discover when to_crd emits it -- except on an
811 # object-bearing field (a referenced class, an inline struct, an array
812 # or hash of objects), where $inner is InstanceOf[...] or wraps it: no
813 # plain hash/array default can ever satisfy that, so there is nothing
814 # useful to check here. The default is recorded as given; to_crd
815 # validates it against the schema instead (Important 3 of the k93
816 # review).
817 70112 ▶ if (exists $opts{default} && !$info{is_object} && !$info{is_array_of_objects}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"default"} and not $info{"is_object"} and not $info{"is_array_of_objects"} and not $info{"is_hash_of_objects"} and not $inner->check($opts{"default"})) 2 70110 70112
Condition91%11/12
exists $opts{"default"} and not $info{"is_object"} and not $info{"is_array_of_objects"} and not $info{"is_hash_of_objects"} and not $inner->check($opts{"default"})
!ll&&!rl&&r
6878513252
exists $opts{"default"} and not $info{"is_object"} and not $info{"is_array_of_objects"} and not $info{"is_hash_of_objects"}
!ll&&!rl&&r
6878501327
exists $opts{"default"} and not $info{"is_object"} and not $info{"is_array_of_objects"}
!ll&&!rl&&r
68714711327
exists $opts{"default"} and not $info{"is_object"}
!ll&&!rl&&r
686041101398
MC/DC80%4/5
exists $opts{"default"} and not $info{"is_object"} and not $info{"is_array_of_objects"} and not $info{"is_hash_of_objects"} and not $inner->check($opts{"default"})
exists $opts{"default"} not $info{"is_object"} not $info{"is_array_of_objects"} not $info{"is_hash_of_objects"} not $inner->check($opts{"default"})
Truth table83%5/6
A and not(B) and not(C) and not(D) and not(E)
A= exists $opts{"default"}B= not $info{"is_object"}C= not $info{"is_array_of_objects"}D= not $info{"is_hash_of_objects"}E= not $inner->check($opts{"default"})
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
818 && !$info{is_hash_of_objects} && !$inner->check($opts{default})) {
819 croak "k8s: 'default' for $where fails the field's own type: "
820 2 . $inner->get_message($opts{default});
821 }
822  
823 # A nullable field takes undef even when required: required means the
824 # key must exist, and null is a value it may exist with (k158).
825 70110 ▶ my $isa = $required && !@nullable_methods ? $inner : Maybe[$inner];
Branch: 1 branch
BranchTrueFalseTotal
$required && !@nullable_methods ? : 7135 62975 70110
Condition100%3/3
$required and not @nullable_methods
!ll&&!rl&&r
6297417135
MC/DC100%2/2
$required and not @nullable_methods
$required not @nullable_methods
Truth table100%3/3
A and not(B)
A= $requiredB= not @nullable_methods
ABresult
0X0
100
111
826  
827 70110 ▶ $info{required} = 1 if $required_recorded;
Branch: 1 branch
BranchTrueFalseTotal
if $required_recorded 13625 56485 70110
828 70110 ▶ if (%opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (%opts) 6272 63838 70110
829 # A one-level copy: the registry must not alias a caller's arrayref
830 # (the common `enum => $schema->{enum}` idiom reads straight off a
831 # shared upstream structure) and later see it mutated out from under
832 # the constraint that was already built from it. A Regexp is
833 # immutable, so 'pattern' needs no copy.
834 6272 my %stored = %opts;
835 6272 ▶ $stored{enum} = [ @{ $stored{enum} } ] if exists $stored{enum};
Branch: 1 branch
BranchTrueFalseTotal
if exists $stored{"enum"} 1965 4307 6272
836 6272 $info{options} = \%stored;
837 }
838  
839 # Store json_key when it differs from the Perl attribute name
840 70110 ▶ $info{json_key} = $json_key if $attr_name ne $json_key;
Branch: 1 branch
BranchTrueFalseTotal
if $attr_name ne $json_key 849 69261 70110
841  
842 # Declared in this very class before (k151): nothing is installed or
843 # registered a second time. The registry entry covers type, nested
844 # class, options, recorded required-ness and JSON key; the Moo spec
845 # adds whether required is enforced (1 and 'schema' record the same).
846 # An identical declaration changes nothing, not even the attribute list.
847 70110 ▶ if ($redeclared) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redeclared) 37 70073 70110
848 _croak_redeclared($where, $caller)
849 unless _same_value(\%info, $registered)
850 37 ▶ && $required == ($spec && $spec->{required} ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
unless _same_value(\%info, $registered) and $required == ($spec && $spec->{"required"} ? 1 : 0) 16 21 37
$spec && $spec->{'required'} ? : 1 21 22
Condition83%5/6
_same_value(\%info, $registered) and $required == ($spec && $spec->{"required"} ? 1 : 0)
!ll&&!rl&&r
15121
$spec and $spec->{"required"}
!ll&&!rl&&r
0211
MC/DC100%2/2
_same_value(\%info, $registered) and $required == ($spec && $spec->{"required"} ? 1 : 0)
_same_value(\%info, $registered) $required == ($spec && $spec->{"required"} ? 1 : 0)
MC/DC50%1/2
$spec and $spec->{"required"}
$spec $spec->{"required"}
Truth table100%3/3
A and B
A= _same_value(\%info, $registered)B= $required == ($spec && $spec->{"required"} ? 1 : 0)
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= $specB= $spec->{"required"}
ABresult
0X0
100
111
851 21 return;
852 }
853  
854 # Adopted (a redeclaration has returned above): see the preflight above
855 # for why there is nothing to install.
856 70073 ▶ return _register_field($caller, $attr_name, \%info) unless $install;
Branch: 1 branch
BranchTrueFalseTotal
unless $install 2879 67194 70073
857  
858 # Call Moo's has — use init_arg to map JSON key to Perl-safe attribute name
859 67194 my $has = $caller->can('has');
860 67194 my @coerce;
861 # Bool attributes: coerce \0/\1 refs, JSON booleans and 'true'/'false'
862 # strings to plain 0/1
863 67194 ▶ if ($info{is_bool}) {
Branch: 6 branches
BranchTrueFalseTotal
if ($info{'is_bool'}) { } 4445 62749 67194
elsif ($info{'is_array_of_bool'}) { } 36 62713 62749
elsif ($info{'is_object'}) { } 17960 44753 62713
elsif ($info{'is_array_of_objects'}) { } 6629 38124 44753
elsif ($info{'is_hash_of_objects'}) { } 347 37777 38124
elsif ($info{'is_hash_of_bool'}) { } 2 37775 37777
864 4445 @coerce = (coerce => \&_normalize_bool);
865 }
866 # Array of bool: the same normalization, per element. A bad element's
867 # message gets the index appended so it can be found in the array.
868 elsif ($info{is_array_of_bool}) {
869 49 ▶ @coerce = (coerce => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
870 49 ▶ return $_[0] unless ref $_[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 49 49
871 49 my $in = $_[0];
872 49 my @out;
873 49 for my $i (0 .. $#$in) {
874 138 push @out, eval { _normalize_bool($in->[$i]) };
875 138 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 3 135 138
876 3 $err =~ s/\n\z//;
877 3 die "$err at element $i\n";
878 }
879 }
880 46 return \@out;
881 36 });
882 }
883 # Named nested class, single (k100) -- body in _object_coercer above,
884 # because IO::K8s::Role::APIObject's `metadata` needs the same one.
885 #
886 # An inline struct takes this branch too, since is_inline_struct always
887 # sets is_object as well (k116). It used to have a branch of its own
888 # doing `$ic->new(%{$_[0]})`, which differs in exactly one visible way:
889 # `->new` stored the caller's inner containers by reference, while every
890 # other route into the same field -- inflate, struct_to_object,
891 # FROM_HASH -- copies them one level (k54). The same field of the same
892 # class therefore had both semantics depending on how it was built,
893 # which is not something a design chooses: the inline coercer landed
894 # with the inline-struct DSL in 2c0c6d02, k54 arrived five months later
895 # in 5ab95ca3 and touched _inflate_struct and TO_JSON without ever
896 # reaching it. Unified on the copying side, the newer and the tested
897 # one; t/29_inline_struct.t pins it, since nothing did before.
898 #
899 # The other differences the k100 review listed turned out not to be
900 # differences worth keeping either. load_class is not a blocker: Moo
901 # registers a generated inline-struct package in %INC (as '(eval NNN)'),
902 # so require_module finds it and returns. The opaque names
903 # fieldsV1/rawExtension/raw that _inflate_struct special-cases are
904 # theoretical here -- ManagedFieldsEntry is the only class shipping one
905 # and it already declares the opaque form. FROM_STRUCT never exists on a
906 # generated class. What is left is the Bool error wrapper, whose text
907 # now matches the one every other object-bearing field produces.
908 #
909 # Two things the three object branches here share:
910 # * one predicate, _object_builds, decides what is built: a plain
911 # hashref -- `ref eq 'HASH'` is false for a blessed one -- or, for a
912 # union class with FROM_STRUCT, any defined value that is not
913 # already of the class (k179). An object of the class is passed
914 # through, which is also what keeps IO::K8s::_inflate_struct from
915 # doing the work twice: it hands $class->new fully built objects and
916 # every one of them lands here.
917 # * anything else goes on unchanged and lets `isa` write the message
918 # -- a coercer never invents a type error of its own.
919 elsif ($info{is_object}) {
920 17960 @coerce = (coerce => _object_coercer($info{class}));
921 }
922 # Array of named nested classes: element-wise, and a failing element
923 # gets its index appended the same way the [Bool] coercer above does,
924 # so the culprit can be found. Scanned first and returned untouched
925 # when no element needs building -- the inflate path arrives here with
926 # an array of ready objects and should pay one check per element, not
927 # a fresh arrayref.
928 #
929 # An element is built by the rule _object_builds states for the single
930 # field: a hashref, or any defined value for a union class (k179).
931 elsif ($info{is_array_of_objects}) {
932 6629 my $oc = $info{class};
933 6629 my $builds = _object_builds($oc);
934 6806 ▶ @coerce = (coerce => sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
935 6806 ▶ return $_[0] unless ref $_[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 2 6804 6806
936 6804 my $in = $_[0];
937 6804 my $needed = 0;
938 6804 for my $elem (@$in) {
939 9118 ▶ next unless $builds->($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless &$builds($elem) 9044 74 9118
940 74 $needed = 1;
941 74 last;
942 }
943 6804 ▶ return $in unless $needed;
Branch: 1 branch
BranchTrueFalseTotal
unless $needed 6730 74 6804
944 74 my @out;
945 74 for my $i (0 .. $#$in) {
946 83 ▶ if ($builds->($in->[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$builds($in->[$i])) { } 80 3 83
947 80 push @out, eval {
948 80 IO::K8s::Role::Resource::_default_k8s()
949 ->_struct_to_object_expanded($oc, $in->[$i])
950 };
951 80 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 2 78 80
952 2 $err =~ s/\n\z//;
953 2 die "$err at element $i\n";
954 }
955 } else {
956 3 push @out, $in->[$i];
957 }
958 }
959 72 return \@out;
960 6629 });
961 }
962 # Hash of named nested classes: value-wise, with the key named on a
963 # failure. Same scan-first shortcut and element rule as the array form;
964 # `sort keys` in the building pass so several bad values still name a
965 # deterministic one, matching the unknown-field walk in
966 # IO::K8s::Role::Resource.
967 elsif ($info{is_hash_of_objects}) {
968 347 my $oc = $info{class};
969 347 my $builds = _object_builds($oc);
970 10512 ▶ @coerce = (coerce => sub {
Subroutine __ANON__: called CC=8 SCAR=20.9
971 10512 ▶ return $_[0] unless ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 10512 10512
972 10512 my $in = $_[0];
973 10512 my $needed = 0;
974 10512 for my $key (keys %$in) {
975 37264 ▶ next unless $builds->($in->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless &$builds($in->{$key}) 37258 6 37264
976 6 $needed = 1;
977 6 last;
978 }
979 10512 ▶ return $in unless $needed;
Branch: 1 branch
BranchTrueFalseTotal
unless $needed 10506 6 10512
980 6 my %out;
981 6 for my $key (sort keys %$in) {
982 8 ▶ if ($builds->($in->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$builds($in->{$key})) { } 8 0 8
983 8 $out{$key} = eval {
984 IO::K8s::Role::Resource::_default_k8s()
985 8 ->_struct_to_object_expanded($oc, $in->{$key})
986 };
987 8 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 1 7 8
988 1 $err =~ s/\n\z//;
989 1 die "$err at key '$key'\n";
990 }
991 } else {
992 0 $out{$key} = $in->{$key};
993 }
994 }
995 5 return \%out;
996 347 });
997 }
998 # Map of Bool (k191): a JSON boolean value -- what a decoded document
999 # carries -- becomes the 0/1 the value check accepts, through the same
1000 # normalization the Bool field uses. Only reference values are touched:
1001 # a plain scalar goes to `isa` as it is, so 'maybe' is still refused
1002 # rather than read as true. A map whose values are all plain scalars is
1003 # returned untouched.
1004 elsif ($info{is_hash_of_bool}) {
1005 7 ▶ @coerce = (coerce => sub {
Subroutine __ANON__: called CC=6 SCAR=28.6
1006 7 ▶ return $_[0] unless ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 7 7
1007 7 my $in = $_[0];
1008 7 ▶ return $in unless grep { ref } values %$in;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {ref $_;} values %$in 7 0 7
1009 0 my %out;
1010 0 for my $key (keys %$in) {
1011 0 my $v = $in->{$key};
1012 0 ▶ my $n = ref $v ? eval { _normalize_bool($v) } : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 0 0 0
1013 0 ▶ $out{$key} = defined $n ? $n : $v;
Branch: 1 branch
BranchTrueFalseTotal
defined $n ? : 0 0 0
1014 }
1015 0 return \%out;
1016 2 });
1017 }
1018 # A yielding role helper composed into this package goes first, so the
1019 # accessor can take its name -- after every check above, so a rejected
1020 # declaration never gets this far.
1021 67194 ▶ Package::Stash->new($caller)->remove_symbol('&'.$attr_name)
Branch: 1 branch
BranchTrueFalseTotal
if $yielding and $local 12 67182 67194
Condition66%2/3
$yielding and $local
!ll&&!rl&&r
67182012
MC/DC50%1/2
$yielding and $local
$yielding $local
Truth table66%2/3
A and B
A= $yieldingB= $local
ABresult
0X0
100
111
1022 if $yielding && $local;
1023  
1024 # A complete spec, never has('+name'). For a field redeclared under the
1025 # same JSON key in a subclass this is what makes nearest-wins real
1026 # (k144): the subclass's spec replaces the inherited one outright, so
1027 # the parent's coercion, required flag and init_arg go with it --
1028 # has('+name') would merge them back in, and refuses coerce => undef.
1029 # The parent class is left untouched.
1030 67194 ▶ $has->($attr_name, is => 'rw', isa => $isa, @coerce,
Branch: 3 branches
BranchTrueFalseTotal
$required ? : 7133 60061 67194
$attr_name ne $json_key ? : 848 66346 67194
@nullable_methods ? : 39 67155 67194
1031 ($required ? (required => 1) : ()),
1032 ($attr_name ne $json_key ? (init_arg => $json_key) : ()),
1033 (@nullable_methods
1034 ? (predicate => $nullable_methods[0], clearer => $nullable_methods[1])
1035 : ()),
1036 );
1037 67194 return _register_field($caller, $attr_name, \%info);
1038 }
1039  
1040 # The predicate and clearer a nullable field gets (k158), in that order:
1041 # has_<accessor> and clear_<accessor>, named after the Perl attribute, the
1042 # same name the accessor itself has. IO::K8s::Role::Resource's TO_JSON and
1043 # IO::K8s::Role::SpecBuilder's spec_delete call them through this too, so
1044 # the names are spelled in one place.
1045 182 ▶sub _nullable_methods {
Subroutine _nullable_methods: called CC=1 SCAR=0.0
1046 182 my ($attr_name) = @_;
1047 182 return ('has_'.$attr_name, 'clear_'.$attr_name);
1048 }
1049  
1050 # An inline struct declared again in the class that declared it (k151). Its
1051 # inner class exists already, and building it a second time would add a new
1052 # field to it, keep a dropped one and set the class up twice -- so it is
1053 # compared instead: the same field names, and every field again an
1054 # identical declaration in the inner class, which the same rule makes a
1055 # no-op or refuses. Refused before any field is looked at when the first
1056 # declaration was not this inline struct or the names differ, so a refusal
1057 # leaves the inner class as it was.
1058 11 ▶sub _redeclare_inline_struct {
Subroutine _redeclare_inline_struct: called CC=7 SCAR=19.7
1059 11 my ($caller, $where, $registered, $inner_class, $fields) = @_;
1060 11 ▶ my $had = $_attr_registry{$inner_class} // {};
Condition100%2/2
$IO::K8s::Resource::_attr_registry{$inner_class} // {}
l!l
101
MC/DC100%1/1
$IO::K8s::Resource::_attr_registry{$inner_class} // {}
$IO::K8s::Resource::_attr_registry{$inner_class}
Truth table100%2/2
A
A= $IO::K8s::Resource::_attr_registry{$inner_class}
Aresult
11
00
1061 11 ▶ my @had = sort map { $had->{$_}{json_key} // $_ } keys %$had;
Condition33%1/3
$had->{$_}{"json_key"} // $_
l!l&&r!l&&!r
0220
MC/DC0%0/2
$had->{$_}{"json_key"} // $_
$had->{$_}{"json_key"} $_
Truth table33%1/3
A // B
A= $had->{$_}{"json_key"}B= $_
ABresult
1X1
011
000
1062 _croak_redeclared($where, $caller)
1063 unless $registered->{is_inline_struct}
1064 11 ▶ && $registered->{class} eq $inner_class
Branch: 1 branch
BranchTrueFalseTotal
unless $registered->{"is_inline_struct"} and $registered->{"class"} eq $inner_class and join("\0", @had) eq join("\0", sort(keys %$fields)) 3 8 11
Condition83%5/6
$registered->{"is_inline_struct"} and $registered->{"class"} eq $inner_class and join("\0", @had) eq join("\0", sort(keys %$fields))
!ll&&!rl&&r
128
$registered->{"is_inline_struct"} and $registered->{"class"} eq $inner_class
!ll&&!rl&&r
1010
MC/DC66%2/3
$registered->{"is_inline_struct"} and $registered->{"class"} eq $inner_class and join("\0", @had) eq join("\0", sort(keys %$fields))
$registered->{"is_inline_struct"} $registered->{"class"} eq $inner_class join("\0", @had) eq join("\0", sort(keys %$fields))
Truth table75%3/4
A and B and C
A= $registered->{"is_inline_struct"}B= $registered->{"class"} eq $inner_classC= join("\0", @had) eq join("\0", sort(keys %$fields))
ABCresult
0XX0
10X0
1100
1111
1065 && join("\0", @had) eq join("\0", sort keys %$fields);
1066 8 __PACKAGE__->_k8s($inner_class, $_, $fields->{$_}) for sort keys %$fields;
1067 4 return;
1068 }
1069  
1070 19 ▶sub _croak_redeclared {
Subroutine _croak_redeclared: called CC=1 SCAR=0.0
1071 19 my ($where, $caller) = @_;
1072 19 croak "k8s: $where is already declared in $caller with a different type, "
1073 . 'options or required-ness; declare each field once per class';
1074 }
1075  
1076 # Deep equality of two registry entries (k151): arrays and hashes element
1077 # by element, everything else by what it stringifies to within the same
1078 # ref type -- a plain scalar as itself, a Regexp as its pattern with its
1079 # flags, a JSON boolean default as 0 or 1.
1080 108 ▶sub _same_value {
Subroutine _same_value: called CC=15 SCAR=27.2
1081 108 my ($x, $y) = @_;
1082 108 ▶ return !defined $y unless defined $x;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 108 108
1083 108 ▶ return 0 unless defined $y && ref $x eq ref $y;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $y and ref $x eq ref $y 0 108 108
Condition33%1/3
defined $y and ref $x eq ref $y
!ll&&!rl&&r
00108
MC/DC0%0/2
defined $y and ref $x eq ref $y
defined $y ref $x eq ref $y
Truth table33%1/3
A and B
A= defined $yB= ref $x eq ref $y
ABresult
0X0
100
111
1084 108 ▶ my $type = reftype($x) // '';
Condition100%2/2
builtin::reftype($x) // ""
l!l
5553
MC/DC100%1/1
builtin::reftype($x) // ""
builtin::reftype($x)
Truth table100%2/2
A
A= builtin::reftype($x)
Aresult
11
00
1085 108 ▶ if ($type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "ARRAY") 5 103 108
1086 5 ▶ return 0 unless @$x == @$y;
Branch: 1 branch
BranchTrueFalseTotal
unless @$x == @$y 1 4 5
1087 4 for my $i (0 .. $#$x) {
1088 8 ▶ return 0 unless _same_value($x->[$i], $y->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless _same_value($x->[$i], $y->[$i]) 1 7 8
1089 }
1090 3 return 1;
1091 }
1092 103 ▶ if ($type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "HASH") 48 55 103
1093 48 ▶ return 0 unless keys %$x == keys %$y;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$x == keys %$y 3 45 48
1094 45 for my $key (keys %$x) {
1095 69 ▶ return 0 unless exists $y->{$key} && _same_value($x->{$key}, $y->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $y->{$key} and _same_value($x->{$key}, $y->{$key}) 17 52 69
Condition100%3/3
exists $y->{$key} and _same_value($x->{$key}, $y->{$key})
!ll&&!rl&&r
61152
MC/DC100%2/2
exists $y->{$key} and _same_value($x->{$key}, $y->{$key})
exists $y->{$key} _same_value($x->{$key}, $y->{$key})
Truth table100%3/3
A and B
A= exists $y->{$key}B= _same_value($x->{$key}, $y->{$key})
ABresult
0X0
100
111
1096 }
1097 28 return 1;
1098 }
1099 55 ▶ return "$x" eq "$y" ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
"$x" eq "$y" ? : 50 5 55
1100 }
1101  
1102 # Record a declared field, only once its attribute is in place (k144): a
1103 # has() that dies must not leave a registry entry behind that no attribute
1104 # backs, nor a name in the attribute list or a merged view already rebuilt
1105 # around it.
1106 70073 ▶sub _register_field {
Subroutine _register_field: called CC=1 SCAR=0.0
1107 70073 my ($caller, $attr_name, $info) = @_;
1108 # Copy the values, not the reference
1109 70073 $_attr_registry{$caller}{$attr_name} = { %$info };
1110 157 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1111 70073 push @{"${caller}::_k8s_attributes"}, $attr_name;
1112  
1113 # The merged @ISA views in IO::K8s::Role::Resource are cached; a new
1114 # registration must not leave a stale merged view behind.
1115 70073 IO::K8s::Role::Resource::_invalidate_k8s_attr_cache($caller);
1116 70073 return;
1117 }
1118  
1119 157 1;
1120  
1121 __END__
« blib/lib/IO/K8s/Role/Resource.pm ↑ Summary blib/lib/IO/K8s/AutoGen.pm »