| 1 | package API::Docker::Role::Filters; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: The filters query parameter, normalised into the one shape the engine reads | ||||||||||||||||||||||||||||
| 3 | 52 | our $VERSION = '0.006'; | |||||||||||||||||||||||||||
| 4 | 52 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 52 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 52 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # The boolean classes JSON::MaybeXS hands back across its backends. Named | ||||||||||||||||||||||||||||
| 10 | # rather than duck-typed: a blessed object that merely overloads bool is not | ||||||||||||||||||||||||||||
| 11 | # a claim of being a JSON boolean. | ||||||||||||||||||||||||||||
| 12 | 52 | my %BOOLEAN_CLASS = map { $_ => 1 } qw( | |||||||||||||||||||||||||||
| 13 | JSON::PP::Boolean | ||||||||||||||||||||||||||||
| 14 | Types::Serialiser::Boolean | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 41 | ▶ | sub _normalise_filters { | ||||||||||||||||||||||||||
|
Subroutine
_normalise_filters: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 18 | 41 | my ($self, $filters) = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 41 | ▶ | croak __PACKAGE__ . '->_normalise_filters filters must be a HashRef of ' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | . 'filter name to value, e.g. { dangling => [\'true\'] }' | ||||||||||||||||||||||||||||
| 22 | unless ref $filters eq 'HASH'; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 38 | my %normalised; | |||||||||||||||||||||||||||
| 25 | 38 | for my $name (sort keys %$filters) { | |||||||||||||||||||||||||||
| 26 | 39 | ▶ | croak __PACKAGE__ . '->_normalise_filters filter name must not be empty' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | unless length $name; | ||||||||||||||||||||||||||||
| 28 | 38 | my $value = $filters->{$name}; | |||||||||||||||||||||||||||
| 29 | # A bare value is one value, not a mistake worth refusing -- the engine | ||||||||||||||||||||||||||||
| 30 | # is the one that insists on the list. | ||||||||||||||||||||||||||||
| 31 | 38 | ▶ | my @values = ref $value eq 'ARRAY' ? @$value : ($value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 38 | $normalised{$name} = [ map { $self->_normalise_filter_value($name, $_) } @values ]; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 17 | return \%normalised; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 38 | ▶ | sub _normalise_filter_value { | ||||||||||||||||||||||||||
|
Subroutine
_normalise_filter_value: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||
| 39 | 38 | my ($self, $name, $value) = @_; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 38 | my $where = __PACKAGE__ . '->_normalise_filters filter \'' . $name . '\' '; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 38 | ▶ | croak $where . 'has an undefined value; the engine reads a JSON null into ' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | . 'a string as the empty string and rejects it there' | ||||||||||||||||||||||||||||
| 45 | unless defined $value; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 22 | my $ref = ref $value; | |||||||||||||||||||||||||||
| 48 | 22 | ▶ | return $value ? 'true' : 'false' if $BOOLEAN_CLASS{$ref}; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 20 | ▶ | if ($ref eq 'SCALAR') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 3 | ▶ | croak $where . 'is a ScalarRef to something other than 1 or 0; \\1 and ' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$$value eq 1 or $$value eq 0
MC/DC100%2/2
$$value eq 1 or $$value eq 0
$$value eq 1
$$value eq 0
Truth table100%3/3
A or B
A= $$value eq 1B= $$value eq 0
| |||||||||||||||||||||||||||||
| 52 | . '\\0 are read as the booleans \'true\' and \'false\'' | ||||||||||||||||||||||||||||
| 53 | unless $$value eq '1' || $$value eq '0'; | ||||||||||||||||||||||||||||
| 54 | 2 | ▶ | return $$value ? 'true' : 'false'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 17 | ▶ | croak $where . 'has a ' . $ref . ' reference as a value; filter values are ' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | . 'strings, or an ArrayRef of them' if $ref; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 15 | ▶ | croak $where . 'has an empty value; the engine rejects it. A Perl boolean ' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | . 'stringifies to \'\' when false -- the engine wants the string ' | ||||||||||||||||||||||||||||
| 62 | . '\'false\'' | ||||||||||||||||||||||||||||
| 63 | unless length $value; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Stringify a copy: a scalar carrying a number would otherwise be | ||||||||||||||||||||||||||||
| 66 | # JSON-encoded as one, and the engine's filter values are strings. | ||||||||||||||||||||||||||||
| 67 | 14 | return "$value"; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 52 | 1; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | __END__ | ||||||||||||||||||||||||||||