Commit | Line | Data |
e59193fb |
1 | |
2 | package MooseX::Storage::Engine; |
3 | use Moose; |
ec73712a |
4 | use Scalar::Util qw(refaddr); |
e59193fb |
5 | |
5ca52230 |
6 | our $VERSION = '0.18'; |
69b45b7d |
7 | our $AUTHORITY = 'cpan:STEVAN'; |
ba5bba75 |
8 | |
9 | # the class marker when |
10 | # serializing an object. |
11 | our $CLASS_MARKER = '__CLASS__'; |
12 | |
e59193fb |
13 | has 'storage' => ( |
7eb5dc63 |
14 | is => 'ro', |
15 | isa => 'HashRef', |
16 | default => sub {{}} |
17 | ); |
18 | |
19 | has 'seen' => ( |
20 | is => 'ro', |
ec73712a |
21 | isa => 'HashRef[Int]', # int is the refaddr |
e59193fb |
22 | default => sub {{}} |
23 | ); |
24 | |
e9739624 |
25 | has 'object' => (is => 'rw', isa => 'Object'); |
26 | has 'class' => (is => 'rw', isa => 'Str'); |
e59193fb |
27 | |
e9739624 |
28 | ## this is the API used by other modules ... |
e59193fb |
29 | |
30 | sub collapse_object { |
34dcaa5d |
31 | my ( $self, %options ) = @_; |
7eb5dc63 |
32 | |
33 | # NOTE: |
34 | # mark the root object as seen ... |
ec73712a |
35 | $self->seen->{refaddr $self->object} = undef; |
7eb5dc63 |
36 | |
34dcaa5d |
37 | $self->map_attributes('collapse_attribute', \%options); |
c1830046 |
38 | $self->storage->{$CLASS_MARKER} = $self->object->meta->identifier; |
e59193fb |
39 | return $self->storage; |
40 | } |
41 | |
e9739624 |
42 | sub expand_object { |
219c1cc5 |
43 | my ($self, $data, %options) = @_; |
44 | |
5b7ea1fd |
45 | $options{check_version} = 1 unless exists $options{check_version}; |
46 | $options{check_authority} = 1 unless exists $options{check_authority}; |
47 | |
7eb5dc63 |
48 | # NOTE: |
49 | # mark the root object as seen ... |
ec73712a |
50 | $self->seen->{refaddr $data} = undef; |
7eb5dc63 |
51 | |
219c1cc5 |
52 | $self->map_attributes('expand_attribute', $data, \%options); |
e9739624 |
53 | return $self->storage; |
e59193fb |
54 | } |
55 | |
e9739624 |
56 | ## this is the internal API ... |
57 | |
58 | sub collapse_attribute { |
f6e1331f |
59 | my ($self, $attr, $options) = @_; |
eea0dfae |
60 | my $value = $self->collapse_attribute_value($attr, $options); |
61 | return if !defined($value); |
62 | $self->storage->{$attr->name} = $value; |
e9739624 |
63 | } |
64 | |
65 | sub expand_attribute { |
219c1cc5 |
66 | my ($self, $attr, $data, $options) = @_; |
14b0a4dc |
67 | my $value = $self->expand_attribute_value($attr, $data->{$attr->name}, $options); |
68 | $self->storage->{$attr->name} = defined $value ? $value : return; |
e59193fb |
69 | } |
70 | |
e9739624 |
71 | sub collapse_attribute_value { |
f6e1331f |
72 | my ($self, $attr, $options) = @_; |
f4ffa4ef |
73 | # Faster, but breaks attributes without readers, do we care? |
74 | #my $value = $attr->get_read_method_ref->($self->object); |
e9739624 |
75 | my $value = $attr->get_value($self->object); |
f4ffa4ef |
76 | |
b430caa3 |
77 | # NOTE: |
f4ffa4ef |
78 | # this might not be enough, we might |
79 | # need to make it possible for the |
b430caa3 |
80 | # cycle checker to return the value |
3513de05 |
81 | $self->check_for_cycle_in_collapse($attr, $value) |
82 | if ref $value; |
f4ffa4ef |
83 | |
e9739624 |
84 | if (defined $value && $attr->has_type_constraint) { |
3db8f791 |
85 | my $type_converter = $self->find_type_handler($attr->type_constraint); |
e9739624 |
86 | (defined $type_converter) |
87 | || confess "Cannot convert " . $attr->type_constraint->name; |
f6e1331f |
88 | $value = $type_converter->{collapse}->($value, $options); |
e9739624 |
89 | } |
90 | return $value; |
91 | } |
92 | |
93 | sub expand_attribute_value { |
219c1cc5 |
94 | my ($self, $attr, $value, $options) = @_; |
7eb5dc63 |
95 | |
b430caa3 |
96 | # NOTE: |
97 | # (see comment in method above ^^) |
5b7ea1fd |
98 | if( ref $value and not( |
99 | $options->{disable_cycle_check} or |
100 | $self->class->does('MooseX::Storage::Traits::DisableCycleDetection') |
101 | )) { |
102 | $self->check_for_cycle_in_collapse($attr, $value) |
103 | } |
7eb5dc63 |
104 | |
e9739624 |
105 | if (defined $value && $attr->has_type_constraint) { |
3db8f791 |
106 | my $type_converter = $self->find_type_handler($attr->type_constraint); |
219c1cc5 |
107 | $value = $type_converter->{expand}->($value, $options); |
e9739624 |
108 | } |
109 | return $value; |
110 | } |
111 | |
b430caa3 |
112 | # NOTE: |
113 | # possibly these two methods will |
114 | # be used by a cycle supporting |
115 | # engine. However, I am not sure |
116 | # if I can make a cycle one work |
117 | # anyway. |
e9739624 |
118 | |
7eb5dc63 |
119 | sub check_for_cycle_in_collapse { |
45d9a73c |
120 | my ($self, $attr, $value) = @_; |
ec73712a |
121 | (!exists $self->seen->{refaddr $value}) |
45d9a73c |
122 | || confess "Basic Engine does not support cycles in class(" |
c1830046 |
123 | . ($attr->associated_class->name) . ").attr(" |
45d9a73c |
124 | . ($attr->name) . ") with $value"; |
ec73712a |
125 | $self->seen->{refaddr $value} = undef; |
7eb5dc63 |
126 | } |
127 | |
128 | sub check_for_cycle_in_expansion { |
45d9a73c |
129 | my ($self, $attr, $value) = @_; |
ec73712a |
130 | (!exists $self->seen->{refaddr $value}) |
45d9a73c |
131 | || confess "Basic Engine does not support cycles in class(" |
c1830046 |
132 | . ($attr->associated_class->name) . ").attr(" |
45d9a73c |
133 | . ($attr->name) . ") with $value"; |
ec73712a |
134 | $self->seen->{refaddr $value} = undef; |
7eb5dc63 |
135 | } |
136 | |
b430caa3 |
137 | # util methods ... |
138 | |
e9739624 |
139 | sub map_attributes { |
140 | my ($self, $method_name, @args) = @_; |
186d680b |
141 | |
142 | # The $self->object check is here to differentiate a ->pack from a |
143 | # ->unpack; ->object is only defined for a ->pack |
144 | |
145 | # no checks needed if this is class based (ie, restore) |
146 | unless( $self->object ) { |
147 | return map { $self->$method_name($_, @args) } |
148 | $self->class->meta->get_all_attributes; |
149 | } |
150 | |
151 | # if it's object based, it's a store -- in that case, |
152 | # check thoroughly |
153 | my @rv; |
154 | my $o = $self->object; |
155 | for my $attr ( $o->meta->get_all_attributes ) { |
156 | |
b430caa3 |
157 | # Skip our special skip attribute :) |
186d680b |
158 | next if $attr->does( |
159 | 'MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize'); |
160 | |
76218b46 |
161 | # If we're invoked with the 'OnlyWhenBuilt' trait, we should |
162 | # only serialize the attribute if it's already built. So, go ahead |
186d680b |
163 | # and check if the attribute has a predicate. If so, check if it's |
164 | # set and then go ahead and look it up. |
165 | if( $o->does('MooseX::Storage::Traits::OnlyWhenBuilt') and |
166 | my $pred = $attr->predicate |
167 | ) { |
168 | next unless $self->object->$pred; |
169 | } |
170 | push @rv, $self->$method_name($attr, @args); |
171 | } |
172 | |
173 | return @rv; |
e59193fb |
174 | } |
175 | |
913d96dd |
176 | ## ------------------------------------------------------------------ |
3db8f791 |
177 | ## This is all the type handler stuff, it is in a state of flux |
178 | ## right now, so this may change, or it may just continue to be |
179 | ## improved upon. Comments and suggestions are welcomed. |
913d96dd |
180 | ## ------------------------------------------------------------------ |
181 | |
182 | # NOTE: |
183 | # these are needed by the |
184 | # ArrayRef and HashRef handlers |
185 | # below, so I need easy access |
186 | my %OBJECT_HANDLERS = ( |
187 | expand => sub { |
219c1cc5 |
188 | my ($data, $options) = @_; |
ba5bba75 |
189 | (exists $data->{$CLASS_MARKER}) |
913d96dd |
190 | || confess "Serialized item has no class marker"; |
c1830046 |
191 | # check the class more thoroughly here ... |
192 | my ($class, $version, $authority) = (split '-' => $data->{$CLASS_MARKER}); |
193 | my $meta = eval { $class->meta }; |
219c1cc5 |
194 | confess "Class ($class) is not loaded, cannot unpack" if $@; |
195 | |
196 | if ($options->{check_version}) { |
197 | my $meta_version = $meta->version; |
198 | if (defined $meta_version && $version) { |
199 | if ($options->{check_version} eq 'allow_less_than') { |
200 | ($meta_version <= $version) |
201 | || confess "Class ($class) versions is not less than currently available." |
202 | . " got=($version) available=($meta_version)"; |
203 | } |
204 | elsif ($options->{check_version} eq 'allow_greater_than') { |
205 | ($meta->version >= $version) |
206 | || confess "Class ($class) versions is not greater than currently available." |
207 | . " got=($version) available=($meta_version)"; |
208 | } |
209 | else { |
210 | ($meta->version == $version) |
211 | || confess "Class ($class) versions don't match." |
212 | . " got=($version) available=($meta_version)"; |
213 | } |
214 | } |
215 | } |
216 | |
217 | if ($options->{check_authority}) { |
218 | my $meta_authority = $meta->authority; |
219 | ($meta->authority eq $authority) |
220 | || confess "Class ($class) authorities don't match." |
221 | . " got=($authority) available=($meta_authority)" |
222 | if defined $meta_authority && defined $authority; |
223 | } |
224 | |
c1830046 |
225 | # all is well ... |
34dcaa5d |
226 | $class->unpack($data, %$options); |
913d96dd |
227 | }, |
228 | collapse => sub { |
34dcaa5d |
229 | my ( $obj, $options ) = @_; |
c4a322ec |
230 | # ($obj->can('does') && $obj->does('MooseX::Storage::Basic')) |
231 | # || confess "Bad object ($obj) does not do MooseX::Storage::Basic role"; |
1c6ac775 |
232 | ($obj->can('pack')) |
3defafb9 |
233 | || confess "Object ($obj) does not have a &pack method, cannot collapse"; |
34dcaa5d |
234 | $obj->pack(%$options); |
913d96dd |
235 | }, |
236 | ); |
237 | |
238 | |
e59193fb |
239 | my %TYPES = ( |
cfee09ad |
240 | # NOTE: |
241 | # we need to make sure that we properly numify the numbers |
242 | # before and after them being futzed with, because some of |
243 | # the JSON engines are stupid/annoying/frustrating |
244 | 'Int' => { expand => sub { $_[0] + 0 }, collapse => sub { $_[0] + 0 } }, |
245 | 'Num' => { expand => sub { $_[0] + 0 }, collapse => sub { $_[0] + 0 } }, |
246 | # These are boring ones, so they use the identity function ... |
e9739624 |
247 | 'Str' => { expand => sub { shift }, collapse => sub { shift } }, |
d691721b |
248 | 'Bool' => { expand => sub { shift }, collapse => sub { shift } }, |
3db8f791 |
249 | # These are the trickier ones, (see notes) |
250 | # NOTE: |
251 | # Because we are nice guys, we will check |
252 | # your ArrayRef and/or HashRef one level |
253 | # down and inflate any objects we find. |
254 | # But this is where it ends, it is too |
255 | # expensive to try and do this any more |
256 | # recursively, when it is probably not |
257 | # nessecary in most of the use cases. |
258 | # However, if you need more then this, subtype |
259 | # and add a custom handler. |
913d96dd |
260 | 'ArrayRef' => { |
e9739624 |
261 | expand => sub { |
34dcaa5d |
262 | my ( $array, @args ) = @_; |
913d96dd |
263 | foreach my $i (0 .. $#{$array}) { |
264 | next unless ref($array->[$i]) eq 'HASH' |
ba5bba75 |
265 | && exists $array->[$i]->{$CLASS_MARKER}; |
34dcaa5d |
266 | $array->[$i] = $OBJECT_HANDLERS{expand}->($array->[$i], @args); |
913d96dd |
267 | } |
268 | $array; |
269 | }, |
34dcaa5d |
270 | collapse => sub { |
271 | my ( $array, @args ) = @_; |
913d96dd |
272 | # NOTE: |
273 | # we need to make a copy cause |
274 | # otherwise it will affect the |
275 | # other real version. |
276 | [ map { |
277 | blessed($_) |
34dcaa5d |
278 | ? $OBJECT_HANDLERS{collapse}->($_, @args) |
913d96dd |
279 | : $_ |
280 | } @$array ] |
281 | } |
282 | }, |
283 | 'HashRef' => { |
3db8f791 |
284 | expand => sub { |
34dcaa5d |
285 | my ( $hash, @args ) = @_; |
3db8f791 |
286 | foreach my $k (keys %$hash) { |
287 | next unless ref($hash->{$k}) eq 'HASH' |
ba5bba75 |
288 | && exists $hash->{$k}->{$CLASS_MARKER}; |
34dcaa5d |
289 | $hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k}, @args); |
3db8f791 |
290 | } |
291 | $hash; |
292 | }, |
293 | collapse => sub { |
34dcaa5d |
294 | my ( $hash, @args ) = @_; |
3db8f791 |
295 | # NOTE: |
296 | # we need to make a copy cause |
297 | # otherwise it will affect the |
298 | # other real version. |
299 | +{ map { |
95f31c36 |
300 | blessed($hash->{$_}) |
f6e1331f |
301 | ? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}, @args)) |
3db8f791 |
302 | : ($_ => $hash->{$_}) |
303 | } keys %$hash } |
304 | } |
e1bb45ff |
305 | }, |
913d96dd |
306 | 'Object' => \%OBJECT_HANDLERS, |
e1bb45ff |
307 | # NOTE: |
308 | # The sanity of enabling this feature by |
309 | # default is very questionable. |
310 | # - SL |
311 | #'CodeRef' => { |
312 | # expand => sub {}, # use eval ... |
313 | # collapse => sub {}, # use B::Deparse ... |
7eb5dc63 |
314 | #} |
e59193fb |
315 | ); |
316 | |
3db8f791 |
317 | sub add_custom_type_handler { |
318 | my ($class, $type_name, %handlers) = @_; |
319 | (exists $handlers{expand} && exists $handlers{collapse}) |
320 | || confess "Custom type handlers need an expand *and* a collapse method"; |
321 | $TYPES{$type_name} = \%handlers; |
322 | } |
323 | |
324 | sub remove_custom_type_handler { |
325 | my ($class, $type_name) = @_; |
326 | delete $TYPES{$type_name} if exists $TYPES{$type_name}; |
327 | } |
328 | |
329 | sub find_type_handler { |
e59193fb |
330 | my ($self, $type_constraint) = @_; |
e1bb45ff |
331 | |
ef87e4a6 |
332 | # check if the type is a Maybe and |
333 | # if its parent is not parameterized. |
334 | # If both is true recurse this method |
335 | # using ->type_parameter. |
336 | return $self->find_type_handler($type_constraint->type_parameter) |
337 | if $type_constraint->parent eq 'Maybe' |
338 | and not $type_constraint->parent->can('type_parameter'); |
339 | |
e1bb45ff |
340 | # this should handle most type usages |
341 | # since they they are usually just |
342 | # the standard set of built-ins |
343 | return $TYPES{$type_constraint->name} |
344 | if exists $TYPES{$type_constraint->name}; |
345 | |
346 | # the next possibility is they are |
347 | # a subtype of the built-in types, |
348 | # in which case this will DWIM in |
349 | # most cases. It is probably not |
350 | # 100% ideal though, but until I |
351 | # come up with a decent test case |
352 | # it will do for now. |
e59193fb |
353 | foreach my $type (keys %TYPES) { |
354 | return $TYPES{$type} |
355 | if $type_constraint->is_subtype_of($type); |
356 | } |
e1bb45ff |
357 | |
358 | # NOTE: |
359 | # the reason the above will work has to |
360 | # do with the fact that custom subtypes |
361 | # are mostly used for validation of |
362 | # the guts of a type, and not for some |
363 | # weird structural thing which would |
364 | # need to be accomidated by the serializer. |
365 | # Of course, mst or phaylon will probably |
366 | # do something to throw this assumption |
367 | # totally out the door ;) |
368 | # - SL |
369 | |
e9739624 |
370 | # NOTE: |
371 | # if this method hasnt returned by now |
372 | # then we have no been able to find a |
373 | # type constraint handler to match |
374 | confess "Cannot handle type constraint (" . $type_constraint->name . ")"; |
e59193fb |
375 | } |
376 | |
3defafb9 |
377 | sub find_type_handler_for { |
378 | my ($self, $type_handler_name) = @_; |
379 | $TYPES{$type_handler_name} |
380 | } |
381 | |
e59193fb |
382 | 1; |
e9739624 |
383 | |
384 | __END__ |
385 | |
386 | =pod |
387 | |
ec9c1923 |
388 | =head1 NAME |
389 | |
1390c23d |
390 | MooseX::Storage::Engine - The meta-engine to handle collapsing and expanding objects |
ec9c1923 |
391 | |
392 | =head1 DESCRIPTION |
393 | |
1390c23d |
394 | No user serviceable parts inside. If you really want to know, read the source :) |
395 | |
ec9c1923 |
396 | =head1 METHODS |
397 | |
398 | =head2 Accessors |
399 | |
400 | =over 4 |
401 | |
402 | =item B<class> |
403 | |
404 | =item B<object> |
405 | |
406 | =item B<storage> |
407 | |
b430caa3 |
408 | =item B<seen> |
409 | |
ec9c1923 |
410 | =back |
411 | |
412 | =head2 API |
413 | |
414 | =over 4 |
415 | |
416 | =item B<expand_object> |
417 | |
418 | =item B<collapse_object> |
419 | |
420 | =back |
421 | |
422 | =head2 ... |
423 | |
424 | =over 4 |
425 | |
426 | =item B<collapse_attribute> |
427 | |
428 | =item B<collapse_attribute_value> |
429 | |
430 | =item B<expand_attribute> |
431 | |
432 | =item B<expand_attribute_value> |
433 | |
b430caa3 |
434 | =item B<check_for_cycle_in_collapse> |
435 | |
436 | =item B<check_for_cycle_in_expansion> |
437 | |
ec9c1923 |
438 | =item B<map_attributes> |
439 | |
3db8f791 |
440 | =back |
441 | |
442 | =head2 Type Constraint Handlers |
443 | |
444 | =over 4 |
445 | |
3defafb9 |
446 | =item B<find_type_handler ($type)> |
447 | |
448 | =item B<find_type_handler_for ($name)> |
3db8f791 |
449 | |
3defafb9 |
450 | =item B<add_custom_type_handler ($name, %handlers)> |
3db8f791 |
451 | |
3defafb9 |
452 | =item B<remove_custom_type_handler ($name)> |
ec9c1923 |
453 | |
454 | =back |
455 | |
456 | =head2 Introspection |
457 | |
458 | =over 4 |
459 | |
460 | =item B<meta> |
461 | |
462 | =back |
463 | |
464 | =head1 BUGS |
465 | |
466 | All complex software has bugs lurking in it, and this module is no |
467 | exception. If you find a bug please either email me, or add the bug |
468 | to cpan-RT. |
469 | |
470 | =head1 AUTHOR |
471 | |
472 | Chris Prather E<lt>chris.prather@iinteractive.comE<gt> |
473 | |
474 | Stevan Little E<lt>stevan.little@iinteractive.comE<gt> |
475 | |
476 | =head1 COPYRIGHT AND LICENSE |
477 | |
1f3074ea |
478 | Copyright 2007-2008 by Infinity Interactive, Inc. |
ec9c1923 |
479 | |
480 | L<http://www.iinteractive.com> |
481 | |
482 | This library is free software; you can redistribute it and/or modify |
483 | it under the same terms as Perl itself. |
484 | |
e9739624 |
485 | =cut |
486 | |
487 | |
ec9c1923 |
488 | |