File: Engine.pm

package info (click to toggle)
libmoosex-storage-perl 0.31-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 464 kB
  • sloc: perl: 4,919; makefile: 2
file content (481 lines) | stat: -rw-r--r-- 14,468 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481

package MooseX::Storage::Engine;
use Moose;
use Scalar::Util qw(refaddr);

our $VERSION   = '0.31';
our $AUTHORITY = 'cpan:STEVAN';

# the class marker when
# serializing an object.
our $CLASS_MARKER = '__CLASS__';

has 'storage' => (
    is      => 'ro',
    isa     => 'HashRef',
    default => sub {{}}
);

has 'seen' => (
    is      => 'ro',
    isa     => 'HashRef[Int]', # int is the refaddr
    default => sub {{}}
);

has 'object' => (is => 'rw', isa => 'Object', predicate => '_has_object');
has 'class'  => (is => 'rw', isa => 'Str');

## this is the API used by other modules ...

sub collapse_object {
    my ( $self, %options ) = @_;

	# NOTE:
	# mark the root object as seen ...
	$self->seen->{refaddr $self->object} = undef;
	
    $self->map_attributes('collapse_attribute', \%options);
    $self->storage->{$CLASS_MARKER} = $self->object->meta->identifier;   
	return $self->storage;
}

sub expand_object {
    my ($self, $data, %options) = @_;
   
    $options{check_version}       = 1 unless exists $options{check_version};
    $options{check_authority}     = 1 unless exists $options{check_authority};  

	# NOTE:
	# mark the root object as seen ...
	$self->seen->{refaddr $data} = undef;   
   
    $self->map_attributes('expand_attribute', $data, \%options);
	return $self->storage;   
}

## this is the internal API ...

sub collapse_attribute {
    my ($self, $attr, $options)  = @_;
    my $value = $self->collapse_attribute_value($attr, $options);
    return if !defined($value);
    $self->storage->{$attr->name} = $value;
}

sub expand_attribute {
    my ($self, $attr, $data, $options)  = @_;
    my $value = $self->expand_attribute_value($attr, $data->{$attr->name}, $options);
    $self->storage->{$attr->name} = defined $value ? $value : return;
}

sub collapse_attribute_value {
    my ($self, $attr, $options)  = @_;
    # Faster, but breaks attributes without readers, do we care?
	#my $value = $attr->get_read_method_ref->($self->object);
	my $value = $attr->get_value($self->object);

	# NOTE:
	# this might not be enough, we might
	# need to make it possible for the
	# cycle checker to return the value
    $self->check_for_cycle_in_collapse($attr, $value)
        if ref $value;

    if (defined $value && $attr->has_type_constraint) {
        my $type_converter = $self->find_type_handler($attr->type_constraint, $value);
        (defined $type_converter)
            || confess "Cannot convert " . $attr->type_constraint->name;
        $value = $type_converter->{collapse}->($value, $options);
    }
	return $value;
}

sub expand_attribute_value {
    my ($self, $attr, $value, $options)  = @_;

	# NOTE:
	# (see comment in method above ^^)
    if( ref $value and not(
        $options->{disable_cycle_check} or
        $self->class->does('MooseX::Storage::Traits::DisableCycleDetection')
    )) {       
        $self->check_for_cycle_in_collapse($attr, $value)
    }
   
    if (defined $value && $attr->has_type_constraint) {
        my $type_converter = $self->find_type_handler($attr->type_constraint, $value);
        $value = $type_converter->{expand}->($value, $options);
    }
	return $value;
}

# NOTE:
# possibly these two methods will
# be used by a cycle supporting
# engine. However, I am not sure
# if I can make a cycle one work
# anyway.

sub check_for_cycle_in_collapse {
    my ($self, $attr, $value) = @_;
    (!exists $self->seen->{refaddr $value})
        || confess "Basic Engine does not support cycles in class("
                 . ($attr->associated_class->name) . ").attr("
                 . ($attr->name) . ") with $value";
    $self->seen->{refaddr $value} = undef;
}

sub check_for_cycle_in_expansion {
    my ($self, $attr, $value) = @_;
    (!exists $self->seen->{refaddr $value})
    || confess "Basic Engine does not support cycles in class("
             . ($attr->associated_class->name) . ").attr("
             . ($attr->name) . ") with $value";
    $self->seen->{refaddr $value} = undef;
}

# util methods ...

sub map_attributes {
    my ($self, $method_name, @args) = @_;
    map {
        $self->$method_name($_, @args)
    } grep {
        # Skip our special skip attribute :)
        !$_->does('MooseX::Storage::Meta::Attribute::Trait::DoNotSerialize')
    } ($self->_has_object ? $self->object : $self->class)->meta->get_all_attributes;
}

