« lib/DBIx/Class/InflateColumn/JSON2Object/Role/Storable.pm
↑ Summary
blib/lib/DBIx/Class/InflateColumn/JSON2Object/Trait/NoSerialize.pm »
| 1 | package DBIx::Class::InflateColumn::JSON2Object; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: convert JSON columns to Perl objects | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | our $VERSION = '0.900'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use JSON::MaybeXS qw(encode_json decode_json ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Encode qw(encode_utf8 decode_utf8); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Module::Runtime 'use_module'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | sub class_in_column { | ||||||||||||||||||||||||||||
| 15 | 3 | ▶ | my ($class,@args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
class_in_column: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 3 | my $caller = caller(0); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 3 | foreach my $def (@args) { | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 3 | my $class_column = $def->{class_column}; | |||||||||||||||||||||||||||
| 21 | 3 | my $data_column = $def->{data_column}; | |||||||||||||||||||||||||||
| 22 | 3 | my $namespace = $def->{namespace}; | |||||||||||||||||||||||||||
| 23 | 3 | ▶ | my $result_source = $def->{result_source} || $caller; | ||||||||||||||||||||||||||
|
Condition33%1/3
$def->{'result_source'} || $caller
MC/DC0%0/2
$def->{'result_source'} || $caller
$def->{'result_source'}
$caller
Truth table33%1/3
A || B
A= $def->{'result_source'}B= $caller
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 3 | use_module($namespace); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | $result_source->inflate_column( | ||||||||||||||||||||||||||||
| 28 | $data_column, | ||||||||||||||||||||||||||||
| 29 | { | ||||||||||||||||||||||||||||
| 30 | inflate => sub { | ||||||||||||||||||||||||||||
| 31 | 4 | ▶ | my ($data,$self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 4 | my $package = $namespace->package($self->$class_column); | |||||||||||||||||||||||||||
| 33 | 4 | return $package->thaw($data); | |||||||||||||||||||||||||||
| 34 | }, | ||||||||||||||||||||||||||||
| 35 | deflate => sub { | ||||||||||||||||||||||||||||
| 36 | 4 | ▶ | my ($data,$self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 37 | 4 | ▶ | if (blessed $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | if ($data->isa($namespace)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 2 | $self->$class_column($data->moniker); | |||||||||||||||||||||||||||
| 40 | } else { | ||||||||||||||||||||||||||||
| 41 | 0 | die('Supplied args object is not a '.$namespace); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | } else { | ||||||||||||||||||||||||||||
| 44 | 2 | my $package = $namespace->package($self->$class_column); | |||||||||||||||||||||||||||
| 45 | 2 | $data = $package->thaw($data); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 4 | return $data->freeze; | |||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 3 | ); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub fixed_class { | ||||||||||||||||||||||||||||
| 55 | 3 | ▶ | my ($class,@args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
fixed_class: called CC=3 SCAR=11.5
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 3 | my $caller = caller(0); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 3 | foreach my $def (@args) { | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 3 | my $data_column = $def->{column}; | |||||||||||||||||||||||||||
| 61 | 3 | my $package = $def->{class}; | |||||||||||||||||||||||||||
| 62 | 3 | ▶ | my $result_source = $def->{result_source} || $caller; | ||||||||||||||||||||||||||
|
Condition33%1/3
$def->{'result_source'} || $caller
MC/DC0%0/2
$def->{'result_source'} || $caller
$def->{'result_source'}
$caller
Truth table33%1/3
A || B
A= $def->{'result_source'}B= $caller
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 3 | use_module($package); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | $result_source->inflate_column( | ||||||||||||||||||||||||||||
| 67 | $data_column, | ||||||||||||||||||||||||||||
| 68 | { | ||||||||||||||||||||||||||||
| 69 | inflate => sub { | ||||||||||||||||||||||||||||
| 70 | 3 | ▶ | my ($data,$self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | 3 | return $package->thaw($data); | |||||||||||||||||||||||||||
| 72 | }, | ||||||||||||||||||||||||||||
| 73 | deflate => sub { | ||||||||||||||||||||||||||||
| 74 | 3 | ▶ | my ($data,$self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.7
| |||||||||||||||||||||||||||||
| 75 | 3 | ▶ | if (blessed $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 3 | ▶ | if (!$data->isa($package)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | die('Supplied args object is not a '.$package); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } else { | ||||||||||||||||||||||||||||
| 80 | 0 | $data = $package->thaw($data); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 3 | return $data->freeze; | |||||||||||||||||||||||||||
| 83 | }, | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 3 | ); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | sub no_class { | ||||||||||||||||||||||||||||
| 90 | 3 | ▶ | my ($class,@args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
no_class: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 3 | my $caller = caller(0); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 3 | foreach my $def (@args) { | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 3 | my $data_column = $def->{column}; | |||||||||||||||||||||||||||
| 96 | 3 | ▶ | my $result_source = $def->{result_source} || $caller; | ||||||||||||||||||||||||||
|
Condition33%1/3
$def->{'result_source'} || $caller
MC/DC0%0/2
$def->{'result_source'} || $caller
$def->{'result_source'}
$caller
Truth table33%1/3
A || B
A= $def->{'result_source'}B= $caller
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | $result_source->inflate_column( | ||||||||||||||||||||||||||||
| 99 | $data_column, | ||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||
| 101 | inflate => sub { | ||||||||||||||||||||||||||||
| 102 | 3 | ▶ | my ($data,$self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||
| 103 | return {} | ||||||||||||||||||||||||||||
| 104 | 3 | ▶ | if !defined $data | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $data or $data =~ /^\s*$/
MC/DC0%0/2
not defined $data or $data =~ /^\s*$/
not defined $data
$data =~ /^\s*$/
Truth table33%1/3
not(A) or B
A= not defined $dataB= $data =~ /^\s*$/
| |||||||||||||||||||||||||||||
| 105 | || $data =~ m/^\s*$/; | ||||||||||||||||||||||||||||
| 106 | 3 | return decode_json( encode_utf8($data) ); | |||||||||||||||||||||||||||
| 107 | }, | ||||||||||||||||||||||||||||
| 108 | deflate => sub { | ||||||||||||||||||||||||||||
| 109 | 2 | ▶ | my ($data,$self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 110 | 2 | ▶ | if ( ref($data) =~ m/^(HASH|ARRAY)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 2 | return decode_utf8( encode_json($data) ); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 0 | return $data; | |||||||||||||||||||||||||||
| 114 | }, | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 3 | ); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 4 | 1; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =pod | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 NAME | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | DBIx::Class::InflateColumn::JSON2Object - convert JSON columns to Perl objects | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head1 VERSION | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | version 0.900 | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # In a DBIx::Class Result | ||||||||||||||||||||||||||||
| 139 | package MyApp::Schema::Result::SomeTable; | ||||||||||||||||||||||||||||
| 140 | ... lots of DBIx::Class code... | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | use DBIx::Class::InflateColumn::JSON2Object; | ||||||||||||||||||||||||||||
| 143 | DBIx::Class::InflateColumn::JSON2Object->fixed_class({ | ||||||||||||||||||||||||||||
| 144 | column=>'data', # a column storing JSON | ||||||||||||||||||||||||||||
| 145 | class=>'MyApp::SomeClass', | ||||||||||||||||||||||||||||
| 146 | }); | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # later, in some code far, far away... | ||||||||||||||||||||||||||||
| 149 | my $row = $schema->resultset('SomeTable')->find(42); | ||||||||||||||||||||||||||||
| 150 | my $obj = $row->data; | ||||||||||||||||||||||||||||
| 151 | $obj->foo; # $obj ISA MyApp::SomeClass | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # store a hash as JSON after "validating" it through MyApp::SomeClass | ||||||||||||||||||||||||||||
| 154 | $schema->resultset('SomeTable')->create({ | ||||||||||||||||||||||||||||
| 155 | data => { | ||||||||||||||||||||||||||||
| 156 | foo=>'bar', | ||||||||||||||||||||||||||||
| 157 | }; | ||||||||||||||||||||||||||||
| 158 | }) | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # you can also use it to just deflate/inflate JSON to a Perl hash | ||||||||||||||||||||||||||||
| 161 | DBIx::Class::InflateColumn::JSON2Object->no_class({ | ||||||||||||||||||||||||||||
| 162 | column=>'args', | ||||||||||||||||||||||||||||
| 163 | }); | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # or have a complex set of objects | ||||||||||||||||||||||||||||
| 166 | DBIx::Class::InflateColumn::JSON2Object->class_in_column({ | ||||||||||||||||||||||||||||
| 167 | data_column => 'object', # some JSON | ||||||||||||||||||||||||||||
| 168 | class_column => 'type', # here we store the name of the object | ||||||||||||||||||||||||||||
| 169 | namespace => 'MyApp::Object', # the namespace of the objects | ||||||||||||||||||||||||||||
| 170 | }); | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | TODO: short overview | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head2 Booleans, JSON, oh my... | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | TODO: describe problem and the (hacky/crappy?) solution | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head2 METHODS | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Please note that you can pass more than one HASHREF per method to | ||||||||||||||||||||||||||||
| 183 | install several inflator/deflators at once. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head3 no_class | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Install a JSON inflator/deflator for each column. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | DBIx::Class::InflateColumn::JSON2Object->no_class({ | ||||||||||||||||||||||||||||
| 190 | column=>'args' | ||||||||||||||||||||||||||||
| 191 | }); | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | You can pass a Perl datastructure to the row and it will be stored as JSON: | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | $resultset->create( { | ||||||||||||||||||||||||||||
| 196 | id => 123, | ||||||||||||||||||||||||||||
| 197 | args => { | ||||||||||||||||||||||||||||
| 198 | some => 'data', | ||||||||||||||||||||||||||||
| 199 | more => [1,1,2,3,5,8] | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | }); | ||||||||||||||||||||||||||||
| 202 | # will be stored as '{"some":"data","more":[1,1,2,3,5,8]}' | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | You can also access the data directly as a Perl hash: | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | my $row = $resultset->find(123); | ||||||||||||||||||||||||||||
| 207 | $row->args->{some}; # 'data' | ||||||||||||||||||||||||||||
| 208 | $row->args->{more}[5]; # 8 | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =head3 fixed_class | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | If plain JSON is to wobbly for you, you can define Moose objects and | ||||||||||||||||||||||||||||
| 213 | have them serialized to JSON. Not only can you now add some custom | ||||||||||||||||||||||||||||
| 214 | methods to the objects, but you can (ab)use the object initalisation | ||||||||||||||||||||||||||||
| 215 | and all features Moose provides to define your objects. | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | TODO ->fixed_class(..) | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | Just pass an object to the row and it will be stored as JSON: | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | TODO | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | And you get back the initated object: | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | TODO | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =head3 class_in_column | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | Sometimes you have a set of similar objects you want to store. TODO: explain oe1.article.paragraph | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | TODO ->class_in_column(..) | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | You can pass an object to the row and the infered type will be stored together with the JSON-payload: | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | TODO | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | To get back the object: | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | TODO | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =head1 THANKS | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =over | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =item * | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | Parts of this code were orginally developed for L<validad.com|https://www.validad.com/> and released as Open Source. | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =item * L<Maroš Kollár|https://metacpan.org/author/MAROS> wrote the prototype of C<class_in_column>, orginally developed for L<http://oe1.orf.at>. | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =back | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | Thomas Klausner <domm@cpan.org> | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | This software is copyright (c) 2017 by Thomas Klausner. | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 262 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | =cut | ||||||||||||||||||||||||||||