File Coverage

blib/lib/Data/Partial/Google/Filter.pm
Criterion Covered Total %
statement 37 37 100.0
branch 18 18 100.0
condition 5 5 100.0
subroutine 6 6 100.0
pod 0 3 0.0
total 66 69 95.6


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