| 1 | package API::Docker::Role::Type; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Instance behaviour of a generated API::Docker::Type class | ||||||||||||||||||||||||||||
| 3 | 55 | our $VERSION = '0.006'; | |||||||||||||||||||||||||||
| 4 | 55 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 55 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 55 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 55 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # The merged views are rebuilt whenever API::Docker::Type registers a new | ||||||||||||||||||||||||||||
| 11 | # attribute (it calls _invalidate_docker_cache), so they can be cached. Keyed | ||||||||||||||||||||||||||||
| 12 | # by class name, one entry each. | ||||||||||||||||||||||||||||
| 13 | 55 | my %ATTR_CACHE; # class -> { perl_name => info } | |||||||||||||||||||||||||||
| 14 | my %ORDER_CACHE; # class -> [ perl_name, ... ] | ||||||||||||||||||||||||||||
| 15 | 55 | my %WIRE_CACHE; # class -> { wire_name => perl_name } | |||||||||||||||||||||||||||
| 16 | 55 | my %ENTITY_CACHE; # class -> { perl_name => 1 } (not daemon fields) | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | has unknown_fields => ( | ||||||||||||||||||||||||||||
| 20 | is => 'ro', | ||||||||||||||||||||||||||||
| 21 | 0 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 22 | 55 | ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | has rejected_fields => ( | ||||||||||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 28 | 55 | ); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # True while `from_data` is inflating a decoded engine response, so that the | ||||||||||||||||||||||||||||
| 31 | # hashref coercion in API::Docker::Type reads a nested literal the same way | ||||||||||||||||||||||||||||
| 32 | # the outermost one was read. Dynamically scoped: `local`ised around the | ||||||||||||||||||||||||||||
| 33 | # whole construction, which is what a nested from_data sees. | ||||||||||||||||||||||||||||
| 34 | 55 | our $RESPONSE = 0; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Constructor-side name resolution for `new`, and one of the two places | ||||||||||||||||||||||||||||
| 38 | # unknown fields are collected. A key is taken as a Perl attribute name | ||||||||||||||||||||||||||||
| 39 | # first and as a wire name second, because `new` assembles a REQUEST out of | ||||||||||||||||||||||||||||
| 40 | # what a caller wrote and both spellings are the caller's to choose. The | ||||||||||||||||||||||||||||
| 41 | # response path does not come through here -- see L</from_data>. | ||||||||||||||||||||||||||||
| 42 | # | ||||||||||||||||||||||||||||
| 43 | # Two keys resolving to one attribute is refused rather than decided by hash | ||||||||||||||||||||||||||||
| 44 | # order, and refused whether or not the two values agree: values that happen | ||||||||||||||||||||||||||||
| 45 | # to match are not the same thing as an unambiguous call, and the caller | ||||||||||||||||||||||||||||
| 46 | # should see the mistake instead of the luck (karr k85). | ||||||||||||||||||||||||||||
| 47 | # | ||||||||||||||||||||||||||||
| 48 | # Idempotent on purpose: where one generated class extends another (the | ||||||||||||||||||||||||||||
| 49 | # `allOf` shape, see API::Docker::Type) the role is composed into both, so | ||||||||||||||||||||||||||||
| 50 | # this modifier runs twice on the same arguments. The second pass sees every | ||||||||||||||||||||||||||||
| 51 | # key already resolved and merges an empty set into unknown_fields. | ||||||||||||||||||||||||||||
| 52 | 602 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 53 | 602 | my ($orig, $class, @args) = @_; | |||||||||||||||||||||||||||
| 54 | 602 | my $args = $class->$orig(@args); | |||||||||||||||||||||||||||
| 55 | 602 | my $known = $class->_docker_attr_registry; | |||||||||||||||||||||||||||
| 56 | 602 | my $wire = $class->_docker_wire_index; | |||||||||||||||||||||||||||
| 57 | 602 | my $mine = $class->_entity_attribute_index; | |||||||||||||||||||||||||||
| 58 | 602 | ▶ | my %unknown = %{ delete($args->{unknown_fields}) || {} }; | ||||||||||||||||||||||||||
|
Condition100%2/2
delete $args->{'unknown_fields'} || {}
MC/DC100%1/1
delete $args->{'unknown_fields'} || {}
delete $args->{'unknown_fields'}
Truth table100%2/2
A
A= delete $args->{'unknown_fields'}
| |||||||||||||||||||||||||||||
| 59 | 602 | ▶ | my %rejected = %{ delete($args->{rejected_fields}) || {} }; | ||||||||||||||||||||||||||
|
Condition100%2/2
delete $args->{'rejected_fields'} || {}
MC/DC100%1/1
delete $args->{'rejected_fields'} || {}
delete $args->{'rejected_fields'}
Truth table100%2/2
A
A= delete $args->{'rejected_fields'}
| |||||||||||||||||||||||||||||
| 60 | 602 | my (%out, %from); | |||||||||||||||||||||||||||
| 61 | 602 | for my $key (keys %$args) { | |||||||||||||||||||||||||||
| 62 | my $attr = $known->{$key} ? $key | ||||||||||||||||||||||||||||
| 63 | : defined $wire->{$key} ? $wire->{$key} | ||||||||||||||||||||||||||||
| 64 | 1776 | ▶ | : $mine->{$key} ? $key | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 65 | : undef; | ||||||||||||||||||||||||||||
| 66 | 1776 | ▶ | unless (defined $attr) { $unknown{$key} = $args->{$key}; next } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | croak __PACKAGE__ . ": $class got '" | ||||||||||||||||||||||||||||
| 68 | . join("' and '", sort($from{$attr}, $key)) | ||||||||||||||||||||||||||||
| 69 | . "' for the same field '$attr'; pass one spelling, not both" | ||||||||||||||||||||||||||||
| 70 | 1766 | ▶ | if exists $out{$attr}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 1764 | $out{$attr} = $args->{$key}; | |||||||||||||||||||||||||||
| 72 | 1764 | $from{$attr} = $key; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 600 | $out{unknown_fields} = \%unknown; | |||||||||||||||||||||||||||
| 75 | 600 | $out{rejected_fields} = \%rejected; | |||||||||||||||||||||||||||
| 76 | 600 | return \%out; | |||||||||||||||||||||||||||
| 77 | 55 | }; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 321 | ▶ | sub from_data { | ||||||||||||||||||||||||||
|
Subroutine
from_data: called CC=10 SCAR=23.3
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 321 | my ($class, $data, %extra) = @_; | |||||||||||||||||||||||||||
| 82 | 321 | ▶ | $class = ref($class) if ref($class); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 321 | ▶ | croak __PACKAGE__ . '->from_data needs a HashRef' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | unless ref $data eq 'HASH'; | ||||||||||||||||||||||||||||
| 85 | 320 | my $reg = $class->_docker_attr_registry; | |||||||||||||||||||||||||||
| 86 | 320 | my $wire = $class->_docker_wire_index; | |||||||||||||||||||||||||||
| 87 | # Lifted out of the loop rather than handled in it, so that what the loop | ||||||||||||||||||||||||||||
| 88 | # files never depends on the order the keys came out in -- the same reason | ||||||||||||||||||||||||||||
| 89 | # the BUILDARGS above deletes them before it starts. | ||||||||||||||||||||||||||||
| 90 | 320 | my %args; | |||||||||||||||||||||||||||
| 91 | 320 | ▶ | my %unknown = %{ $data->{unknown_fields} || {} }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$data->{'unknown_fields'} || {}
MC/DC0%0/1
$data->{'unknown_fields'} || {}
$data->{'unknown_fields'}
Truth table50%1/2
A
A= $data->{'unknown_fields'}
| |||||||||||||||||||||||||||||
| 92 | 320 | ▶ | my %rejected = %{ $data->{rejected_fields} || {} }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$data->{'rejected_fields'} || {}
MC/DC0%0/1
$data->{'rejected_fields'} || {}
$data->{'rejected_fields'}
Truth table50%1/2
A
A= $data->{'rejected_fields'}
| |||||||||||||||||||||||||||||
| 93 | # In effect for the coercions _fits runs below as well as for the | ||||||||||||||||||||||||||||
| 94 | # constructor at the end: a nested hashref is part of the same response | ||||||||||||||||||||||||||||
| 95 | # and has to be read as one. | ||||||||||||||||||||||||||||
| 96 | 320 | local $RESPONSE = 1; | |||||||||||||||||||||||||||
| 97 | 320 | for my $key (keys %$data) { | |||||||||||||||||||||||||||
| 98 | 1662 | ▶ | next if $key eq 'unknown_fields' || $key eq 'rejected_fields'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$key eq "unknown_fields" or $key eq "rejected_fields"
MC/DC0%0/2
$key eq "unknown_fields" or $key eq "rejected_fields"
$key eq "unknown_fields"
$key eq "rejected_fields"
Truth table33%1/3
A or B
A= $key eq "unknown_fields"B= $key eq "rejected_fields"
| |||||||||||||||||||||||||||||
| 99 | 1662 | my $attr = $wire->{$key}; | |||||||||||||||||||||||||||
| 100 | # A decoded response is a map of wire names and nothing else, so a key | ||||||||||||||||||||||||||||
| 101 | # that is not one is a field we have not heard of -- even where it spells | ||||||||||||||||||||||||||||
| 102 | # an entity attribute such as `client`. Those never come from the daemon: | ||||||||||||||||||||||||||||
| 103 | # the resource API injects them as the %extra above, kept apart from $data | ||||||||||||||||||||||||||||
| 104 | # on purpose, so a key of that name in the response cannot overwrite ours | ||||||||||||||||||||||||||||
| 105 | # and is forwarded verbatim like any other unknown field (karr k104). | ||||||||||||||||||||||||||||
| 106 | 1662 | ▶ | unless (defined $attr) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 76 | $unknown{$key} = $data->{$key}; | |||||||||||||||||||||||||||
| 108 | 76 | next; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 1586 | my ($fits, $value) = _fits($reg->{$attr}, $data->{$key}); | |||||||||||||||||||||||||||
| 111 | 1586 | ▶ | if ($fits) { $args{$attr} = $value; next } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | # A value the model cannot use costs that one field, not the response it | ||||||||||||||||||||||||||||
| 113 | # arrived in. It is kept under its wire name exactly as an unknown field | ||||||||||||||||||||||||||||
| 114 | # is, and named in rejected_fields so the caller can tell it apart from a | ||||||||||||||||||||||||||||
| 115 | # field the engine never sent (karr k83). | ||||||||||||||||||||||||||||
| 116 | 11 | $unknown{$key} = $data->{$key}; | |||||||||||||||||||||||||||
| 117 | 11 | $rejected{$key} = $attr; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | # Resolved to Perl names already, so the BUILDARGS above is a no-op over | ||||||||||||||||||||||||||||
| 120 | # them and stays idempotent across the two passes the `allOf` shape makes. | ||||||||||||||||||||||||||||
| 121 | 320 | return $class->new(%args, %extra, | |||||||||||||||||||||||||||
| 122 | unknown_fields => \%unknown, | ||||||||||||||||||||||||||||
| 123 | rejected_fields => \%rejected, | ||||||||||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # Does a value an engine sent fit the attribute the wire name resolved to? | ||||||||||||||||||||||||||||
| 128 | # Answers with the coerced value where it does. The coercion runs here rather | ||||||||||||||||||||||||||||
| 129 | # than being left to the constructor because it is what turns a nested | ||||||||||||||||||||||||||||
| 130 | # hashref into an object and what refuses a Bool that is neither -- both have | ||||||||||||||||||||||||||||
| 131 | # to happen before the value can be judged at all. Every coercion the DSL | ||||||||||||||||||||||||||||
| 132 | # builds is idempotent, so the constructor running it again on the result | ||||||||||||||||||||||||||||
| 133 | # changes nothing. | ||||||||||||||||||||||||||||
| 134 | # | ||||||||||||||||||||||||||||
| 135 | # The leniency is this sub and its one caller. `new` never comes through | ||||||||||||||||||||||||||||
| 136 | # here, so a caller who writes a value the swagger does not allow is still | ||||||||||||||||||||||||||||
| 137 | # croaked at by Moo: that is a typo in a request, not an engine being itself. | ||||||||||||||||||||||||||||
| 138 | 1586 | ▶ | sub _fits { | ||||||||||||||||||||||||||
|
Subroutine
_fits: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 139 | 1586 | my ($info, $value) = @_; | |||||||||||||||||||||||||||
| 140 | 1586 | local $@; | |||||||||||||||||||||||||||
| 141 | 1586 | ▶ | my $coerced = $info->{coerce} ? eval { $info->{coerce}->($value) } : $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 1586 | ▶ | return (0) if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 1585 | ▶ | return (0) if $info->{isa} && !$info->{isa}->check($coerced); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$info->{"isa"} and not $info->{"isa"}->check($coerced)
MC/DC100%2/2
$info->{"isa"} and not $info->{"isa"}->check($coerced)
$info->{"isa"}
not $info->{"isa"}->check($coerced)
Truth table100%3/3
A and not(B)
A= $info->{"isa"}B= not $info->{"isa"}->check($coerced)
| |||||||||||||||||||||||||||||
| 144 | 1575 | return (1, $coerced); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | sub from_json { | ||||||||||||||||||||||||||
|
Subroutine
from_json: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 0 | my ($class, $json) = @_; | |||||||||||||||||||||||||||
| 150 | 0 | return $class->from_data(JSON::MaybeXS->new(utf8 => 1)->decode($json)); | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 364 | ▶ | sub TO_JSON { | ||||||||||||||||||||||||||
|
Subroutine
TO_JSON: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | 364 | my ($self) = @_; | |||||||||||||||||||||||||||
| 156 | 364 | my %out = %{ $self->unknown_fields }; | |||||||||||||||||||||||||||
| 157 | 364 | my $reg = $self->_docker_attr_registry; | |||||||||||||||||||||||||||
| 158 | 364 | for my $attr (@{ $self->_docker_attr_order }) { | |||||||||||||||||||||||||||
| 159 | 3429 | my $value = $self->$attr; | |||||||||||||||||||||||||||
| 160 | 3429 | ▶ | next unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | $out{ $reg->{$attr}{wire} } | ||||||||||||||||||||||||||||
| 162 | 585 | = API::Docker::Type::_encode_value($reg->{$attr}{type}, $value); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | 364 | return \%out; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 9 | ▶ | sub to_json { | ||||||||||||||||||||||||||
|
Subroutine
to_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 169 | 9 | my ($self) = @_; | |||||||||||||||||||||||||||
| 170 | 9 | return JSON::MaybeXS->new(utf8 => 1, canonical => 1, convert_blessed => 1) | |||||||||||||||||||||||||||
| 171 | ->encode($self->TO_JSON); | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 1342 | ▶ | sub docker_attributes { return $_[0]->_docker_attr_registry } | ||||||||||||||||||||||||||
|
Subroutine
docker_attributes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 2 | ▶ | sub docker_attribute_order { return $_[0]->_docker_attr_order } | ||||||||||||||||||||||||||
|
Subroutine
docker_attribute_order: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | # --- attributes that are not the daemon's ---------------------------------- | ||||||||||||||||||||||||||||
| 181 | # | ||||||||||||||||||||||||||||
| 182 | # API::Docker::Role::Entity puts `client` on a generated class so the | ||||||||||||||||||||||||||||
| 183 | # convenience methods have something to delegate through. It arrives at the | ||||||||||||||||||||||||||||
| 184 | # same constructor as the daemon's fields, and without being named here it is | ||||||||||||||||||||||||||||
| 185 | # neither a registry entry nor a wire name, so BUILDARGS would file it under | ||||||||||||||||||||||||||||
| 186 | # unknown_fields -- where TO_JSON would faithfully offer the client object to | ||||||||||||||||||||||||||||
| 187 | # the engine and JSON::MaybeXS would die trying to encode it. A class that | ||||||||||||||||||||||||||||
| 188 | # composes such a role answers _entity_attributes with their names. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 602 | ▶ | sub _entity_attribute_index { | ||||||||||||||||||||||||||
|
Subroutine
_entity_attribute_index: called CC=7 SCAR=20.8
| |||||||||||||||||||||||||||||
| 191 | 602 | ▶ | my $class = ref($_[0]) || $_[0]; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||
| 192 | 602 | ▶ | return $ENTITY_CACHE{$class} //= do { | ||||||||||||||||||||||||||
|
Condition66%2/3
$ENTITY_CACHE{$class} //= do {
my(%mine) = $class->can("_entity_attributes") ? map({$_, 1;} $class->_entity_attributes) : ();
my $reg = _docker_attr_registry($class);
my $wire = _docker_wire_index($class);
foreach my $name (sort keys %mine) {
croak('API::Docker::Role::Type' . ": $class has '${name}' as an entity attribute and as " . 'a daemon field; one of the two has to be renamed') if $reg->{$name} or defined $wire->{$name};
};
\%mine
}
MC/DC0%0/2
$ENTITY_CACHE{$class} //= do {
my(%mine) = $class->can("_entity_attributes") ? map({$_, 1;} $class->_entity_attributes) : ();
my $reg = _docker_attr_registry($class);
my $wire = _docker_wire_index($class);
foreach my $name (sort keys %mine) {
croak('API::Docker::Role::Type' . ": $class has '${name}' as an entity attribute and as " . 'a daemon field; one of the two has to be renamed') if $reg->{$name} or defined $wire->{$name};
};
\%mine
}
$ENTITY_CACHE{$class}
do {
my(%mine) = $class->can("_entity_attributes") ? map({$_, 1;} $class->_entity_attributes) : ();
my $reg = _docker_attr_registry($class);
my $wire = _docker_wire_index($class);
foreach my $name (sort keys %mine) {
croak('API::Docker::Role::Type' . ": $class has '${name}' as an entity attribute and as " . 'a daemon field; one of the two has to be renamed') if $reg->{$name} or defined $wire->{$name};
};
\%mine
}
Truth table66%2/3
A //= B
A= $ENTITY_CACHE{$class}B= do {
my(%mine) = $class->can("_entity_attributes") ? map({$_, 1;} $class->_entity_attributes) : ();
my $reg = _docker_attr_registry($class);
my $wire = _docker_wire_index($class);
foreach my $name (sort keys %mine) {
croak('API::Docker::Role::Type' . ": $class has '${name}' as an entity attribute and as " . 'a daemon field; one of the two has to be renamed') if $reg->{$name} or defined $wire->{$name};
};
\%mine
}
| |||||||||||||||||||||||||||||
| 193 | my %mine = $class->can('_entity_attributes') | ||||||||||||||||||||||||||||
| 194 | 355 | ▶ | ? (map { ($_ => 1) } $class->_entity_attributes) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | : (); | ||||||||||||||||||||||||||||
| 196 | 355 | my $reg = _docker_attr_registry($class); | |||||||||||||||||||||||||||
| 197 | 355 | my $wire = _docker_wire_index($class); | |||||||||||||||||||||||||||
| 198 | # Both sets reach the same constructor, so a name in both is an ambiguity | ||||||||||||||||||||||||||||
| 199 | # nobody can resolve at runtime -- say so instead of picking one. | ||||||||||||||||||||||||||||
| 200 | 355 | for my $name (sort keys %mine) { | |||||||||||||||||||||||||||
| 201 | croak __PACKAGE__ . ": $class has '$name' as an entity attribute and as " | ||||||||||||||||||||||||||||
| 202 | . 'a daemon field; one of the two has to be renamed' | ||||||||||||||||||||||||||||
| 203 | 35 | ▶ | if $reg->{$name} || defined $wire->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$reg->{$name} or defined $wire->{$name}
MC/DC0%0/2
$reg->{$name} or defined $wire->{$name}
$reg->{$name}
defined $wire->{$name}
Truth table33%1/3
A or B
A= $reg->{$name}B= defined $wire->{$name}
| |||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | 355 | \%mine; | |||||||||||||||||||||||||||
| 206 | }; | ||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # --- merged views over @ISA ------------------------------------------------ | ||||||||||||||||||||||||||||
| 210 | # | ||||||||||||||||||||||||||||
| 211 | # A generated class that resolves an `allOf` inherits its parent's fields | ||||||||||||||||||||||||||||
| 212 | # (see API::Docker::Type), so every lookup below is the class's own registry | ||||||||||||||||||||||||||||
| 213 | # entry merged with its ancestors'. Nearest declaration wins. | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 3338 | ▶ | sub _docker_attr_registry { | ||||||||||||||||||||||||||
|
Subroutine
_docker_attr_registry: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 216 | 3338 | ▶ | my $class = ref($_[0]) || $_[0]; | ||||||||||||||||||||||||||
|
Condition66%2/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||
| 217 | 3338 | ▶ | return $ATTR_CACHE{$class} //= _merge_registry($class); | ||||||||||||||||||||||||||
|
Condition66%2/3
$ATTR_CACHE{$class} //= _merge_registry($class)
MC/DC0%0/2
$ATTR_CACHE{$class} //= _merge_registry($class)
$ATTR_CACHE{$class}
_merge_registry($class)
Truth table66%2/3
A //= B
A= $ATTR_CACHE{$class}B= _merge_registry($class)
| |||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 1587 | ▶ | sub _merge_registry { | ||||||||||||||||||||||||||
|
Subroutine
_merge_registry: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 221 | 1587 | my ($class) = @_; | |||||||||||||||||||||||||||
| 222 | 1587 | ▶ | my %info = %{ $API::Docker::Type::REGISTRY{$class} // {} }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$API::Docker::Type::REGISTRY{$class} // {}
MC/DC100%1/1
$API::Docker::Type::REGISTRY{$class} // {}
$API::Docker::Type::REGISTRY{$class}
Truth table100%2/2
A
A= $API::Docker::Type::REGISTRY{$class}
| |||||||||||||||||||||||||||||
| 223 | 55 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 224 | 1587 | for my $parent (@{"${class}::ISA"}) { | |||||||||||||||||||||||||||
| 225 | 797 | my $up = _merge_registry($parent); | |||||||||||||||||||||||||||
| 226 | 797 | ▶ | $info{$_} //= $up->{$_} for keys %$up; | ||||||||||||||||||||||||||
|
Condition33%1/3
$info{$_} //= $up->{$_}
MC/DC0%0/2
$info{$_} //= $up->{$_}
$info{$_}
$up->{$_}
Truth table33%1/3
A //= B
A= $info{$_}B= $up->{$_}
| |||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | 1587 | return \%info; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 366 | ▶ | sub _docker_attr_order { | ||||||||||||||||||||||||||
|
Subroutine
_docker_attr_order: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 232 | 366 | ▶ | my $class = ref($_[0]) || $_[0]; | ||||||||||||||||||||||||||
|
Condition66%2/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||
| 233 | 366 | ▶ | return $ORDER_CACHE{$class} //= _merge_order($class); | ||||||||||||||||||||||||||
|
Condition66%2/3
$ORDER_CACHE{$class} //= _merge_order($class)
MC/DC0%0/2
$ORDER_CACHE{$class} //= _merge_order($class)
$ORDER_CACHE{$class}
_merge_order($class)
Truth table66%2/3
A //= B
A= $ORDER_CACHE{$class}B= _merge_order($class)
| |||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 273 | ▶ | sub _merge_order { | ||||||||||||||||||||||||||
|
Subroutine
_merge_order: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 237 | 273 | my ($class) = @_; | |||||||||||||||||||||||||||
| 238 | 273 | my (@order, %seen); | |||||||||||||||||||||||||||
| 239 | 273 | _append_order($class, \@order, \%seen); | |||||||||||||||||||||||||||
| 240 | 273 | return \@order; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 550 | ▶ | sub _append_order { | ||||||||||||||||||||||||||
|
Subroutine
_append_order: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 244 | 550 | my ($class, $order, $seen) = @_; | |||||||||||||||||||||||||||
| 245 | 55 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 246 | # Parents first: the swagger lists the inherited `$ref` ahead of the | ||||||||||||||||||||||||||||
| 247 | # class's own properties, and this keeps TO_JSON in that order. | ||||||||||||||||||||||||||||
| 248 | 550 | _append_order($_, $order, $seen) for @{"${class}::ISA"}; | |||||||||||||||||||||||||||
| 249 | 550 | for my $attr (@{"${class}::_docker_attr_order"}) { | |||||||||||||||||||||||||||
| 250 | 1842 | ▶ | next if $seen->{$attr}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 1842 | push @$order, $attr; | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | 550 | return; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 1277 | ▶ | sub _docker_wire_index { | ||||||||||||||||||||||||||
|
Subroutine
_docker_wire_index: called CC=3 SCAR=11.6
| |||||||||||||||||||||||||||||
| 257 | 1277 | ▶ | my $class = ref($_[0]) || $_[0]; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||
| 258 | 1277 | ▶ | return $WIRE_CACHE{$class} //= do { | ||||||||||||||||||||||||||
|
Condition66%2/3
$WIRE_CACHE{$class} //= do {
my $reg = _docker_attr_registry($class);
+{map({$reg->{$_}{"wire"}, $_;} keys %$reg)}
}
MC/DC0%0/2
$WIRE_CACHE{$class} //= do {
my $reg = _docker_attr_registry($class);
+{map({$reg->{$_}{"wire"}, $_;} keys %$reg)}
}
$WIRE_CACHE{$class}
do {
my $reg = _docker_attr_registry($class);
+{map({$reg->{$_}{"wire"}, $_;} keys %$reg)}
}
Truth table66%2/3
A //= B
A= $WIRE_CACHE{$class}B= do {
my $reg = _docker_attr_registry($class);
+{map({$reg->{$_}{"wire"}, $_;} keys %$reg)}
}
| |||||||||||||||||||||||||||||
| 259 | 355 | my $reg = _docker_attr_registry($class); | |||||||||||||||||||||||||||
| 260 | 355 | +{ map { ($reg->{$_}{wire} => $_) } keys %$reg }; | |||||||||||||||||||||||||||
| 261 | }; | ||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | # Called by API::Docker::Type after every registration: a merged view | ||||||||||||||||||||||||||||
| 265 | # computed before a parent gained an attribute must not survive. | ||||||||||||||||||||||||||||
| 266 | 27709 | ▶ | sub _invalidate_docker_cache { | ||||||||||||||||||||||||||
|
Subroutine
_invalidate_docker_cache: called CC=4 SCAR=15.7
| |||||||||||||||||||||||||||||
| 267 | 27709 | my ($class) = @_; | |||||||||||||||||||||||||||
| 268 | 27709 | my %sweep; | |||||||||||||||||||||||||||
| 269 | 27709 | @sweep{ keys %ATTR_CACHE, keys %ORDER_CACHE, keys %WIRE_CACHE, | |||||||||||||||||||||||||||
| 270 | keys %ENTITY_CACHE } = (); | ||||||||||||||||||||||||||||
| 271 | 27709 | for my $cached (keys %sweep) { | |||||||||||||||||||||||||||
| 272 | 138144 | ▶ | next unless $cached eq $class || $cached->isa($class); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$cached eq $class or $cached->isa($class)
MC/DC0%0/2
$cached eq $class or $cached->isa($class)
$cached eq $class
$cached->isa($class)
Truth table33%1/3
A or B
A= $cached eq $classB= $cached->isa($class)
| |||||||||||||||||||||||||||||
| 273 | 0 | delete $ATTR_CACHE{$cached}; | |||||||||||||||||||||||||||
| 274 | 0 | delete $ORDER_CACHE{$cached}; | |||||||||||||||||||||||||||
| 275 | 0 | delete $WIRE_CACHE{$cached}; | |||||||||||||||||||||||||||
| 276 | 0 | delete $ENTITY_CACHE{$cached}; | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | 27709 | delete $ATTR_CACHE{$class}; | |||||||||||||||||||||||||||
| 279 | 27709 | delete $ORDER_CACHE{$class}; | |||||||||||||||||||||||||||
| 280 | 27709 | delete $WIRE_CACHE{$class}; | |||||||||||||||||||||||||||
| 281 | 27709 | delete $ENTITY_CACHE{$class}; | |||||||||||||||||||||||||||
| 282 | 27709 | return; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 55 | 1; | |||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | __END__ | ||||||||||||||||||||||||||||