File Coverage

blib/lib/Thrift/Parser/Type.pm
Criterion Covered Total %
statement 54 106 50.9
branch 12 40 30.0
condition 1 6 16.6
subroutine 13 19 68.4
pod 10 11 90.9
total 90 182 49.4


line stmt bran cond sub pod time code
1             package Thrift::Parser::Type;
2              
3             =head1 NAME
4              
5             Thrift::Parser::Type - Base clase for OO types
6              
7             =head1 DESCRIPTION
8              
9             =cut
10              
11 6     6   35 use strict;
  6         11  
  6         206  
12 6     6   33 use warnings;
  6         18  
  6         171  
13 6     6   30 use base qw(Class::Data::Accessor Class::Accessor);
  6         11  
  6         7379  
14             __PACKAGE__->mk_accessors(qw(value));
15             __PACKAGE__->mk_classaccessors(qw(idl name idl_doc));
16 6     6   8821 use Class::ISA;
  6         18107  
  6         1672  
17 6     6   49 use Scalar::Util qw(blessed);
  6         12  
  6         430  
18 6     6   39 use Carp;
  6         13  
  6         6687  
19              
20             =head1 METHODS
21              
22             =head2 idl
23              
24             Returns a reference to the L that informed the creation of this class.
25              
26             =head2 idl_doc
27              
28             Returns a reference to the L object that this was formed from.
29              
30             =head2 name
31              
32             Returns the simple name of the type.
33              
34             =head2 value
35              
36             Returns the internal representation of the value of this object.
37              
38             =head2 defined
39              
40             Boolean; is a value defined for this object.
41              
42             =cut
43              
44             sub defined {
45 0     0 1 0 my $self = shift;
46 0 0       0 return defined $self->value ? 1 : 0;
47             }
48              
49             =head2 compose
50              
51             my $object = $subclass->compose(..);
52              
53             Returns a new object in the class/subclass namespace with the value given. Generally you may pass a simple perl variable or another object in this same class to create the new object. Throws L or L. See subclass documentation for more specifics.
54              
55             =cut
56              
57             sub compose {
58 78     78 1 11169 my ($class, $value) = @_;
59 78 50       231 if (blessed $value) {
60 0 0       0 if (! $value->isa($class)) {
61 0         0 Thrift::Parser::InvalidArgument->throw("$class compose() can't take a value of ".ref($value));
62             }
63 0         0 return $value;
64             }
65 78 50 33     447 Thrift::Parser::InvalidTypedValue->throw("Value '$value' is not a plain scalar") if defined $value && ref $value;
66 78         698 return $class->new({ value => $value });
67             }
68              
69             =head2 compose_with_idl
70              
71             Used internally. Overridden for complex type classes that require the IDL to inform their creation and schema (mainly L).
72              
73             =cut
74              
75             sub compose_with_idl {
76 8     8 1 463 my ($class, $type) = (shift, shift);
77 8         55 return $class->compose(@_);
78             }
79              
80             =head2 read
81              
82             my $object = $class->read($protocol);
83              
84             Implemented in subclasses, this will create new objects from a L.
85              
86             =head2 write
87              
88             $object->write($protocol)
89              
90             Given a L object, will write out this type to the buffer. Overridden in all most subclasses.
91              
92             =cut
93              
94             sub write {
95 2     2 1 41 my ($self, $output) = @_;
96 2         5 my $class = ref $self;
97              
98             # Attempt to do a generic writeType call (where 'Type' is found from the type id)
99 2 50       7 if (my $type_id = $class->type_id) {
100 2         11 my $method = Thrift::Parser::Types->write_method($type_id);
101 2 50       22 if ($output->can($method)) {
102 2         27 $output->$method($self->value);
103 2         7 return;
104             }
105             }
106              
107 0         0 Thrift::Parser::NotImplemented->throw($class . "->write() hasn't been overridden yet");
108             }
109              
110             =head2 equal_to
111              
112             if ($object_a->equal_to($object_b)) { ... }
113              
114             Performs an equality test between two blessed objects. You may also call with a non-blessed reference (a perl scalar, for instance), which will be passed to C to be formed into a proper object before the comparison is run. Throws L.
115              
116             =cut
117              
118             sub equal_to {
119 10     10 1 13 my ($self, $value) = @_;
120 10         17 my $class = ref $self;
121              
122 10 50       60 if (! blessed $value) {
    50          
123 0         0 $value = $class->compose($value);
124             }
125             elsif (! $value->isa($class)) {
126 0         0 Thrift::Parser::InvalidArgument->throw("equal_to() must be called with the same type of object on both sides ($class)");
127             }
128              
129 10         32 return $class->values_equal($self->value, $value->value);
130             }
131              
132             =head2 values_equal
133              
134             Used internally for the L call.
135              
136             =cut
137              
138             sub values_equal {
139 0     0 1 0 my ($class, $value_a, $value_b) = @_;
140 0         0 Thrift::Parser::NotImplemented->throw($class . "->values_equal() hasn't been overridden yet");
141             }
142              
143             =head2 value_name
144              
145             Implemented by the specific type.
146              
147             =cut
148              
149 0     0 1 0 sub value_name { croak "Not implemented in subclass" }
150              
151             =head2 value_plain
152              
153             =cut
154              
155 0     0 1 0 sub value_plain { croak "Not implemented in subclass" }
156              
157             =head2 type_id
158              
159             Returns the L type id. Overridden in subclasses. Throws L.
160              
161             =cut
162              
163             sub type_id {
164 47     47 1 183 my ($self) = @_;
165 47 100       135 my $class = ref $self ? ref $self : $self;
166              
167             # Find the base type ('i32', 'string', 'Struct')
168 47         49 my $base_type;
169 47         159 foreach my $test_class (Class::ISA::self_and_super_path( $class )) {
170 48 100       2879 if ($test_class =~ m{^Thrift::Parser::Type::(.+)$}) {
171 47         118 $base_type = $1;
172 47         77 last;
173             }
174             }
175 47 50       118 if (! $base_type) {
176 0         0 Thrift::Parser::Exception->throw("Couldn't resolve '$class' to a basic type class");
177             }
178              
179             # Lookup the value
180 47         177 my $id = Thrift::Parser::Types->to_id($base_type);
181 47 50       136 if (! defined $id) {
182 0         0 Thrift::Parser::Exception->throw("Couldn't resolve base type '$base_type' to an id");
183             }
184 47         232 return $id;
185             }
186              
187             sub mk_value_passthrough_methods {
188 6     6 0 24 my ($class, @methods) = @_;
189              
190 6         18 foreach my $method (@methods) {
191 42         80 my $full_class = $class . '::' . $method;
192 6     6   42 no strict 'refs';
  6         11  
  6         3903  
193 42         428 *{ $full_class } = sub {
194 0     0     my $self = shift;
195 0           return $self->value->$method(@_);
196 42         131 };
197             }
198             }
199              
200             =head2 docs_as_pod
201              
202             Returns a POD formatted string that documents a derived class.
203              
204             =cut
205              
206             sub docs_as_pod {
207 0     0 1   my ($class, $base_class) = @_;
208              
209 0           my $idl = $class->idl;
210 0           my $name = $class->name;
211            
212 0           my ($synopsis, $usage, $arguments);
213              
214 0           my $description = "This is an auto-generated subclass of L<$base_class>; see the docs for that module for inherited methods. Check the L below for details on the auto-generated methods within this class.\n";
215              
216 0 0         my $is_method = $base_class eq 'Thrift::Parser::Method' ? 1 : 0;
217 0 0 0       my $is_struct = ($base_class eq 'Thrift::Parser::Method' || $base_class eq 'Thrift::Parser::Type::Struct' || $base_class eq 'Thrift::Parser::Type::Exception') ? 1 : 0;
218 0           my $usage_items;
219              
220 0 0         if ($is_struct) {
221 0           my @usage_items;
222 0           foreach my $field (@{ $class->idl->fields }) {
  0            
223 0           my $type = $field->type;
224 0           my $type_class;
225 0 0         if ($type->isa('Thrift::IDL::Type::Custom')) {
226 0           my $type_name = $type->full_name;
227 0           my $referenced_type = $class->idl_doc->object_full_named($type_name);
228 0 0         if (! $referenced_type) {
229 0           Thrift::Parser::InvalidSpec->throw("Couldn't find definition of custom type '".$type_name."'");
230             }
231 0           my $namespace = $referenced_type->{header}->namespace('perl');
232 0 0         $type_class = join '::', (defined $namespace ? ($namespace) : ()), $type->local_name;
233             }
234             else {
235 0           $type_class = 'Thrift::Parser::Type::' . $type->name;
236             }
237 0           $arguments .= ' ' . $field->name . " => $type_class\->compose(...),\n";
238 0           push @usage_items, '=item I<' . $field->name . "> (type: L<$type_class>)";
239             }
240 0           chomp $arguments; # trailing \n
241 0           $usage_items = join "\n\n", @usage_items;
242             }
243              
244 0 0         if ($is_method) {
    0          
245 0           $usage = <
246             =head2 compose_message_call
247              
248             my \$message = $class\->compose_message_call(...);
249              
250             Call with a list of key/value pairs. The accepted pairs are as follows:
251              
252             =over
253              
254             $usage_items
255              
256             =back
257              
258             The value can either be an object that's strictly typed or simple Perl data structure that is a permissable argument to the C call of the given L.
259              
260             EOF
261 0           $synopsis = <
262             # Create a new Method object
263             my \$message = $class\->compose_message_call(
264             $arguments
265             );
266              
267             # Create a return value from this method
268             my \$return = $class\->return_class->compose(...);
269             EOF
270             }
271             elsif ($is_struct) {
272 0           $usage = <
273             =head2 compose
274              
275             my \$struct = $class\->compose(...);
276              
277             Call with a list of key/value pairs. The accepted pairs are as follows:
278              
279             =over
280              
281             $usage_items
282              
283             =back
284              
285             The value can either be an object that's strictly typed or simple Perl data structure that is a permissable argument to the C call of the given L.
286              
287             EOF
288 0           $synopsis = <
289             # Create a new struct object
290             my \$struct = $class\->compose(
291             $arguments
292             );
293             EOF
294              
295             }
296             else {
297 0           $usage = <
298             EOF
299 0           $synopsis = <
300             # Create a new Type object
301             my \$object = $class\->compose(\$value);
302             EOF
303             }
304              
305 0           my $pod = <
306             =pod
307              
308             =head1 NAME
309              
310             $class - An auto-generated subclass of $base_class
311              
312             =head1 SYNOPSIS
313              
314             $synopsis
315              
316             =head1 DESCRIPTION
317              
318             $description
319              
320             =head1 USAGE
321              
322             $usage
323              
324             =head1 SEE ALSO
325              
326             L<$base_class>
327              
328             EOF
329              
330 0           $pod =~ s{^ =}{=}gms;
331              
332 0           return $pod;
333             }
334              
335             =head1 COPYRIGHT
336              
337             Copyright (c) 2009 Eric Waters and XMission LLC (http://www.xmission.com/). All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
338              
339             The full text of the license can be found in the LICENSE file included with this module.
340              
341             =head1 AUTHOR
342              
343             Eric Waters
344              
345             =cut
346              
347             1;