blib/lib/IO/K8s/CRD/Emitter.pm

filter: Statementstmt 98.1% 309 / 315 Branchbran 82.6% 167 / 202 Conditioncond 68.6% 57 / 83 MC/DCmcdc 50.0% 25 / 50 Subroutinesub 100% 35 / 35 Podpod 100% 2 / 2 totaltotal 86.6% 595 / 687 CC 135 SCAR 28.0
CC
135
Cov
89%
_type_source
43.0
_render_class
27.2
_literal
10.5
CRAP 16.5
« blib/lib/IO/K8s/Role/Routable.pm ↑ Summary blib/lib/IO/K8s/CRD.pm »
1 package IO::K8s::CRD::Emitter;
2 # ABSTRACT: Render generated IO::K8s classes as house-style Perl source
3 19 our $VERSION = '1.110';
4 19 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 # Deliberate exception to the "no Data::Dumper in shipped code" house rule:
8 # _scalar_literal below renders a single scalar as a Perl literal via Dumper,
9 # to reproduce its number-vs-string quoting byte-for-byte. Codegen, not debug.
10 19 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 19 ▶use Digest::SHA qw( sha1_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use re ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 19 ▶use Types::Standard qw( Bool Str HashRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 19 ▶use IO::K8s::AutoGen ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 19 ▶use IO::K8s::Resource ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 19 ▶use IO::K8s::Role::Resource ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19  
20 19 has base => (is => 'ro', isa => Str, required => 1);
21 20 ▶has names => (is => 'ro', isa => HashRef, default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
22 25 ▶has overlay => (is => 'ro', isa => HashRef, default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
23 27 ▶has version => (is => 'ro', isa => Str, default => sub { $VERSION });
Subroutine __ANON__: called CC=1 SCAR=0.0
24 19 has subresources => (is => 'ro', isa => Bool, default => 1);
25  
26 # The reverse of IO::K8s::Resource's class-prefix map (full namespace ->
27 # short prefix), longest full namespace first so a more specific prefix
28 # ('IO::K8s::Api::Core') is tried before a shorter one that would also
29 # match as a '::'-bounded prefix. Built once per emitter instance -- the
30 # map itself never changes mid-render.
31 19 has _prefix_pairs => (is => 'lazy', init_arg => undef);
32  
33 7 ▶sub _build__prefix_pairs {
Subroutine _build__prefix_pairs: called CC=1 SCAR=0.0
34 7 my $prefixes = IO::K8s::Resource::class_prefixes();
35 return [
36 899 sort { length($b->[1]) <=> length($a->[1]) }
37 7 map { [ $_, $prefixes->{$_} ] } keys %$prefixes
38 ];
39 }
40  
41  
42 # Bounds only the suffix this method appends below `base` -- AutoGen's own
43 # $MAX_CLASS_NAME bounds the full namespace-qualified name IT generates,
44 # which this has no control over and does not need to duplicate.
45 19 my $MAX_PACKAGE_SUFFIX = 200;
46  
47 # A filesystem path component -- and much tooling that shells out to a file
48 # name -- tops out around 255 bytes. A long `base` plus an otherwise-short
49 # suffix can still cross that even when the suffix alone passes the check
50 # above, so the joined package name as a whole is capped too.
51 19 my $MAX_PACKAGE_LENGTH = 255;
52  
53 475 ▶sub package_for {
Subroutine package_for: called CC=10 SCAR=23.1
Pod documented
54 475 my ($self, $class) = @_;
55 475 ▶ my $root = $self->{_root} // croak 'package_for needs a render() first';
Condition33%1/3
$self->{"_root"} // croak("package_for needs a render() first")
l!l&&r!l&&!r
47500
MC/DC0%0/2
$self->{"_root"} // croak("package_for needs a render() first")
$self->{"_root"} croak("package_for needs a render() first")
Truth table33%1/3
A // B
A= $self->{"_root"}B= croak("package_for needs a render() first")
ABresult
1X1
011
000
56 475 ▶ return $self->base . '::' . $self->names->{$class} if $self->names->{$class};
Branch: 1 branch
BranchTrueFalseTotal
if $self->names->{$class} 62 413 475
57 413 my $kind = (split /::/, $root)[-1];
58  
59 # The field path, independent of whatever (possibly hash-shortened)
60 # Perl name AutoGen gave the class -- see the POD above.
61 413 my $path = IO::K8s::AutoGen::class_path($class);
62 413 ▶ unless (defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 70 343 413
63 70 ($path = $class) =~ s/^\Q$root\E(?:::)?//;
64 }
65  
66 # overlay.names is keyed by that same logical path, not by $class --
67 # unlike L</names>, an overlay is written once per Kind against the
68 # upstream Go types and has no way to know what (possibly shortened)
69 # Perl name AutoGen happened to give a class this run.
70 413 ▶ if (length $path and my $overlay_names = $self->overlay->{names}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $path and my $overlay_names = $self->overlay->{"names"}) 116 297 413
Condition100%3/3
length $path and my $overlay_names = $self->overlay->{"names"}
!ll&&!rl&&r
70227116
MC/DC100%2/2
length $path and my $overlay_names = $self->overlay->{"names"}
length $path my $overlay_names = $self->overlay->{"names"}
Truth table100%3/3
A and B
A= length $pathB= my $overlay_names = $self->overlay->{"names"}
ABresult
0X0
100
111
71 116 ▶ if (my $name = $overlay_names->{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $overlay_names->{$path}) 66 50 116
72 # k120: an overlay name carrying '::' (or a leading '+') is an
73 # ABSOLUTE target -- a fully-qualified package under another
74 # version directory of this same provider (ExternalSecrets'
75 # v1alpha1 generators reuse the v1 Auth types) or any other
76 # checked-in class. It is used verbatim rather than joined below
77 # $base, and _is_external_ref treats it as a bare reference that
78 # this render does not itself emit a file for. A bare name (no
79 # '::') is a package under $base as before.
80 66 $name =~ s/^\+//;
81 66 ▶ return $name =~ /::/ ? $name : $self->base . '::' . $name;
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /::/ ? : 6 60 66
82 }
83 }
84  
85 347 my $joined = join '', $kind, split /::/, $path;
86 347 my $full = $self->base . '::' . $joined;
87 347 ▶ if (length($joined) > $MAX_PACKAGE_SUFFIX || length($full) > $MAX_PACKAGE_LENGTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $joined > $MAX_PACKAGE_SUFFIX or length $full > $MAX_PACKAGE_LENGTH) 16 331 347
Condition66%2/3
length $joined > $MAX_PACKAGE_SUFFIX or length $full > $MAX_PACKAGE_LENGTH
l!l&&r!l&&!r
160331
MC/DC50%1/2
length $joined > $MAX_PACKAGE_SUFFIX or length $full > $MAX_PACKAGE_LENGTH
length $joined > $MAX_PACKAGE_SUFFIX length $full > $MAX_PACKAGE_LENGTH
Truth table66%2/3
A or B
A= length $joined > $MAX_PACKAGE_SUFFIXB= length $full > $MAX_PACKAGE_LENGTH
ABresult
1X1
011
000
88 16 $joined = $kind . '_' . substr(sha1_hex($path), 0, 10);
89 16 $full = $self->base . '::' . $joined;
90 }
91 347 return $full;
92 }
93  
94  
95 35 ▶sub render {
Subroutine render: called CC=5 SCAR=16.1
Pod documented
96 35 my ($self, $root) = @_;
97 35 $self->{_root} = $root; # kept after render() so package_for() stays usable
98 35 my %files;
99 my %functional;
100 35 my %origins;
101 35 my @todo = ($root);
102 35 my %seen;
103 35 ▶ while (my $class = shift @todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = shift @todo) 114 33 147
104 114 ▶ next if $seen{$class}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$class}++ 0 114 114
105 114 my ($source, @nested) = $self->_render_class($class);
106 114 my $functional = $self->_functional_source($source);
107 114 (my $path = $self->package_for($class)) =~ s{::}{/}g;
108 114 $path .= '.pm';
109 114 ▶ if (exists $files{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $files{$path}) 4 110 114
110 croak 'IO::K8s::CRD::Emitter: target ' . $path
111 . ' has non-identical generated classes ' . $origins{$path}
112 . ' and ' . $class
113 4 ▶ if $functional{$path} ne $functional;
Branch: 1 branch
BranchTrueFalseTotal
if $functional{$path} ne $functional 2 2 4
114 }
115 # Deliberately retain the last equivalent source, matching the
116 # overwrite selection render() made before collision detection.
117 112 $files{$path} = $source;
118 112 $functional{$path} = $functional;
119 112 $origins{$path} = $class;
120 112 push @todo, @nested;
121 }
122 33 return \%files;
123 }
124  
125 # The collision identity is the generated class's executable declaration
126 # surface. Contextual # ABSTRACT and POD text describe the same wire class
127 # differently across upstream GVKs; they must not turn that alias into a
128 # rejected collision. `use utf8` is equally documentation-only when no
129 # remaining executable source needs it, because _render_class adds it for
130 # non-ASCII POD too.
131 138 ▶sub _functional_source {
Subroutine _functional_source: called CC=2 SCAR=7.0
132 138 my ($self, $source) = @_;
133 138 (my $functional = $source) =~ s/\n\n(?:=encoding UTF-8\n\n=cut\n\n)?=attr\b.*\z//s;
134 138 $functional =~ s/^# ABSTRACT:.*\n//m;
135 138 my $without_utf8 = $functional;
136 138 $without_utf8 =~ s/^use utf8;\n//m;
137 138 ▶ $functional = $without_utf8 unless $without_utf8 =~ /[^\x00-\x7F]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $without_utf8 =~ /[^\x00-\x7F]/ 138 0 138
138 138 return $functional;
139 }
140  
141 # A generated class is one this emitter renders; anything else is stock.
142 # IO::K8s::AutoGen::class_root is authoritative when it knows the class --
143 # it is exactly the bookkeeping _nested_class itself relies on, so it is
144 # never fooled by a name merely looking related. Fall back to the string
145 # prefix test for anything AutoGen has no record of (a stock class, or a
146 # class from a namespace this emitter never generated) -- a plain
147 # index()==0 test on its own would also match a sibling whose name merely
148 # starts with the same string -- root ...::V1::Knob would wrongly claim
149 # ...::V1::KnobExtra as its own -- so the fallback still requires the '::'
150 # boundary (or an exact match on the root itself).
151 340 ▶sub _is_generated {
Subroutine _is_generated: called CC=3 SCAR=11.3
152 340 my ($self, $class) = @_;
153 340 my $root = $self->{_root};
154 340 ▶ return 1 if IO::K8s::AutoGen::class_root($class) eq $root;
Branch: 1 branch
BranchTrueFalseTotal
if IO::K8s::AutoGen::class_root($class) eq $root 322 18 340
155 18 ▶ return $class eq $root || index($class, "$root\::") == 0;
Condition33%1/3
$class eq $root || index($class, "${root}::") == 0
l!l&&r!l&&!r
0018
MC/DC0%0/2
$class eq $root || index($class, "${root}::") == 0
$class eq $root index($class, "${root}::") == 0
Truth table33%1/3
A || B
A= $class eq $rootB= index($class, "${root}::") == 0
ABresult
1X1
011
000
156 }
157  
158 # k120: a generated class an overlay `names` entry redirects to an ABSOLUTE
159 # package outside this render's own `base` -- a cross-version reference to a
160 # type another version directory already ships. It is still referenced
161 # (_class_ref emits '+<that package>' via package_for, exactly as a
162 # hand-written class writes it), but this render neither recurses into it nor
163 # writes a file for it: the version directory that owns the target renders it.
164 # A normal generated class resolves under `base::...` and is not external.
165 168 ▶sub _is_external_ref {
Subroutine _is_external_ref: called CC=2 SCAR=6.9
166 168 my ($self, $class) = @_;
167 168 ▶ return 0 unless $self->_is_generated($class);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_generated($class) 6 162 168
168 162 return index($self->package_for($class), $self->base . '::') != 0;
169 }
170  
171 87 ▶sub _class_ref {
Subroutine _class_ref: called CC=7 SCAR=19.8
172 87 my ($self, $class) = @_;
173  
174 # k120: an overlay redirect to an absolute package outside this render
175 # (a cross-version provider type, or a core class the reuse heuristic
176 # would not pick on its own -- a single-key {name} that stays
177 # Core::V1::LocalObjectReference). Reference the TARGET package exactly
178 # as a hand-written class does: a core/apimachinery type by its short
179 # prefix (Core::V1::X, Meta::V1::X), any other package -- a cross-version
180 # provider type -- kept in the full '+IO::K8s::...' form. class_prefixes()
181 # also carries provider prefixes (for the DSL's own expansion), so
182 # restricting the shortening to the core groups here is what keeps a
183 # provider cross-ref from collapsing to 'ExternalSecrets::V1::X' where
184 # lib writes '+IO::K8s::ExternalSecrets::V1::X'.
185 87 ▶ if ($self->_is_external_ref($class)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_external_ref($class)) { } 2 85 87
elsif ($self->_is_generated($class)) { } 79 6 85
186 2 my $target = $self->package_for($class);
187 2 ▶ return "'+$target'" unless $target =~ /^IO::K8s::(?:Api|Apimachinery)::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $target =~ /^IO::K8s::(?:Api|Apimachinery)::/ 1 1 2
188 1 $class = $target;
189 }
190 elsif ($self->_is_generated($class)) {
191 79 return "'+" . $self->package_for($class) . "'";
192 }
193  
194 # IO::K8s::Resource::class_prefixes(), longest full namespace first --
195 # see _build__prefix_pairs.
196 7 for my $pair (@{ $self->_prefix_pairs }) {
197 91 my ($short, $full) = @$pair;
198 91 ▶ next unless index($class, "$full\::") == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($class, "${full}::") == 0 84 7 91
199 7 return "'$short\::" . substr($class, length($full) + 2) . "'";
200 }
201  
202 # class_prefixes() only carries the groups with their own entry
203 # (Core, Apps, Meta, ...); a shipped IO::K8s::Api::<X> subgroup with no
204 # entry of its own (e.g. Apiserverinternal) still renders the short way
205 # a hand-written class under it would, by dropping just the common
206 # IO::K8s::Api:: prefix -- the same fallback the old @SHORT_PREFIXES
207 # catch-all gave it.
208 0 ▶ return "'" . substr($class, length('IO::K8s::Api::')) . "'"
Branch: 1 branch
BranchTrueFalseTotal
if index($class, "IO::K8s::Api::") == 0 0 0 0
209 if index($class, 'IO::K8s::Api::') == 0;
210  
211 0 return "'+$class'";
212 }
213  
214 # The apimachinery Quantity pattern carried by the affected CRD schemas.
215 # It is an exact wire contract, so a merely similar pattern must not select
216 # Quantity during rendering.
217 19 my $CANONICAL_QUANTITY_PATTERN = q{^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$};
218  
219 # `u` is the implicit UTF-8 artifact and `p` does not affect matching, the
220 # same two flags _pattern_literal ignores. Every other flag changes the
221 # contract and keeps the field as IntOrStr.
222 12 ▶sub _is_canonical_quantity_pattern {
Subroutine _is_canonical_quantity_pattern: called CC=6 SCAR=19.6
223 12 my ($value) = @_;
224 12 ▶ return defined($value) && $value eq $CANONICAL_QUANTITY_PATTERN unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 0 12 12
Condition0%0/3
defined $value && $value eq $CANONICAL_QUANTITY_PATTERN
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value && $value eq $CANONICAL_QUANTITY_PATTERN
defined $value $value eq $CANONICAL_QUANTITY_PATTERN
Truth table0%0/3
A && B
A= defined $valueB= $value eq $CANONICAL_QUANTITY_PATTERN
ABresult
0X0
100
111
225 12 ▶ return 0 unless ref $value eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "Regexp" 0 12 12
226  
227 12 my ($pattern, $flags) = re::regexp_pattern($value);
228 12 ▶ $flags = '' unless defined $flags;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 0 12 12
229 12 $flags =~ s/[up]//g;
230 12 ▶ return $pattern eq $CANONICAL_QUANTITY_PATTERN && !length $flags;
Condition100%3/3
$pattern eq $CANONICAL_QUANTITY_PATTERN && !length($flags)
!ll&&!rl&&r
426
MC/DC100%2/2
$pattern eq $CANONICAL_QUANTITY_PATTERN && !length($flags)
$pattern eq $CANONICAL_QUANTITY_PATTERN !length($flags)
Truth table100%3/3
A && not(B)
A= $pattern eq $CANONICAL_QUANTITY_PATTERNB= !length($flags)
ABresult
0X0
100
111
231 }
232  
233 184 ▶sub _is_quantity_entry {
Subroutine _is_quantity_entry: called CC=4 SCAR=13.9
234 184 my ($info) = @_;
235 184 ▶ return unless $info->{is_int_or_string};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"is_int_or_string"} 164 20 184
236 20 my $opts = $info->{options};
237 20 ▶ return unless $opts && exists $opts->{pattern};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts and exists $opts->{"pattern"} 8 12 20
Condition100%3/3
$opts and exists $opts->{"pattern"}
!ll&&!rl&&r
6212
MC/DC100%2/2
$opts and exists $opts->{"pattern"}
$opts exists $opts->{"pattern"}
Truth table100%3/3
A and B
A= $optsB= exists $opts->{"pattern"}
ABresult
0X0
100
111
238 12 return _is_canonical_quantity_pattern($opts->{pattern});
239 }
240  
241 # The DSL type spec for one registry entry, as source. Returns
242 # ($source, $nested_class_or_undef). $class/$key are diagnostic context
243 # only, for the croak below -- the caller already has both.
244 174 ▶sub _type_source {
Subroutine _type_source: called CC=34 SCAR=37.6
245 174 my ($self, $info, $class, $key) = @_;
246 my $nested = $info->{class} && $self->_is_generated($info->{class})
247 174 ▶ && !$self->_is_external_ref($info->{class}) ? $info->{class} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$info->{'class'} && $self->_is_generated($info->{'class'}) && !$self->_is_external_ref($info->{'class'}) ? : 79 95 174
Condition100%6/6
$info->{"class"} and $self->_is_generated($info->{"class"}) and not $self->_is_external_ref($info->{"class"})
!ll&&!rl&&r
93279
$info->{"class"} and $self->_is_generated($info->{"class"})
!ll&&!rl&&r
87681
MC/DC100%3/3
$info->{"class"} and $self->_is_generated($info->{"class"}) and not $self->_is_external_ref($info->{"class"})
$info->{"class"} $self->_is_generated($info->{"class"}) not $self->_is_external_ref($info->{"class"})
Truth table100%4/4
A and B and not(C)
A= $info->{"class"}B= $self->_is_generated($info->{"class"})C= not $self->_is_external_ref($info->{"class"})
ABCresult
0XX0
10X0
1100
1111
248 174 ▶ return ($self->_class_ref($info->{class}), $nested) if $info->{is_object};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_object"} 83 91 174
249 91 ▶ return ('[' . $self->_class_ref($info->{class}) . ']', $nested) if $info->{is_array_of_objects};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_array_of_objects"} 4 87 91
250 87 ▶ return ('{ ' . $self->_class_ref($info->{class}) . ' => 1 }', $nested) if $info->{is_hash_of_objects};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_hash_of_objects"} 0 87 87
251 87 ▶ return ('[Str]') if $info->{is_array_of_str};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_array_of_str"} 0 87 87
252 87 ▶ return ('[Int]') if $info->{is_array_of_int};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_array_of_int"} 0 87 87
253 87 ▶ return ('[Bool]') if $info->{is_array_of_bool};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_array_of_bool"} 0 87 87
254 87 ▶ return ('[Num]') if $info->{is_array_of_num};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_array_of_num"} 2 85 87
255 85 ▶ return ('[IntOrStr]') if $info->{is_array_of_int_or_string};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_array_of_int_or_string"} 2 83 85
256 83 ▶ return ('[Quantity]') if $info->{is_array_of_quantity};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_array_of_quantity"} 1 82 83
257 82 ▶ return ('[Time]') if $info->{is_array_of_time};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_array_of_time"} 2 80 82
258 80 ▶ return ('[ {} ]') if $info->{is_array_of_hash};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_array_of_hash"} 0 80 80
259 80 ▶ return ('[ [] ]') if $info->{is_array_of_array};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_array_of_array"} 0 80 80
260 # The string map in the spelling that reads back as itself (k191): the
261 # lenient legacy form only where the registry says lenient, a strict
262 # one -- AutoGen's additionalProperties {type: string} -- as
263 # HashRef[Str]. The free map is Opaque.
264 80 ▶ return ('{ Str => 1 }') if $info->{is_hash_of_str} && $info->{is_hash_of_str_lenient};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_hash_of_str"} and $info->{"is_hash_of_str_lenient"} 1 79 80
Condition100%3/3
$info->{"is_hash_of_str"} and $info->{"is_hash_of_str_lenient"}
!ll&&!rl&&r
7811
MC/DC100%2/2
$info->{"is_hash_of_str"} and $info->{"is_hash_of_str_lenient"}
$info->{"is_hash_of_str"} $info->{"is_hash_of_str_lenient"}
Truth table100%3/3
A and B
A= $info->{"is_hash_of_str"}B= $info->{"is_hash_of_str_lenient"}
ABresult
0X0
100
111
265 79 ▶ return ('HashRef[Str]') if $info->{is_hash_of_str};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_hash_of_str"} 1 78 79
266 78 ▶ return ('Opaque') if $info->{is_hash_opaque};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_hash_opaque"} 4 74 78
267 74 ▶ return ('{ Int => 1 }') if $info->{is_hash_of_int};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_hash_of_int"} 1 73 74
268 73 ▶ return ('{ Num => 1 }') if $info->{is_hash_of_num};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_hash_of_num"} 0 73 73
269 73 ▶ return ('{ Bool => 1 }') if $info->{is_hash_of_bool};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_hash_of_bool"} 0 73 73
270 73 ▶ return ('{ Quantity => 1 }') if $info->{is_hash_of_quantity};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_hash_of_quantity"} 0 73 73
271 73 ▶ return ('{ Time => 1 }') if $info->{is_hash_of_time};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_hash_of_time"} 0 73 73
272 73 ▶ return ('{ IntOrStr => 1 }') if $info->{is_hash_of_int_or_string};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_hash_of_int_or_string"} 0 73 73
273 73 ▶ return ('Str') if $info->{is_str};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_str"} 42 31 73
274 31 ▶ return ('Int') if $info->{is_int};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_int"} 18 13 31
275 13 ▶ return ('Num') if $info->{is_num};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_num"} 0 13 13
276 13 ▶ return ('Bool') if $info->{is_bool};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_bool"} 3 10 13
277 10 ▶ return ('Quantity') if _is_quantity_entry($info);
Branch: 1 branch
BranchTrueFalseTotal
if _is_quantity_entry($info) 3 7 10
278 7 ▶ return ('IntOrStr') if $info->{is_int_or_string};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_int_or_string"} 7 0 7
279 0 ▶ return ('Quantity') if $info->{is_quantity};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_quantity"} 0 0 0
280 0 ▶ return ('Time') if $info->{is_time};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_time"} 0 0 0
281 0 croak "IO::K8s::CRD::Emitter: registry entry with no recognizable type for field '$key' of $class";
282 }
283  
284 # A pattern string re-quoted into `qr/.../ ` source is interpolated by Perl
285 # just like a double-quoted string: an unescaped '@' tries to interpolate an
286 # array -- '^[a-z]+@example\.com$' would die "Global symbol '@example'
287 # requires explicit package name" at compile time -- and an unescaped '$'
288 # not already meaning "end of string/group/alternative" tries to
289 # interpolate a scalar. '@' is always escaped, unconditionally: '\@' means
290 # a literal '@' in a regex no matter what follows it, so escaping it can
291 # never change what the pattern matches, and it is the only way to rule out
292 # every '@'-led interpolation form at once -- '@name', '@{...}', and
293 # '@$name'/'@$' (an array deref through a scalar, including the
294 # punctuation variable $) or $| that '@$' immediately before a bare ')' or
295 # '|' would reach for). A narrower rule that only escaped '@' before a word
296 # character or '{' missed exactly that last form: '(a@$)' left both the
297 # '@' (not followed by \w/{) and the '$' (immediately before ')') bare,
298 # and '@$)' interpolated away to nothing, silently turning the pattern
299 # into '(a)'. '$' cannot be escaped unconditionally the same way -- unlike
300 # '@', a bare '$' is meaningful in a regex (the end-of-string/line anchor),
301 # so it is only escaped when it is NOT in one of the anchor positions
302 # ("...$", "(...$)", "...$|...") a schema pattern realistically uses;
303 # escaping it there would turn the anchor into a literal '$' character
304 # instead of leaving it as an anchor -- wrong regex, not just wrong Perl.
305 # Walks the pattern one (possibly backslash-escaped) unit at a time so an
306 # already-escaped character -- notably a pattern that came in as
307 # '\/api\/v1' -- is left exactly as it was instead of gaining a second
308 # backslash.
309 #
310 # Since k114 only the '/' branch normally reaches a rendered file: an escape
311 # this adds that Perl would still be carrying in the compiled pattern makes
312 # _pattern_literal reject the qr// form altogether and emit a plain string
313 # instead, so the '@'/'$'/non-ASCII branches now mostly serve the round-trip
314 # probe that decides that. They are not dead either way -- a '@' left bare
315 # would interpolate the candidate away and the probe would then compare
316 # against a different pattern, which is the same bug one step earlier.
317 606 ▶sub _escape_pattern_body {
Subroutine _escape_pattern_body: called CC=9 SCAR=22.0
318 606 my ($pattern) = @_;
319 606 my $out = '';
320 606 ▶ while ($pattern =~ /\G(?:(\\.)|(.))/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /\G(?:(\\.)|(.))/gs) 54702 606 55308
321 54702 ▶ if (defined $1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) 1435 53267 54702
322 1435 $out .= $1;
323 1435 next;
324 }
325 53267 my $c = $2;
326 53267 ▶ if ($c eq '/') {
Branch: 4 branches
BranchTrueFalseTotal
if ($c eq '/') { } 214 53053 53267
elsif ($c eq '@') { } 5 53048 53053
elsif ($c eq '$') { } 679 52369 53048
elsif (ord $c > 127) { } 2 52367 52369
327 214 $out .= '\\/';
328 }
329 elsif ($c eq '@') {
330 5 $out .= '\\@';
331 }
332 elsif ($c eq '$') {
333 679 my $rest = substr($pattern, pos($pattern));
334 679 ▶ $out .= ($rest eq '' || $rest =~ /\A[)|]/) ? '$' : '\\$';
Branch: 1 branch
BranchTrueFalseTotal
$rest eq '' || $rest =~ /\A[)|]/ ? : 678 1 679
Condition100%3/3
$rest eq "" or $rest =~ /\A[)|]/
l!l&&r!l&&!r
581971
MC/DC100%2/2
$rest eq "" or $rest =~ /\A[)|]/
$rest eq "" $rest =~ /\A[)|]/
Truth table100%3/3
A or B
A= $rest eq ""B= $rest =~ /\A[)|]/
ABresult
1X1
011
000
335 }
336 elsif (ord($c) > 0x7F) {
337 # A codepoint above ASCII, interpolated raw into 'qr/.../ ',
338 # only round-trips through this emitted .pm file when it is
339 # BOTH saved to disk as UTF-8 bytes AND compiled under 'use
340 # utf8' -- neither of which this method controls once the
341 # caller has the rendered source in hand. \x{HEX} means the
342 # same codepoint either way: a schema pattern like
343 # '^[0-9]+\x{b5}s$' matches '100\x{b5}s' (100, MICRO SIGN, s)
344 # exactly like the generated class does, independent of that
345 # pragma. See render()'s own use-utf8/=encoding decision for
346 # the one place non-ASCII text still goes into the file
347 # unescaped: a schema's free-form 'description', in POD.
348 2 $out .= sprintf('\x{%x}', ord($c));
349 }
350 else {
351 52367 $out .= $c;
352 }
353 }
354 606 return $out;
355 }
356  
357 # A double-quoted Perl string literal that stays ASCII-only regardless of
358 # what $str holds: '\', '"', '$' and '@' are escaped so the double-quoted
359 # form -- unlike Data::Dumper's default single-quoted style, this one DOES
360 # interpolate -- carries nothing but the literal value, and every character
361 # above ASCII becomes \x{HEX}, the same escape and the same reason as
362 # _escape_pattern_body above. Single-quoted Perl strings cannot carry a
363 # \x{...} escape at all (only \\ and \' mean anything inside them), which is
364 # why a value that needs this can't just be tacked onto Data::Dumper's
365 # normal single-quoted output.
366 19 ▶sub _quote_utf8_string {
Subroutine _quote_utf8_string: called CC=2 SCAR=6.9
367 19 my ($str) = @_;
368 19 (my $escaped = $str) =~ s{ ([\\"\$\@]) | ([^\x00-\x7F]) }{
369 37 ▶ defined $1 ? "\\$1" : sprintf('\x{%x}', ord($2))
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 18 19 37
370 }gex;
371 19 return qq{"$escaped"};
372 }
373  
374 # One scalar option value (an enum member, or `default`), rendered via
375 # Data::Dumper's own single-quoted style for anything ASCII -- byte-for-byte
376 # what every rendered file already produced -- or, for anything that is
377 # not, via _quote_utf8_string above.
378 101 ▶sub _scalar_literal {
Subroutine _scalar_literal: called CC=4 SCAR=14.1
379 101 my ($value) = @_;
380 101 ▶ return _quote_utf8_string($value)
Branch: 1 branch
BranchTrueFalseTotal
if not ref $value and defined $value and $value =~ /[^\x00-\x7F]/ 19 82 101
Condition50%3/6
not ref $value and defined $value and $value =~ /[^\x00-\x7F]/
!ll&&!rl&&r
08219
not ref $value and defined $value
!ll&&!rl&&r
00101
MC/DC33%1/3
not ref $value and defined $value and $value =~ /[^\x00-\x7F]/
not ref $value defined $value $value =~ /[^\x00-\x7F]/
Truth table50%2/4
not(A) and B and C
A= not ref $valueB= defined $valueC= $value =~ /[^\x00-\x7F]/
ABCresult
0XX0
10X0
1100
1111
381 if !ref($value) && defined($value) && $value =~ /[^\x00-\x7F]/;
382 82 local $Data::Dumper::Terse = 1;
383 82 local $Data::Dumper::Indent = 0;
384 82 local $Data::Dumper::Sortkeys = 1;
385 82 local $Data::Dumper::Useqq = 0;
386 82 my $dumped = Data::Dumper::Dumper($value);
387 82 $dumped =~ s/^\s+|\s+$//g;
388 82 return $dumped;
389 }
390  
391 # A Perl literal for an option value. A Regexp is rendered as qr/.../, read
392 # back out via re::regexp_pattern in LIST context -- the scalar-context form
393 # returns the whole "(?^:PATTERN)" wrapper, which is not what a hand-written
394 # `k8s x => Str, { pattern => qr/.../ };` line looks like anywhere else in
395 # this distribution. 'u' is dropped from the flags before they are appended:
396 # every pattern IO::K8s::CRD compiles starts life as a UTF8-flagged string
397 # out of YAML::PP, and interpolating one into qr/$p/ tags the result with an
398 # implicit 'u' that reflects nothing the CRD schema's `pattern` asked for --
399 # rendering it back would forge a modifier into the source. A real modifier
400 # a pattern did ask for (i, m, s, x) still comes through.
401 378 ▶sub _literal {
Subroutine _literal: called CC=10 SCAR=23.5
402 378 my ($value) = @_;
403 378 ▶ if (ref $value eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "Regexp") 298 80 378
404 298 my ($pattern, $flags) = re::regexp_pattern($value);
405 298 my $body = _escape_pattern_body($pattern);
406 298 ▶ $flags =~ s/u//g if defined $flags;
Branch: 1 branch
BranchTrueFalseTotal
if defined $flags 298 0 298
407 298 ▶ return "qr/$body/" . ($flags // '');
Condition50%1/2
$flags // ""
l!l
2980
MC/DC0%0/1
$flags // ""
$flags
Truth table50%1/2
A
A= $flags
Aresult
11
00
408 }
409 80 ▶ if (ref $value eq 'ARRAY' && @$value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY" and @$value) 5 75 80
Condition66%2/3
ref $value eq "ARRAY" and @$value
!ll&&!rl&&r
7505
MC/DC50%1/2
ref $value eq "ARRAY" and @$value
ref $value eq "ARRAY" @$value
Truth table66%2/3
A and B
A= ref $value eq "ARRAY"B= @$value
ABresult
0X0
100
111
410 # The qw() shorthand only for a list of genuinely bareword-safe
411 # values (D6-friendly enum members like Always/IfNotPresent): word
412 # characters, dots, colons, dashes and slashes -- ASCII only, never
413 # empty. An enum entry that is the empty string -- a real, if
414 # unusual, upstream value -- would otherwise vanish: 'qw( Always
415 # IfNotPresent)' from ('', 'Always', 'IfNotPresent') silently drops
416 # the '', and the emitted class would then reject a value the
417 # generated one accepts. A non-ASCII entry is excluded the same
418 # way even though Perl's \w matches a Unicode letter like 'µ' when
419 # the string carries the UTF8 flag (as one loaded from a CRD
420 # manifest does) -- qw() has no escape syntax at all, so a raw
421 # non-ASCII byte inside it would be exactly the bug this emitter
422 # exists to avoid.
423 5 ▶ my $qw_safe = !grep { ref $_ || !defined $_ || $_ !~ /\A[\w.:\/-]+\z/ || $_ =~ /[^\x00-\x7F]/ } @$value;
Condition66%6/9
ref $_ || !defined($_) || !($_ =~ m[\A[\w.:/-]+\z]) || $_ =~ /[^\x00-\x7F]/
l!l&&r!l&&!r
119
ref $_ or not defined $_ or not $_ =~ m[\A[\w.:/-]+\z]
l!l&&r!l&&!r
0110
ref $_ or not defined $_
l!l&&r!l&&!r
0011
MC/DC50%2/4
ref $_ || !defined($_) || !($_ =~ m[\A[\w.:/-]+\z]) || $_ =~ /[^\x00-\x7F]/
ref $_ not defined $_ not $_ =~ m[\A[\w.:/-]+\z] $_ =~ /[^\x00-\x7F]/
Truth table60%3/5
A or not(B) or not(C) || D
A= ref $_B= not defined $_C= not $_ =~ m[\A[\w.:/-]+\z]D= $_ =~ /[^\x00-\x7F]/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
424 5 ▶ return '[qw(' . join(' ', @$value) . ')]' if $qw_safe;
Branch: 1 branch
BranchTrueFalseTotal
if $qw_safe 3 2 5
425 # Not qw()-safe: render element by element rather than handing the
426 # whole arrayref to Data::Dumper, so a non-ASCII element can switch
427 # to the double-quoted \x{HEX} form on its own while its ASCII
428 # siblings keep Dumper's ordinary single-quoted style -- Dumper has
429 # no way to quote just one element of a list differently.
430 2 return '[' . join(',', map { _scalar_literal($_) } @$value) . ']';
431 }
432 75 return _scalar_literal($value);
433 }
434  
435 # A `pattern` option specifically (k110, k114). Everything else still goes
436 # through _literal above.
437 #
438 # The qr/.../ form stays the default: it is what every hand-written class in
439 # this distribution writes, and IO::K8s::CRD translates one back into the
440 # ECMA262 text a CRD's openAPIV3Schema.pattern is specified in.
441 #
442 # What it cannot translate is a regex FLAG. A CRD pattern is a bare string
443 # with nowhere to carry one, so IO::K8s::CRD croaks on a flagged qr// rather
444 # than dropping the flag and emitting a pattern that means something else.
445 # Rendering `qr/.../i` would therefore hand the caller a class that compiles,
446 # validates correctly in Perl, and then cannot be turned back into a CRD --
447 # which is how IO::K8s::PrometheusOperator::V1::RuleGroup came to carry one
448 # (upstream's own `^(?i)(abort|warn)?$` picked up an 'i' flag on the way
449 # through AutoGen's qr/$p/, and this method wrote it back out).
450 #
451 # So a flagged pattern is folded back into the pattern TEXT as an inline
452 # (?flags) group -- the spelling upstream uses anyway, and one Go's regexp
453 # engine takes -- and rendered as a plain string. That is the escape hatch
454 # IO::K8s::CRD passes through untouched: whatever upstream put in its CRD
455 # goes back out unchanged, because upstream knows what its apiserver
456 # accepts. Perl-side validation is unaffected either way -- Resource.pm
457 # compiles a string pattern with qr/$p/, and an inline (?i) sets exactly
458 # what the /i modifier set.
459 #
460 # The other thing a qr// cannot always carry is the pattern's own BYTES
461 # (k114). Rendering one means writing the text back as `qr/.../` SOURCE, and
462 # _escape_pattern_body has to escape whatever Perl would otherwise
463 # interpolate -- an '@', a '$' that is not in an anchor position -- and to
464 # spell a non-ASCII codepoint as \x{HEX} so the file needs no `use utf8`.
465 # Perl keeps those backslashes in the compiled pattern: only the delimiter's
466 # own '\/' is stripped again by the tokenizer. So they survive into the
467 # registry and out through IO::K8s::CRD, and upstream's
468 # '^.*@.*\.iam\.gserviceaccount\.com$' goes back to a cluster as
469 # '^.*\@.*...', Traefik's '^([0-9]+(ns|us|µs|ms|s|m|h)?)+$' as
470 # '...\x{b5}s...'. Neither breaks the Go/RE2 engine the apiserver actually
471 # validates with -- '\@' is not even a valid ECMA262 identity escape and
472 # '\x{...}' is no ECMA262 at all -- but both are IO::K8s' own Perl artifacts
473 # sitting in a text that is supposed to be upstream's, which is the thing
474 # k110 set out to stop rather than a different problem.
475 #
476 # So the qr// form ships only where it is byte-exact: the candidate source is
477 # compiled and its text read back, and a pattern that does not come out
478 # identical takes the same plain-string path a flagged one does. That also
479 # catches the loss in the other direction -- a '\/' upstream wrote itself
480 # ('^\/api\/v1$') passes through _escape_pattern_body untouched, but the
481 # tokenizer then strips that backslash, so the qr// form would hand back
482 # '^/api/v1$', again not the bytes upstream wrote.
483 #
484 # Perl-side validation is unaffected either way: Resource.pm compiles a
485 # string pattern with qr/$p/, where '\@' and '@', '\$' and a '$' inside a
486 # character class, and '\x{b5}' and a literal MICRO SIGN are each the same
487 # regex as the other.
488 358 ▶sub _pattern_literal {
Subroutine _pattern_literal: called CC=5 SCAR=16.1
489 358 my ($value) = @_;
490 358 ▶ return _literal($value) unless ref $value eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "Regexp" 44 314 358
491  
492 314 my ($pattern, $flags) = re::regexp_pattern($value);
493 314 ▶ $flags = '' unless defined $flags;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 0 314 314
494 # 'u' for the same reason _literal drops it (an artifact of the
495 # UTF8-flagged string the pattern was compiled from, not something the
496 # schema asked for); 'p' never changes what a pattern matches.
497 314 $flags =~ s/[up]//g;
498 314 ▶ return _scalar_literal(_fold_pattern_flags($pattern, $flags)) if length $flags;
Branch: 1 branch
BranchTrueFalseTotal
if length $flags 6 308 314
499  
500 308 ▶ return _literal($value) if _qr_round_trips($pattern);
Branch: 1 branch
BranchTrueFalseTotal
if _qr_round_trips($pattern) 298 10 308
501 10 return _scalar_literal($pattern);
502 }
503  
504 # Whether `qr/BODY/` -- BODY as _escape_pattern_body renders it, which is
505 # literally the source _literal is about to emit -- compiles back to exactly
506 # $pattern, carrying no flag it was not given.
507 #
508 # Verified by compiling that source, the same discipline _fold_pattern_flags
509 # uses, and here the only faithful one: `qr/$body/` with $body INTERPOLATED
510 # is a different thing from the literal the rendered file carries, because
511 # delimiter unescaping ('\/' -> '/') happens in the tokenizer and never
512 # touches an interpolated string. A candidate that will not compile at all
513 # (a pattern ending in a lone backslash, say) fails the check too, which is
514 # the right answer: that source would not have compiled in the rendered file
515 # either, and the string form carries it without a delimiter to escape.
516 308 ▶sub _qr_round_trips {
Subroutine _qr_round_trips: called CC=4 SCAR=14.2
517 308 my ($pattern) = @_;
518 308 my $re = eval 'qr/' . _escape_pattern_body($pattern) . '/';
519 308 ▶ return 0 unless ref $re eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $re eq "Regexp" 0 308 308
520 308 my ($text, $flags) = re::regexp_pattern($re);
521 308 ▶ $flags = '' unless defined $flags;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 0 308 308
522 308 $flags =~ s/[up]//g;
523 308 ▶ return $text eq $pattern && $flags eq '';
Condition66%2/3
$text eq $pattern && $flags eq ''
!ll&&!rl&&r
100298
MC/DC50%1/2
$text eq $pattern && $flags eq ''
$text eq $pattern $flags eq ''
Truth table66%2/3
A && B
A= $text eq $patternB= $flags eq ''
ABresult
0X0
100
111
524 }
525  
526 # The fold is verified rather than assumed: the candidate text is compiled
527 # and its own (text, flags) read back, so the result only ships when Perl
528 # agrees it carries the same modifiers. Two candidates, in order:
529 #
530 # * the text unchanged -- Perl hoists a leading (?i) INTO the flags while
531 # leaving it in the text, so a pattern upstream already wrote that way
532 # needs no prefix and must not get a duplicate one;
533 # * the text with '(?flags)' prefixed.
534 #
535 # Neither verifying is a croak, not a silent flag drop.
536 7 ▶sub _fold_pattern_flags {
Subroutine _fold_pattern_flags: called CC=6 SCAR=18.0
537 7 my ($pattern, $flags) = @_;
538 7 my $want = join '', sort split //, $flags;
539 7 for my $candidate ($pattern, '(?' . $flags . ')' . $pattern) {
540 11 ▶ my $re = eval { qr/$candidate/ } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $re = eval { do { qr/$candidate/ } } or next 10 1 11
541 10 my ($text, $got) = re::regexp_pattern($re);
542 10 ▶ $got = '' unless defined $got;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got 0 10 10
543 10 $got =~ s/[up]//g;
544 10 ▶ return $candidate
Branch: 1 branch
BranchTrueFalseTotal
if $text eq $candidate and join("", sort(split(//, $got, 0))) eq $want 6 4 10
Condition66%2/3
$text eq $candidate and join("", sort(split(//, $got, 0))) eq $want
!ll&&!rl&&r
046
MC/DC50%1/2
$text eq $candidate and join("", sort(split(//, $got, 0))) eq $want
$text eq $candidate join("", sort(split(//, $got, 0))) eq $want
Truth table66%2/3
A and B
A= $text eq $candidateB= join("", sort(split(//, $got, 0))) eq $want
ABresult
0X0
100
111
545 if $text eq $candidate && join('', sort split //, $got) eq $want;
546 }
547 1 croak "IO::K8s::CRD::Emitter: the /$flags flags on qr/$pattern/ cannot be"
548 . ' folded into the pattern text, and a CRD pattern has nowhere to'
549 . ' carry a flag';
550 }
551  
552 19 my @OPTION_ORDER = qw( required enum minimum maximum pattern default nullable preserve_unknown );
553  
554 # `required` is rendered as `required => 'schema'`: recorded for the CRD
555 # schema, never enforced at construction -- the same policy AutoGen applies,
556 # because a cluster returns `status: {}` for a fresh object whatever the
557 # status schema requires. A hand-written class that wants enforcement
558 # writes `'required'` itself.
559 174 ▶sub _options_source {
Subroutine _options_source: called CC=6 SCAR=17.9
560 174 my ($info) = @_;
561 174 ▶ my %opts = %{ $info->{options} // {} };
Condition100%2/2
$info->{"options"} // {}
l!l
9678
MC/DC100%1/1
$info->{"options"} // {}
$info->{"options"}
Truth table100%2/2
A
A= $info->{"options"}
Aresult
11
00
562 174 delete $opts{description}; # goes to POD
563 174 ▶ delete $opts{pattern} if _is_quantity_entry($info);
Branch: 1 branch
BranchTrueFalseTotal
if _is_quantity_entry($info) 3 171 174
564 174 ▶ $opts{required} = 'schema' if $info->{required};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"required"} 9 165 174
565 174 ▶ return '' unless %opts;
Branch: 1 branch
BranchTrueFalseTotal
unless %opts 131 43 174
566 54 ▶ my @parts = map { "$_ => " . ($_ eq 'pattern' ? _pattern_literal($opts{$_}) : _literal($opts{$_})) }
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'pattern' ? : 18 36 54
567 43 grep { exists $opts{$_} } @OPTION_ORDER;
568 43 return ', { ' . join(', ', @parts) . ' }';
569 }
570  
571 # The class-level ABSTRACT text from a schema's `description`: every run of
572 # whitespace (a multi-line description included -- a literal newline in a
573 # `# ABSTRACT:` comment would not compile) collapsed to one space, then the
574 # first sentence of that (up to the first '. ' or the end), the same way a
575 # hand-written class's # ABSTRACT line is one line, not the whole paragraph.
576 # Returns undef -- never an empty string -- for a missing or whitespace-only
577 # description, so the caller's fallback triggers on truthiness.
578 114 ▶sub _first_sentence {
Subroutine _first_sentence: called CC=4 SCAR=14.0
579 114 my ($text) = @_;
580 114 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 92 22 114
581 22 (my $flat = $text) =~ s/\s+/ /g;
582 22 $flat =~ s/\A\s+|\s+\z//g;
583 22 ▶ return undef unless length $flat;
Branch: 1 branch
BranchTrueFalseTotal
unless length $flat 0 22 22
584 22 my ($first) = $flat =~ /\A(.*?\.)(?:\s|\z)/;
585 22 ▶ return $first // $flat;
Condition66%2/3
$first // $flat
l!l&&r!l&&!r
2110
MC/DC0%0/2
$first // $flat
$first $flat
Truth table66%2/3
A // B
A= $firstB= $flat
ABresult
1X1
011
000
586 }
587  
588 # A schema's free-form description lands in POD (the =attr block). Guard
589 # against it breaking the surrounding document: a line starting with '='
590 # would open a bogus POD command instead of just being text -- E<61> is the
591 # POD escape for a literal '=' (ASCII 61) -- and a run of blank lines would
592 # split the paragraph into several, an artifact of however the upstream
593 # text happened to be wrapped rather than anything meaningful.
594 174 ▶sub _pod_safe {
Subroutine _pod_safe: called CC=2 SCAR=6.9
595 174 my ($text) = @_;
596 174 ▶ return $text unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 128 46 174
597 46 $text =~ s/^[ \t]+$//gm;
598 46 $text =~ s/\n{3,}/\n\n/g;
599 46 $text =~ s/^=/E<61>/gm;
600 46 return $text;
601 }
602  
603 # The value of the subresources import parameter (k158), as source in the
604 # column of the `use IO::K8s::APIObject` parameter it follows. status alone
605 # fits on the line; scale, with its three paths, gets one line per key,
606 # keys sorted and => aligned like the k8s lines, no trailing commas.
607 7 ▶sub _subresources_source {
Subroutine _subresources_source: called CC=4 SCAR=13.9
608 7 my ($subresources) = @_;
609 7 ▶ return '{}' unless %$subresources;
Branch: 1 branch
BranchTrueFalseTotal
unless %$subresources 1 6 7
610 6 ▶ return '{ status => {} }' unless $subresources->{scale};
Branch: 1 branch
BranchTrueFalseTotal
unless $subresources->{"scale"} 4 2 6
611 2 my $scale = $subresources->{scale};
612 2 my ($kw) = sort { $b <=> $a } map { length } keys %$scale;
613 my %value = (
614 scale => "{\n"
615 2 . join(",\n", map { sprintf(' %-*s => %s', $kw, $_, _scalar_literal($scale->{$_})) }
616 sort keys %$scale)
617 . "\n }"
618 );
619 2 ▶ $value{status} = '{}' if exists $subresources->{status};
Branch: 1 branch
BranchTrueFalseTotal
if exists $subresources->{"status"} 1 1 2
620 2 my ($sw) = sort { $b <=> $a } map { length } keys %value;
621 return "{\n"
622 2 . join(",\n", map { sprintf(' %-*s => %s', $sw, $_, $value{$_}) } sort keys %value)
623 . "\n }";
624 }
625  
626 114 ▶sub _render_class {
Subroutine _render_class: called CC=26 SCAR=33.0
627 114 my ($self, $class) = @_;
628 114 my $info = $class->_k8s_attr_info;
629 114 my $attrs = $class->_k8s_attributes;
630 114 ▶ my $is_top = $class->can('_is_resource') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$class->can('_is_resource') ? : 32 82 114
631 114 my $package = $self->package_for($class);
632  
633 # `metadata` is registered in the generated class's own registry (D10's
634 # AutoGen adds it via the k8s DSL so _inflate_struct knows its type),
635 # but no hand-written top-level class ever declares it explicitly --
636 # `use IO::K8s::APIObject ...;` already does, the same way this
637 # rendered source will. Emitting it again would be a harmless but
638 # un-idiomatic duplicate line no template class carries.
639 114 ▶ my %skip = $is_top ? (metadata => 1) : ();
Branch: 1 branch
BranchTrueFalseTotal
$is_top ? : 32 82 114
640  
641 # The printed field name -- quoted when it is not a bareword-safe Perl
642 # identifier -- is what actually lines up in the source, so the column
643 # width is measured on it, not on the (possibly shorter, unquoted) raw
644 # JSON key.
645 my %name = map {
646 174 ▶ my $key = $info->{$_}{json_key} // $_;
Condition33%1/3
$info->{$_}{"json_key"} // $_
l!l&&r!l&&!r
01740
MC/DC0%0/2
$info->{$_}{"json_key"} // $_
$info->{$_}{"json_key"} $_
Truth table33%1/3
A // B
A= $info->{$_}{"json_key"}B= $_
ABresult
1X1
011
000
647 174 ▶ ($_ => ($key =~ /^[A-Za-z_]\w*$/ ? $key : "'$key'"));
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /^[A-Za-z_]\w*$/ ? : 174 0 174
648 114 } grep { !$skip{$_} } @$attrs;
649  
650 114 my @nested;
651 114 my (@lines, @pod);
652 114 my $width = 0;
653 114 for my $attr (@$attrs) {
654 206 ▶ next if $skip{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if $skip{$attr} 32 174 206
655 174 ▶ $width = length $name{$attr} if length $name{$attr} > $width;
Branch: 1 branch
BranchTrueFalseTotal
if length $name{$attr} > $width 142 32 174
656 }
657 114 for my $attr (@$attrs) {
658 206 ▶ next if $skip{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if $skip{$attr} 32 174 206
659 174 my $i = $info->{$attr};
660 174 ▶ my $key = $i->{json_key} // $attr;
Condition33%1/3
$i->{"json_key"} // $attr
l!l&&r!l&&!r
01740
MC/DC0%0/2
$i->{"json_key"} // $attr
$i->{"json_key"} $attr
Truth table33%1/3
A // B
A= $i->{"json_key"}B= $attr
ABresult
1X1
011
000
661 174 my ($type, $nested) = $self->_type_source($i, $class, $key);
662 174 ▶ push @nested, $nested if $nested;
Branch: 1 branch
BranchTrueFalseTotal
if $nested 79 95 174
663 174 push @lines, sprintf("k8s %-*s => %s%s;", $width, $name{$attr}, $type, _options_source($i));
664 174 ▶ my $desc = _pod_safe($i->{options}{description}) // 'No description in the upstream schema.';
Condition100%2/2
_pod_safe($i->{"options"}{"description"}) // "No description in the upstream schema."
l!l
46128
MC/DC100%1/1
_pod_safe($i->{"options"}{"description"}) // "No description in the upstream schema."
_pod_safe($i->{"options"}{"description"})
Truth table100%2/2
A
A= _pod_safe($i->{"options"}{"description"})
Aresult
11
00
665 174 push @pod, "=attr $key\n\n$desc\n\n=cut\n";
666 }
667  
668 114 ▶ my $abstract = _first_sentence(IO::K8s::AutoGen::class_description($class))
Branch: 1 branch
BranchTrueFalseTotal
$is_top ? : 31 61 92
Condition66%2/3
_first_sentence(IO::K8s::AutoGen::class_description($class)) || ($is_top ? $class->kind : (split(/::/, $package, 0))[-1])
l!l&&r!l&&!r
22920
MC/DC0%0/2
_first_sentence(IO::K8s::AutoGen::class_description($class)) || ($is_top ? $class->kind : (split(/::/, $package, 0))[-1])
_first_sentence(IO::K8s::AutoGen::class_description($class)) ($is_top ? $class->kind : (split(/::/, $package, 0))[-1])
Truth table66%2/3
A || B
A= _first_sentence(IO::K8s::AutoGen::class_description($class))B= ($is_top ? $class->kind : (split(/::/, $package, 0))[-1])
ABresult
1X1
011
000
669 || ($is_top ? $class->kind : (split /::/, $package)[-1]);
670 114 my $header = join "\n",
671 "package $package;",
672 "# ABSTRACT: $abstract",
673 "our \$VERSION = '" . $self->version . "';";
674 114 my $use;
675 114 ▶ if ($is_top) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_top) { } 32 82 114
676 32 my $plural = $class->resource_plural;
677 32 my @params = ([ api_version => "'" . $class->api_version . "'" ]);
678 32 ▶ push @params, [ resource_plural => "'$plural'" ] if defined $plural && length $plural;
Branch: 1 branch
BranchTrueFalseTotal
if defined $plural and length $plural 32 0 32
Condition33%1/3
defined $plural and length $plural
!ll&&!rl&&r
0032
MC/DC0%0/2
defined $plural and length $plural
defined $plural length $plural
Truth table33%1/3
A and B
A= defined $pluralB= length $plural
ABresult
0X0
100
111
679 32 ▶ push @params, [ subresources => _subresources_source($class->subresources) ]
Branch: 1 branch
BranchTrueFalseTotal
if $self->subresources and $class->can("subresources") 7 25 32
Condition100%3/3
$self->subresources and $class->can("subresources")
!ll&&!rl&&r
1247
MC/DC100%2/2
$self->subresources and $class->can("subresources")
$self->subresources $class->can("subresources")
Truth table100%3/3
A and B
A= $self->subresourcesB= $class->can("subresources")
ABresult
0X0
100
111
680 if $self->subresources && $class->can('subresources');
681 32 my @use_lines;
682 32 ▶ if (@params == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params == 1) { } 0 32 32
683 0 @use_lines = ("use IO::K8s::APIObject $params[0][0] => $params[0][1];");
684 } else {
685 32 my ($width) = sort { $b <=> $a } map { length $_->[0] } @params;
686 @use_lines = ('use IO::K8s::APIObject',
687 32 join(",\n", map { sprintf(' %-*s => %s', $width, @$_) } @params) . ';');
688 }
689  
690 # overlay: with/extra, for the root Kind only. 'with' defaults to
691 # Namespaced when the class composes it and to no roles otherwise
692 # -- an overlay entry (including an explicit empty list) always
693 # wins over that default.
694 32 my $with = $self->overlay->{with};
695 32 ▶ $with = [ $class->does('IO::K8s::Role::Namespaced') ? 'IO::K8s::Role::Namespaced' : () ]
Branch: 2 branches
BranchTrueFalseTotal
unless defined $with 31 1 32
$class->does('IO::K8s::Role::Namespaced') ? : 31 0 31
696 unless defined $with;
697 32 ▶ push @use_lines, "with " . join(', ', map { "'$_'" } @$with) . ';' if @$with;
Branch: 1 branch
BranchTrueFalseTotal
if @$with 32 0 32
698 32 ▶ push @use_lines, @{ $self->overlay->{extra} // [] };
Condition100%2/2
$self->overlay->{"extra"} // []
l!l
131
MC/DC100%1/1
$self->overlay->{"extra"} // []
$self->overlay->{"extra"}
Truth table100%2/2
A
A= $self->overlay->{"extra"}
Aresult
11
00
699  
700 32 $use = join "\n", @use_lines;
701 }
702 else {
703 82 $use = 'use IO::K8s::Resource;';
704 }
705  
706 # Every value that could carry non-ASCII text -- a pattern, an enum
707 # member, a default -- is already rendered ASCII-only above (\x{HEX}
708 # escapes; see _escape_pattern_body / _quote_utf8_string). The one place
709 # non-ASCII text still reaches this file unescaped is a schema's
710 # free-form 'description', left verbatim in POD (the # ABSTRACT comment
711 # above, or an =attr block) by design -- descriptions are documentation,
712 # not data a client-side check runs against, so there is nothing to
713 # protect by mangling them. 'use utf8' and '=encoding UTF-8' are added
714 # together, only when that happens: the former is Perl's own rule for a
715 # literal non-ASCII byte anywhere in a file it compiles (POD included),
716 # the latter is what a POD viewer needs to decode that text correctly.
717 # An all-ASCII file -- the overwhelming majority -- carries neither.
718 114 ▶ if (grep { /[^\x00-\x7F]/ } ($header, $use, @lines, @pod)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/[^\x00-\x7F]/;} $header, $use, @lines, @pod) 3 111 114
719 3 $header .= "\nuse utf8;";
720 3 unshift @pod, "=encoding UTF-8\n\n=cut\n";
721 }
722  
723 114 my $source = join "\n", $header, $use, '', @lines, '', @pod, '1;', '';
724 114 return ($source, @nested);
725 }
726  
727 19 1;
728  
729 __END__
« blib/lib/IO/K8s/Role/Routable.pm ↑ Summary blib/lib/IO/K8s/CRD.pm »