Parser/Type.pm

filter: Statementstmt 26.6% 29 / 109 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 42.1% 8 / 19 Podpod 90.9% 10 / 11 totaltotal 24.3% 47 / 193 CC 27 SCAR 39.7
CC
27
Cov
18%
docs_as_pod
156.0
type_id
42.0
compose
30.0
CRAP 52.9
« Parser/FieldSet.pm ↑ Summary Parser/Type/Number.pm »
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use base qw(Class::Data::Accessor Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 __PACKAGE__->mk_accessors(qw(value));
15 6 __PACKAGE__->mk_classaccessors(qw(idl name idl_doc));
16 6 ▶use Class::ISA;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 METHODS
21  
22 =head2 idl
23  
24 Returns a reference to the L<Thrift::IDL::Type> that informed the creation of this class.
25  
26 =head2 idl_doc
27  
28 Returns a reference to the L<Thrift::IDL> 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 0 ▶sub defined {
Subroutine defined: not called CC=2 SCAR=17.9
Pod documented
45 0 my $self = shift;
46 0 ▶ return defined $self->value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->value ? : 0 0 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<Thrift::Parser::InvalidArgument> or L<Thrift::Parser::InvalidTypedValue>. See subclass documentation for more specifics.
54  
55 =cut
56  
57 0 ▶sub compose {
Subroutine compose: not called CC=5 SCAR=34.0
Pod documented
58 0 my ($class, $value) = @_;
59 0 ▶ if (blessed $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value)) 0 0 0
60 0 ▶ if (! $value->isa($class)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value->isa($class)) 0 0 0
61 0 Thrift::Parser::InvalidArgument->throw("$class compose() can't take a value of ".ref($value));
62 }
63 0 return $value;
64 }
65 0 ▶ Thrift::Parser::InvalidTypedValue->throw("Value '$value' is not a plain scalar") if defined $value && ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and ref $value 0 0 0
Condition0%0/3
defined $value and ref $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and ref $value
defined $value ref $value
Truth table0%0/3
A and B
A= defined $valueB= ref $value
ABresult
0X0
100
111
66 0 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<Thrift::Parser::Type::Container>).
72  
73 =cut
74  
75 0 ▶sub compose_with_idl {
Subroutine compose_with_idl: not called CC=1 SCAR=6.9
Pod documented
76 0 my ($class, $type) = (shift, shift);
77 0 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<Thrift::Protocol>.
85  
86 =head2 write
87  
88 $object->write($protocol)
89  
90 Given a L<Thrift::Protocol> object, will write out this type to the buffer. Overridden in all most subclasses.
91  
92 =cut
93  
94 0 ▶sub write {
Subroutine write: not called CC=3 SCAR=24.8
Pod documented
95 0 my ($self, $output) = @_;
96 0 my $class = ref $self;
97  
98 # Attempt to do a generic writeType call (where 'Type' is found from the type id)
99 0 ▶ if (my $type_id = $class->type_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type_id = $class->type_id) 0 0 0
100 0 my $method = Thrift::Parser::Types->write_method($type_id);
101 0 ▶ if ($output->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->can($method)) 0 0 0
102 0 $output->$method($self->value);
103 0 return;
104 }
105 }
106  
107 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<compose()> to be formed into a proper object before the comparison is run. Throws L<Thrift::Parser::InvalidArgument>.
115  
116 =cut
117  
118 0 ▶sub equal_to {
Subroutine equal_to: not called CC=3 SCAR=24.8
Pod documented
119 0 my ($self, $value) = @_;
120 0 my $class = ref $self;
121  
122 0 ▶ if (! blessed $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (not builtin::blessed($value)) { } 0 0 0
elsif (not $value->isa($class)) { } 0 0 0
123 0 $value = $class->compose($value);
124 }
125 elsif (! $value->isa($class)) {
126 0 Thrift::Parser::InvalidArgument->throw("equal_to() must be called with the same type of object on both sides ($class)");
127 }
128  
129 0 return $class->values_equal($self->value, $value->value);
130 }
131  
132 =head2 values_equal
133  
134 Used internally for the L</equal_to> call.
135  
136 =cut
137  
138 0 ▶sub values_equal {
Subroutine values_equal: not called CC=1 SCAR=6.9
Pod documented
139 0 my ($class, $value_a, $value_b) = @_;
140 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 ▶sub value_name { croak "Not implemented in subclass" }
Subroutine value_name: not called CC=1 SCAR=6.9
Pod documented
150  
151 =head2 value_plain
152  
153 =cut
154  
155 0 ▶sub value_plain { croak "Not implemented in subclass" }
Subroutine value_plain: not called CC=1 SCAR=6.9
Pod documented
156  
157 =head2 type_id
158  
159 Returns the L<Thrift> type id. Overridden in subclasses. Throws L<Thrift::Parser::Exception>.
160  
161 =cut
162  
163 0 ▶sub type_id {
Subroutine type_id: not called CC=6 SCAR=37.4
Pod documented
164 0 my ($self) = @_;
165 0 ▶ my $class = ref $self ? ref $self : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
166  
167 # Find the base type ('i32', 'string', 'Struct')
168 0 my $base_type;
169 0 foreach my $test_class (Class::ISA::self_and_super_path( $class )) {
170 0 ▶ if ($test_class =~ m{^Thrift::Parser::Type::(.+)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_class =~ /^Thrift::Parser::Type::(.+)$/) 0 0 0
171 0 $base_type = $1;
172 0 last;
173 }
174 }
175 0 ▶ if (! $base_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base_type) 0 0 0
176 0 Thrift::Parser::Exception->throw("Couldn't resolve '$class' to a basic type class");
177 }
178  
179 # Lookup the value
180 0 my $id = Thrift::Parser::Types->to_id($base_type);
181 0 ▶ if (! defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
182 0 Thrift::Parser::Exception->throw("Couldn't resolve base type '$base_type' to an id");
183 }
184 0 return $id;
185 }
186  
187 6 ▶sub mk_value_passthrough_methods {
Subroutine mk_value_passthrough_methods: called CC=2 SCAR=7.1
Pod undocumented
188 6 my ($class, @methods) = @_;
189  
190 6 foreach my $method (@methods) {
191 42 my $full_class = $class . '::' . $method;
192 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
193 42 ▶ *{ $full_class } = sub {
Subroutine __ANON__: not called CC=1 SCAR=2.6
194 0 my $self = shift;
195 0 return $self->value->$method(@_);
196 42 };
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 0 ▶sub docs_as_pod {
Subroutine docs_as_pod: not called CC=12 SCAR=50.5
Pod documented
207 0 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</USAGE> below for details on the auto-generated methods within this class.\n";
215  
216 0 ▶ my $is_method = $base_class eq 'Thrift::Parser::Method' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$base_class eq 'Thrift::Parser::Method' ? : 0 0 0
217 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;
Branch: 1 branch
BranchTrueFalseTotal
$base_class eq 'Thrift::Parser::Method' || $base_class eq 'Thrift::Parser::Type::Struct' || $base_class eq 'Thrift::Parser::Type::Exception' ? : 0 0 0
Condition0%0/6
$base_class eq "Thrift::Parser::Method" or $base_class eq "Thrift::Parser::Type::Struct" or $base_class eq "Thrift::Parser::Type::Exception"
l!l&&r!l&&!r
000
$base_class eq "Thrift::Parser::Method" or $base_class eq "Thrift::Parser::Type::Struct"
l!l&&r!l&&!r
000
MC/DC0%0/3
$base_class eq "Thrift::Parser::Method" or $base_class eq "Thrift::Parser::Type::Struct" or $base_class eq "Thrift::Parser::Type::Exception"
$base_class eq "Thrift::Parser::Method" $base_class eq "Thrift::Parser::Type::Struct" $base_class eq "Thrift::Parser::Type::Exception"
Truth table0%0/4
A or B or C
A= $base_class eq "Thrift::Parser::Method"B= $base_class eq "Thrift::Parser::Type::Struct"C= $base_class eq "Thrift::Parser::Type::Exception"
ABCresult
1XX1
01X1
0011
0000
218 0 my $usage_items;
219  
220 0 ▶ if ($is_struct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_struct) 0 0 0
221 0 my @usage_items;
222 0 foreach my $field (@{ $class->idl->fields }) {
223 0 my $type = $field->type;
224 0 my $type_class;
225 0 ▶ if ($type->isa('Thrift::IDL::Type::Custom')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->isa('Thrift::IDL::Type::Custom')) { } 0 0 0
226 0 my $type_name = $type->full_name;
227 0 my $referenced_type = $class->idl_doc->object_full_named($type_name);
228 0 ▶ if (! $referenced_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($referenced_type) 0 0 0
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 ▶ $type_class = join '::', (defined $namespace ? ($namespace) : ()), $type->local_name;
Branch: 1 branch
BranchTrueFalseTotal
defined $namespace ? : 0 0 0
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 ▶ if ($is_method) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_method) { } 0 0 0
elsif ($is_struct) { } 0 0 0
245 0 $usage = <<EOF;
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<compose()> call of the given L<Thrift::Parser::Type>.
259  
260 EOF
261 0 $synopsis = <<EOF;
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 = <<EOF;
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<compose()> call of the given L<Thrift::Parser::Type>.
286  
287 EOF
288 0 $synopsis = <<EOF;
289 # Create a new struct object
290 my \$struct = $class\->compose(
291 $arguments
292 );
293 EOF
294  
295 }
296 else {
297 0 $usage = <<EOF;
298 EOF
299 0 $synopsis = <<EOF;
300 # Create a new Type object
301 my \$object = $class\->compose(\$value);
302 EOF
303 }
304  
305 0 my $pod = <<EOF;
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 <ewaters@gmail.com>
344  
345 =cut
346  
347 6 1;
« Parser/FieldSet.pm ↑ Summary Parser/Type/Number.pm »