File Coverage

blib/lib/IO/K8s/List.pm
Criterion Covered Total %
statement 14 45 31.1
branch 0 22 0.0
condition 0 21 0.0
subroutine 5 9 55.5
pod 2 4 50.0
total 21 101 20.7


line stmt bran cond sub pod time code
1             package IO::K8s::List;
2             # ABSTRACT: Generic list container for Kubernetes API responses
3             our $VERSION = '1.008';
4 1     1   1210 use v5.10;
  1         7  
5 1     1   7 use Moo;
  1         2  
  1         8  
6 1     1   483 use Types::Standard qw( ArrayRef InstanceOf Maybe Str );
  1         2  
  1         12  
7 1     1   4016 use JSON::MaybeXS ();
  1         3  
  1         28  
8 1     1   6 use Scalar::Util qw(blessed);
  1         2  
  1         855  
9              
10              
11             has items => (
12             is => 'ro',
13             isa => ArrayRef,
14             required => 1,
15             );
16              
17              
18             has metadata => (
19             is => 'ro',
20             isa => Maybe[InstanceOf['IO::K8s::Apimachinery::Pkg::Apis::Meta::V1::ListMeta']],
21             );
22              
23              
24             has _item_class => (
25             is => 'ro',
26             isa => Maybe[Str],
27             init_arg => 'item_class',
28             );
29              
30              
31             sub api_version {
32 0     0 1   my $self = shift;
33              
34             # Try to get from first item
35 0 0 0       if (@{$self->items} && blessed($self->items->[0]) && $self->items->[0]->can('api_version')) {
  0   0        
36 0           return $self->items->[0]->api_version;
37             }
38              
39             # Fall back to deriving from item_class
40 0 0         if (my $class = $self->_item_class) {
41 0 0         if ($class =~ /^IO::K8s::Api::(\w+)::(\w+)::/) {
42 0           my ($group, $version) = ($1, $2);
43 0           $version = lc($version);
44 0 0         return $group eq 'Core' ? $version : lc($group) . '/' . $version;
45             }
46             }
47              
48 0           return undef;
49             }
50              
51              
52             sub kind {
53 0     0 1   my $self = shift;
54              
55             # Try to get from first item
56 0 0 0       if (@{$self->items} && blessed($self->items->[0]) && $self->items->[0]->can('kind')) {
  0   0        
57 0           return $self->items->[0]->kind . 'List';
58             }
59              
60             # Fall back to deriving from item_class
61 0 0         if (my $class = $self->_item_class) {
62 0 0         if ($class =~ /::(\w+)$/) {
63 0           return $1 . 'List';
64             }
65             }
66              
67 0           return undef;
68             }
69              
70              
71             sub TO_JSON {
72 0     0 0   my $self = shift;
73 0           my %data;
74              
75 0 0         $data{apiVersion} = $self->api_version if $self->api_version;
76 0 0         $data{kind} = $self->kind if $self->kind;
77              
78             $data{items} = [
79 0 0 0       map { blessed($_) && $_->can('TO_JSON') ? $_->TO_JSON : $_ } @{$self->items}
  0            
  0            
80             ];
81              
82 0 0 0       if ($self->metadata && blessed($self->metadata) && $self->metadata->can('TO_JSON')) {
      0        
83 0           $data{metadata} = $self->metadata->TO_JSON;
84             }
85              
86 0           return \%data;
87             }
88              
89             sub to_json {
90 0     0 0   my $self = shift;
91 0           state $json = JSON::MaybeXS->new->canonical;
92 0           return $json->encode($self->TO_JSON);
93             }
94              
95             1;
96              
97             __END__