lib/IO/K8s/List.pm

filter: Statementstmt 98.2% 110 / 112 Branchbran 82.8% 53 / 64 Conditioncond 75.6% 31 / 41 MC/DCmcdc 52.1% 12 / 23 Subroutinesub 100% 15 / 15 Podpod 100% 6 / 6 totaltotal 86.9% 227 / 261 CC 49 SCAR 24.8
CC
49
Cov
89%
FROM_STRUCT
21.1
TO_JSON
12.8
kind
8.4
CRAP 11.9
« blib/lib/IO/K8s/APIObject.pm ↑ Summary blib/lib/IO/K8s.pm »
1 package IO::K8s::List;
2 # ABSTRACT: Generic list container for Kubernetes API responses
3 10 our $VERSION = '1.112';
4 10 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 with 'IO::K8s::Role::Resource';
7 10 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Module::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Types::Standard qw( Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Carp treats IO::K8s as part of this module (k170), the way IO::K8s::CRD
14 # does (k165): FROM_STRUCT's own croak reached through IO::K8s->inflate
15 # names the line that called inflate, not the FROM_STRUCT call in
16 # lib/IO/K8s.pm, and a croak of the IO::K8s shape helpers FROM_STRUCT calls
17 # -- and a constructor error for an item, which IO::K8s moves to Carp's
18 # answer (k164) -- names the caller of inflate, FROM_STRUCT or from_json,
19 # not this file. A caller in any other package still sees its own line.
20 10 our @CARP_NOT = ('IO::K8s');
21  
22  
23 10 has items => (
24 is => 'ro',
25 isa => Types::Standard::ArrayRef,
26 required => 1,
27 );
28  
29  
30 10 has metadata => (
31 is => 'ro',
32 isa => Types::Standard::Maybe[
33 Types::Standard::InstanceOf['IO::K8s::Apimachinery::Pkg::Apis::Meta::V1::ListMeta']
34 ],
35 );
36  
37  
38 10 has _item_class => (
39 is => 'ro',
40 isa => Types::Standard::Maybe[Str],
41 init_arg => 'item_class',
42 );
43  
44  
45 # Strip the '+' convention this distribution uses everywhere else to mean
46 # "this is already a full class name" (k49). Returns undef if there is
47 # no item_class at all, same as _item_class itself.
48 151 ▶sub _resolved_item_class {
Subroutine _resolved_item_class: called CC=2 SCAR=7.0
49 151 my ($self) = @_;
50 151 my $class = $self->_item_class;
51 151 ▶ return undef unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 151 151
52 151 $class =~ s/\A\+//;
53 151 return $class;
54 }
55  
56 123 ▶sub api_version {
Subroutine api_version: called CC=8 SCAR=21.0
Pod documented
57 123 my ($self) = @_;
58  
59 # Try to get from first item
60 123 ▶ if (@{$self->items} && Scalar::Util::blessed($self->items->[0]) && $self->items->[0]->can('api_version')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->items;} and builtin::blessed($self->items->[0]) and $self->items->[0]->can("api_version")) 16 103 119
Condition66%4/6
@{$self->items;} and builtin::blessed($self->items->[0]) and $self->items->[0]->can("api_version")
!ll&&!rl&&r
103016
@{$self->items;} and builtin::blessed($self->items->[0])
!ll&&!rl&&r
103016
MC/DC33%1/3
@{$self->items;} and builtin::blessed($self->items->[0]) and $self->items->[0]->can("api_version")
@{$self->items;} builtin::blessed($self->items->[0]) $self->items->[0]->can("api_version")
Truth table50%2/4
A and B and C
A= @{$self->items;}B= builtin::blessed($self->items->[0])C= $self->items->[0]->can("api_version")
ABCresult
0XX0
10X0
1100
1111
61 16 return $self->items->[0]->api_version;
62 }
63  
64 # Fall back to the item_class's own api_version class method, which
65 # knows the full wire group (rbac.authorization.k8s.io, storage.k8s.io,
66 # apiextensions.k8s.io, ...). undef unless the class loads, has an
67 # api_version method and answers without error.
68 103 ▶ if (my $class = $self->_resolved_item_class) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = $self->_resolved_item_class) 103 0 103
69 103 eval { Module::Runtime::require_module($class) };
70 103 ▶ return undef if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 20 83 103
71 83 ▶ return undef unless $class->can('api_version');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("api_version") 4 79 83
72 79 my $api_version = eval { $class->api_version };
73 79 ▶ return undef if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 4 75 79
74 75 return $api_version;
75 }
76  
77 0 return undef;
78 }
79  
80  
81 74 ▶sub kind {
Subroutine kind: called CC=8 SCAR=21.3
Pod documented
82 74 my $self = shift;
83  
84 # Try to get from first item
85 74 ▶ if (@{$self->items} && Scalar::Util::blessed($self->items->[0]) && $self->items->[0]->can('kind')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->items;} and builtin::blessed($self->items->[0]) and $self->items->[0]->can("kind")) 26 48 74
Condition66%4/6
@{$self->items;} and builtin::blessed($self->items->[0]) and $self->items->[0]->can("kind")
!ll&&!rl&&r
48026
@{$self->items;} and builtin::blessed($self->items->[0])
!ll&&!rl&&r
48026
MC/DC33%1/3
@{$self->items;} and builtin::blessed($self->items->[0]) and $self->items->[0]->can("kind")
@{$self->items;} builtin::blessed($self->items->[0]) $self->items->[0]->can("kind")
Truth table50%2/4
A and B and C
A= @{$self->items;}B= builtin::blessed($self->items->[0])C= $self->items->[0]->can("kind")
ABCresult
0XX0
10X0
1100
1111
86 26 return $self->items->[0]->kind . 'List';
87 }
88  
89 # Fall back to deriving from item_class -- but only when api_version()
90 # can also answer for it. kind() and api_version() derive from the same
91 # item_class, and this class must never serialize a kind without an
92 # apiVersion alongside it (k49): a short or unloadable item_class
93 # (e.g. 'Pod' instead of 'IO::K8s::Api::Core::V1::Pod') would otherwise
94 # still produce a Kind by regex here while api_version() -- which
95 # actually has to load the class -- came back undef.
96 48 ▶ if (my $class = $self->_resolved_item_class) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = $self->_resolved_item_class) 48 0 48
97 48 ▶ return undef unless defined $self->api_version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->api_version 14 34 48
98  
99 34 ▶ if ($class =~ /::(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /::(\w+)$/) 31 3 34
100 31 return $1 . 'List';
101 }
102  
103 # No '::' at all: a CRD registered as a single-segment top-level
104 # package, so the whole name is the last segment -- the same
105 # derivation IO::K8s::Role::APIObject::kind() makes (k38). This
106 # is the empty-list case item_class exists for, and without it
107 # TO_JSON emits apiVersion but no kind: (k41).
108 3 ▶ if ($class =~ /\A(\w+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /\A(\w+)\z/) 3 0 3
109 3 return $1 . 'List';
110 }
111 }
112  
113 0 return undef;
114 }
115  
116  
117  
118 10 my $LIST_META = 'IO::K8s::Apimachinery::Pkg::Apis::Meta::V1::ListMeta';
119  
120 # Mirrors IO::K8s::_die_resolution_error()'s message format exactly, so a
121 # caller can't tell whether a GVK failure came from a top-level entry point
122 # or from inside a List's own item inflation. Kept local rather than
123 # reaching into IO::K8s's private subs -- this class does not otherwise
124 # depend on IO::K8s internals beyond the $k8s instance it is handed.
125 2 ▶sub _die_gvk {
Subroutine _die_gvk: called CC=3 SCAR=11.7
126 2 my ($kind, $api_version) = @_;
127 2 ▶ my $display = !defined($api_version) ? '<undef>'
Branch: 2 branches
BranchTrueFalseTotal
!defined($api_version) ? : 0 2 2
length $api_version ? : 2 0 2
128 : length($api_version) ? $api_version
129 : '<empty>';
130 2 die "Cannot resolve Kubernetes GVK: kind '$kind', apiVersion '$display'\n";
131 }
132  
133 363 ▶sub FROM_STRUCT {
Subroutine FROM_STRUCT: called CC=21 SCAR=30.5
Pod documented
134 363 my ($class, $struct, $k8s) = @_;
135  
136 363 ▶ $k8s //= do { require IO::K8s; IO::K8s->new };
Condition66%2/3
$k8s //= do { require IO::K8s; "IO::K8s"->new }
l!l&&r!l&&!r
335280
MC/DC0%0/2
$k8s //= do { require IO::K8s; "IO::K8s"->new }
$k8s do { require IO::K8s; "IO::K8s"->new }
Truth table66%2/3
A //= B
A= $k8sB= do { require IO::K8s; "IO::K8s"->new }
ABresult
1X1
011
000
137  
138 # The envelope, `items` and each item are refused in the message form
139 # every other inflation uses (k146, k154), through IO::K8s's own
140 # helpers so the wording cannot drift (k161). They used to reach a bare
141 # Perl dereference: "Not a HASH reference" for a struct that is no hash,
142 # "Can't use string as an ARRAY ref" for items => 'x'. undef stays "no
143 # value" -- an empty list, like a missing `items`.
144 363 ▶ $k8s->_refuse_object_shape($class, $k8s->_describe_shape($struct))
Branch: 1 branch
BranchTrueFalseTotal
if defined $struct and ref $struct ne "HASH" 31 332 363
Condition100%3/3
defined $struct and ref $struct ne "HASH"
!ll&&!rl&&r
133131
MC/DC100%2/2
defined $struct and ref $struct ne "HASH"
defined $struct ref $struct ne "HASH"
Truth table100%3/3
A and B
A= defined $structB= ref $struct ne "HASH"
ABresult
0X0
100
111
145 if defined $struct && ref $struct ne 'HASH';
146  
147 332 my $kind = $struct->{kind};
148 332 my $api_version = $struct->{apiVersion};
149  
150 # item_class as an explicit override -- checked before any derivation,
151 # exactly like the empty-list case item_class already covers for a
152 # hand-built list. Same '+' handling as everywhere else (k49).
153 332 my $resolved_item_class;
154 332 ▶ if (defined(my $override = $struct->{item_class})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $override = $struct->{'item_class'})) { } 82 250 332
elsif (defined $kind and $kind =~ /\A(.+)List\z/) { } 247 4 251
155 # A class name and nothing else (k166). A reference used to reach
156 # Module::Runtime ("argument is not a module name") with items, or
157 # the attribute's Maybe[Str] check without; '' or a bare '+' died
158 # "`' is not a module name" with items and was kept silently without.
159 # Carp qualified, not imported: no croak method on a resource class
160 # (k118, t/27_no_import_leak.t).
161 82 ▶ Carp::croak($class.'->FROM_STRUCT: item_class must be a class name, got '
Branch: 3 branches
BranchTrueFalseTotal
if ref $override or $override eq "" or $override eq "+" 77 5 82
ref $override ? : 53 24 77
$override eq '' ? : 12 12 24
Condition100%6/6
ref $override or $override eq "" or $override eq "+"
l!l&&r!l&&!r
65125
ref $override or $override eq ""
l!l&&r!l&&!r
531217
MC/DC100%3/3
ref $override or $override eq "" or $override eq "+"
ref $override $override eq "" $override eq "+"
Truth table100%4/4
A or B or C
A= ref $overrideB= $override eq ""C= $override eq "+"
ABCresult
1XX1
01X1
0011
0000
162 .(ref $override ? $k8s->_describe_shape($override)
163 : $override eq '' ? 'an empty string'
164 : "a bare '+'"))
165 if ref $override || $override eq '' || $override eq '+';
166 5 $override =~ s/\A\+//;
167 5 $resolved_item_class = $override;
168 }
169 ▶ elsif (defined $kind && $kind =~ /\A(.+)List\z/) {
Condition100%3/3
defined $kind and $kind =~ /\A(.+)List\z/
!ll&&!rl&&r
12247
MC/DC100%2/2
defined $kind and $kind =~ /\A(.+)List\z/
defined $kind $kind =~ /\A(.+)List\z/
Truth table100%3/3
A and B
A= defined $kindB= $kind =~ /\A(.+)List\z/
ABresult
0X0
100
111
170 247 my $item_kind = $1;
171 247 $resolved_item_class = $k8s->expand_class($item_kind, $api_version);
172 247 ▶ _die_gvk($item_kind, $api_version) unless defined $resolved_item_class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $resolved_item_class 2 245 247
173 }
174 # else: a bare 'kind: List' (or no kind at all) with no override -- items
175 # are only inflatable if the list turns out to be empty; see below.
176  
177 253 my $metadata;
178 253 ▶ if (defined(my $meta_struct = $struct->{metadata})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $meta_struct = $struct->{"metadata"})) 48 205 253
179 # $LIST_META is already a final class name -- go straight to the
180 # pre-expanded path so it is not re-interpreted by expand_class()
181 # (k35), same reasoning IO::K8s::_inflate_struct applies to
182 # every nested object it inflates.
183 48 $metadata = $k8s->_struct_to_object_expanded($LIST_META, $meta_struct,
184 $class.' field metadata');
185 }
186  
187 217 ▶ my $items = $struct->{items} // [];
Condition100%2/2
$struct->{"items"} // []
l!l
19819
MC/DC100%1/1
$struct->{"items"} // []
$struct->{"items"}
Truth table100%2/2
A
A= $struct->{"items"}
Aresult
11
00
188 217 ▶ $k8s->_refuse_container_shape($class, 'items', 'ARRAY', $resolved_item_class, $items)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $items eq "ARRAY" 115 102 217
189 unless ref $items eq 'ARRAY';
190  
191 102 my @items;
192 102 for my $i (0 .. $#$items) {
193 113 ▶ die "Cannot inflate item in List payload: kind '"
Branch: 2 branches
BranchTrueFalseTotal
unless defined $resolved_item_class 0 113 113
defined $kind ? : 0 0 0
194 . (defined $kind ? $kind : '<undef>')
195 . "' has no derivable item type and no 'item_class' override was given\n"
196 unless defined $resolved_item_class;
197 # Already resolved above (via expand_class() or the override) --
198 # the pre-expanded path, for the same k35 reason as metadata. The
199 # third argument names the element for a shape error, worded like
200 # IO::K8s::_inflate_struct names an element of an object array.
201 113 push @items, $k8s->_struct_to_object_expanded($resolved_item_class, $items->[$i],
202 $class.' field items at element '.$i);
203 }
204  
205 # Any top-level key besides the ones this envelope itself understands
206 # (kind/apiVersion, consumed above to derive item_class; metadata/items,
207 # handled below) is forwarded straight into ->new(). List composes
208 # IO::K8s::Role::Resource for exactly this: its `BUILD` bags an
209 # undeclared key into _unknown_fields, or dies under strict -- the
210 # same envelope-level handling every non-list resource already gets
211 # (k99). Items themselves already round-trip through their own class's
212 # composition of the role; this only covers the list wrapper.
213 59 my %known_top_level = map { $_ => 1 } qw(kind apiVersion metadata items item_class);
214 59 my %extra = map { $_ => $struct->{$_} } grep { !$known_top_level{$_} } keys %$struct;
215  
216 59 ▶ return $class->new(
Branch: 2 branches
BranchTrueFalseTotal
defined $metadata ? : 12 47 59
defined $resolved_item_class ? : 58 1 59
217 items => \@items,
218 (defined $metadata ? (metadata => $metadata) : ()),
219 (defined $resolved_item_class ? (item_class => $resolved_item_class) : ()),
220 %extra,
221 );
222 }
223  
224  
225 25 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=12 SCAR=25.5
Pod documented
226 25 my $self = shift;
227 25 my %data;
228  
229 25 ▶ $data{apiVersion} = $self->api_version if $self->api_version;
Branch: 1 branch
BranchTrueFalseTotal
if $self->api_version 19 6 25
230 25 ▶ $data{kind} = $self->kind if $self->kind;
Branch: 1 branch
BranchTrueFalseTotal
if $self->kind 19 6 25
231  
232 $data{items} = [
233 25 ▶ map { Scalar::Util::blessed($_) && $_->can('TO_JSON') ? $_->TO_JSON : $_ } @{$self->items}
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) && $_->can('TO_JSON') ? : 7 0 7
Condition33%1/3
builtin::blessed($_) and $_->can("TO_JSON")
!ll&&!rl&&r
007
MC/DC0%0/2
builtin::blessed($_) and $_->can("TO_JSON")
builtin::blessed($_) $_->can("TO_JSON")
Truth table33%1/3
A and B
A= builtin::blessed($_)B= $_->can("TO_JSON")
ABresult
0X0
100
111
234 ];
235  
236 25 ▶ if ($self->metadata && Scalar::Util::blessed($self->metadata) && $self->metadata->can('TO_JSON')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->metadata and builtin::blessed($self->metadata) and $self->metadata->can("TO_JSON")) 3 22 25
Condition66%4/6
$self->metadata and builtin::blessed($self->metadata) and $self->metadata->can("TO_JSON")
!ll&&!rl&&r
2203
$self->metadata and builtin::blessed($self->metadata)
!ll&&!rl&&r
2203
MC/DC33%1/3
$self->metadata and builtin::blessed($self->metadata) and $self->metadata->can("TO_JSON")
$self->metadata builtin::blessed($self->metadata) $self->metadata->can("TO_JSON")
Truth table50%2/4
A and B and C
A= $self->metadataB= builtin::blessed($self->metadata)C= $self->metadata->can("TO_JSON")
ABCresult
0XX0
10X0
1100
1111
237 3 $data{metadata} = $self->metadata->TO_JSON;
238 }
239  
240 # Unknown top-level fields ride along (D1/k99), the same bag every
241 # Role::Resource class re-emits from TO_JSON -- List composes the role
242 # for this mechanism even though it keeps its own TO_JSON. A declared
243 # field above wins on a name clash, same rule as everywhere else.
244 # _copy_one_level is the role's, reached unqualified because composing
245 # the role installed it into this package too (k54 depth).
246 25 my $extra = $self->_unknown_fields;
247 25 ▶ if ($extra && %$extra) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra and %$extra) 4 21 25
Condition66%2/3
$extra and %$extra
!ll&&!rl&&r
0214
MC/DC50%1/2
$extra and %$extra
$extra %$extra
Truth table66%2/3
A and B
A= $extraB= %$extra
ABresult
0X0
100
111
248 4 for my $key (keys %$extra) {
249 4 ▶ next if exists $data{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data{$key} 0 4 4
250 4 $data{$key} = _copy_one_level($extra->{$key});
251 }
252 }
253  
254 25 return \%data;
255 }
256  
257 20 ▶sub to_json {
Subroutine to_json: called CC=1 SCAR=0.0
Pod documented
258 20 my $self = shift;
259 # utf8 => 1, so this emits a UTF-8 byte string -- the same convention every
260 # IO::K8s::Role::Resource class uses (k53), and exactly what
261 # L</from_json> reads back. A character string here would round-trip
262 # non-ASCII data to mojibake once it hit a byte-oriented sink (k64).
263 20 state $json = JSON::MaybeXS->new(utf8 => 1, canonical => 1);
264 20 return $json->encode($self->TO_JSON);
265 }
266  
267  
268  
269 52 ▶sub from_json {
Subroutine from_json: called CC=1 SCAR=0.0
Pod documented
270 52 my ($class, $json_str, $k8s) = @_;
271 52 state $json = JSON::MaybeXS->new(utf8 => 1);
272 52 return $class->FROM_STRUCT($json->decode($json_str), $k8s);
273 }
274  
275 10 1;
276  
277 __END__
« blib/lib/IO/K8s/APIObject.pm ↑ Summary blib/lib/IO/K8s.pm »