Parser/Type/Container.pm

filter: Statementstmt 10.4% 17 / 162 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 31.2% 5 / 16 Podpod 100% 10 / 10 totaltotal 9.5% 32 / 334 CC 68 SCAR 55.7
CC
68
Cov
6%
compose
702.0
define
132.0
_recursive_define_resolve
90.0
CRAP 262.1
↑ Summary Parser/FieldSet.pm »
1 package Thrift::Parser::Type::Container;
2  
3 =head1 NAME
4  
5 Thrift::Parser::Type::Container - Container base class
6  
7 =head1 DESCRIPTION
8  
9 This class implements common behavior for L<Thrift::Parser::Type::list>, L<Thrift::Parser::Type::map> and L<Thrift::Parser::Type::set>.
10  
11 =cut
12  
13 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use base qw(Thrift::Parser::Type);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 __PACKAGE__->mk_accessors(qw(val_type));
19  
20 =head1 METHODS
21  
22 This class inherits from L<Thrift::Parser::Type>; see docs there for inherited methods.
23  
24 =head2 compose
25  
26 $map_class->define(..)->compose({ key => $value, ... });
27  
28 $map_class->define(..)->compose([ $key => $value, ... ]);
29  
30 $list_set_class->define(..)->compose([ $item, $item ]);
31  
32 Call L</define> first before using this method. If map, call with array or hashref. For set and list, call with arrayref. Creates a new object in this class. Throws L<Thrift::Parser::InvalidArgument>.
33  
34 =cut
35  
36 0 ▶sub compose {
Subroutine compose: not called CC=26 SCAR=65.5
Pod documented
37 0 my ($self, $value) = @_;
38 0 my $class;
39 0 ▶ if (! ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $self) { } 0 0 0
40 0 $class = $self;
41 0 $self = $class->new();
42 }
43 else {
44 0 $class = ref $self;
45 }
46  
47 # If compose() has been called on an already existant container, copy myself to a
48 # newly blessed object (sans value) so we can inherit the same val/key_type_class/define values
49 0 ▶ if (defined $self->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"value"}) 0 0 0
50 0 $self = bless { %$self, value => [] }, $class;
51 }
52  
53 0 my $val_type = $self->{val_type_class};
54 0 ▶ if (! defined $val_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val_type) 0 0 0
55 0 ▶ if ($class->idl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->idl) 0 0 0
56 # We should be able to infer the define call for this compose() automatically
57 0 return $class->compose_with_idl($class->idl->type, $value);
58 }
59 0 Thrift::Parser::InvalidArgument->throw("Must call define() on $class before compose()");
60 }
61  
62 0 ▶ if ($self->{val_type_define}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"val_type_define"}) 0 0 0
63 0 $val_type = $self->{val_type_class}->define(@{ $self->{val_type_define} });
64 }
65  
66 0 my $key_type;
67 0 ▶ if ($class->can('key_type')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("key_type")) 0 0 0
68 0 $key_type = $self->{key_type_class};
69 0 ▶ if (! defined $key_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key_type) 0 0 0
70 0 Thrift::Parser::InvalidArgument->throw("Must call define() on $class before compose() for both key and val types");
71 }
72  
73 0 ▶ if ($self->{key_type_define}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"key_type_define"}) 0 0 0
74 0 $key_type = $self->{key_type_class}->define(@{ $self->{key_type_define} });
75 }
76 }
77  
78 0 ▶ if (blessed $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value)) 0 0 0
79 0 ▶ if (! $value->isa($class)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value->isa($class)) 0 0 0
80 0 Thrift::Parser::InvalidArgument->throw("$class compose() can't take a value of ".ref($value));
81 }
82 #print Dumper({ self => $self, new_value => $value });
83 0 foreach my $key (qw(val_type val_type_class val_type_define key_type key_type_class key_type_define)) {
84 0 ▶ next unless defined $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$key} 0 0 0
85 0 my ($value_a, $value_b) = ($self->{$key}, $value->{$key});
86 0 ▶ if ($key =~ m{_define$}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key =~ /_define$/) { } 0 0 0
elsif ($value_a ne $value_b) { } 0 0 0
87 # Deep check of similarity
88 0 ▶ if (Dumper($value_a) ne Dumper($value_b)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Dumper($value_a) ne Dumper($value_b)) 0 0 0
89 0 Thrift::Parser::InvalidArgument->throw("$class compose() invalid typed object passed; $key expected '".Dumper($value_a)."', got '".Dumper($value_b)."'");
90 }
91 }
92 elsif ($value_a ne $value_b) {
93 0 Thrift::Parser::InvalidArgument->throw("$class compose() invalid typed object passed; $key expected '$value_a', got '$value_b'");
94 }
95 }
96 0 return $value;
97 }
98  
99 0 my @values;
100 0 ▶ if ($key_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_type) { } 0 0 0
101 0 ▶ if (! ref $value || (ref $value ne 'HASH' && ref $value ne 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $value or ref $value ne "HASH" and ref $value ne "ARRAY") 0 0 0
Condition0%0/6
not ref $value or ref $value ne "HASH" and ref $value ne "ARRAY"
l!l&&r!l&&!r
000
ref $value ne "HASH" and ref $value ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
not ref $value or ref $value ne "HASH" and ref $value ne "ARRAY"
not ref $value ref $value ne "HASH" ref $value ne "ARRAY"
Truth table0%0/4
not(A) or B and C
A= not ref $valueB= ref $value ne "HASH"C= ref $value ne "ARRAY"
ABCresult
1XX1
0111
00X0
0100
102 0 Thrift::Parser::InvalidArgument->throw("Composing a $class requires a HASHREF or ARRAYREF");
103 }
104 0 ▶ my @args = ( ref $value eq 'HASH' ? %$value : @$value );
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'HASH' ? : 0 0 0
105 0 ▶ if (int @args % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (int @args % 2 != 0) 0 0 0
106 0 Thrift::Parser::InvalidArgument->throw("Composing containers with key's requires a hash; I see an odd number of pairs here");
107 }
108 0 ▶ for (my $i = 0; $i <= $#args; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#args) 0 0 0
109 0 my ($key, $val) = @args[$i .. $i + 1];
110 0 push @values, [
111 $key_type->compose($key),
112 $val_type->compose($val),
113 ];
114 }
115 }
116 else {
117 0 ▶ if (! ref $value || ref $value ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $value or ref $value ne "ARRAY") 0 0 0
Condition0%0/3
not ref $value or ref $value ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $value or ref $value ne "ARRAY"
not ref $value ref $value ne "ARRAY"
Truth table0%0/3
not(A) or B
A= not ref $valueB= ref $value ne "ARRAY"
ABresult
1X1
011
000
118 0 Thrift::Parser::InvalidArgument->throw("Composing a $class requires an ARRAYREF");
119 }
120 0 foreach my $val (@$value) {
121 0 push @values, $val_type->compose($val);
122 }
123 }
124  
125 0 $self->value(\@values);
126  
127 0 return $self;
128 }
129  
130 =head2 define
131  
132 my $list = $list_class->define('::string')->compose([ ... ]);
133  
134 my $nested_list = $list_class->define('::list' => [ '::string' ])->compose([ ... ]);
135  
136 my $map = $map_class->define('::i32', '::string')->compose([ ... ]);
137  
138 Call define with a list of class names that define the structure of this container. In the case of map, pass two values, $key_class and $val_class. For set and list, just $val_class. If either the key or the value class are themselves containers, the next argument is expected to be an arrayref of arguments for the nested C<define()> call. Returns a new object which is ready for L</compose> to be called.
139  
140 =cut
141  
142 0 ▶sub define {
Subroutine define: not called CC=11 SCAR=48.8
Pod documented
143 0 my ($class, @args) = @_;
144  
145 0 my $self = $class->new();
146  
147 0 foreach my $key (grep { $self->can($_) } qw(key_type val_type)) {
148 0 my $type = shift @args;
149 0 ▶ if (! defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 0 0
150 0 Thrift::Parser::InvalidArgument->throw("$class define() requires type for '$key'");
151 }
152 0 ▶ if (ref $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type) 0 0 0
153 0 Thrift::Parser::InvalidArgument->throw("$class define() invalid type $type");
154 }
155 0 ▶ if ($type =~ m{^::}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^::/) 0 0 0
156 0 $type = 'Thrift::Parser::Type' . $type;
157 }
158 0 ▶ if (! $type->can('type_id')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type->can("type_id")) 0 0 0
159 0 Thrift::Parser::InvalidArgument->throw("$type doesn't support type_id()");
160 }
161 0 $self->$key( $type->type_id );
162 0 $self->{$key . '_class'} = $type;
163  
164 0 ▶ if ($type->can('val_type') && defined $args[0] && ref $args[0] && ref $args[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->can("val_type") and defined $args[0] and ref $args[0] and ref $args[0] eq "ARRAY") 0 0 0
Condition0%0/9
$type->can("val_type") and defined $args[0] and ref $args[0] and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
000
$type->can("val_type") and defined $args[0] and ref $args[0]
!ll&&!rl&&r
000
$type->can("val_type") and defined $args[0]
!ll&&!rl&&r
000
MC/DC0%0/4
$type->can("val_type") and defined $args[0] and ref $args[0] and ref $args[0] eq "ARRAY"
$type->can("val_type") defined $args[0] ref $args[0] ref $args[0] eq "ARRAY"
Truth table0%0/5
A and B and C and D
A= $type->can("val_type")B= defined $args[0]C= ref $args[0]D= ref $args[0] eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
165 0 $self->{$key . '_define'} = shift @args;
166 }
167 }
168  
169 0 ▶ if (int @args) {
Branch: 1 branch
BranchTrueFalseTotal
if (int @args) 0 0 0
170 0 Thrift::Parser::InvalidArgument->throw("$class define() unexpected number of args (".int(@args).") left over");
171 }
172  
173 0 return $self;
174 }
175  
176 0 ▶sub compose_with_idl {
Subroutine compose_with_idl: not called CC=3 SCAR=24.8
Pod documented
177 0 my ($class, $type, $value) = @_;
178  
179 0 ▶ if (blessed $value && $value->isa($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa($class)) 0 0 0
Condition0%0/3
builtin::blessed($value) and $value->isa($class)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and $value->isa($class)
builtin::blessed($value) $value->isa($class)
Truth table0%0/3
A and B
A= builtin::blessed($value)B= $value->isa($class)
ABresult
0X0
100
111
180 # Thrift::Parser::Unimplemented->throw("TODO: determine if this typed value adheres to the IDL type");
181 0 return $value;
182 }
183  
184 0 my @define = $class->_recursive_define_resolve($type);
185  
186 0 return $class->define(@define)->compose($value);
187 }
188  
189 0 ▶sub _recursive_define_resolve {
Subroutine _recursive_define_resolve: not called CC=9 SCAR=45.0
190 0 my ($class, $type) = @_;
191  
192 0 my @define;
193 0 foreach my $key (grep { $class->can($_) } qw(key_type val_type)) {
194 # If the type of the container is a typedef, and we're in a custom class, resolve it via the object's idl
195 # TODO: there's no guarantee the first referenced type is a container type; may need to recurse
196 0 ▶ if ($type->isa('Thrift::IDL::Type::Custom') && $class->can('idl') && $class->idl->isa('Thrift::IDL::TypeDef')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->isa("Thrift::IDL::Type::Custom") and $class->can("idl") and $class->idl->isa("Thrift::IDL::TypeDef")) 0 0 0
Condition0%0/6
$type->isa("Thrift::IDL::Type::Custom") and $class->can("idl") and $class->idl->isa("Thrift::IDL::TypeDef")
!ll&&!rl&&r
000
$type->isa("Thrift::IDL::Type::Custom") and $class->can("idl")
!ll&&!rl&&r
000
MC/DC0%0/3
$type->isa("Thrift::IDL::Type::Custom") and $class->can("idl") and $class->idl->isa("Thrift::IDL::TypeDef")
$type->isa("Thrift::IDL::Type::Custom") $class->can("idl") $class->idl->isa("Thrift::IDL::TypeDef")
Truth table0%0/4
A and B and C
A= $type->isa("Thrift::IDL::Type::Custom")B= $class->can("idl")C= $class->idl->isa("Thrift::IDL::TypeDef")
ABCresult
0XX0
10X0
1100
1111
197 0 $type = $class->idl->type;
198 }
199 0 ▶ next if ! $type->can($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $type->can($key) 0 0 0
200 0 my $idl_type = $type->$key;
201  
202 0 ▶ if ($idl_type->isa('Thrift::IDL::Type::Custom')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($idl_type->isa('Thrift::IDL::Type::Custom')) { } 0 0 0
elsif ($idl_type->can('val_type')) { } 0 0 0
203 0 my $namespace = $idl_type->{header}->namespace('perl');
204 0 ▶ push @define, join ('::', (defined $namespace ? ($namespace) : ()), $idl_type->name);
Branch: 1 branch
BranchTrueFalseTotal
defined $namespace ? : 0 0 0
205 }
206 elsif ($idl_type->can('val_type')) {
207 0 push @define, '::' . $idl_type->name;
208 0 push @define, [ _recursive_define_resolve($class, $idl_type) ];
209 }
210 else {
211 0 push @define, '::' . $idl_type->name;
212 }
213 }
214 0 return @define;
215 }
216  
217 =head2 keys
218  
219 Valid only for map types. Returns a list of the keys.
220  
221 =cut
222  
223 0 ▶sub keys {
Subroutine keys: not called CC=3 SCAR=24.8
Pod documented
224 0 my $self = shift;
225 0 ▶ if (! $self->can('key_type')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can("key_type")) 0 0 0
226 0 Thrift::Parser::Exception->throw("Can't call 'keys()' on ".ref($self));
227 }
228 0 my @keys;
229 0 foreach my $pair (@{ $self->{value} }) {
230 0 push @keys, $pair->[0];
231 }
232 0 return @keys;
233 }
234  
235 =head2 values
236  
237 Returns a list of the values.
238  
239 =cut
240  
241 0 ▶sub values {
Subroutine values: not called CC=4 SCAR=30.0
Pod documented
242 0 my $self = shift;
243 0 ▶ my $is_map = $self->can('key_type') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->can('key_type') ? : 0 0 0
244  
245 0 my @values;
246 0 foreach my $value (@{ $self->{value} }) {
247 0 ▶ push @values, $is_map ? $value->[1] : $value;
Branch: 1 branch
BranchTrueFalseTotal
$is_map ? : 0 0 0
248 }
249 0 return @values;
250 }
251  
252 0 ▶sub value_plain {
Subroutine value_plain: not called CC=6 SCAR=37.4
Pod documented
253 0 my ($self) = @_;
254  
255 0 ▶ if ($self->can('key_type')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('key_type')) { } 0 0 0
256 0 my %hash;
257 0 foreach my $pair (@{ $self->{value} }) {
258 0 my ($key, $value) = @$pair;
259 0 ▶ if (! blessed $key) {
Branch: 2 branches
BranchTrueFalseTotal
if (not builtin::blessed($key)) { } 0 0 0
elsif (not builtin::blessed($value)) { } 0 0 0
260 0 die "Key is not blessed: " . Dumper($key);
261 }
262 elsif (! blessed $value) {
263 0 die "Value is not blessed: " . Dumper($value);
264 }
265 0 $hash{ $key->value_plain } = $value->value_plain;
266 }
267 0 return \%hash;
268 }
269 else {
270 0 my @array;
271 0 foreach my $value (@{ $self->{value} }) {
272 0 push @array, $value->value_plain;
273 }
274 0 return \@array;
275 }
276 }
277  
278 =head2 size
279  
280 Returns the number of values or key/value pairs (in the case of a map).
281  
282 =cut
283  
284 0 ▶sub size {
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
285 0 my $self = shift;
286 0 return int @{ $self->{value} };
287 }
288  
289 =head2 each
290  
291 Sets up an iterator over all the elements of this object and returns the next value or key/value pair (as list). Returns undef or an empty list (in the case of a map). Does not auto-reset; call L</each_reset> to reset the iterator.
292  
293 =cut
294  
295 0 ▶sub each {
Subroutine each: not called CC=6 SCAR=37.4
Pod documented
296 0 my $self = shift;
297 0 ▶ my $is_map = $self->can('key_type') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->can('key_type') ? : 0 0 0
298  
299 0 my $idx = $self->{_each_idx};
300 0 ▶ $idx = 0 unless defined $idx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx 0 0 0
301  
302 0 ▶ if ($idx > $#{ $self->{value} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx > $#{$$self{"value"};}) 0 0 0
303 0 $self->{_each_idx} = undef;
304 0 ▶ return $is_map ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
$is_map ? : 0 0 0
305 }
306  
307 0 $self->{_each_idx} = $idx + 1;
308 0 ▶ return $is_map ? @{ $self->{value}[$idx] } : $self->{value}[$idx];
Branch: 1 branch
BranchTrueFalseTotal
$is_map ? : 0 0 0
309 }
310  
311 =head2 each_reset
312  
313 Resets the L</reset> iterator.
314  
315 =cut
316  
317 0 ▶sub each_reset {
Subroutine each_reset: not called CC=1 SCAR=6.9
Pod documented
318 0 my $self = shift;
319 0 $self->{_each_idx} = undef;
320 }
321  
322 =head2 index
323  
324 my $value = $list->index(0);
325 my ($key, $value) = $map->index(0);
326  
327 Returns the value at the index given (zero starting). Returns a list if a map type. Returns undef or () if not present. Throws L<Thrift::Parser::InvalidArgument>.
328  
329 =cut
330  
331 0 ▶sub index {
Subroutine index: not called CC=8 SCAR=42.8
Pod documented
332 0 my ($self, $idx) = @_;
333 0 ▶ Thrift::Parser::InvalidArgument->throw("Pass an index number")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx and not ref $idx and $idx =~ /^\d+$/ 0 0 0
Condition0%0/6
defined $idx and not ref $idx and $idx =~ /^\d+$/
!ll&&!rl&&r
000
defined $idx and not ref $idx
!ll&&!rl&&r
000
MC/DC0%0/3
defined $idx and not ref $idx and $idx =~ /^\d+$/
defined $idx not ref $idx $idx =~ /^\d+$/
Truth table0%0/4
A and not(B) and C
A= defined $idxB= not ref $idxC= $idx =~ /^\d+$/
ABCresult
0XX0
10X0
1100
1111
334 unless defined $idx && ! ref $idx && $idx =~ m{^\d+$};
335 0 ▶ my $is_map = $self->can('key_type') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->can('key_type') ? : 0 0 0
336 0 ▶ return ($is_map ? () : undef) if $idx > $#{ $self->{value} };
Branch: 2 branches
BranchTrueFalseTotal
if $idx > $#{$$self{"value"};} 0 0 0
$is_map ? : 0 0 0
337 0 ▶ return $is_map ? @{ $self->{value}[$idx] } : $self->{value}[$idx];
Branch: 1 branch
BranchTrueFalseTotal
$is_map ? : 0 0 0
338 }
339  
340 =head1 COPYRIGHT
341  
342 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.
343  
344 The full text of the license can be found in the LICENSE file included with this module.
345  
346 =head1 AUTHOR
347  
348 Eric Waters <ewaters@gmail.com>
349  
350 =cut
351  
352 6 1;
↑ Summary Parser/FieldSet.pm »