| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | package KiokuDB::Backend::Serialize::JSPON::Expander; | ||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use KiokuDB::Entry; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use KiokuDB::Reference; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | 6 | with qw(KiokuDB::Backend::Serialize::JSPON::Converter); | |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | sub expand_jspon { | ||||||||||||||||||||||||||||||||||
| 17 | 1754 | ▶ | my ( $self, $data, @attrs ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
expand_jspon: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | 1754 | return $self->_expander->($data, @attrs); | |||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | 6 | has _expander => ( | |||||||||||||||||||||||||||||||||
| 23 | isa => "CodeRef", | ||||||||||||||||||||||||||||||||||
| 24 | is => "ro", | ||||||||||||||||||||||||||||||||||
| 25 | lazy_build => 1, | ||||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | sub _build__expander { | ||||||||||||||||||||||||||||||||||
| 29 | 19 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_build__expander: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | 19 | my $expander; | |||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | my ( | ||||||||||||||||||||||||||||||||||
| 34 | $ref_field, $id_field, $data_field, | ||||||||||||||||||||||||||||||||||
| 35 | $class_field, $tied_field, $root_field, | ||||||||||||||||||||||||||||||||||
| 36 | $deleted_field, $class_meta_field, $class_version_field, | ||||||||||||||||||||||||||||||||||
| 37 | $backend_data_field | ||||||||||||||||||||||||||||||||||
| 38 | ) | ||||||||||||||||||||||||||||||||||
| 39 | 19 | = map { my $m = $_ . "_field"; $self->$m() } | |||||||||||||||||||||||||||||||||
| 40 | qw(ref id data class tied root deleted class_meta class_version backend_data); | ||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | 19 | ▶ | unless ( $self->inline_data ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 43 | 18 | my $data_field_re = qr/\. \Q$data_field\E $/x; | |||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||
| 45 | $expander = sub { | ||||||||||||||||||||||||||||||||||
| 46 | 9686 | ▶ | my ( $data, @attrs ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=22 SCAR=31.0
| |||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||
| 48 | 9686 | ▶ | if ( my $ref = ref($data) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 49 | 9638 | ▶ | if ( $ref eq 'HASH' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 50 | 5028 | ▶ | if ( my $id = $data->{$ref_field} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 51 | 1476 | $id =~ s/$data_field_re//; | |||||||||||||||||||||||||||||||||
| 52 | 1476 | ▶ | return KiokuDB::Reference->new( id => $id, ( $data->{weak} ? ( is_weak => 1 ) : () ) ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 53 | ▶ | } elsif ( exists $data->{$class_field} | |||||||||||||||||||||||||||||||||
|
Condition100%6/6
exists $data->{$class_field} or exists $data->{$id_field} or exists $data->{$tied_field}
exists $data->{$class_field} or exists $data->{$id_field}
MC/DC100%3/3
exists $data->{$class_field} or exists $data->{$id_field} or exists $data->{$tied_field}
exists $data->{$class_field}
exists $data->{$id_field}
exists $data->{$tied_field}
Truth table100%4/4
A or B or C
A= exists $data->{$class_field}B= exists $data->{$id_field}C= exists $data->{$tied_field}
| |||||||||||||||||||||||||||||||||||
| 54 | or exists $data->{$id_field} | ||||||||||||||||||||||||||||||||||
| 55 | or exists $data->{$tied_field} | ||||||||||||||||||||||||||||||||||
| 56 | ) { | ||||||||||||||||||||||||||||||||||
| 57 | 1808 | ▶ | if ( exists $data->{$class_field} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 58 | # check the class more thoroughly here ... | ||||||||||||||||||||||||||||||||||
| 59 | 1748 | my ($class, $version, $authority) = (split '-' => $data->{$class_field}); | |||||||||||||||||||||||||||||||||
| 60 | 1748 | push @attrs, class => $class; | |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | 1748 | ▶ | push @attrs, class_meta => $data->{$class_meta_field} if exists $data->{$class_meta_field}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 63 | 1748 | ▶ | push @attrs, class_version => $data->{$class_version_field} if exists $data->{$class_version_field}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||
| 66 | 1808 | ▶ | push @attrs, id => $data->{$id_field} if exists $data->{$id_field}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 67 | 1808 | ▶ | push @attrs, tied => substr($data->{$tied_field}, 0, 1) if exists $data->{$tied_field}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 68 | 1808 | ▶ | push @attrs, root => $data->{$root_field} ? 1 : 0 if exists $data->{$root_field}; | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 69 | 1808 | ▶ | push @attrs, deleted => $data->{$deleted_field} ? 1 : 0 if exists $data->{$deleted_field}; | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 70 | 1808 | ▶ | push @attrs, backend_data => $data->{$backend_data_field} if exists $data->{$backend_data_field}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | 1808 | push @attrs, data => $expander->( $data->{$data_field} ); | |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | 1808 | return KiokuDB::Entry->new( @attrs ); | |||||||||||||||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||||||||||||||||
| 76 | 1744 | my %hash; | |||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | 1744 | foreach my $key ( keys %$data ) { | |||||||||||||||||||||||||||||||||
| 79 | 7619 | my $unescaped = $key; | |||||||||||||||||||||||||||||||||
| 80 | 7619 | $unescaped =~ s/^public:://; | |||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | 7619 | my $value = $data->{$key}; | |||||||||||||||||||||||||||||||||
| 83 | 7619 | ▶ | $hash{$unescaped} = ref($value) ? $expander->($value) : $value; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | 1744 | return \%hash; | |||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||
| 88 | } elsif ( ref $data eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||
| 89 | 4610 | ▶ | return [ map { ref($_) ? $expander->($_) : $_ } @$data ]; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||
| 93 | 48 | return $data; | |||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||
| 95 | 18 | } else { | |||||||||||||||||||||||||||||||||
| 96 | $expander = sub { | ||||||||||||||||||||||||||||||||||
| 97 | 4 | ▶ | my ( $data, @attrs ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=20 SCAR=35.3
| |||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||
| 99 | 4 | ▶ | if ( my $ref = ref($data) ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 100 | 4 | ▶ | if ( $ref eq 'HASH' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | 4 | ▶ | if ( my $id = $data->{$ref_field} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 103 | 1 | ▶ | return KiokuDB::Reference->new( id => $id, ( $data->{weak} ? ( is_weak => 1 ) : () ) ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 104 | ▶ | } elsif ( exists $data->{$class_field} | |||||||||||||||||||||||||||||||||
|
Condition66%4/6
exists $data->{$class_field} or exists $data->{$id_field} or exists $data->{$tied_field}
exists $data->{$class_field} or exists $data->{$id_field}
MC/DC33%1/3
exists $data->{$class_field} or exists $data->{$id_field} or exists $data->{$tied_field}
exists $data->{$class_field}
exists $data->{$id_field}
exists $data->{$tied_field}
Truth table50%2/4
A or B or C
A= exists $data->{$class_field}B= exists $data->{$id_field}C= exists $data->{$tied_field}
| |||||||||||||||||||||||||||||||||||
| 105 | or exists $data->{$id_field} | ||||||||||||||||||||||||||||||||||
| 106 | or exists $data->{$tied_field} | ||||||||||||||||||||||||||||||||||
| 107 | ) { | ||||||||||||||||||||||||||||||||||
| 108 | 1 | my %copy = %$data; | |||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | 1 | ▶ | if ( exists $copy{$class_field} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 111 | # check the class more thoroughly here ... | ||||||||||||||||||||||||||||||||||
| 112 | 1 | my ($class, $version, $authority) = (split '-' => delete $copy{$class_field}); | |||||||||||||||||||||||||||||||||
| 113 | 1 | push @attrs, class => $class; | |||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||
| 115 | 1 | ▶ | push @attrs, class_meta => delete $copy{$class_meta_field} if exists $copy{$class_meta_field}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | 1 | ▶ | push @attrs, id => delete $copy{$id_field} if exists $copy{$id_field}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 119 | 1 | ▶ | push @attrs, tied => delete $copy{$tied_field} if exists $copy{$tied_field}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 120 | 1 | ▶ | push @attrs, root => delete $copy{$root_field} ? 1 : 0 if exists $copy{$root_field}; | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 121 | 1 | ▶ | push @attrs, deleted => delete $copy{$deleted_field} ? 1 : 0 if exists $copy{$deleted_field}; | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||
| 123 | 1 | push @attrs, data => $expander->( \%copy ); | |||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | 1 | return KiokuDB::Entry->new( @attrs ); | |||||||||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||||||||
| 127 | 2 | my %hash; | |||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||
| 129 | 2 | foreach my $key ( keys %$data ) { | |||||||||||||||||||||||||||||||||
| 130 | 5 | my $unescaped = $key; | |||||||||||||||||||||||||||||||||
| 131 | 5 | $unescaped =~ s/^public:://; | |||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||
| 133 | 5 | my $value = $data->{$key}; | |||||||||||||||||||||||||||||||||
| 134 | 5 | ▶ | $hash{$unescaped} = ref($value) ? $expander->($value) : $value; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||
| 137 | 2 | return \%hash; | |||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||
| 139 | } elsif ( ref $data eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | return [ map { ref($_) ? $expander->($_) : $_ } @$data ]; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||
| 144 | 0 | return $data; | |||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||
| 146 | 1 | } | |||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||
| 148 | 19 | my $copy = $expander; | |||||||||||||||||||||||||||||||||
| 149 | 19 | weaken($expander); | |||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||
| 151 | 19 | return $copy; | |||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||
| 154 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||
| 156 | 6 | __PACKAGE__ | |||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||
| 160 | =pod | ||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||
| 162 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | KiokuDB::Backend::Serialize::JSPON::Expander - Inflate JSPON to entry | ||||||||||||||||||||||||||||||||||
| 165 | data. | ||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||
| 167 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||
| 169 | my $c = KiokuDB::Backend::Serialize::JSPON::Expander->new( | ||||||||||||||||||||||||||||||||||
| 170 | id_field => "_id", | ||||||||||||||||||||||||||||||||||
| 171 | ); | ||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||
| 173 | my $entry = $c->collapse_jspon($hashref); | ||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||
| 175 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||
| 177 | This object is used by L<KiokuDB::Backend::Serialize::JSPON> to expand JSPON | ||||||||||||||||||||||||||||||||||
| 178 | compliant hash references to L<KiokuDB::Entry> objects. | ||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||
| 180 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||
| 182 | See L<KiokuDB::Backend::Serialize::JSPON::Converter> for attributes shared by | ||||||||||||||||||||||||||||||||||
| 183 | L<KiokuDB::Backend::Serialize::JSPON::Collapser> and | ||||||||||||||||||||||||||||||||||
| 184 | L<KiokuDB::Backend::Serialize::JSPON::Expander>. | ||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||
| 186 | =head1 METHODS | ||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||
| 188 | =over 4 | ||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||
| 190 | =item expand_jspon $hashref | ||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||
| 192 | Recursively inlates the hash reference, returning a L<KiokuDB::Entry> object. | ||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||
| 194 | =back | ||||||||||||||||||||||||||||||||||