## ------------------------------------------------------------------
## This is all the type handler stuff, it is in a state of flux
## right now, so this may change, or it may just continue to be
## improved upon. Comments and suggestions are welcomed.
## ------------------------------------------------------------------

# NOTE:
# these are needed by the
# ArrayRef and HashRef handlers
# below, so I need easy access
my %OBJECT_HANDLERS = (
    expand => sub {
        my ($data, $options) = @_;  
        (exists $data->{$CLASS_MARKER})
            || confess "Serialized item has no class marker";
        # check the class more thoroughly here ...
        my ($class, $version, $authority) = (split '-' => $data->{$CLASS_MARKER});
        my $meta = eval { $class->meta };
        confess "Class ($class) is not loaded, cannot unpack" if $@;    
       
        if ($options->{check_version}) {
            my $meta_version = $meta->version;
            if (defined $meta_version && $version) {           
                if ($options->{check_version} eq 'allow_less_than') {
                    ($meta_version <= $version)
                        || confess "Class ($class) versions is not less than currently available."
                                 . " got=($version) available=($meta_version)";               
                }
                elsif ($options->{check_version} eq 'allow_greater_than') {
                    ($meta->version >= $version)
                        || confess "Class ($class) versions is not greater than currently available."
                                 . " got=($version) available=($meta_version)";               
                }           
                else {
                    ($meta->version == $version)
                        || confess "Class ($class) versions don't match."
                                 . " got=($version) available=($meta_version)";
                }
            }
        }
       
        if ($options->{check_authority}) {
            my $meta_authority = $meta->authority;
            ($meta->authority eq $authority)
                || confess "Class ($class) authorities don't match."
                         . " got=($authority) available=($meta_authority)"
                if defined $meta_authority && defined $authority;           
        }
           
        # all is well ...
        $class->unpack($data, %$options);
    },
    collapse => sub {
        my ( $obj, $options ) = @_;
#        ($obj->can('does') && $obj->does('MooseX::Storage::Basic'))
#            || confess "Bad object ($obj) does not do MooseX::Storage::Basic role";
        ($obj->can('pack'))
            || confess "Object ($obj) does not have a &pack method, cannot collapse";
        $obj->pack(%$options);
    },
);


my %TYPES = (
    # NOTE:
    # we need to make sure that we properly numify the numbers
    # before and after them being futzed with, because some of
    # the JSON engines are stupid/annoying/frustrating
    'Int'      => { expand => sub { $_[0] + 0 }, collapse => sub { $_[0] + 0 } },
    'Num'      => { expand => sub { $_[0] + 0 }, collapse => sub { $_[0] + 0 } },
    # These are boring ones, so they use the identity function ...   
    'Str'      => { expand => sub { shift }, collapse => sub { shift } },
    'Bool'     => { expand => sub { shift }, collapse => sub { shift } },
    # These are the trickier ones, (see notes)
    # NOTE:
    # Because we are nice guys, we will check
    # your ArrayRef and/or HashRef one level
    # down and inflate any objects we find.
    # But this is where it ends, it is too
    # expensive to try and do this any more 
    # recursively, when it is probably not
    # nessecary in most of the use cases.
    # However, if you need more then this, subtype
    # and add a custom handler.   
    'ArrayRef' => {
        expand => sub {
            my ( $array, @args ) = @_;
            foreach my $i (0 .. $#{$array}) {
                next unless ref($array->[$i]) eq 'HASH'
                         && exists $array->[$i]->{$CLASS_MARKER};
                $array->[$i] = $OBJECT_HANDLERS{expand}->($array->[$i], @args);
            }
            $array;
        },
        collapse => sub {
            my ( $array, @args ) = @_;
            # NOTE:        
            # we need to make a copy cause
            # otherwise it will affect the
            # other real version.
            [ map {
                blessed($_)
                    ? $OBJECT_HANDLERS{collapse}->($_, @args)
                    : $_
            } @$array ]
        }
    },
    'HashRef'  => {
        expand   => sub {
            my ( $hash, @args ) = @_;
            foreach my $k (keys %$hash) {
                next unless ref($hash->{$k}) eq 'HASH'
                         && exists $hash->{$k}->{$CLASS_MARKER};
                $hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k}, @args);
            }
            $hash;           
        },
        collapse => sub {
            my ( $hash, @args ) = @_;
            # NOTE:        
            # we need to make a copy cause
            # otherwise it will affect the
            # other real version.
            +{ map {
                blessed($hash->{$_})
                    ? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}, @args))
                    : ($_ => $hash->{$_})
            } keys %$hash }           
        }
    },
    'Object'   => \%OBJECT_HANDLERS,
    # NOTE:
    # The sanity of enabling this feature by
    # default is very questionable.
    # - SL
    #'CodeRef' => {
    #    expand   => sub {}, # use eval ...
    #    collapse => sub {}, # use B::Deparse ...       
    #}
);

