| 1 | package API::Docker::Type; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: The DSL and attribute registry behind the generated Docker types | ||||||||||||||||||||||||||||
| 3 | 55 | our $VERSION = '0.006'; | |||||||||||||||||||||||||||
| 4 | 55 | our %REGISTRY; | |||||||||||||||||||||||||||
| 5 | 55 | ▶ | use Moo (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 55 | ▶ | use Moo::Role (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 55 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 55 | ▶ | use Import::Into; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 55 | ▶ | use Module::Runtime qw( use_module ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 55 | ▶ | use Package::Stash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 55 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 55 | ▶ | use Types::Standard qw( Any ArrayRef Bool HashRef InstanceOf Int Maybe Num Str ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 55 | ▶ | use API::Docker::Role::Type (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 55 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 55 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 55 | my %SCALAR_TYPE = ( | |||||||||||||||||||||||||||
| 19 | Str => Str, | ||||||||||||||||||||||||||||
| 20 | Int => Int, | ||||||||||||||||||||||||||||
| 21 | Num => Num, | ||||||||||||||||||||||||||||
| 22 | Bool => Bool, | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Names API::Docker::Role::Type already occupies. A generated attribute that | ||||||||||||||||||||||||||||
| 26 | # collided with one of these would silently replace it, so it is refused | ||||||||||||||||||||||||||||
| 27 | # instead -- the generator has to pick another Perl name and say so with an | ||||||||||||||||||||||||||||
| 28 | # explicit `wire`. | ||||||||||||||||||||||||||||
| 29 | 55 | my %RESERVED = map { ($_ => 1) } qw( | |||||||||||||||||||||||||||
| 30 | new BUILDARGS unknown_fields rejected_fields from_data from_json TO_JSON to_json | ||||||||||||||||||||||||||||
| 31 | docker_attributes docker_attribute_order docker docker_extends | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 4668 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 4668 | my ($class) = @_; | |||||||||||||||||||||||||||
| 36 | 4668 | $class->_setup_class(scalar caller); | |||||||||||||||||||||||||||
| 37 | 4668 | return; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Per-class state that has to outlive the class's own compilation, because a | ||||||||||||||||||||||||||||
| 41 | # generated class ends its body with `use namespace::clean`. That pragma takes | ||||||||||||||||||||||||||||
| 42 | # a snapshot of the package's subs when it is reached and strips every one of | ||||||||||||||||||||||||||||
| 43 | # them at the end of the class's compilation -- the imported Moo and type | ||||||||||||||||||||||||||||
| 44 | # sugar, the two DSL keywords, AND anything a role composed at that point had | ||||||||||||||||||||||||||||
| 45 | # already installed. Two consequences shape the setup below. | ||||||||||||||||||||||||||||
| 46 | # | ||||||||||||||||||||||||||||
| 47 | # has / extends -- The runtime `docker ...;` and `docker_extends ...;` lines | ||||||||||||||||||||||||||||
| 48 | # fire after that cleanup, so by then `has` and `extends` are gone from the | ||||||||||||||||||||||||||||
| 49 | # class and `$target->can('has')` returns undef. The `docker` keyword and | ||||||||||||||||||||||||||||
| 50 | # the `Str`/`Int`/... it is handed survive the same cleanup only because | ||||||||||||||||||||||||||||
| 51 | # Perl bound their CV into the call site at compile time; a name the DSL | ||||||||||||||||||||||||||||
| 52 | # looks up by string at runtime has no such binding, so the two it resolves | ||||||||||||||||||||||||||||
| 53 | # that way are captured here while they are still imported. | ||||||||||||||||||||||||||||
| 54 | # | ||||||||||||||||||||||||||||
| 55 | # the role -- API::Docker::Role::Type is NOT composed here. Composed at | ||||||||||||||||||||||||||||
| 56 | # import (a BEGIN action) its methods and its two attributes would sit in | ||||||||||||||||||||||||||||
| 57 | # namespace::clean's snapshot and be stripped with the sugar. So it is | ||||||||||||||||||||||||||||
| 58 | # composed on the first `docker`/`docker_extends` call instead -- at | ||||||||||||||||||||||||||||
| 59 | # runtime, after the cleanup, exactly as a `with 'Role'` line in a Moo | ||||||||||||||||||||||||||||
| 60 | # class body would run. Every generated class issues at least one such | ||||||||||||||||||||||||||||
| 61 | # call, and the composition lands before any object of the class is built. | ||||||||||||||||||||||||||||
| 62 | 55 | my %CLASS_SUGAR; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 4668 | ▶ | sub _setup_class { | ||||||||||||||||||||||||||
|
Subroutine
_setup_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 4668 | my ($class, $target) = @_; | |||||||||||||||||||||||||||
| 66 | 4668 | Moo->import::into($target); | |||||||||||||||||||||||||||
| 67 | 4668 | Types::Standard->import::into($target, qw( Any Bool Int Num Str )); | |||||||||||||||||||||||||||
| 68 | 4668 | $CLASS_SUGAR{$target} = { | |||||||||||||||||||||||||||
| 69 | has => $target->can('has'), | ||||||||||||||||||||||||||||
| 70 | extends => $target->can('extends'), | ||||||||||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||||||||||
| 72 | 4668 | my $stash = Package::Stash->new($target); | |||||||||||||||||||||||||||
| 73 | 4668 | ▶ | $stash->add_symbol('&docker' => sub { $class->_docker($target, @_) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 4668 | ▶ | $stash->add_symbol('&docker_extends' => sub { $class->_docker_extends($target, @_) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | 4668 | return; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # Compose API::Docker::Role::Type once, on the first DSL call the class makes. | ||||||||||||||||||||||||||||
| 79 | # Runtime, so it outlasts the class's `use namespace::clean`; idempotent, so | ||||||||||||||||||||||||||||
| 80 | # the second and later DSL calls are a cheap flag check. | ||||||||||||||||||||||||||||
| 81 | 27711 | ▶ | sub _ensure_role { | ||||||||||||||||||||||||||
|
Subroutine
_ensure_role: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 82 | 27711 | my ($target) = @_; | |||||||||||||||||||||||||||
| 83 | 27711 | ▶ | return if $CLASS_SUGAR{$target}{role_composed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 4668 | $CLASS_SUGAR{$target}{role_composed} = 1; | |||||||||||||||||||||||||||
| 85 | 4668 | Moo::Role->apply_roles_to_package($target, 'API::Docker::Role::Type'); | |||||||||||||||||||||||||||
| 86 | 4668 | return; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # The one place a short class name becomes a full one. 'Mount' is | ||||||||||||||||||||||||||||
| 90 | # API::Docker::Type::Mount; a name that already starts with the prefix is | ||||||||||||||||||||||||||||
| 91 | # left alone; a leading + means "this is the full name, take it as it is". | ||||||||||||||||||||||||||||
| 92 | # Docker's definitions are flat -- there are no groups to map, which is why | ||||||||||||||||||||||||||||
| 93 | # there is no prefix table here and only this one rule. | ||||||||||||||||||||||||||||
| 94 | 5430 | ▶ | sub _expand_class { | ||||||||||||||||||||||||||
|
Subroutine
_expand_class: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 95 | 5430 | my ($short) = @_; | |||||||||||||||||||||||||||
| 96 | 5430 | ▶ | return substr($short, 1) if $short =~ /\A\+/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 5430 | ▶ | return $short if $short =~ /\AAPI::Docker::Type::/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 5430 | return 'API::Docker::Type::' . $short; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # PortBindings <- port_bindings. One direction only; see the POD above. | ||||||||||||||||||||||||||||
| 102 | 23536 | ▶ | sub _wire_from_perl { | ||||||||||||||||||||||||||
|
Subroutine
_wire_from_perl: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | 23536 | my ($name) = @_; | |||||||||||||||||||||||||||
| 104 | 23536 | return join '', map { ucfirst } split /_/, $name; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 34746 | ▶ | sub _is_type_tiny { return blessed($_[0]) && $_[0]->isa('Type::Tiny') } | ||||||||||||||||||||||||||
|
Subroutine
_is_type_tiny: called CC=2 SCAR=7.2
Condition66%2/3
builtin::blessed($_[0]) && $_[0]->isa('Type::Tiny')
MC/DC50%1/2
builtin::blessed($_[0]) && $_[0]->isa('Type::Tiny')
builtin::blessed($_[0])
$_[0]->isa('Type::Tiny')
Truth table66%2/3
A && B
A= builtin::blessed($_[0])B= $_[0]->isa('Type::Tiny')
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # A type spec becomes a descriptor, recursively: | ||||||||||||||||||||||||||||
| 110 | # { kind => 'scalar', scalar => 'Str' } | ||||||||||||||||||||||||||||
| 111 | # { kind => 'object', class => 'API::Docker::Type::PortBinding' } | ||||||||||||||||||||||||||||
| 112 | # { kind => 'array', inner => <descriptor> } | ||||||||||||||||||||||||||||
| 113 | # { kind => 'hash', inner => <descriptor> } keys are caller data | ||||||||||||||||||||||||||||
| 114 | # { kind => 'any' } | ||||||||||||||||||||||||||||
| 115 | # One recursive shape rather than a flag per combination, so { Str, | ||||||||||||||||||||||||||||
| 116 | # ['PortBinding'] } and [[Str]] need no cases of their own anywhere. | ||||||||||||||||||||||||||||
| 117 | 34746 | ▶ | sub _parse_type { | ||||||||||||||||||||||||||
|
Subroutine
_parse_type: called CC=12 SCAR=26.5
| |||||||||||||||||||||||||||||
| 118 | 34746 | my ($spec, $where) = @_; | |||||||||||||||||||||||||||
| 119 | 34746 | ▶ | if (_is_type_tiny($spec)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 22279 | my $name = $spec->name; | |||||||||||||||||||||||||||
| 121 | 22279 | ▶ | return { kind => 'any' } if $name eq 'Any'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 21949 | ▶ | return { kind => 'scalar', scalar => $name } if $SCALAR_TYPE{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | croak __PACKAGE__ . ": $where has unsupported type " . $name; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 12467 | ▶ | if (ref $spec eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 4936 | ▶ | croak __PACKAGE__ . ": $where is an array type with " | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | . scalar(@$spec) . ' element types, it needs exactly one' | ||||||||||||||||||||||||||||
| 128 | unless @$spec == 1; | ||||||||||||||||||||||||||||
| 129 | 4936 | return { kind => 'array', inner => _parse_type($spec->[0], $where) }; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 7531 | ▶ | if (ref $spec eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 2157 | my @keys = keys %$spec; | |||||||||||||||||||||||||||
| 133 | 2157 | ▶ | croak __PACKAGE__ . ": $where is a hash type; write it as { Str, \$value_type }" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@keys == 1 and $keys[0] eq "Str"
MC/DC0%0/2
@keys == 1 and $keys[0] eq "Str"
@keys == 1
$keys[0] eq "Str"
Truth table33%1/3
A and B
A= @keys == 1B= $keys[0] eq "Str"
| |||||||||||||||||||||||||||||
| 134 | unless @keys == 1 && $keys[0] eq 'Str'; | ||||||||||||||||||||||||||||
| 135 | 2157 | return { kind => 'hash', inner => _parse_type($spec->{Str}, $where) }; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 5374 | ▶ | if (!ref $spec) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 5374 | ▶ | return { kind => 'any' } if $spec eq 'Any'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 5374 | ▶ | return { kind => 'scalar', scalar => $spec } if $SCALAR_TYPE{$spec}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 5374 | return { kind => 'object', class => _expand_class($spec) }; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 0 | croak __PACKAGE__ . ": $where has an unreadable type spec (" . ref($spec) . ')'; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 7 | ▶ | sub describe_type { | ||||||||||||||||||||||||||
|
Subroutine
describe_type: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 7 | my ($d) = @_; | |||||||||||||||||||||||||||
| 148 | 7 | my $kind = $d->{kind}; | |||||||||||||||||||||||||||
| 149 | 7 | ▶ | return lc $d->{scalar} if $kind eq 'scalar'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 5 | ▶ | return 'object<' . $d->{class} . '>' if $kind eq 'object'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 3 | ▶ | return 'array<' . describe_type($d->{inner}) . '>' if $kind eq 'array'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 1 | ▶ | return 'hash<' . describe_type($d->{inner}) . '>' if $kind eq 'hash'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | return 'any'; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # Nothing is required, so every attribute is Maybe[...]. Hash values and | ||||||||||||||||||||||||||||
| 157 | # array elements are Maybe[...] too: the daemon really does answer | ||||||||||||||||||||||||||||
| 158 | # "2377/tcp": null inside a PortMap, and croaking while inflating a response | ||||||||||||||||||||||||||||
| 159 | # we could otherwise use is not an improvement. | ||||||||||||||||||||||||||||
| 160 | 34746 | ▶ | sub _isa_for { | ||||||||||||||||||||||||||
|
Subroutine
_isa_for: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 161 | 34746 | my ($d) = @_; | |||||||||||||||||||||||||||
| 162 | 34746 | my $kind = $d->{kind}; | |||||||||||||||||||||||||||
| 163 | 34746 | ▶ | return undef if $kind eq 'any'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 34416 | ▶ | return $SCALAR_TYPE{ $d->{scalar} } if $kind eq 'scalar'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 12467 | ▶ | return InstanceOf[ $d->{class} ] if $kind eq 'object'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 7093 | my $inner = _isa_for($d->{inner}); | |||||||||||||||||||||||||||
| 167 | 7093 | ▶ | return $kind eq 'array' ? ArrayRef : HashRef unless $inner; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 168 | 6823 | ▶ | return $kind eq 'array' ? ArrayRef[ Maybe[$inner] ] : HashRef[ Maybe[$inner] ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # The boolean normalisation, borrowed from IO::K8s::Resource (../io-k8s-p5), | ||||||||||||||||||||||||||||
| 172 | # which documents the two traps: every reference is true in Perl, so \0 and a | ||||||||||||||||||||||||||||
| 173 | # JSON::PP::Boolean have to be dereferenced rather than tested; and 'false' | ||||||||||||||||||||||||||||
| 174 | # is a non-empty string and therefore true, so the strings are spelled out. | ||||||||||||||||||||||||||||
| 175 | # | ||||||||||||||||||||||||||||
| 176 | # undef stays undef rather than becoming 0. Docker tells an absent flag apart | ||||||||||||||||||||||||||||
| 177 | # from a false one, TO_JSON omits undef, and "no value" must not turn into an | ||||||||||||||||||||||||||||
| 178 | # explicit false on the wire. | ||||||||||||||||||||||||||||
| 179 | 334 | ▶ | sub _normalize_bool { | ||||||||||||||||||||||||||
|
Subroutine
_normalize_bool: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 180 | 334 | my ($value) = @_; | |||||||||||||||||||||||||||
| 181 | 334 | ▶ | if (ref $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 145 | my $reftype = Scalar::Util::reftype($value); | |||||||||||||||||||||||||||
| 183 | 145 | ▶ | croak __PACKAGE__ . ': a Bool wants a scalar or a scalar ref, got ' . $reftype | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$reftype eq "SCALAR" or $reftype eq "REF"
MC/DC50%1/2
$reftype eq "SCALAR" or $reftype eq "REF"
$reftype eq "SCALAR"
$reftype eq "REF"
Truth table66%2/3
A or B
A= $reftype eq "SCALAR"B= $reftype eq "REF"
| |||||||||||||||||||||||||||||
| 184 | unless $reftype eq 'SCALAR' || $reftype eq 'REF'; | ||||||||||||||||||||||||||||
| 185 | 142 | $value = $$value; | |||||||||||||||||||||||||||
| 186 | 142 | ▶ | croak __PACKAGE__ . ': a Bool scalar ref dereferenced to another reference (' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | . ref($value) . '), not a boolean' if ref $value; | ||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | 331 | ▶ | return undef unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 323 | ▶ | return 0 if lc($value) eq 'false'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 321 | ▶ | return $value ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # A hashref handed to an object-typed field is inflated the way the entry | ||||||||||||||||||||||||||||
| 195 | # point that started the construction reads keys: through from_data while an | ||||||||||||||||||||||||||||
| 196 | # engine response is being inflated, through new otherwise. So a nested | ||||||||||||||||||||||||||||
| 197 | # literal in a request a caller assembled takes both spellings, and a nested | ||||||||||||||||||||||||||||
| 198 | # object in a daemon response resolves wire names only -- the same | ||||||||||||||||||||||||||||
| 199 | # distinction the two entry points draw at the top level, carried one level | ||||||||||||||||||||||||||||
| 200 | # down. The class is loaded on first use rather than at declaration time: the | ||||||||||||||||||||||||||||
| 201 | # registry is the only place its name appears, and loading it while the | ||||||||||||||||||||||||||||
| 202 | # declaring class is still compiling would close a cycle the moment two | ||||||||||||||||||||||||||||
| 203 | # definitions reference each other. | ||||||||||||||||||||||||||||
| 204 | 34746 | ▶ | sub _coerce_for { | ||||||||||||||||||||||||||
|
Subroutine
_coerce_for: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 205 | 34746 | my ($d) = @_; | |||||||||||||||||||||||||||
| 206 | 34746 | my $kind = $d->{kind}; | |||||||||||||||||||||||||||
| 207 | 34746 | ▶ | return \&_normalize_bool if $kind eq 'scalar' && $d->{scalar} eq 'Bool'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$kind eq "scalar" and $d->{"scalar"} eq "Bool"
MC/DC100%2/2
$kind eq "scalar" and $d->{"scalar"} eq "Bool"
$kind eq "scalar"
$d->{"scalar"} eq "Bool"
Truth table100%3/3
A and B
A= $kind eq "scalar"B= $d->{"scalar"} eq "Bool"
| |||||||||||||||||||||||||||||
| 208 | 32562 | ▶ | if ($kind eq 'object') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 5374 | my $class = $d->{class}; | |||||||||||||||||||||||||||
| 210 | 5374 | my $loaded; | |||||||||||||||||||||||||||
| 211 | 403 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 212 | 403 | my ($value) = @_; | |||||||||||||||||||||||||||
| 213 | 403 | ▶ | return $value unless ref $value eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 192 | ▶ | $loaded ||= use_module($class); | ||||||||||||||||||||||||||
|
Condition66%2/3
$loaded ||= &use_module($class)
MC/DC0%0/2
$loaded ||= &use_module($class)
$loaded
&use_module($class)
Truth table66%2/3
A ||= B
A= $loadedB= &use_module($class)
| |||||||||||||||||||||||||||||
| 215 | 192 | ▶ | return $API::Docker::Role::Type::RESPONSE | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | ? $class->from_data($value) | ||||||||||||||||||||||||||||
| 217 | : $class->new(%$value); | ||||||||||||||||||||||||||||
| 218 | 5374 | }; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | 27188 | ▶ | my $inner = $kind eq 'array' || $kind eq 'hash' ? _coerce_for($d->{inner}) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$kind eq "array" or $kind eq "hash"
MC/DC100%2/2
$kind eq "array" or $kind eq "hash"
$kind eq "array"
$kind eq "hash"
Truth table100%3/3
A or B
A= $kind eq "array"B= $kind eq "hash"
| |||||||||||||||||||||||||||||
| 221 | 27188 | ▶ | return undef unless $inner; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 148 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 223 | 148 | my ($value) = @_; | |||||||||||||||||||||||||||
| 224 | 148 | ▶ | return $value unless ref $value eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 68 | return [ map { $inner->($_) } @$value ]; | |||||||||||||||||||||||||||
| 226 | 2118 | ▶ | } if $kind eq 'array'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 44 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 228 | 44 | my ($value) = @_; | |||||||||||||||||||||||||||
| 229 | 44 | ▶ | return $value unless ref $value eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | # The keys are the caller's data: copied across, never touched. | ||||||||||||||||||||||||||||
| 231 | 44 | return { map { ($_ => $inner->($value->{$_})) } keys %$value }; | |||||||||||||||||||||||||||
| 232 | 336 | }; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | # The serialisation half of a descriptor. Called by | ||||||||||||||||||||||||||||
| 236 | # API::Docker::Role::Type::TO_JSON, which decides what to skip. | ||||||||||||||||||||||||||||
| 237 | 751 | ▶ | sub _encode_value { | ||||||||||||||||||||||||||
|
Subroutine
_encode_value: called CC=12 SCAR=25.0
| |||||||||||||||||||||||||||||
| 238 | 751 | my ($d, $value) = @_; | |||||||||||||||||||||||||||
| 239 | 751 | ▶ | return undef unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 749 | my $kind = $d->{kind}; | |||||||||||||||||||||||||||
| 241 | 749 | ▶ | if ($kind eq 'scalar') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | 563 | my $scalar = $d->{scalar}; | |||||||||||||||||||||||||||
| 243 | 563 | ▶ | return $value ? JSON::MaybeXS::true() : JSON::MaybeXS::false() if $scalar eq 'Bool'; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 244 | 485 | ▶ | return int($value) if $scalar eq 'Int'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 393 | ▶ | return 0 + $value if $scalar eq 'Num'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | 393 | return "$value"; | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | 186 | ▶ | return $value->TO_JSON if $kind eq 'object'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | 122 | ▶ | return [ map { _encode_value($d->{inner}, $_) } @$value ] if $kind eq 'array'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | 61 | ▶ | return { map { ($_ => _encode_value($d->{inner}, $value->{$_})) } keys %$value } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | if $kind eq 'hash'; | ||||||||||||||||||||||||||||
| 252 | 10 | ▶ | return [ @$value ] if ref $value eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | 10 | ▶ | return { %$value } if ref $value eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | 0 | return $value; | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 27655 | ▶ | sub _docker { | ||||||||||||||||||||||||||
|
Subroutine
_docker: called CC=15 SCAR=27.4
| |||||||||||||||||||||||||||||
| 258 | 27655 | my ($class, $target, $name, $type_spec, %opt) = @_; | |||||||||||||||||||||||||||
| 259 | 27655 | _ensure_role($target); | |||||||||||||||||||||||||||
| 260 | 27655 | ▶ | croak __PACKAGE__ . ": '$name' is not a snake_case attribute name" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $name and $name =~ /\A[a-z][a-z0-9_]*\z/
MC/DC0%0/2
defined $name and $name =~ /\A[a-z][a-z0-9_]*\z/
defined $name
$name =~ /\A[a-z][a-z0-9_]*\z/
Truth table33%1/3
A and B
A= defined $nameB= $name =~ /\A[a-z][a-z0-9_]*\z/
| |||||||||||||||||||||||||||||
| 261 | unless defined $name && $name =~ /\A[a-z][a-z0-9_]*\z/; | ||||||||||||||||||||||||||||
| 262 | croak __PACKAGE__ . ": '$name' in $target is a name this role already uses" | ||||||||||||||||||||||||||||
| 263 | 27655 | ▶ | if $RESERVED{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | croak __PACKAGE__ . ": '$name' is declared twice in $target" | ||||||||||||||||||||||||||||
| 265 | 27655 | ▶ | if $REGISTRY{$target} && $REGISTRY{$target}{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$REGISTRY{$target} and $REGISTRY{$target}{$name}
MC/DC100%2/2
$REGISTRY{$target} and $REGISTRY{$target}{$name}
$REGISTRY{$target}
$REGISTRY{$target}{$name}
Truth table100%3/3
A and B
A= $REGISTRY{$target}B= $REGISTRY{$target}{$name}
| |||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 27654 | ▶ | my $wire = delete $opt{wire} // _wire_from_perl($name); | ||||||||||||||||||||||||||
|
Condition66%2/3
delete $opt{"wire"} // _wire_from_perl($name)
MC/DC0%0/2
delete $opt{"wire"} // _wire_from_perl($name)
delete $opt{"wire"}
_wire_from_perl($name)
Truth table66%2/3
A // B
A= delete $opt{"wire"}B= _wire_from_perl($name)
| |||||||||||||||||||||||||||||
| 268 | # The Perl-name guard above has a twin: _docker_wire_index maps a wire name | ||||||||||||||||||||||||||||
| 269 | # to one Perl name, so a second field claiming a wire name would make the | ||||||||||||||||||||||||||||
| 270 | # first unreachable on inflation while TO_JSON wrote both to that one key. | ||||||||||||||||||||||||||||
| 271 | # None of the 201 generated classes does this; the generator could emit it | ||||||||||||||||||||||||||||
| 272 | # the day a hand-picked `wire` collides with a derived one (karr k85). | ||||||||||||||||||||||||||||
| 273 | 27654 | ▶ | if (my ($taken) = sort grep { $REGISTRY{$target}{$_}{wire} eq $wire } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | 27654 | ▶ | keys %{ $REGISTRY{$target} || {} }) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$REGISTRY{$target} || {}
MC/DC100%1/1
$REGISTRY{$target} || {}
$REGISTRY{$target}
Truth table100%2/2
A
A= $REGISTRY{$target}
| |||||||||||||||||||||||||||||
| 275 | 1 | croak __PACKAGE__ . ": '$name' in $target asks for the wire name " | |||||||||||||||||||||||||||
| 276 | . "'$wire', which '$taken' already has"; | ||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | 27653 | my $since = delete $opt{since}; | |||||||||||||||||||||||||||
| 279 | 27653 | my $enum = delete $opt{enum}; | |||||||||||||||||||||||||||
| 280 | 27653 | ▶ | my $required = delete $opt{required} ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | 27653 | ▶ | croak __PACKAGE__ . ": '$name' in $target got unknown option(s): " | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | . join(', ', sort keys %opt) if %opt; | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 27653 | my $descriptor = _parse_type($type_spec, "$target\::$name"); | |||||||||||||||||||||||||||
| 285 | 27653 | my $isa = _isa_for($descriptor); | |||||||||||||||||||||||||||
| 286 | 27653 | my $coerce = _coerce_for($descriptor); | |||||||||||||||||||||||||||
| 287 | 27653 | ▶ | $REGISTRY{$target}{$name} = { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | name => $name, | ||||||||||||||||||||||||||||
| 289 | wire => $wire, | ||||||||||||||||||||||||||||
| 290 | type => $descriptor, | ||||||||||||||||||||||||||||
| 291 | since => $since, | ||||||||||||||||||||||||||||
| 292 | enum => $enum, | ||||||||||||||||||||||||||||
| 293 | required => $required, | ||||||||||||||||||||||||||||
| 294 | # The same two the Moo attribute below is given, kept so the response | ||||||||||||||||||||||||||||
| 295 | # path can ask whether a value fits before handing it to the constructor | ||||||||||||||||||||||||||||
| 296 | # rather than finding out by being croaked at (karr k83). | ||||||||||||||||||||||||||||
| 297 | isa => $isa ? Maybe[$isa] : undef, | ||||||||||||||||||||||||||||
| 298 | coerce => $coerce, | ||||||||||||||||||||||||||||
| 299 | }; | ||||||||||||||||||||||||||||
| 300 | { | ||||||||||||||||||||||||||||
| 301 | 55 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 302 | 27653 | push @{"${target}::_docker_attr_order"}, $name; | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | 27653 | API::Docker::Role::Type::_invalidate_docker_cache($target); | |||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 27653 | ▶ | my $has = $CLASS_SUGAR{$target}{has} // $target->can('has'); | ||||||||||||||||||||||||||
|
Condition33%1/3
$CLASS_SUGAR{$target}{"has"} // $target->can("has")
MC/DC0%0/2
$CLASS_SUGAR{$target}{"has"} // $target->can("has")
$CLASS_SUGAR{$target}{"has"}
$target->can("has")
Truth table33%1/3
A // B
A= $CLASS_SUGAR{$target}{"has"}B= $target->can("has")
| |||||||||||||||||||||||||||||
| 307 | 27653 | my $info = $REGISTRY{$target}{$name}; | |||||||||||||||||||||||||||
| 308 | $has->($name, | ||||||||||||||||||||||||||||
| 309 | is => 'rw', | ||||||||||||||||||||||||||||
| 310 | ($info->{isa} ? (isa => $info->{isa}) : ()), | ||||||||||||||||||||||||||||
| 311 | 27653 | ▶ | ($info->{coerce} ? (coerce => $info->{coerce}) : ()), | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 312 | ); | ||||||||||||||||||||||||||||
| 313 | 27653 | return; | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 56 | ▶ | sub _docker_extends { | ||||||||||||||||||||||||||
|
Subroutine
_docker_extends: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 317 | 56 | my ($class, $target, @parents) = @_; | |||||||||||||||||||||||||||
| 318 | 56 | _ensure_role($target); | |||||||||||||||||||||||||||
| 319 | 56 | ▶ | croak __PACKAGE__ . ": docker_extends in $target needs at least one class" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 320 | unless @parents; | ||||||||||||||||||||||||||||
| 321 | 56 | my @full = map { use_module(_expand_class($_)) } @parents; | |||||||||||||||||||||||||||
| 322 | 56 | ▶ | my $extends = $CLASS_SUGAR{$target}{extends} // $target->can('extends'); | ||||||||||||||||||||||||||
|
Condition33%1/3
$CLASS_SUGAR{$target}{"extends"} // $target->can("extends")
MC/DC0%0/2
$CLASS_SUGAR{$target}{"extends"} // $target->can("extends")
$CLASS_SUGAR{$target}{"extends"}
$target->can("extends")
Truth table33%1/3
A // B
A= $CLASS_SUGAR{$target}{"extends"}B= $target->can("extends")
| |||||||||||||||||||||||||||||
| 323 | 56 | $extends->(@full); | |||||||||||||||||||||||||||
| 324 | 56 | API::Docker::Role::Type::_invalidate_docker_cache($target); | |||||||||||||||||||||||||||
| 325 | 56 | return; | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 55 | 1; | |||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | __END__ | ||||||||||||||||||||||||||||