Docker/Role/Filters.pm

filter: Statementstmt 100% 34 / 34 Branchbran 100% 22 / 22 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 100% 66 / 66 CC 14 SCAR 19.6
CC
14
Cov
100%
_normalise_filter_value
10.0
_normalise_filters
5.0
BEGIN
1.0
CRAP 7.1
« Docker/Type.pm ↑ Summary Docker/Role/Type.pm »
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
BranchTrueFalseTotal
unless ref $filters eq "HASH" 3 38 41
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
BranchTrueFalseTotal
unless length $name 1 38 39
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
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 16 22 38
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
BranchTrueFalseTotal
unless defined $value 16 22 38
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
BranchTrueFalseTotal
if $BOOLEAN_CLASS{$ref} 2 20 22
$value ? : 1 1 2
49  
50 20 ▶ if ($ref eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "SCALAR") 3 17 20
51 3 ▶ croak $where . 'is a ScalarRef to something other than 1 or 0; \\1 and '
Branch: 1 branch
BranchTrueFalseTotal
unless $$value eq 1 or $$value eq 0 1 2 3
Condition100%3/3
$$value eq 1 or $$value eq 0
l!l&&r!l&&!r
111
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
$$value ? : 1 1 2
55 }
56  
57 17 ▶ croak $where . 'has a ' . $ref . ' reference as a value; filter values are '
Branch: 1 branch
BranchTrueFalseTotal
if $ref 2 15 17
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
BranchTrueFalseTotal
unless length $value 1 14 15
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__
« Docker/Type.pm ↑ Summary Docker/Role/Type.pm »