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