blib/lib/IO/K8s/Resource.pm

filter: Statementstmt 96.6% 487 / 504 Branchbran 89.3% 286 / 320 Conditioncond 79.8% 139 / 174 MC/DCmcdc 62.1% 64 / 103 Subroutinesub 100% 62 / 62 Podpod 0.0% 0 / 1 totaltotal 89.1% 1038 / 1164 CC 240 SCAR 38.1
CC
240
Cov
91%
_declare_field
116.7
_constrain
19.0
__ANON__
17.5
CRAP 45.2
« blib/lib/IO/K8s/AutoGen.pm ↑ Summary blib/lib/IO/K8s/Role/APIObject.pm »
1 package IO::K8s::Resource;
2 # ABSTRACT: Base class for all Kubernetes resources
3 160 our $VERSION = '1.112';
4 160 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 160 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 160 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 160 ▶use Moo ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 160 ▶use Moo::Role ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 160 ▶use Import::Into;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 160 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 160 ▶use Types::Standard qw( ArrayRef Bool HashRef InstanceOf Int Maybe Num Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 160 ▶use IO::K8s::Types qw( IntOrStr Quantity Time );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 160 ▶use IO::K8s::Role::Resource ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 160 ▶use namespace::clean ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 160 ▶use Scalar::Util qw( blessed reftype looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 160 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 160 ▶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 160 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 160 our $STRICT = 0;
32  
33 # Class name expansion map
34 160 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 160 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 160 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 160 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 160 my %FIELD_OPTIONS = map { $_ => 1 } qw(
140 required default enum minimum maximum pattern description nullable
141 preserve_unknown
142 );
143 160 my %NUMERIC_KIND = (Int => 1, Num => 1);
144 160 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 17721 ▶sub _constrain {
Subroutine _constrain: called CC=19 SCAR=29.5
151 17721 my ($base, $kind, $opts, $where) = @_;
152 17721 my $type = $base;
153  
154 17721 ▶ if (exists $opts->{enum}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"enum"}) 763 16958 17721
155 croak "k8s: 'enum' for $where must be a non-empty arrayref"
156 763 ▶ unless ref $opts->{enum} eq 'ARRAY' && @{ $opts->{enum} };
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts->{"enum"} eq "ARRAY" and @{$opts->{"enum"};} 1 762 763
Condition66%2/3
ref $opts->{"enum"} eq "ARRAY" and @{$opts->{"enum"};}
!ll&&!rl&&r
01762
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 762 ▶ croak "k8s: 'enum' is not allowed on a Bool field ($where)" if $kind eq 'Bool';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "Bool" 1 761 762
158 761 my %allowed = map { $_ => 1 } @{ $opts->{enum} };
159 croak "k8s: 'enum' for $where has duplicate entries"
160 761 ▶ unless @{ $opts->{enum} } == keys %allowed;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$opts->{"enum"};} == keys %allowed 1 760 761
161 760 my $list = join ', ', @{ $opts->{enum} };
162 $type = $type->create_child_type(
163 display_name => $type->display_name . '[enum]',
164 2709 ▶ constraint => sub { defined $_ && exists $allowed{$_} },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and exists $allowed{$_} 2709 0 2709
165 11 ▶ message => sub { "Value \"$_\" is not one of: $list" },
Subroutine __ANON__: called CC=1 SCAR=0.0
166 760 );
167 }
168  
169 17718 ▶ if (exists $opts->{minimum} || exists $opts->{maximum}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"minimum"} or exists $opts->{"maximum"}) 427 17291 17718
Condition100%3/3
exists $opts->{"minimum"} or exists $opts->{"maximum"}
l!l&&r!l&&!r
418917291
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 427 ▶ unless $NUMERIC_KIND{$kind};
Branch: 1 branch
BranchTrueFalseTotal
unless $NUMERIC_KIND{$kind} 1 426 427
172 426 my ($min, $max) = @{$opts}{qw(minimum maximum)};
173 426 for my $bound ($min, $max) {
174 851 ▶ croak "k8s: 'minimum' and 'maximum' for $where must be numbers"
Branch: 1 branch
BranchTrueFalseTotal
if defined $bound and not &looks_like_number($bound) 1 850 851
Condition100%3/3
defined $bound and not &looks_like_number($bound)
!ll&&!rl&&r
2615891
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 425 ▶ 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 424 425
Condition100%6/6
defined $min and defined $max and $min > $max
!ll&&!rl&&r
2611631
defined $min and defined $max
!ll&&!rl&&r
9252164
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 1302 ▶ constraint => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
182 1302 ▶ (!defined $min || $_ >= $min) && (!defined $max || $_ <= $max)
Branch: 1 branch
BranchTrueFalseTotal
!defined($min) || $_ >= $min and !defined($max) || $_ <= $max 1293 9 1302
Condition100%9/9
(!defined($min) || $_ >= $min) && (!defined($max) || $_ <= $max)
!ll&&!rl&&r
9241269
not defined $min or $_ >= $min
l!l&&r!l&&!r
2712669
not defined $max or $_ <= $max
l!l&&r!l&&!r
60466524
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 424 );
190 }
191  
192 17715 ▶ if (exists $opts->{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"pattern"}) 688 17027 17715
193 croak "k8s: 'pattern' needs a string field, not $kind ($where)"
194 688 ▶ unless $STRING_KIND{$kind};
Branch: 1 branch
BranchTrueFalseTotal
unless $STRING_KIND{$kind} 1 687 688
195 my $re = ref $opts->{pattern} eq 'Regexp'
196 ? $opts->{pattern}
197 687 ▶ : eval { my $p = $opts->{pattern}; qr/$p/ };
Branch: 1 branch
BranchTrueFalseTotal
ref $opts->{'pattern'} eq 'Regexp' ? : 595 92 687
198 687 ▶ croak "k8s: 'pattern' for $where does not compile: $@" unless $re;
Branch: 1 branch
BranchTrueFalseTotal
unless $re 1 686 687
199 $type = $type->create_child_type(
200 display_name => $type->display_name . '[pattern]',
201 4921 ▶ constraint => sub { defined $_ && $_ =~ $re },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $_ =~ /$re/ 4921 0 4921
202 14 ▶ message => sub { "Value \"$_\" does not match the pattern $re" },
Subroutine __ANON__: called CC=1 SCAR=0.0
203 686 );
204 }
205  
206 17713 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 12835 ▶sub _reject_value_options {
Subroutine _reject_value_options: called CC=5 SCAR=16.1
212 12835 my ($opts, $where) = @_;
213 12835 ▶ croak "k8s: 'enum' needs a scalar field ($where)" if exists $opts->{enum};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"enum"} 1 12834 12835
214 croak "k8s: 'minimum' and 'maximum' need a scalar field ($where)"
215 12834 ▶ if exists $opts->{minimum} || exists $opts->{maximum};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"minimum"} or exists $opts->{"maximum"} 1 12833 12834
Condition66%2/3
exists $opts->{"minimum"} or exists $opts->{"maximum"}
l!l&&r!l&&!r
1012833
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 12833 ▶ croak "k8s: 'pattern' needs a scalar field ($where)" if exists $opts->{pattern};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"pattern"} 1 12832 12833
217 }
218  
219 6513 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
220 6513 my $class = shift;
221 6513 my $caller = caller;
222 6513 $class->_setup_class($caller);
223 6513 $class->_import_map_types($caller);
224 6513 $class->_sweep_sugar($caller, 'scope');
225 }
226  
227 # HashRef, ArrayRef, InstanceOf and Opaque for map declarations (k191, k194):
228 # `k8s data => HashRef[Str]`, `k8s extra => HashRef[ArrayRef[Str]]`,
229 # `k8s m => HashRef[ArrayRef[InstanceOf['Some::Class']]]`, `k8s raw => Opaque`.
230 # Only here, where a class is compiled from source --
231 # _setup_class also serves inline structs and IO::K8s::AutoGen, which build
232 # their fields at runtime and never name a type in code. All four names are
233 # swept from the package again once the scope that said `use` has finished
234 # compiling (namespace::clean, the explicit-list form): the declarations
235 # compiled against them keep working, but neither becomes a method of every
236 # shipped class, which is what k118 took HashRef off (t/27_no_import_leak.t).
237 6513 ▶sub _import_map_types {
Subroutine _import_map_types: called CC=1 SCAR=0.0
238 6513 my ($class, $target) = @_;
239 6513 Types::Standard->import::into($target, qw( ArrayRef HashRef InstanceOf ));
240 6513 IO::K8s::Types->import::into($target, qw( Opaque ));
241 6513 namespace::clean->import(-cleanee => $target, qw( ArrayRef HashRef InstanceOf Opaque ));
242 }
243  
244 # The compile-time sugar _setup_class gives a class: the Moo keywords, the
245 # `k8s` declaration function and the type names a `k8s` line is written
246 # with. It exists so the declarations of a class can be written; once they
247 # are done it has no business being a method (k200). Left in place, every
248 # class `can` has / with / extends / before / after / around / k8s / Str /
249 # Int / ..., and a schema property of one of those names (Kong's
250 # ordering.after, ordering.before) had its accessor refused as a collision.
251 160 my @SUGAR = qw(
252 extends has with before after around k8s
253 Str Int Bool Num IntOrStr Quantity Time
254 );
255  
256 # The `has` Moo installed for each class, kept so _declare_field can still
257 # declare an attribute after the keyword itself has been swept away. It is
258 # the closure Moo made for that very package, so it behaves exactly as the
259 # imported one did.
260 160 my %_has;
261  
262 7040 ▶sub _setup_class {
Subroutine _setup_class: called CC=1 SCAR=0.0
263 7040 my ($class, $target) = @_;
264 7040 Moo->import::into($target);
265 7040 Types::Standard->import::into($target, qw( Str Int Bool Num ));
266 7040 IO::K8s::Types->import::into($target, qw( IntOrStr Quantity Time ));
267 7040 Moo::Role->apply_roles_to_package($target, 'IO::K8s::Role::Resource');
268 7040 my $stash = Package::Stash->new($target);
269 7040 ▶ $stash->add_symbol('&k8s', sub { $class->_k8s($target, @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
270 7040 $_has{$target} = $target->can('has');
271 }
272  
273 # Remove the sugar again. A class compiled from source (`use
274 # IO::K8s::Resource`) keeps it until the scope that said `use` has finished
275 # compiling -- namespace::clean, the same mechanism as the HashRef/Opaque
276 # sweep above -- so the file's own `k8s`, `with` and `has` lines, compiled
277 # against it, keep working. A class built at runtime (inline struct,
278 # IO::K8s::AutoGen) has no source to compile: its fields are declared by
279 # calling IO::K8s::Resource->_k8s directly, so the sugar goes at once.
280 7017 ▶sub _sweep_sugar {
Subroutine _sweep_sugar: called CC=4 SCAR=13.9
281 7017 my ($class, $target, $when) = @_;
282 7017 ▶ if ($when && $when eq 'scope') {
Branch: 1 branch
BranchTrueFalseTotal
if ($when and $when eq 'scope') { } 6513 504 7017
Condition66%2/3
$when and $when eq "scope"
!ll&&!rl&&r
50406513
MC/DC50%1/2
$when and $when eq "scope"
$when $when eq "scope"
Truth table66%2/3
A and B
A= $whenB= $when eq "scope"
ABresult
0X0
100
111
283 6513 namespace::clean->import(-cleanee => $target, @SUGAR);
284 } else {
285 504 my $stash = Package::Stash->new($target);
286 504 $stash->remove_symbol('&'.$_) for grep { $stash->has_symbol('&'.$_) } @SUGAR;
287 }
288 }
289  
290 12525 ▶sub _expand_class {
Subroutine _expand_class: called CC=6 SCAR=17.9
291 12525 my ($short) = @_;
292  
293 # +FullClassName - strip + and use as-is
294 12525 ▶ return substr($short, 1) if $short =~ /^\+/;
Branch: 1 branch
BranchTrueFalseTotal
if $short =~ /^\+/ 4550 7975 12525
295  
296 # Already fully qualified?
297 7975 ▶ return $short if $short =~ /^IO::K8s::/;
Branch: 1 branch
BranchTrueFalseTotal
if $short =~ /^IO::K8s::/ 1 7974 7975
298  
299 # Prefix match against %_class_prefix: try the longest key first so that
300 # CamelCase prefixes like KubeAggregator win over a hypothetical shorter
301 # substring. Anything in the map is canonical — the lookup IS the source
302 # of truth, the regex is not. Unknown short names still fall through to
303 # the IO::K8s::Api default so this stays backwards compatible.
304 7974 ▶ if ($short =~ /^([A-Z]\w*)::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($short =~ /^([A-Z]\w*)::/) 7973 1 7974
305 7973 for my $prefix (sort { length($b) <=> length($a) } keys %_class_prefix) {
306 200093 ▶ next unless $short =~ /^\Q$prefix\E::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $short =~ /^\Q$prefix\E::/ 192139 7954 200093
307 7954 $short =~ s/^\Q$prefix\E:://;
308 7954 return $_class_prefix{$prefix} . '::' . $short;
309 }
310 }
311  
312 # Default: assume it's under IO::K8s::Api
313 20 return "IO::K8s::Api::$short";
314 }
315  
316 # A read-only view of %_class_prefix (short prefix -> full namespace),
317 # keyed the same as the map _expand_class walks above. This started as a
318 # lexical 'my' with a single reader inside this file; IO::K8s::CRD::Emitter
319 # is the first outside consumer, building the REVERSE map (full namespace
320 # -> short prefix) from it so a rendered class reference uses the short
321 # form a hand-written class would ('Traefik::V1alpha1::Middleware',
322 # 'Core::V1::PodTemplateSpec') instead of always spelling out '+Full::Name'.
323 8 ▶sub class_prefixes { \%_class_prefix }
Subroutine class_prefixes: called CC=1 SCAR=0.0
Pod undocumented
324  
325 44808 ▶sub _is_type_tiny {
Subroutine _is_type_tiny: called CC=2 SCAR=7.1
326 44808 my ($obj) = @_;
327 44808 ▶ return blessed($obj) && $obj->isa('Type::Tiny');
Condition66%2/3
builtin::blessed($obj) && $obj->isa('Type::Tiny')
!ll&&!rl&&r
27417017391
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
328 }
329  
330 # Sanitize JSON field names into valid Perl identifiers for Moo attributes:
331 # every character outside [A-Za-z0-9_] becomes '_' -- $ref -> _ref,
332 # $schema -> _schema, x-kubernetes-foo -> x_kubernetes_foo, x.y/z -> x_y_z,
333 # and the same for any other character a schema's property key happens to
334 # carry, not only the '-', '.' and '/' realistic CRD schemas mostly produce.
335 # This matters beyond cosmetics: `has` is always called with an `isa` type
336 # constraint here (see _k8s below), which routes through Sub::Quote's
337 # inlined accessor generation, and that dies naming the attribute as an
338 # invalid sub name for any of these characters -- the same failure
339 # IO::K8s::AutoGen::_class_segments exists to avoid for package segments,
340 # just one call site over (the attribute name here, not a class name; the
341 # two use the same blanket rule). A CRD schema is free to use any of these
342 # characters in a property key; the original is preserved on the wire via
343 # the caller's existing json_key/init_arg mapping below whenever this
344 # changes the name.
345 30535 ▶sub _sanitize_attr_name {
Subroutine _sanitize_attr_name: called CC=2 SCAR=6.9
346 30535 my ($name) = @_;
347 30535 ▶ return $name unless $name =~ /[^a-zA-Z0-9_]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /[^a-zA-Z0-9_]/ 30120 415 30535
348 415 (my $safe = $name) =~ s/[^A-Za-z0-9_]/_/g;
349 415 return $safe;
350 }
351  
352 # The one boolean normalization in the distribution: everything that can
353 # arrive on a Bool attribute, reduced to a plain 0/1 -- or undef, meaning
354 # "leave the field unset". Used by the Bool and [Bool] coercers below and,
355 # before the constructor even runs, by the is_bool branch of
356 # IO::K8s::_inflate_struct -- those two used to disagree (k37).
357 #
358 # Two traps, both of which silently flip false into true:
359 # * every reference is true in Perl, so \0 (the bare false idiom) and a
360 # JSON::PP::Boolean (a blessed ref to 0) must be dereferenced, not tested;
361 # * 'false' is a non-empty string and therefore true, so the strings have to
362 # be spelled out rather than left to truthiness.
363 #
364 # Anything that cannot mean true or false dies (k42): a non-scalar
365 # reference, and a scalar ref that dereferences to yet another reference
366 # (\\0 used to come out silently true). reftype, not ref, so that blessed
367 # scalar refs -- JSON::PP::Boolean, boolean.pm, Types::Serialiser, any
368 # bless \(my $x = 0) -- keep working. Messages end in \n deliberately:
369 # they are diagnostics, and the callers (Moo's coercion wrapper, the
370 # eval/rethrow in _inflate_struct) attach the attribute context.
371 19333 ▶sub _normalize_bool {
Subroutine _normalize_bool: called CC=8 SCAR=20.8
372 19333 my ($value) = @_;
373 19333 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 8006 11327 19333
374 8006 my $reftype = reftype($value);
375 8006 ▶ 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 7994 8006
Condition100%3/3
$reftype eq "SCALAR" or $reftype eq "REF"
l!l&&r!l&&!r
7987712
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
376 unless $reftype eq 'SCALAR' || $reftype eq 'REF';
377 7994 $value = $$value;
378 7994 ▶ die 'Bool scalar ref dereferenced to another reference ('
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 7 7987 7994
379 . ref($value) . "), not a boolean\n" if ref $value;
380 }
381 # Explicit undef stays undef: the attribute is Maybe[Bool], TO_JSON
382 # omits undef, and "no value" must not turn into an explicit false on
383 # the wire (k48). `return undef`, not bare `return` -- in the
384 # [Bool] coercer's list context a bare return would drop the element.
385 19314 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2940 16374 19314
386 16374 ▶ return 0 if lc($value) eq 'false';
Branch: 1 branch
BranchTrueFalseTotal
if lc $value eq "false" 27 16347 16374
387 16347 ▶ return $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 9572 6775 16347
388 }
389  
390 # The coercion a single object-bearing field gets (k100): a plain hashref
391 # becomes an instance of $class_name, built by the very call FROM_HASH
392 # makes, so bool normalization and the D1 unknown-field policy are the ones
393 # the inflate path already uses instead of a second, subtly different way of
394 # building the same object. See the is_object branch of _k8s below for why
395 # one `ref eq 'HASH'` test is the whole guard.
396 #
397 # Named rather than written inline in that branch for the same reason
398 # _normalize_bool above is: a second caller needs exactly this, and the two
399 # must not drift. That caller is IO::K8s::Role::APIObject, whose `metadata`
400 # is a plain `has` -- the role composes before any k8s declaration runs, and
401 # metadata is then only registered through _k8s_adopt, which never calls
402 # has(), so a coercer installed here would never reach it (k115). The role
403 # therefore declares metadata with this coercion from the start.
404 #
405 # $class_name is captured; IO::K8s::Role::Resource::_default_k8s() is
406 # touched at COERCION time only, never while the attribute is installed --
407 # _k8s runs at compile time of every one of the ~850 classes, and
408 # _default_k8s requires IO::K8s, which loads this file.
409 7936 ▶sub _object_coercer {
Subroutine _object_coercer: called CC=1 SCAR=0.0
410 7936 my ($class_name) = @_;
411 7936 my $builds = _object_builds($class_name);
412 23246 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
413 23246 ▶ return $_[0] unless $builds->($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless &$builds($_[0]) 23096 150 23246
414 150 return IO::K8s::Role::Resource::_default_k8s()
415 ->_struct_to_object_expanded($class_name, $_[0]);
416 7936 };
417 }
418  
419 # Whether a value handed to an object-bearing field of $class_name is built
420 # into that class by the object coercers, as a predicate the three of them
421 # share -- the single field above and, element by element, the array and
422 # hash forms in _declare_field.
423 #
424 # A plain hashref always is. A class with FROM_STRUCT -- the apiextensions
425 # union classes, which serialize as the bare value they hold -- takes every
426 # defined value (k179): inflation hands it anything, so values => [1, 2]
427 # or enum => ['a'] inflated fine while ->new, the setter and spec_set died
428 # on the InstanceOf check for the very same values. An object already of
429 # the class is left alone, and undef stays "no value". Every other class
430 # keeps the k146 rule: anything but a hashref goes on to `isa`, which
431 # refuses it.
432 #
433 # FROM_STRUCT is only known once the class is loaded, and the coercer is
434 # installed long before (see _object_coercer). It is asked the first time
435 # a value is not a hashref, and the answer is kept; a class that does not
436 # load is asked again next time and meanwhile counts as an ordinary class,
437 # so such a value still meets the type check it met before.
438 11155 ▶sub _object_builds {
Subroutine _object_builds: called CC=1 SCAR=0.0
439 11155 my ($class_name) = @_;
440 11155 my $from_struct;
441 70109 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
442 70109 my ($value) = @_;
443 70109 ▶ return 0 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 18 70091 70109
444 70091 ▶ return 1 if ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 293 69798 70091
445 69798 ▶ $from_struct //= _takes_any_value($class_name);
Condition66%2/3
$from_struct //= _takes_any_value($class_name)
l!l&&r!l&&!r
6528245160
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
446 69798 ▶ return 0 unless $from_struct;
Branch: 1 branch
BranchTrueFalseTotal
unless $from_struct 58993 10805 69798
447 10805 ▶ return blessed($value) && $value->isa($class_name) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) && $value->isa($class_name) ? : 10777 28 10805
Condition100%3/3
builtin::blessed($value) and $value->isa($class_name)
!ll&&!rl&&r
26210777
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
448 11155 };
449 }
450  
451 # True when $class_name inflates through FROM_STRUCT (k179), false when it
452 # does not, nothing when it cannot be loaded.
453 4516 ▶sub _takes_any_value {
Subroutine _takes_any_value: called CC=3 SCAR=11.0
454 4516 my ($class_name) = @_;
455 4516 ▶ return unless eval {
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { IO::K8s::Role::Resource::_default_k8s()->load_class($class_name); 1 } } 0 4516 4516
456 4516 IO::K8s::Role::Resource::_default_k8s()->load_class($class_name);
457 4516 1;
458 };
459 4516 ▶ return $class_name->can('FROM_STRUCT') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$class_name->can('FROM_STRUCT') ? : 91 4425 4516
460 }
461  
462 # A map whose values have a type (k191): HashRef[X], and the { X => 1 } form
463 # that means the same for every X but Str. Sets the registry flag in $info
464 # and returns the field's type. X is a Type::Tiny value type: a scalar kind
465 # by its name -- Str (the strict string map), Int, Num, Bool, IntOrStr,
466 # Quantity, Time -- or InstanceOf[Class], the map of objects. The value
467 # constraint is X itself, so HashRef[Time] checks what { Time => 1 } checks
468 # and a lenient Str-based Time (IO::K8s::AutoGen's) stays lenient.
469 944 ▶sub _typed_map {
Subroutine _typed_map: called CC=6 SCAR=18.1
470 944 my ($info, $value_type, $opts, $where) = @_;
471 # HashRef[ArrayRef[X]]: the map of lists (k194)
472 944 ▶ return _array_map($info, $value_type->type_parameter, $opts, $where)
Branch: 1 branch
BranchTrueFalseTotal
if $value_type->is_parameterized and $value_type->parameterized_from->name eq "ArrayRef" 72 872 944
Condition66%2/3
$value_type->is_parameterized and $value_type->parameterized_from->name eq "ArrayRef"
!ll&&!rl&&r
872072
MC/DC50%1/2
$value_type->is_parameterized and $value_type->parameterized_from->name eq "ArrayRef"
$value_type->is_parameterized $value_type->parameterized_from->name eq "ArrayRef"
Truth table66%2/3
A and B
A= $value_type->is_parameterizedB= $value_type->parameterized_from->name eq "ArrayRef"
ABresult
0X0
100
111
473 if $value_type->is_parameterized
474 && $value_type->parameterized_from->name eq 'ArrayRef';
475 872 my $kind = $value_type->name;
476 872 ▶ if ($kind eq 'Str') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "Str") 670 202 872
477 670 $info->{is_hash_of_str} = 1;
478 670 return HashRef[ _constrain($value_type, 'Str', $opts, $where) ];
479 }
480 202 ▶ if (my $vt = $HASH_VALUE_TYPES{$kind}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $vt = $HASH_VALUE_TYPES{$kind}) 201 1 202
481 201 $info->{ $vt->{flag} } = 1;
482 201 return HashRef[ _constrain($value_type, $kind, $opts, $where) ];
483 }
484 1 ▶ return _object_map($info, $value_type->class, $opts, $where)
Branch: 1 branch
BranchTrueFalseTotal
if $value_type->isa("Type::Tiny::Class") 1 0 1
485 if $value_type->isa('Type::Tiny::Class');
486 0 croak "k8s: cannot interpret the value type of $where: HashRef["
487 . $value_type->display_name . '] (a scalar type, or InstanceOf[Class])';
488 }
489  
490 # The map of lists (k194), map[string][]X upstream: HashRef[ArrayRef[Str]]
491 # (every element a JSON string, the strict form) and
492 # HashRef[ArrayRef[InstanceOf[Class]]] (every element an object). Other
493 # element types are not modelled; a field of that shape stays Opaque.
494 72 ▶sub _array_map {
Subroutine _array_map: called CC=3 SCAR=11.1
495 72 my ($info, $elem_type, $opts, $where) = @_;
496 72 ▶ if ($elem_type->name eq 'Str') {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem_type->name eq "Str") 32 40 72
497 32 $info->{is_hash_of_str_arrays} = 1;
498 32 return HashRef[ ArrayRef[ _constrain($elem_type, 'Str', $opts, $where) ] ];
499 }
500 40 ▶ if ($elem_type->isa('Type::Tiny::Class')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem_type->isa("Type::Tiny::Class")) 40 0 40
501 40 $info->{is_hash_of_object_arrays} = 1;
502 40 $info->{class} = $elem_type->class;
503 40 _reject_value_options($opts, $where);
504 40 return HashRef[ ArrayRef[ InstanceOf[ $elem_type->class ] ] ];
505 }
506 0 croak "k8s: cannot interpret the element type of $where: HashRef[ArrayRef["
507 . $elem_type->display_name . ']] (Str, or InstanceOf[Class])';
508 }
509  
510 # The map of objects: { Class => 1 } and HashRef[InstanceOf[Class]].
511 188 ▶sub _object_map {
Subroutine _object_map: called CC=1 SCAR=0.0
512 188 my ($info, $full_class, $opts, $where) = @_;
513 188 $info->{is_hash_of_objects} = 1;
514 188 $info->{class} = $full_class;
515 188 _reject_value_options($opts, $where);
516 188 return HashRef[InstanceOf[$full_class]];
517 }
518  
519 78 ▶sub _generate_inline_struct {
Subroutine _generate_inline_struct: called CC=2 SCAR=6.9
520 78 my ($class_name, $fields) = @_;
521 78 __PACKAGE__->_setup_class($class_name);
522 78 __PACKAGE__->_sweep_sugar($class_name);
523 78 for my $field_name (sort keys %$fields) {
524 226 __PACKAGE__->_k8s($class_name, $field_name, $fields->{$field_name});
525 }
526 }
527  
528 # The nearest registry entry for a Perl attribute name: $class's own, else
529 # the first one found walking @ISA depth-first, left to right -- the order
530 # IO::K8s::Role::Resource::_merged_attr_info resolves in, so this answers
531 # for the same view TO_JSON and FROM_HASH read. Returns the declaring class
532 # and its entry, or nothing. Uncached: it runs while classes are still
533 # being declared, and populating the role's merged-view cache from here
534 # would be a side effect of a declaration that may yet be rejected.
535 118851 ▶sub _nearest_registration {
Subroutine _nearest_registration: called CC=5 SCAR=16.1
536 118851 my ($class, $attr_name) = @_;
537 118851 my $own = $_attr_registry{$class};
538 118851 ▶ return ($class, $own->{$attr_name}) if $own && $own->{$attr_name};
Branch: 1 branch
BranchTrueFalseTotal
if $own and $own->{$attr_name} 103 118748 118851
Condition100%3/3
$own and $own->{$attr_name}
!ll&&!rl&&r
7165947089103
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
539 160 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
540 118748 for my $parent (@{"${class}::ISA"}) {
541 59384 my @found = _nearest_registration($parent, $attr_name);
542 59384 ▶ return @found if @found;
Branch: 1 branch
BranchTrueFalseTotal
if @found 12 59372 59384
543 }
544 118736 return;
545 }
546  
547 # The role that $class's method $attr_name comes from, when that role lists
548 # the name in its %YIELDS_TO_K8S_FIELD -- a role helper meant to give way to
549 # a declared wire field of the same name (IO::K8s::Role::APIObject's
550 # conditions, for ComponentStatus). Found through the method's own name
551 # rather than a list of classes, and checked against the role itself: the
552 # code must be the role's sub, and $class must do the role. Anything else --
553 # a method the class wrote, one a role does not declare as yielding, a
554 # modifier-wrapped helper -- returns nothing and stays a collision.
555 34 ▶sub _yielding_role_helper {
Subroutine _yielding_role_helper: called CC=10 SCAR=24.2
556 34 my ($class, $attr_name) = @_;
557 34 ▶ my $code = $class->can($attr_name) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $code = $class->can($attr_name) or return 34 0 34
558 34 ▶ my ($role, $sub) = subname($code) =~ /\A(.+)::([^:]+)\z/ or return;
Branch: 1 branch
BranchTrueFalseTotal
() = subname($code) =~ /\A(.+)::([^:]+)\z/ or return 34 0 34
559 34 ▶ 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) 9 25 34
Condition100%3/3
$sub eq $attr_name and "Moo::Role"->is_role($role)
!ll&&!rl&&r
2725
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
560 160 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
561 25 ▶ return unless ${"${role}::YIELDS_TO_K8S_FIELD"}{$attr_name};
Branch: 1 branch
BranchTrueFalseTotal
unless do { "${role}::YIELDS_TO_K8S_FIELD" }->{$attr_name} 11 14 25
562 14 return unless defined &{"${role}::${attr_name}"}
563 14 ▶ && \&{"${role}::${attr_name}"} == $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &{"${role}::$attr_name";} and \&{"${role}::$attr_name";} == $code 0 14 14
Condition33%1/3
defined &{"${role}::$attr_name";} and \&{"${role}::$attr_name";} == $code
!ll&&!rl&&r
0014
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
564 14 ▶ return unless $class->can('does') && $class->does($role);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("does") and $class->does($role) 0 14 14
Condition33%1/3
$class->can("does") and $class->does($role)
!ll&&!rl&&r
0014
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
565 14 return $role;
566 }
567  
568 # The `k8s` DSL entry point. A thin wrapper so that every argument a caller
569 # of `k8s` can pass has a meaning; the adopt switch below is not one of them.
570 28907 ▶sub _k8s {
Subroutine _k8s: called CC=1 SCAR=0.0
571 28907 my ($class, $caller, $name, $type_spec, $marker) = @_;
572 28907 return $class->_declare_field($caller, $name, $type_spec, $marker, 0);
573 }
574  
575 # Register a wire field for a Moo attribute the class already has, without
576 # calling has() -- the one field this exists for is metadata, which
577 # IO::K8s::Role::APIObject declares itself (with the ObjectMeta coercion)
578 # and which IO::K8s::APIObject::import and IO::K8s::AutoGen then register
579 # for the registry readers. Private on purpose: the public `k8s` never
580 # adopts, so a field can no longer end up registered over an attribute
581 # nobody declared for it (k144). Refused unless an attribute of that name
582 # is in effect and its init_arg is the field's JSON key.
583 1628 ▶sub _k8s_adopt {
Subroutine _k8s_adopt: called CC=1 SCAR=0.0
584 1628 my ($class, $caller, $name, $type_spec, $marker) = @_;
585 1628 return $class->_declare_field($caller, $name, $type_spec, $marker, 1);
586 }
587  
588 # The Perl accessor a JSON key gets in $caller (k200). The key sanitized
589 # to a valid identifier (_sanitize_attr_name) is the name wanted; it is
590 # taken unless
591 # * another JSON key already holds it -- in this class or, nearest wins,
592 # in an ancestor: sanitizing is not injective (x-value and x_value both
593 # become x_value), or
594 # * it is a real method of the class that is no attribute: new, meta,
595 # does, can, isa, import, DOES, BUILD, TO_JSON, a role's helpers (label,
596 # save, is_ready, spec_get, ...) -- a wire field must not replace them.
597 # In either case the accessor gets a leading underscore -- the mark
598 # _sanitize_attr_name already puts on `$ref` (_ref) -- and another one for
599 # each further clash (new -> _new -> __new). The JSON key is untouched: the
600 # field keeps it as its json_key and constructor argument, which is what
601 # TO_JSON, FROM_HASH, the CRD emitter and compare_to_schema read. The
602 # first of two clashing keys in declaration order keeps the plain name
603 # (AutoGen and inline structs declare in sorted key order, so it is
604 # deterministic). A key that already has an accessor in $caller -- declared
605 # before, or by an ancestor -- gets that one back, so a redeclaration and
606 # an override of the same key land on the same attribute. An adopted field
607 # (metadata) is never renamed.
608 #
609 # Left alone are the attributes a class defines outside the DSL (a role's
610 # own `has`) and the role helpers that yield to a wire field
611 # (_yielding_role_helper): the declaration preflight in _declare_field
612 # decides about those.
613 30535 ▶sub _accessor_name {
Subroutine _accessor_name: called CC=8 SCAR=20.8
614 30535 my ($caller, $json_key, $adopt) = @_;
615 30535 my $candidate = _sanitize_attr_name($json_key);
616 30535 ▶ return $candidate if $adopt;
Branch: 1 branch
BranchTrueFalseTotal
if $adopt 1628 28907 30535
617 28907 while (1) {
618 28932 my ($declarer, $registered) = _nearest_registration($caller, $candidate);
619 28932 ▶ if ($registered) {
Branch: 3 branches
BranchTrueFalseTotal
if ($registered) { } 54 28878 28932
elsif (not $caller->can($candidate)) { } 28851 27 28878
elsif (IO::K8s::Role::Resource::_effective_attribute_specs($caller)->{$candidate} or _yielding_role_helper($caller, $candidate)) { } 7 20 27
620 54 ▶ return $candidate if ($registered->{json_key} // $candidate) eq $json_key;
Branch: 1 branch
BranchTrueFalseTotal
if ($registered->{"json_key"} // $candidate) eq $json_key 49 5 54
Condition66%2/3
$registered->{"json_key"} // $candidate
l!l&&r!l&&!r
5490
MC/DC0%0/2
$registered->{"json_key"} // $candidate
$registered->{"json_key"} $candidate
Truth table66%2/3
A // B
A= $registered->{"json_key"}B= $candidate
ABresult
1X1
011
000
621 } elsif (!$caller->can($candidate)) {
622 28851 return $candidate;
623 ▶ } elsif (IO::K8s::Role::Resource::_effective_attribute_specs($caller)->{$candidate}
Condition66%2/3
IO::K8s::Role::Resource::_effective_attribute_specs($caller)->{$candidate} or _yielding_role_helper($caller, $candidate)
l!l&&r!l&&!r
0720
MC/DC50%1/2
IO::K8s::Role::Resource::_effective_attribute_specs($caller)->{$candidate} or _yielding_role_helper($caller, $candidate)
IO::K8s::Role::Resource::_effective_attribute_specs($caller)->{$candidate} _yielding_role_helper($caller, $candidate)
Truth table66%2/3
A or B
A= IO::K8s::Role::Resource::_effective_attribute_specs($caller)->{$candidate}B= _yielding_role_helper($caller, $candidate)
ABresult
1X1
011
000
624 || _yielding_role_helper($caller, $candidate)) {
625 7 return $candidate;
626 }
627 25 $candidate = '_'.$candidate;
628 }
629 }
630  
631 30535 ▶sub _declare_field {
Subroutine _declare_field: called CC=103 SCAR=47.6
632 30535 my ($class, $caller, $name, $type_spec, $marker, $adopt) = @_;
633  
634 30535 my $json_key = $name;
635 30535 my $attr_name = _accessor_name($caller, $name, $adopt);
636 30535 my $where = "field '$name' of $caller";
637  
638 # Declaration preflight (k144). Every conflict is refused here, before
639 # anything below builds an inline-struct class, calls has() or writes
640 # the registry, the attribute list or the merged-view cache -- a
641 # rejected declaration leaves the class exactly as it was.
642 #
643 # Two JSON keys wanting one accessor (x-value and x_value) and a key
644 # wanting the name of a real method (new, meta, ...) are no conflicts
645 # to refuse: _accessor_name has already given the field a free
646 # accessor (k200), so what is looked at below is that accessor.
647 30535 my ($declarer, $registered) = _nearest_registration($caller, $attr_name);
648 # The same JSON key declared a second time in this very class (k151).
649 # Moo keeps the first attribute, so a second declaration is only ever
650 # compared with the first one, below, once its type is interpreted --
651 # identical is a no-op, anything else is refused.
652 30535 ▶ my $redeclared = $registered && $declarer eq $caller;
Condition100%3/3
$registered && $declarer eq $caller
!ll&&!rl&&r
30486544
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
653 # A method that is not a Moo attribute -- an IO::K8s::Role::APIObject
654 # helper such as get_condition -- used to make the old
655 # `return if $caller->can($attr_name)` register the field and skip
656 # has(), leaving the wire field served by that method. What counts is
657 # the effective Moo spec; a registry entry proves nothing. The one way
658 # past this is a role helper its role declares as yielding to a wire
659 # field of the same name (see _yielding_role_helper): the field is then
660 # installed over it. Any other real method does not get here under the
661 # field's own name -- _accessor_name moved the accessor (k200); only an
662 # adopted field, which is never renamed, can still meet one.
663 30535 my ($spec, $local, $yielding);
664 30535 ▶ if ($caller->can($attr_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caller->can($attr_name)) 1684 28851 30535
665 1684 $spec = IO::K8s::Role::Resource::_effective_attribute_specs($caller)->{$attr_name};
666 1684 ▶ $yielding = _yielding_role_helper($caller, $attr_name) unless $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec 7 1677 1684
667 1684 ▶ croak "k8s: $where collides with the method '$attr_name' of $caller, "
Branch: 1 branch
BranchTrueFalseTotal
unless $spec or $yielding 0 1684 1684
Condition66%2/3
$spec or $yielding
l!l&&r!l&&!r
167770
MC/DC0%0/2
$spec or $yielding
$spec $yielding
Truth table66%2/3
A or B
A= $specB= $yielding
ABresult
1X1
011
000
668 . 'which is not an attribute'
669 unless $spec || $yielding;
670 160 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
671 1684 $local = defined &{"${caller}::${attr_name}"};
672 }
673 # Moo refuses has() for an accessor this very package already defines,
674 # so a fresh declaration can replace an inherited attribute (nearest
675 # wins, below) but never one of the class's own. That leaves three
676 # cases where has() is not called at all:
677 # * adopt -- the explicit path above; the attribute must exist and
678 # take the JSON key as its constructor argument;
679 # * a field this class already declared (or adopted), declared again
680 # with the same JSON key -- compared with the first declaration
681 # further down, and a no-op when identical (k151). It used to
682 # overwrite the registry entry while Moo kept the first spec, so
683 # the two described different fields;
684 # * anything else the class defines itself (a role's attribute, a
685 # plain has) -- refused, since the field would be registered over
686 # an attribute that does not follow its declaration.
687 30535 my $install = 1;
688 30535 ▶ if ($adopt) {
Branch: 3 branches
BranchTrueFalseTotal
if ($adopt) { } 1628 28907 30535
elsif ($yielding) { } 7 28900 28907
elsif ($local) { } 44 28856 28900
689 1628 ▶ my $init = $spec && exists $spec->{init_arg} ? $spec->{init_arg} : $attr_name;
Branch: 1 branch
BranchTrueFalseTotal
$spec && exists $spec->{'init_arg'} ? : 0 1628 1628
Condition33%1/3
$spec and exists $spec->{"init_arg"}
!ll&&!rl&&r
016280
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
690 1628 ▶ 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 1628 1628
Condition33%2/6
$spec and defined $init and $init eq $json_key
!ll&&!rl&&r
001628
$spec and defined $init
!ll&&!rl&&r
001628
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
691 . "taking '$json_key' as its constructor argument"
692 unless $spec && defined $init && $init eq $json_key;
693 1628 $install = 0;
694 } elsif ($yielding) {
695 # Installed below; a helper composed into this very package is
696 # removed right before has(), which refuses to overwrite it.
697 } elsif ($local) {
698 44 ▶ croak "k8s: $where would take over the attribute '$attr_name' that "
Branch: 1 branch
BranchTrueFalseTotal
unless $redeclared 0 44 44
699 . "$caller defines outside the k8s DSL"
700 unless $redeclared;
701 44 $install = 0;
702 }
703  
704 # Inline-struct form: name => [ Type, { options } ]. Exactly two elements
705 # with a hashref second is unambiguous -- every array type spec ([Str],
706 # ['Core::V1::Container'], [ {} ], [ [] ]) has one element.
707 30535 ▶ 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 30525 30535
Condition83%5/6
ref $type_spec eq "ARRAY" and @$type_spec == 2 and ref $type_spec->[1] eq "HASH"
!ll&&!rl&&r
30525010
ref $type_spec eq "ARRAY" and @$type_spec == 2
!ll&&!rl&&r
26018450710
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
708 10 ($type_spec, $marker) = @$type_spec;
709 }
710  
711 30535 my %opts;
712 30535 ▶ if (ref $marker eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $marker eq 'HASH') { } 4172 26363 30535
elsif (defined $marker and $marker eq 'required') { } 2587 23776 26363
elsif (defined $marker) { } 1 23775 23776
713 4172 %opts = %$marker;
714 ▶ } elsif (defined $marker && $marker eq 'required') {
Condition100%3/3
defined $marker and $marker eq "required"
!ll&&!rl&&r
2377512587
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
715 2587 $opts{required} = 1;
716 } elsif (defined $marker) {
717 1 croak "k8s: third argument for $where must be 'required' or a hashref of field options, got '$marker'";
718 }
719 30534 for my $key (sort keys %opts) {
720 croak "k8s: unknown field option '$key' for $where (known: "
721 . join(', ', sort keys %FIELD_OPTIONS) . ')'
722 7661 ▶ unless $FIELD_OPTIONS{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $FIELD_OPTIONS{$key} 1 7660 7661
723 }
724 # An option present with an undef value is a declaration error, not a
725 # no-op: `pattern => $schema->{pattern}` on an upstream schema with no
726 # pattern would otherwise compile to a match-everything regex instead
727 # of simply not declaring the option, and the same silent trap applies
728 # to every other option (an undef default is not a JSON null we model).
729 30533 for my $key (sort keys %opts) {
730 croak "k8s: field option '$key' for $where must not be undef"
731 7660 ▶ unless defined $opts{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{$key} 2 7658 7660
732 }
733 # A nullable field keeps an explicit JSON null (k158) and brings two
734 # methods of its own: has_<accessor>, true while the key exists (null
735 # included), and clear_<accessor>, which makes the field absent again.
736 # Both names are part of the declaration preflight: taken by anything
737 # but this very field's own predicate and clearer -- declared before in
738 # this class, or inherited from the field an ancestor declares -- they
739 # are refused like a colliding field name, before anything is built.
740 30531 ▶ my @nullable_methods = $opts{nullable} ? _nullable_methods($attr_name) : ();
Branch: 1 branch
BranchTrueFalseTotal
$opts{'nullable'} ? : 33 30498 30531
741 30531 ▶ if (@nullable_methods) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nullable_methods) 33 30498 30531
742 33 ▶ 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
231
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
743 33 ▶ my %own = map { defined $_ ? ($_ => 1) : () } @{$own}{qw( predicate clearer )};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 4 62 66
744 33 for my $method (@nullable_methods) {
745 croak "k8s: $where needs the method '$method' as a nullable field, "
746 . "but $caller already has a method of that name"
747 64 ▶ if $caller->can($method) && !$own{$method};
Branch: 1 branch
BranchTrueFalseTotal
if $caller->can($method) and not $own{$method} 3 61 64
Condition100%3/3
$caller->can($method) and not $own{$method}
!ll&&!rl&&r
5743
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
748 }
749 }
750 # required => 1 (or the legacy 'required' marker / '!' suffix) both
751 # enforces the field at construction and records required => 1 in the
752 # registry. required => 'schema' does the second half only: AutoGen
753 # uses it for an OpenAPI schema's required list, since a real cluster
754 # document can omit a field the schema requires -- a server-side
755 # default, or an object still short of that field (an empty status
756 # right after creation) -- and Moo enforcement would reject a
757 # perfectly valid document for it (Critical 1 of the k93 review). Any
758 # other true value is treated the same as 1.
759 30528 my $required_opt = delete $opts{required};
760 30528 ▶ my $required_recorded = $required_opt ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$required_opt ? : 5011 25517 30528
761 30528 ▶ my $required = ($required_opt && $required_opt ne 'schema') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$required_opt && $required_opt ne 'schema' ? : 2593 27935 30528
Condition100%3/3
$required_opt and $required_opt ne "schema"
!ll&&!rl&&r
2551724182593
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
762  
763 # `!` suffix on strings (legacy/alternative required syntax)
764 30528 ▶ 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 30525 30528
elsif (ref $type_spec eq 'ARRAY' and not ref $type_spec->[0] and $type_spec->[0] =~ s/!$//) { } 1 58054 58055
Condition83%5/6
not ref $type_spec || _is_type_tiny($type_spec) and $type_spec =~ s/!$//
!ll&&!rl&&r
2073797883
ref $type_spec || _is_type_tiny($type_spec)
l!l&&r!l&&!r
2073709791
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
765 3 $required = $required_recorded = 1;
766 ▶ } 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
2753029941
ref $type_spec eq "ARRAY" and not ref $type_spec->[0]
!ll&&!rl&&r
2601615142995
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
767 1 $required = $required_recorded = 1;
768 }
769  
770 # Every branch below sets $inner, the type of a present value; the
771 # Maybe wrapping for an optional field happens once at the end.
772 30528 my %info;
773 my $inner;
774  
775 # Handle Type::Tiny objects directly (Str, Int, Bool, IntOrStr, Quantity,
776 # Time) -- and, since k191, the map types: Opaque or a bare HashRef is
777 # the free map, HashRef[X] a map whose values are X.
778 30528 ▶ if (_is_type_tiny($type_spec)) {
Branch: 4 branches
BranchTrueFalseTotal
if (_is_type_tiny($type_spec)) { } 15897 14631 30528
elsif (not ref $type_spec) { } 9791 4840 14631
elsif (ref $type_spec eq 'ARRAY') { } 4509 331 4840
elsif (ref $type_spec eq 'HASH') { } 331 0 331
779 15897 my $kind = $type_spec->name;
780 15897 my $flags = $TYPE_FLAGS{$kind};
781 15897 ▶ if ($flags) {
Branch: 3 branches
BranchTrueFalseTotal
if ($flags) { } 14841 1056 15897
elsif ($kind eq 'Opaque' or $kind eq 'HashRef') { } 135 921 1056
elsif ($type_spec->is_parameterized and $type_spec->parameterized_from->name eq 'HashRef') { } 921 0 921
782 14841 %info = %$flags;
783 14841 $inner = _constrain($type_spec, $kind, \%opts, $where);
784 ▶ } elsif ($kind eq 'Opaque' || $kind eq 'HashRef') {
Condition100%3/3
$kind eq "Opaque" or $kind eq "HashRef"
l!l&&r!l&&!r
1332921
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
785 # Values untyped and copied through as they are; there is no
786 # scalar to put a value option on.
787 135 $info{is_hash_opaque} = 1;
788 135 _reject_value_options(\%opts, $where);
789 135 $inner = HashRef;
790 ▶ } elsif ($type_spec->is_parameterized
Condition33%1/3
$type_spec->is_parameterized and $type_spec->parameterized_from->name eq "HashRef"
!ll&&!rl&&r
00921
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
791 && $type_spec->parameterized_from->name eq 'HashRef') {
792 921 $inner = _typed_map(\%info, $type_spec->type_parameter, \%opts, $where);
793 }
794 } elsif (!ref $type_spec) {
795 9791 ▶ if (my $flags = $TYPE_FLAGS{$type_spec}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $flags = $TYPE_FLAGS{$type_spec}) { } 444 9347 9791
796 444 %info = %$flags;
797 444 ▶ my $base = $STR_ISA_MAP{$type_spec} // Str;
Condition66%2/3
$STR_ISA_MAP{$type_spec} // &Str()
l!l&&r!l&&!r
409350
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
798 444 $inner = _constrain($base, $type_spec, \%opts, $where);
799 } else {
800 9347 my $full_class = _expand_class($type_spec);
801 9347 $info{is_object} = 1;
802 9347 $info{class} = $full_class;
803 9347 _reject_value_options(\%opts, $where);
804 9346 $inner = InstanceOf[$full_class];
805 }
806 } elsif (ref $type_spec eq 'ARRAY') {
807 4509 my $elem = $type_spec->[0];
808 # [ {} ] / [ [] ] -- an array of opaque hashes or opaque arrays, for a
809 # schema whose items are `type: object` / `type: array` with no further
810 # structure (k66). Validated as arrays of the right container
811 # shape; the contents pass through untyped, the same one-level-copy
812 # opaque handling a free-form HashRef gets in TO_JSON / _inflate_struct.
813 4509 ▶ if (ref $elem eq 'HASH') {
Branch: 5 branches
BranchTrueFalseTotal
if (ref $elem eq 'HASH') { } 16 4493 4509
elsif (ref $elem eq 'ARRAY') { } 4 4489 4493
elsif (_is_type_tiny($elem)) { } 1494 2995 4489
elsif ($elem eq 'Str') { } 4 2991 2995
elsif ($elem eq 'Int') { } 0 2991 2991
814 16 $info{is_array_of_hash} = 1;
815 16 _reject_value_options(\%opts, $where);
816 15 $inner = ArrayRef[HashRef];
817 } elsif (ref $elem eq 'ARRAY') {
818 4 $info{is_array_of_array} = 1;
819 4 _reject_value_options(\%opts, $where);
820 4 $inner = ArrayRef[ArrayRef];
821 # Handle [Str] with Type::Tiny object -- and, since 1.108's to_crd
822 # (D9), every other scalar kind the DSL knows too. Before, only
823 # Str/Int/Bool recorded an is_array_of_* flag here; [Num]/[Quantity]/
824 # [Time]/[IntOrStr] still built and validated a working ArrayRef
825 # attribute (Moo's own isa/coerce below is unaffected either way),
826 # but left the registry entry with NO classifying flag at all --
827 # invisible to anything that reads the registry instead of the Moo
828 # attribute, which is exactly what to_crd's _schema_for_class does
829 # (found via IO::K8s::Api::Resource::V1::ResourceSlice, whose
830 # DeviceCapacity.validValues is [Quantity]; k96 task-2 review).
831 # Purely additive when introduced (k96 task-2): TO_JSON and
832 # _inflate_struct had no branch keyed on any of these four flags, so
833 # they fell through to the same generic ArrayRef copy an unflagged
834 # entry already used. _inflate_struct still does today -- inflation
835 # is unchanged. TO_JSON no longer does: it now reads is_array_of_num
836 # for JSON-number elements (k155), is_array_of_quantity and
837 # is_array_of_time to stringify elements (k180), and
838 # is_array_of_int_or_string for the per-element int-or-string rule
839 # (k167, k181).
840 } elsif (_is_type_tiny($elem)) {
841 1494 my $kind = $elem->name;
842 1494 ▶ if ($kind eq 'Str') {
Branch: 7 branches
BranchTrueFalseTotal
if ($kind eq 'Str') { } 1378 116 1494
elsif ($kind eq 'Int') { } 40 76 116
elsif ($kind eq 'Bool') { } 19 57 76
elsif ($kind eq 'Num') { } 8 49 57
elsif ($kind eq 'IntOrStr') { } 13 36 49
elsif ($kind eq 'Quantity') { } 17 19 36
elsif ($kind eq 'Time') { } 19 0 19
843 1378 $info{is_array_of_str} = 1;
844 } elsif ($kind eq 'Int') {
845 40 $info{is_array_of_int} = 1;
846 } elsif ($kind eq 'Bool') {
847 19 $info{is_array_of_bool} = 1;
848 } elsif ($kind eq 'Num') {
849 8 $info{is_array_of_num} = 1;
850 } elsif ($kind eq 'IntOrStr') {
851 13 $info{is_array_of_int_or_string} = 1;
852 } elsif ($kind eq 'Quantity') {
853 17 $info{is_array_of_quantity} = 1;
854 } elsif ($kind eq 'Time') {
855 19 $info{is_array_of_time} = 1;
856 }
857 1494 $inner = ArrayRef[ _constrain($elem, $kind, \%opts, $where) ];
858 } elsif ($elem eq 'Str') {
859 4 $info{is_array_of_str} = 1;
860 4 $inner = ArrayRef[ _constrain(Str, 'Str', \%opts, $where) ];
861 } elsif ($elem eq 'Int') {
862 0 $info{is_array_of_int} = 1;
863 0 $inner = ArrayRef[ _constrain(Int, 'Int', \%opts, $where) ];
864 } else {
865 2991 my $full_class = _expand_class($elem);
866 2991 $info{is_array_of_objects} = 1;
867 2991 $info{class} = $full_class;
868 2991 _reject_value_options(\%opts, $where);
869 2991 $inner = ArrayRef[InstanceOf[$full_class]];
870 }
871 } elsif (ref $type_spec eq 'HASH') {
872 331 my @keys = keys %$type_spec;
873 331 ▶ 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') { } 242 89 331
Condition100%6/6
@keys == 1 and not ref $$type_spec{$keys[0]} and $$type_spec{$keys[0]} eq 1
!ll&&!rl&&r
827242
@keys == 1 and not ref $$type_spec{$keys[0]}
!ll&&!rl&&r
6022249
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
874 # Hash-of-X pattern: { TypeName => 1 }
875 242 my $vkind = $keys[0];
876 242 ▶ if ($vkind eq 'Str') {
Branch: 2 branches
BranchTrueFalseTotal
if ($vkind eq 'Str') { } 32 210 242
elsif (my $vt = $HASH_VALUE_TYPES{$vkind}) { } 23 187 210
877 # The string map, lenient (k191): TO_JSON puts every scalar
878 # value out as a JSON string, but the check stays the bare
879 # HashRef it was while { Str => 1 } meant the opaque map, so
880 # a declaration still relying on that keeps loading and
881 # serializing -- its reference values pass through, with a
882 # one-time warning. HashRef[Str] is the strict spelling.
883 32 $info{is_hash_of_str} = 1;
884 32 $info{is_hash_of_str_lenient} = 1;
885 32 _reject_value_options(\%opts, $where);
886 31 $inner = HashRef;
887 } elsif (my $vt = $HASH_VALUE_TYPES{$vkind}) {
888 # { Quantity => 1 } and friends: a typed value map. Each value
889 # is validated against the scalar type (k63).
890 23 $inner = _typed_map(\%info, $vt->{isa}, \%opts, $where);
891 } else {
892 187 $inner = _object_map(\%info, _expand_class($vkind), \%opts, $where);
893 }
894 } else {
895 # Inline struct: { field => TypeSpec, ... }
896 89 my $inner_class = $caller . '::_' . ucfirst($attr_name);
897 89 ▶ if ($redeclared) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redeclared) { } 11 78 89
898 11 _redeclare_inline_struct($caller, $where, $registered, $inner_class, $type_spec);
899 } else {
900 78 _generate_inline_struct($inner_class, $type_spec);
901 }
902 82 $info{is_object} = 1;
903 82 $info{is_inline_struct} = 1;
904 82 $info{class} = $inner_class;
905 82 _reject_value_options(\%opts, $where);
906 82 $inner = InstanceOf[$inner_class];
907 }
908 }
909  
910 30510 ▶ croak "k8s: cannot interpret the type of $where" unless defined $inner;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inner 0 30510 30510
911  
912 # A default that the field's own type rejects is a declaration error,
913 # not something to discover when to_crd emits it -- except on an
914 # object-bearing field (a referenced class, an inline struct, an array
915 # or hash of objects), where $inner is InstanceOf[...] or wraps it: no
916 # plain hash/array default can ever satisfy that, so there is nothing
917 # useful to check here. The default is recorded as given; to_crd
918 # validates it against the schema instead (Important 3 of the k93
919 # review).
920 30510 ▶ 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 30508 30510
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
300944142
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
300940416
exists $opts{"default"} and not $info{"is_object"} and not $info{"is_array_of_objects"}
!ll&&!rl&&r
3007321416
exists $opts{"default"} and not $info{"is_object"}
!ll&&!rl&&r
3003835437
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
921 && !$info{is_hash_of_objects} && !$inner->check($opts{default})) {
922 croak "k8s: 'default' for $where fails the field's own type: "
923 2 . $inner->get_message($opts{default});
924 }
925  
926 # A nullable field takes undef even when required: required means the
927 # key must exist, and null is a value it may exist with (k158).
928 30508 ▶ my $isa = $required && !@nullable_methods ? $inner : Maybe[$inner];
Branch: 1 branch
BranchTrueFalseTotal
$required && !@nullable_methods ? : 2596 27912 30508
Condition100%3/3
$required and not @nullable_methods
!ll&&!rl&&r
2791112596
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
929  
930 30508 ▶ $info{required} = 1 if $required_recorded;
Branch: 1 branch
BranchTrueFalseTotal
if $required_recorded 5015 25493 30508
931 30508 ▶ if (%opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (%opts) 2138 28370 30508
932 # A one-level copy: the registry must not alias a caller's arrayref
933 # (the common `enum => $schema->{enum}` idiom reads straight off a
934 # shared upstream structure) and later see it mutated out from under
935 # the constraint that was already built from it. A Regexp is
936 # immutable, so 'pattern' needs no copy.
937 2138 my %stored = %opts;
938 2138 ▶ $stored{enum} = [ @{ $stored{enum} } ] if exists $stored{enum};
Branch: 1 branch
BranchTrueFalseTotal
if exists $stored{"enum"} 741 1397 2138
939 2138 $info{options} = \%stored;
940 }
941  
942 # Store json_key when it differs from the Perl attribute name
943 30508 ▶ $info{json_key} = $json_key if $attr_name ne $json_key;
Branch: 1 branch
BranchTrueFalseTotal
if $attr_name ne $json_key 438 30070 30508
944  
945 # Declared in this very class before (k151): nothing is installed or
946 # registered a second time. The registry entry covers type, nested
947 # class, options, recorded required-ness and JSON key; the Moo spec
948 # adds whether required is enforced (1 and 'schema' record the same).
949 # An identical declaration changes nothing, not even the attribute list.
950 30508 ▶ if ($redeclared) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redeclared) 37 30471 30508
951 _croak_redeclared($where, $caller)
952 unless _same_value(\%info, $registered)
953 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
954 21 return;
955 }
956  
957 # Adopted (a redeclaration has returned above): see the preflight above
958 # for why there is nothing to install.
959 30471 ▶ return _register_field($caller, $attr_name, \%info) unless $install;
Branch: 1 branch
BranchTrueFalseTotal
unless $install 1628 28843 30471
960  
961 # Call Moo's has — use init_arg to map JSON key to Perl-safe attribute name
962 28843 ▶ my $has = $_has{$caller} // $caller->can('has')
Branch: 1 branch
BranchTrueFalseTotal
my $has = $_has{$caller} // $caller->can("has") or croak("k8s: $caller has no 'has' to declare $where with") 28843 0 28843
Condition33%1/3
$_has{$caller} // $caller->can("has")
l!l&&r!l&&!r
2884300
MC/DC0%0/2
$_has{$caller} // $caller->can("has")
$_has{$caller} $caller->can("has")
Truth table33%1/3
A // B
A= $_has{$caller}B= $caller->can("has")
ABresult
1X1
011
000
963 or croak "k8s: $caller has no 'has' to declare $where with";
964 28843 my @coerce;
965 # Bool attributes: coerce \0/\1 refs, JSON booleans and 'true'/'false'
966 # strings to plain 0/1
967 28843 ▶ if ($info{is_bool}) {
Branch: 7 branches
BranchTrueFalseTotal
if ($info{'is_bool'}) { } 1918 26925 28843
elsif ($info{'is_array_of_bool'}) { } 19 26906 26925
elsif ($info{'is_object'}) { } 7793 19113 26906
elsif ($info{'is_array_of_objects'}) { } 2991 16122 19113
elsif ($info{'is_hash_of_objects'}) { } 188 15934 16122
elsif ($info{'is_hash_of_object_arrays'}) { } 40 15894 15934
elsif ($info{'is_hash_of_bool'}) { } 2 15892 15894
968 1918 @coerce = (coerce => \&_normalize_bool);
969 }
970 # Array of bool: the same normalization, per element. A bad element's
971 # message gets the index appended so it can be found in the array.
972 elsif ($info{is_array_of_bool}) {
973 49 ▶ @coerce = (coerce => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
974 49 ▶ return $_[0] unless ref $_[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 49 49
975 49 my $in = $_[0];
976 49 my @out;
977 49 for my $i (0 .. $#$in) {
978 138 push @out, eval { _normalize_bool($in->[$i]) };
979 138 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 3 135 138
980 3 $err =~ s/\n\z//;
981 3 die "$err at element $i\n";
982 }
983 }
984 46 return \@out;
985 19 });
986 }
987 # Named nested class, single (k100) -- body in _object_coercer above,
988 # because IO::K8s::Role::APIObject's `metadata` needs the same one.
989 #
990 # An inline struct takes this branch too, since is_inline_struct always
991 # sets is_object as well (k116). It used to have a branch of its own
992 # doing `$ic->new(%{$_[0]})`, which differs in exactly one visible way:
993 # `->new` stored the caller's inner containers by reference, while every
994 # other route into the same field -- inflate, struct_to_object,
995 # FROM_HASH -- copies them one level (k54). The same field of the same
996 # class therefore had both semantics depending on how it was built,
997 # which is not something a design chooses: the inline coercer landed
998 # with the inline-struct DSL in 2c0c6d02, k54 arrived five months later
999 # in 5ab95ca3 and touched _inflate_struct and TO_JSON without ever
1000 # reaching it. Unified on the copying side, the newer and the tested
1001 # one; t/29_inline_struct.t pins it, since nothing did before.
1002 #
1003 # The other differences the k100 review listed turned out not to be
1004 # differences worth keeping either. load_class is not a blocker: Moo
1005 # registers a generated inline-struct package in %INC (as '(eval NNN)'),
1006 # so require_module finds it and returns. The opaque names
1007 # fieldsV1/rawExtension/raw that _inflate_struct special-cases are
1008 # theoretical here -- ManagedFieldsEntry is the only class shipping one
1009 # and it already declares the opaque form. FROM_STRUCT never exists on a
1010 # generated class. What is left is the Bool error wrapper, whose text
1011 # now matches the one every other object-bearing field produces.
1012 #
1013 # Two things the three object branches here share:
1014 # * one predicate, _object_builds, decides what is built: a plain
1015 # hashref -- `ref eq 'HASH'` is false for a blessed one -- or, for a
1016 # union class with FROM_STRUCT, any defined value that is not
1017 # already of the class (k179). An object of the class is passed
1018 # through, which is also what keeps IO::K8s::_inflate_struct from
1019 # doing the work twice: it hands $class->new fully built objects and
1020 # every one of them lands here.
1021 # * anything else goes on unchanged and lets `isa` write the message
1022 # -- a coercer never invents a type error of its own.
1023 elsif ($info{is_object}) {
1024 7793 @coerce = (coerce => _object_coercer($info{class}));
1025 }
1026 # Array of named nested classes: element-wise, and a failing element
1027 # gets its index appended the same way the [Bool] coercer above does,
1028 # so the culprit can be found. Scanned first and returned untouched
1029 # when no element needs building -- the inflate path arrives here with
1030 # an array of ready objects and should pay one check per element, not
1031 # a fresh arrayref.
1032 #
1033 # An element is built by the rule _object_builds states for the single
1034 # field: a hashref, or any defined value for a union class (k179).
1035 elsif ($info{is_array_of_objects}) {
1036 2991 my $oc = $info{class};
1037 2991 my $builds = _object_builds($oc);
1038 6875 ▶ @coerce = (coerce => sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
1039 6875 ▶ return $_[0] unless ref $_[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 2 6873 6875
1040 6873 my $in = $_[0];
1041 6873 my $needed = 0;
1042 6873 for my $elem (@$in) {
1043 9190 ▶ next unless $builds->($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless &$builds($elem) 9116 74 9190
1044 74 $needed = 1;
1045 74 last;
1046 }
1047 6873 ▶ return $in unless $needed;
Branch: 1 branch
BranchTrueFalseTotal
unless $needed 6799 74 6873
1048 74 my @out;
1049 74 for my $i (0 .. $#$in) {
1050 83 ▶ if ($builds->($in->[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$builds($in->[$i])) { } 80 3 83
1051 80 push @out, eval {
1052 80 IO::K8s::Role::Resource::_default_k8s()
1053 ->_struct_to_object_expanded($oc, $in->[$i])
1054 };
1055 80 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 2 78 80
1056 2 $err =~ s/\n\z//;
1057 2 die "$err at element $i\n";
1058 }
1059 } else {
1060 3 push @out, $in->[$i];
1061 }
1062 }
1063 72 return \@out;
1064 2991 });
1065 }
1066 # Hash of named nested classes: value-wise, with the key named on a
1067 # failure. Same scan-first shortcut and element rule as the array form;
1068 # `sort keys` in the building pass so several bad values still name a
1069 # deterministic one, matching the unknown-field walk in
1070 # IO::K8s::Role::Resource.
1071 elsif ($info{is_hash_of_objects}) {
1072 188 my $oc = $info{class};
1073 188 my $builds = _object_builds($oc);
1074 10562 ▶ @coerce = (coerce => sub {
Subroutine __ANON__: called CC=8 SCAR=20.9
1075 10562 ▶ return $_[0] unless ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 10562 10562
1076 10562 my $in = $_[0];
1077 10562 my $needed = 0;
1078 10562 for my $key (keys %$in) {
1079 37424 ▶ next unless $builds->($in->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless &$builds($in->{$key}) 37418 6 37424
1080 6 $needed = 1;
1081 6 last;
1082 }
1083 10562 ▶ return $in unless $needed;
Branch: 1 branch
BranchTrueFalseTotal
unless $needed 10556 6 10562
1084 6 my %out;
1085 6 for my $key (sort keys %$in) {
1086 8 ▶ if ($builds->($in->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$builds($in->{$key})) { } 8 0 8
1087 8 $out{$key} = eval {
1088 IO::K8s::Role::Resource::_default_k8s()
1089 8 ->_struct_to_object_expanded($oc, $in->{$key})
1090 };
1091 8 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 1 7 8
1092 1 $err =~ s/\n\z//;
1093 1 die "$err at key '$key'\n";
1094 }
1095 } else {
1096 0 $out{$key} = $in->{$key};
1097 }
1098 }
1099 5 return \%out;
1100 188 });
1101 }
1102 # Map of lists of objects (k194): each list built element-wise, the
1103 # failure naming the key and the index. Same scan-first shortcut and
1104 # element rule as the array and hash forms above.
1105 elsif ($info{is_hash_of_object_arrays}) {
1106 40 my $oc = $info{class};
1107 40 my $builds = _object_builds($oc);
1108 156 ▶ @coerce = (coerce => sub {
Subroutine __ANON__: called CC=12 SCAR=26.0
1109 156 ▶ return $_[0] unless ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 156 156
1110 156 my $in = $_[0];
1111 156 my $needed = 0;
1112 156 KEY: for my $key (keys %$in) {
1113 156 ▶ next unless ref $in->{$key} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $in->{$key} eq "ARRAY" 1 155 156
1114 155 for my $elem (@{ $in->{$key} }) {
1115 156 ▶ next unless $builds->($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless &$builds($elem) 155 1 156
1116 1 $needed = 1;
1117 1 last KEY;
1118 }
1119 }
1120 156 ▶ return $in unless $needed;
Branch: 1 branch
BranchTrueFalseTotal
unless $needed 155 1 156
1121 1 my %out;
1122 1 for my $key (sort keys %$in) {
1123 1 my $list = $in->{$key};
1124 1 ▶ unless (ref $list eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $list eq "ARRAY") 0 1 1
1125 0 $out{$key} = $list;
1126 0 next;
1127 }
1128 1 my @built;
1129 1 for my $i (0 .. $#$list) {
1130 2 ▶ if ($builds->($list->[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$builds($list->[$i])) { } 2 0 2
1131 2 push @built, eval {
1132 2 IO::K8s::Role::Resource::_default_k8s()
1133 ->_struct_to_object_expanded($oc, $list->[$i])
1134 };
1135 2 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 2 2
1136 0 $err =~ s/\n\z//;
1137 0 die "$err at key '$key' element $i\n";
1138 }
1139 } else {
1140 0 push @built, $list->[$i];
1141 }
1142 }
1143 1 $out{$key} = \@built;
1144 }
1145 1 return \%out;
1146 40 });
1147 }
1148 # Map of Bool (k191): a JSON boolean value -- what a decoded document
1149 # carries -- becomes the 0/1 the value check accepts, through the same
1150 # normalization the Bool field uses. Only reference values are touched:
1151 # a plain scalar goes to `isa` as it is, so 'maybe' is still refused
1152 # rather than read as true. A map whose values are all plain scalars is
1153 # returned untouched.
1154 elsif ($info{is_hash_of_bool}) {
1155 7 ▶ @coerce = (coerce => sub {
Subroutine __ANON__: called CC=6 SCAR=28.6
1156 7 ▶ return $_[0] unless ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 7 7
1157 7 my $in = $_[0];
1158 7 ▶ return $in unless grep { ref } values %$in;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {ref $_;} values %$in 7 0 7
1159 0 my %out;
1160 0 for my $key (keys %$in) {
1161 0 my $v = $in->{$key};
1162 0 ▶ my $n = ref $v ? eval { _normalize_bool($v) } : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 0 0 0
1163 0 ▶ $out{$key} = defined $n ? $n : $v;
Branch: 1 branch
BranchTrueFalseTotal
defined $n ? : 0 0 0
1164 }
1165 0 return \%out;
1166 2 });
1167 }
1168 # A yielding role helper composed into this package goes first, so the
1169 # accessor can take its name -- after every check above, so a rejected
1170 # declaration never gets this far.
1171 28843 ▶ Package::Stash->new($caller)->remove_symbol('&'.$attr_name)
Branch: 1 branch
BranchTrueFalseTotal
if $yielding and $local 7 28836 28843
Condition66%2/3
$yielding and $local
!ll&&!rl&&r
2883607
MC/DC50%1/2
$yielding and $local
$yielding $local
Truth table66%2/3
A and B
A= $yieldingB= $local
ABresult
0X0
100
111
1172 if $yielding && $local;
1173  
1174 # A complete spec, never has('+name'). For a field redeclared under the
1175 # same JSON key in a subclass this is what makes nearest-wins real
1176 # (k144): the subclass's spec replaces the inherited one outright, so
1177 # the parent's coercion, required flag and init_arg go with it --
1178 # has('+name') would merge them back in, and refuses coerce => undef.
1179 # The parent class is left untouched.
1180 28843 ▶ $has->($attr_name, is => 'rw', isa => $isa, @coerce,
Branch: 3 branches
BranchTrueFalseTotal
$required ? : 2594 26249 28843
$attr_name ne $json_key ? : 437 28406 28843
@nullable_methods ? : 29 28814 28843
1181 ($required ? (required => 1) : ()),
1182 ($attr_name ne $json_key ? (init_arg => $json_key) : ()),
1183 (@nullable_methods
1184 ? (predicate => $nullable_methods[0], clearer => $nullable_methods[1])
1185 : ()),
1186 );
1187 28843 return _register_field($caller, $attr_name, \%info);
1188 }
1189  
1190 # The predicate and clearer a nullable field gets (k158), in that order:
1191 # has_<accessor> and clear_<accessor>, named after the Perl attribute, the
1192 # same name the accessor itself has. IO::K8s::Role::Resource's TO_JSON and
1193 # IO::K8s::Role::SpecBuilder's spec_delete call them through this too, so
1194 # the names are spelled in one place.
1195 173 ▶sub _nullable_methods {
Subroutine _nullable_methods: called CC=1 SCAR=0.0
1196 173 my ($attr_name) = @_;
1197 173 return ('has_'.$attr_name, 'clear_'.$attr_name);
1198 }
1199  
1200 # An inline struct declared again in the class that declared it (k151). Its
1201 # inner class exists already, and building it a second time would add a new
1202 # field to it, keep a dropped one and set the class up twice -- so it is
1203 # compared instead: the same field names, and every field again an
1204 # identical declaration in the inner class, which the same rule makes a
1205 # no-op or refuses. Refused before any field is looked at when the first
1206 # declaration was not this inline struct or the names differ, so a refusal
1207 # leaves the inner class as it was.
1208 11 ▶sub _redeclare_inline_struct {
Subroutine _redeclare_inline_struct: called CC=7 SCAR=19.7
1209 11 my ($caller, $where, $registered, $inner_class, $fields) = @_;
1210 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
1211 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
1212 _croak_redeclared($where, $caller)
1213 unless $registered->{is_inline_struct}
1214 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
1215 && join("\0", @had) eq join("\0", sort keys %$fields);
1216 8 __PACKAGE__->_k8s($inner_class, $_, $fields->{$_}) for sort keys %$fields;
1217 4 return;
1218 }
1219  
1220 19 ▶sub _croak_redeclared {
Subroutine _croak_redeclared: called CC=1 SCAR=0.0
1221 19 my ($where, $caller) = @_;
1222 19 croak "k8s: $where is already declared in $caller with a different type, "
1223 . 'options or required-ness; declare each field once per class';
1224 }
1225  
1226 # Deep equality of two registry entries (k151): arrays and hashes element
1227 # by element, everything else by what it stringifies to within the same
1228 # ref type -- a plain scalar as itself, a Regexp as its pattern with its
1229 # flags, a JSON boolean default as 0 or 1.
1230 108 ▶sub _same_value {
Subroutine _same_value: called CC=15 SCAR=27.2
1231 108 my ($x, $y) = @_;
1232 108 ▶ return !defined $y unless defined $x;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 108 108
1233 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
1234 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
1235 108 ▶ if ($type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "ARRAY") 5 103 108
1236 5 ▶ return 0 unless @$x == @$y;
Branch: 1 branch
BranchTrueFalseTotal
unless @$x == @$y 1 4 5
1237 4 for my $i (0 .. $#$x) {
1238 8 ▶ return 0 unless _same_value($x->[$i], $y->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless _same_value($x->[$i], $y->[$i]) 1 7 8
1239 }
1240 3 return 1;
1241 }
1242 103 ▶ if ($type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "HASH") 48 55 103
1243 48 ▶ return 0 unless keys %$x == keys %$y;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$x == keys %$y 3 45 48
1244 45 for my $key (keys %$x) {
1245 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
1246 }
1247 28 return 1;
1248 }
1249 55 ▶ return "$x" eq "$y" ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
"$x" eq "$y" ? : 50 5 55
1250 }
1251  
1252 # Record a declared field, only once its attribute is in place (k144): a
1253 # has() that dies must not leave a registry entry behind that no attribute
1254 # backs, nor a name in the attribute list or a merged view already rebuilt
1255 # around it.
1256 30471 ▶sub _register_field {
Subroutine _register_field: called CC=1 SCAR=0.0
1257 30471 my ($caller, $attr_name, $info) = @_;
1258 # Copy the values, not the reference
1259 30471 $_attr_registry{$caller}{$attr_name} = { %$info };
1260 160 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1261 30471 push @{"${caller}::_k8s_attributes"}, $attr_name;
1262  
1263 # The merged @ISA views in IO::K8s::Role::Resource are cached; a new
1264 # registration must not leave a stale merged view behind.
1265 30471 IO::K8s::Role::Resource::_invalidate_k8s_attr_cache($caller);
1266 30471 return;
1267 }
1268  
1269 160 1;
1270  
1271 __END__
« blib/lib/IO/K8s/AutoGen.pm ↑ Summary blib/lib/IO/K8s/Role/APIObject.pm »