File Coverage

blib/lib/Data/Partial/Google/Filter.pm
Criterion Covered Total %
statement 35 35 100.0
branch 20 20 100.0
condition 2 2 100.0
subroutine 6 6 100.0
pod 0 3 0.0
total 63 66 95.4


line stmt bran cond sub pod time code
1             package Data::Partial::Google::Filter;
2             our $VERSION = '0.02'; # VERSION
3             our $AUTHORITY = 'cpan:ARODLAND'; # AUTHORITY
4 2     2   14 use Moo;
  2         4  
  2         18  
5 2     2   817 use Scalar::Util 'reftype';
  2         4  
  2         150  
6 2     2   15 use Carp;
  2         5  
  2         993  
7              
8             has 'properties' => (
9             is => 'ro',
10             );
11              
12             sub mask {
13 63     63 0 321 my ($self, $thing) = @_;
14              
15 63   100     248 my $type = reftype $thing || '';
16              
17 63 100       184 if ($type eq 'ARRAY') {
    100          
18 10         42 return $self->mask_array($thing);
19             } elsif ($type eq 'HASH') {
20 50         175 return $self->mask_hash($thing);
21             } else {
22             # If we're looking for properties on a thing with no internal structure,
23             # don't return it at all. So for example, if we have a/*/b, a might contain
24             # some scalars, we don't want them, we only want things that have bs.
25 3         10 return;
26             }
27             }
28              
29             sub mask_hash {
30 50     50 0 75 my ($self, $hash) = @_;
31              
32 50         131 my $props = $self->properties;
33 50         80 my $out = {};
34              
35 50         157 for my $key (keys %$props) {
36 61         100 my $filter = $props->{$key};
37 61 100       234 if ($key eq '*') {
    100          
38             # For * go over all keys in the object, but only produce output keys if the
39             # mask returned something useful.
40 4         15 for my $hash_key (keys %$hash) {
41 11 100       41 my $masked = $filter ? $filter->mask($hash->{$hash_key}) : $hash->{$hash_key};
42 11 100       52 $out->{$hash_key} = $masked if defined $masked;
43             }
44             } elsif (exists $hash->{$key}) {
45 52 100       119 if ($filter) {
46 19         68 my $masked = $filter->mask($hash->{$key});
47 19 100       85 $out->{$key} = $masked if defined $masked;
48             } else {
49 33         122 $out->{$key} = $hash->{$key};
50             }
51             }
52             }
53 50 100       260 return $out if keys %$out;
54 5         19 return;
55             }
56              
57             sub mask_array {
58 10     10 0 27 my ($self, $array) = @_;
59              
60 10         22 my @out = map { $self->mask($_) } @$array;
  13         40  
61 10 100       53 return \@out if @out;
62 2         8 return;
63             }
64              
65             1;
66              
67             __END__