sub add_custom_type_handler {
    my ($class, $type_name, %handlers) = @_;
    (exists $handlers{expand} && exists $handlers{collapse})
        || confess "Custom type handlers need an expand *and* a collapse method";
    $TYPES{$type_name} = \%handlers;
}

sub remove_custom_type_handler {
    my ($class, $type_name) = @_;
    delete $TYPES{$type_name} if exists $TYPES{$type_name};
}

sub find_type_handler {
    my ($self, $type_constraint, $value) = @_;

    # check if the type is a Maybe and
    # if its parent is not parameterized.
    # If both is true recurse this method
    # using ->type_parameter.
    return $self->find_type_handler($type_constraint->type_parameter, $value)
        if ($type_constraint->parent && $type_constraint->parent eq 'Maybe'
          and not $type_constraint->parent->can('type_parameter'));

    # find_type_for is a method of a union type.  If we can call that method
    # then we are dealign with a union and we need to ascertain which of
    # the union's types we need to use for the value we are serializing.
    if($type_constraint->can('find_type_for')) {
        my $tc = $type_constraint->find_type_for($value);
        return $self->find_type_handler($tc, $value) if defined($tc);
    }

    # this should handle most type usages
    # since they they are usually just
    # the standard set of built-ins
    return $TYPES{$type_constraint->name}
        if exists $TYPES{$type_constraint->name};
     
    # the next possibility is they are
    # a subtype of the built-in types,
    # in which case this will DWIM in
    # most cases. It is probably not
    # 100% ideal though, but until I
    # come up with a decent test case
    # it will do for now.
    foreach my $type (keys %TYPES) {
        return $TYPES{$type}
            if $type_constraint->is_subtype_of($type);
    }
   
    # NOTE:
    # the reason the above will work has to
    # do with the fact that custom subtypes
    # are mostly used for validation of
    # the guts of a type, and not for some
    # weird structural thing which would
    # need to be accomidated by the serializer.
    # Of course, mst or phaylon will probably 
    # do something to throw this assumption
    # totally out the door ;)
    # - SL
   
    # NOTE:
    # if this method hasnt returned by now
    # then we have no been able to find a
    # type constraint handler to match
    confess "Cannot handle type constraint (" . $type_constraint->name . ")";   
}

sub find_type_handler_for {
    my ($self, $type_handler_name) = @_;
    $TYPES{$type_handler_name}
}

no Moose::Role;

1;

__END__

=pod

=head1 NAME

MooseX::Storage::Engine - The meta-engine to handle collapsing and expanding objects

=head1 DESCRIPTION

There really aren't any major user serviceable parts here. However the typical
use case is adding new non-Moose classes to the type registry for
serialization. Here is an example of this for DateTime objects. This
assumes a C<DateTime> type has been registered.

    MooseX::Storage::Engine->add_custom_type_handler(
        'DateTime' => (
            expand   => sub { DateTime->new(shift) },
            collapse => sub { (shift)->iso8601 },
        )
    );

=head1 METHODS

=head2 Accessors

=over 4

=item B<class>

=item B<object>

=item B<storage>

=item B<seen>

=back

=head2 API

=over 4

=item B<expand_object>

=item B<collapse_object>

=back

=head2 ...

=over 4

=item B<collapse_attribute>

=item B<collapse_attribute_value>

=item B<expand_attribute>

=item B<expand_attribute_value>

=item B<check_for_cycle_in_collapse>

=item B<check_for_cycle_in_expansion>

=item B<map_attributes>

=back

=head2 Type Constraint Handlers

=over 4

=item B<find_type_handler ($type)>

=item B<find_type_handler_for ($name)>

=item B<add_custom_type_handler ($name, %handlers)>

=item B<remove_custom_type_handler ($name)>

=back

=head2 Introspection

=over 4

=item B<meta>

=back

=head1 BUGS

All complex software has bugs lurking in it, and this module is no
exception. If you find a bug please either email me, or add the bug
to cpan-RT.

=head1 AUTHOR

Chris Prather E<lt>chris.prather@iinteractive.comE<gt>

Stevan Little E<lt>stevan.little@iinteractive.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2007-2008 by Infinity Interactive, Inc.

L<http://www.iinteractive.com>

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut