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