ProtocolBuffers.pm

filter: Statementstmt 94.1% 340 / 361 Branchbran 71.4% 90 / 126 Conditioncond 53.4% 23 / 43 MC/DCmcdc 22.2% 6 / 27 Subroutinesub 100% 47 / 47 Podpod 18.1% 2 / 11 totaltotal 82.6% 508 / 615 CC 91 SCAR 26.4
CC
91
Cov
85%
_parse
31.6
_create_message_or_group
22.7
get
21.5
CRAP 14.0
« ProtocolBuffers/CodecIV32.pm ↑ Summary ProtocolBuffers/CodeGen.pm »
1 package Google::ProtocolBuffers;
2
3 14 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 14 ▶use Google::ProtocolBuffers::Codec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Google::ProtocolBuffers::Constants qw/:complex_types :labels/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Class::Accessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 14 our $VERSION = "0.12";
15
16 10 ▶sub parsefile {
Subroutine parsefile: called CC=2 SCAR=7.0
Pod documented
17 10 my $self = shift;
18 10 my $proto_filename = shift;
19 10 ▶ my $opts = shift || {};
Condition50%1/2
shift() || {}
l!l
100
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
20
21 10 return $self->_parse({file=>$proto_filename}, $opts);
22 }
23
24 12 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
Pod documented
25 12 my $self = shift;
26 12 my $proto_text = shift;
27 12 ▶ my $opts = shift || {};
Condition100%2/2
shift() || {}
l!l
84
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
28
29 12 return $self->_parse({text=>$proto_text}, $opts);
30 }
31
32 ## Positional access is slightly faster than named one.
33 ## Currently, it's in the same order as text in proto file
34 ## "optional" (LABEL) int32 (type) foo (name) = 1 (number) [default=...]
35 14 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
36 14 F_LABEL => 0,
37 F_TYPE => 1,
38 F_NAME => 2,
39 F_NUMBER => 3,
40 F_DEFAULT => 4,
41 14 };
42
43 22 ▶sub _parse {
Subroutine _parse: called CC=31 SCAR=34.5
44 22 my $self = shift;
45 22 my $source = shift;
46 22 my $opts = shift;
47
48 22 require 'Google/ProtocolBuffers/Compiler.pm';
49 22 my $types = Google::ProtocolBuffers::Compiler->parse($source, $opts);
50
51 ##
52 ## 1. Create enums - they will be used as default values for fields
53 ##
54 22 my @created_enums;
55 22 ▶ while (my ($type_name, $desc) = each %$types) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$types) 344 22 366
56 344 ▶ next unless $desc->{kind} eq 'enum';
Branch: 1 branch
BranchTrueFalseTotal
unless $desc->{"kind"} eq "enum" 295 49 344
57 49 my $class_name = $self->_get_class_name_for($type_name, $opts);
58 49 $self->create_enum($class_name, $desc->{fields});
59 49 push @created_enums, $class_name;
60 }
61
62 ##
63 ## 2. Create groups and messages,
64 ## Fill default values of fields and convert their
65 ## types (my_package.message_a) into Perl classes names (MyPackage::MessageA)
66 ##
67 22 my @created_messages;
68 22 ▶ while (my ($type_name, $desc) = each %$types) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$types) 344 21 365
69 344 my $kind = $desc->{kind};
70 344 my @fields;
71 my %oneofs;
72
73 344 ▶ if ($kind =~ /^(enum|oneof)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($kind =~ /^(enum|oneof)$/) { } 50 294 344
elsif ($kind eq 'group') { } 57 237 294
elsif ($kind eq 'message') { } 237 0 237
74 50 next;
75 } elsif ($kind eq 'group') {
76 57 push @fields, @{$desc->{fields}};
77 } elsif ($kind eq 'message') {
78 237 push @fields, @{$desc->{fields}};
79
80 ##
81 ## Get names for extensions fields.
82 ## Original (full quilified) name is like 'package.MessageA.field'.
83 ## If 'simple_extensions' is true, it will be cut to the last element: 'field'.
84 ## Otherwise, it will be enclosed in brackets and all part common to message type
85 ## will be removed, e.g. for message 'package.MessageB' it will be '[MessageA.field]'
86 ## If message is 'other_package.MessageB', it will be '[package.MessageA.field]'
87 ##
88 237 foreach my $e (@{$desc->{extensions}}) {
89 658 my $field_name = $e->[F_NAME];
90 658 my $new_name;
91 658 ▶ if ($opts->{simple_extensions}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'simple_extensions'}) { } 6 652 658
92 6 ▶ $new_name = ($field_name =~ /\.(\w+)$/) ? $1 : $field_name;
Branch: 1 branch
BranchTrueFalseTotal
$field_name =~ /\.(\w+)$/ ? : 5 1 6
93 } else {
94 ## remove common identifiers from start of f.q.i.
95 652 my @type_idents = split qr/\./, $type_name;
96 652 my @field_idents = split qr/\./, $field_name;
97 652 ▶ while (@type_idents && @field_idents) {
Branch: 1 branch
BranchTrueFalseTotal
if (@type_idents and @field_idents) 1308 0 1308
Condition33%1/3
@type_idents and @field_idents
!ll&&!rl&&r
001308
MC/DC0%0/2
@type_idents and @field_idents
@type_idents @field_idents
Truth table33%1/3
A and B
A= @type_identsB= @field_idents
ABresult
0X0
100
111
98 1308 ▶ last if $type_idents[0] ne $field_idents[0];
Branch: 1 branch
BranchTrueFalseTotal
if $type_idents[0] ne $field_idents[0] 652 656 1308
99 656 shift @type_idents;
100 656 shift @field_idents;
101 }
102 652 ▶ die "Can't create name for extension field '$field_name' in '$type_name'"
Branch: 1 branch
BranchTrueFalseTotal
unless @field_idents 0 652 652
103 unless @field_idents;
104 652 $new_name = '[' . join('.', @field_idents) . ']';
105 }
106 658 $e->[F_NAME] = $new_name;
107 658 push @fields, $e;
108 }
109  
110 ##
111 ## Get names for oneof fields.
112 ##
113 237 foreach my $oneof_name (@{$desc->{oneofs}}) {
114 1 my $oneof = $types->{$oneof_name};
115 1 my @oneof_fields = map { $_->[F_NAME] } @{$oneof->{fields}};
116 1 ▶ my $new_name = ($oneof_name =~ /\.(\w+)$/) ? $1 : $oneof_name;
Branch: 1 branch
BranchTrueFalseTotal
$oneof_name =~ /\.(\w+)$/ ? : 1 0 1
117 1 $oneofs{$new_name} = \@oneof_fields;
118 1 push @fields, @{$oneof->{fields}};
119 }
120 } else {
121 0 die;
122 }
123
124 ##
125 ## Replace proto type names by Perl classes names
126 ##
127 294 foreach my $f (@fields) {
128 2081 my $type = $f->[F_TYPE];
129 2081 ▶ if ($type !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /^\d+$/) 462 1619 2081
130 ## not a primitive type
131 462 $f->[F_TYPE] = $self->_get_class_name_for($type, $opts);
132 }
133 }
134
135 ##
136 ## Default values: replace references to enum idents by their values
137 ##
138 294 foreach my $f (@fields) {
139 2081 my $default_value = $f->[F_DEFAULT];
140 2081 ▶ if ($default_value && ref $default_value) {
Branch: 2 branches
BranchTrueFalseTotal
if ($default_value and ref $default_value) { } 371 1710 2081
elsif ($default_value) { } 56 1654 1710
Condition100%3/3
$default_value and ref $default_value
!ll&&!rl&&r
165456371
MC/DC100%2/2
$default_value and ref $default_value
$default_value ref $default_value
Truth table100%3/3
A and B
A= $default_valueB= ref $default_value
ABresult
0X0
100
111
141 ## this default value is a literal
142 371 ▶ die "Unknown default value " . Data::Dumper::Dumper($default_value)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $default_value eq "HASH" 0 371 371
143 unless ref($default_value) eq 'HASH';
144 371 $f->[F_DEFAULT] = $default_value->{value};
145 } elsif ($default_value) {
146 ## this default is an enum value
147 56 my ($enum_name, $enum_field_name) = ($default_value =~ /(.*)\.(\w+)$/);
148 56 my $class_name = $self->_get_class_name_for($enum_name, $opts);
149 14 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
150 56 $f->[F_DEFAULT] = &{"${class_name}::$enum_field_name"};
151 14 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
152 }
153 }
154
155 ##
156 ## Create Perl classes
157 ##
158 294 my $class_name = $self->_get_class_name_for($type_name, $opts);
159 294 ▶ if ($kind eq 'message') {
Branch: 2 branches
BranchTrueFalseTotal
if ($kind eq 'message') { } 237 57 294
elsif ($kind eq 'group') { } 57 0 57
160 237 $self->create_message($class_name, \@fields, \%oneofs, $opts);
161 } elsif ($kind eq 'group') {
162 57 $self->create_group($class_name, \@fields, $opts);
163 }
164 293 push @created_messages, $class_name;
165 }
166
167 21 my @created_classes = sort @created_enums;
168 21 push @created_classes, sort @created_messages;
169
170 ## Generate Perl code of created classes
171 21 ▶ if ($opts->{generate_code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"generate_code"}) 3 18 21
172 3 require 'Google/ProtocolBuffers/CodeGen.pm';
173 3 my $fh;
174 3 ▶ if (!ref($opts->{generate_code})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $opts->{'generate_code'}) { } 3 0 3
175 3 ▶ open($fh, ">$opts->{generate_code}")
Branch: 1 branch
BranchTrueFalseTotal
open $fh, qq[>$opts->{"generate_code"}] or die qq[Can't write to '$opts->{"generate_code"}': $!] 3 0 3
176 or die "Can't write to '$opts->{generate_code}': $!";
177 } else {
178 0 $fh = $opts->{generate_code};
179 }
180
181 3 ▶ my $package_str = ($opts->{'package_name'}) ?
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'package_name'} ? : 1 2 3
182 "package $opts->{'package_name'};" : "";
183
184 3 ▶ my $source_str = ($source->{'file'}) ?
Branch: 1 branch
BranchTrueFalseTotal
$source->{'file'} ? : 1 2 3
185 "$source->{'file'}" : "inline text";
186
187 3 print $fh <<"HEADER";
188 # Generated by the protocol buffer compiler (protoc-perl) DO NOT EDIT!
189 # source: $source_str
190
191 $package_str
192
193 use strict;
194 use warnings;
195
196 use Google::ProtocolBuffers;
197 {
198 HEADER
199 3 foreach my $class_name (@created_classes) {
200 39 print $fh $class_name->getPerlCode($opts);
201 }
202 3 print $fh "}\n1;\n";
203 }
204 21 return @created_classes;
205 }
206
207 # Google::ProtocolBuffers->create_message(
208 # 'AccountRecord',
209 # [
210 # ## required string name = 1;
211 # [LABEL_REQUIRED, TYPE_STRING, 'name', 1 ],
212 # [LABEL_OPTIONAL, TYPE_INT32, 'id', 2 ],
213 # ],
214 # );
215 265 ▶sub create_message {
Subroutine create_message: called CC=1 SCAR=0.0
Pod undocumented
216 265 my $self = shift;
217 265 my $class_name = shift;
218 265 my $fields = shift;
219 265 my $oneofs = shift;
220 265 my $opts = shift;
221
222 265 return $self->_create_message_or_group(
223 $class_name, $fields, $oneofs, $opts,
224 'Google::ProtocolBuffers::Message'
225 );
226 }
227
228 63 ▶sub create_group {
Subroutine create_group: called CC=1 SCAR=0.0
Pod undocumented
229 63 my $self = shift;
230 63 my $class_name = shift;
231 63 my $fields = shift;
232 63 my $opts = shift;
233
234 63 return $self->_create_message_or_group(
235 $class_name, $fields, undef, $opts,
236 'Google::ProtocolBuffers::Group'
237 );
238 }
239
240 328 ▶sub _create_message_or_group {
Subroutine _create_message_or_group: called CC=22 SCAR=31.2
241 328 my $self = shift;
242 328 my $class_name = shift;
243 328 my $fields = shift;
244 328 my $oneofs = shift;
245 328 my $opts = shift;
246 328 my $base_class = shift;
247
248 ##
249 ## Sanity checks
250 ## 1. Class name must be a valid Perl class name
251 ## (should we check that this class doesn't exist yet?)
252 ##
253 328 ▶ die "Invalid class name: '$class_name'"
Branch: 1 branch
BranchTrueFalseTotal
unless $class_name =~ /^[a-z_]\w*(?:::[a-z_]\w*)*$/i 0 328 328
254 unless $class_name =~ /^[a-z_]\w*(?:::[a-z_]\w*)*$/i;
255
256 ##
257 ##
258 328 my (%field_names, %field_numbers);
259 328 foreach my $f (@$fields) {
260 2307 my ($label, $type_name, $name, $field_number, $default_value) = @$f;
261 2307 ▶ die Dumper $f unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 2307 2307
262
263 ##
264 ## field names must be valid identifiers and be unique
265 ##
266 2307 ▶ die "Invalid field name: '$name'"
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $name =~ /^\[?[a-z_][\w\.]*\]?$/i 0 2307 2307
Condition33%1/3
$name and $name =~ /^\[?[a-z_][\w\.]*\]?$/i
!ll&&!rl&&r
002307
MC/DC0%0/2
$name and $name =~ /^\[?[a-z_][\w\.]*\]?$/i
$name $name =~ /^\[?[a-z_][\w\.]*\]?$/i
Truth table33%1/3
A and B
A= $nameB= $name =~ /^\[?[a-z_][\w\.]*\]?$/i
ABresult
0X0
100
111
267 unless $name && $name =~ /^\[?[a-z_][\w\.]*\]?$/i;
268 2307 ▶ if ($field_names{$name}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_names{$name}++) 1 2306 2307
269 1 die "Field '$name' is defined more than once";
270 }
271
272 ##
273 ## field number must be positive and unique
274 ##
275 2306 ▶ die "Invalid field number: $field_number" unless $field_number>0;
Branch: 1 branch
BranchTrueFalseTotal
unless $field_number > 0 0 2306 2306
276 2306 ▶ if ($field_numbers{$field_number}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_numbers{$field_number}++) 0 2306 2306
277 0 die "Field number $field_number is used more than once";
278 }
279
280 ## type is either a number (for primitive types)
281 ## or a class name. Can't check that complex $type
282 ## is valid, because it may not exist yet.
283 2306 ▶ die "Field '$name' doesn't has a type" unless $type_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $type_name 0 2306 2306
284 2306 ▶ if ($type_name =~/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_name =~ /^\d+$/) { } 1794 512 2306
285 ## ok, this is an ID of primitive type
286 } else {
287 512 ▶ die "Type '$type_name' is not valid Perl class name"
Branch: 1 branch
BranchTrueFalseTotal
unless $type_name =~ /^[a-z_]\w*(?:::[a-z_]\w*)*$/i 0 512 512
288 unless $type_name =~ /^[a-z_]\w*(?:::[a-z_]\w*)*$/i;
289 }
290
291 2306 ▶ die "Unknown label value: $label"
Branch: 1 branch
BranchTrueFalseTotal
unless $label == 1 or $label == 2 or $label == 3 0 2306 2306
Condition83%5/6
$label == 1 or $label == 2 or $label == 3
l!l&&r!l&&!r
17255810
$label == 1 or $label == 2
l!l&&r!l&&!r
168045581
MC/DC0%0/3
$label == 1 or $label == 2 or $label == 3
$label == 1 $label == 2 $label == 3
Truth table75%3/4
A or B or C
A= $label == 1B= $label == 2C= $label == 3
ABCresult
1XX1
01X1
0011
0000
292 unless $label==LABEL_OPTIONAL || $label==LABEL_REQUIRED || $label==LABEL_REPEATED;
293 }
294
295
296 ## Make a copy of values and sort them so that field_numbers increase,
297 ## this is a requirement of protocol
298 ## Postitional addressation of field parts is sucks, TODO: replace by hash
299 327 my @field_list = sort { $a->[F_NUMBER] <=> $b->[F_NUMBER] } map { [@$_] } @$fields;
300 327 my %fields_by_field_name = map { $_->[F_NAME] => $_ } @field_list;
301 327 my %fields_by_field_number = map { $_->[F_NUMBER] => $_ } @field_list;
302
303 327 ▶ my $has_oneofs = defined($oneofs) && %$oneofs;
Condition100%3/3
defined $oneofs && %$oneofs
!ll&&!rl&&r
912351
MC/DC100%2/2
defined $oneofs && %$oneofs
defined $oneofs %$oneofs
Truth table100%3/3
A && B
A= defined $oneofsB= %$oneofs
ABresult
0X0
100
111
304 327 my %oneofs_rev;
305  
306 327 ▶ if ($has_oneofs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_oneofs) 1 326 327
307 1 ▶ while (my ($name, $fields) = each %$oneofs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$oneofs) 1 1 2
308 1 %oneofs_rev = (%oneofs_rev, map { $_, $name } @$fields);
309 }
310 }
311  
312 14 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
313 327 @{"${class_name}::ISA"} = $base_class;
314 327 ▶ *{"${class_name}::_pb_fields_list"} = sub { \@field_list };
Subroutine __ANON__: called CC=1 SCAR=0.0
315 327 ▶ *{"${class_name}::_pb_fields_by_name"} = sub { \%fields_by_field_name };
Subroutine __ANON__: called CC=1 SCAR=0.0
316 327 ▶ *{"${class_name}::_pb_fields_by_number"} = sub { \%fields_by_field_number };
Subroutine __ANON__: called CC=1 SCAR=0.0
317 327 ▶ if ($has_oneofs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_oneofs) 1 326 327
318 1 ▶ *{"${class_name}::_pb_oneofs"} = sub { $oneofs };
Subroutine __ANON__: called CC=1 SCAR=0.0
319 1 ▶ *{"${class_name}::_pb_oneofs_rev"} = sub { \%oneofs_rev };
Subroutine __ANON__: called CC=1 SCAR=0.0
320 }
321 14 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
322
323 327 ▶ if ($opts->{create_accessors}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"create_accessors"}) 158 169 327
324 14 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
325 158 push @{"${class_name}::ISA"}, 'Class::Accessor';
326 158 ▶ if ($has_oneofs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_oneofs) 1 157 158
327 1 *{"${class_name}::new"} = \&Google::ProtocolBuffers::new;
328 1 *{"${class_name}::which_oneof"} = \&Google::ProtocolBuffers::which_oneof;
329 }
330 158 *{"${class_name}::get"} = \&Google::ProtocolBuffers::get;
331 158 *{"${class_name}::set"} = \&Google::ProtocolBuffers::set;
332 14 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
333
334 158 ▶ if ($opts->{follow_best_practice}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"follow_best_practice"}) 0 158 158
335 0 $class_name->follow_best_practice;
336 }
337 158 my @accessors = grep { /^[a-z_]\w*$/i } map { $_->[2] } @$fields;
338 158 $class_name->mk_accessors(@accessors);
339 }
340 }
341
342 54 ▶sub create_enum {
Subroutine create_enum: called CC=7 SCAR=19.6
Pod undocumented
343 54 my $self = shift;
344 54 my $class_name = shift;
345 54 my $fields = shift;
346 54 my $options = shift;
347
348 ##
349 ## Sanity checks
350 ## 1. Class name must be a valid Perl class name
351 ## (should we check that this class doesn't exist yet?)
352 ## 2. Field names must be valid identifiers and be unique
353 ##
354 54 ▶ die "Invalid class name: '$class_name'"
Branch: 1 branch
BranchTrueFalseTotal
unless $class_name =~ /^[a-z_]\w*(?:::[a-z_]\w*)*$/i 0 54 54
355 unless $class_name =~ /^[a-z_]\w*(?:::[a-z_]\w*)*$/i;
356 54 my %names;
357 54 foreach my $f (@$fields) {
358 222 my ($name, $value) = @$f;
359 222 ▶ die "Invalid field name: '$name'"
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $name =~ /^[a-z_]\w*$/i 0 222 222
Condition33%1/3
$name and $name =~ /^[a-z_]\w*$/i
!ll&&!rl&&r
00222
MC/DC0%0/2
$name and $name =~ /^[a-z_]\w*$/i
$name $name =~ /^[a-z_]\w*$/i
Truth table33%1/3
A and B
A= $nameB= $name =~ /^[a-z_]\w*$/i
ABresult
0X0
100
111
360 unless $name && $name =~ /^[a-z_]\w*$/i;
361 222 ▶ if ($names{$name}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($names{$name}++) 0 222 222
362 0 die "Field '$name' is defined more than once";
363 }
364 }
365
366 ## base class and constants export
367 14 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
368 54 @{"${class_name}::ISA"} = "Google::ProtocolBuffers::Enum";
369 54 %{"${class_name}::EXPORT_TAGS"} = ('constants'=>[]);
370 14 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
371
372 ## create the constants
373 0 foreach my $f (@$fields) {
374 222 my ($name, $value) = @$f;
375 14 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
376 222 ▶ *{"${class_name}::$name"} = sub { $value };
Subroutine __ANON__: called CC=1 SCAR=0.0
377 222 push @{ ${"${class_name}::EXPORT_TAGS"}{'constants'} }, $name;
378 222 push @{"${class_name}::EXPORT_OK"}, $name;
379 14 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
380 }
381
382 ## create a copy of fields for introspection/code generation
383 54 my @fields = map { [@$_] } @$fields;
384 14 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
385 54 ▶ *{"${class_name}::_pb_fields_list"} = sub { \@fields };
Subroutine __ANON__: called CC=1 SCAR=0.0
386
387 }
388
389 ##
390 ## Accessors
391 ##
392 14 ▶sub getExtension {
Subroutine getExtension: called CC=6 SCAR=18.8
Pod undocumented
393 14 my $self = shift;
394 14 ▶ my $data = (ref $self) ? $self : shift();
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 14 0 14
395 14 my $extension_name = shift;
396
397 14 ▶ unless($extension_name){
Branch: 1 branch
BranchTrueFalseTotal
unless ($extension_name) 0 14 14
398 0 return \%{$self->_pb_fields_by_name()};
399 }
400
401 14 $extension_name =~ s/::/./g;
402 14 my $key = "[$extension_name]";
403
404 14 my $field = $self->_pb_fields_by_name->{$key};
405 14 ▶ if ($field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field) { } 13 1 14
406 13 ▶ return (exists $data->{$key}) ? $data->{$key} : $field->[F_DEFAULT];
Branch: 1 branch
BranchTrueFalseTotal
exists $data->{$key} ? : 10 3 13
407 } else {
408 1 ▶ my $class_name = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
409 1 die "There is no extension '$extension_name' in '$class_name'";
410 }
411 }
412
413
414
415 5 ▶sub setExtension {
Subroutine setExtension: called CC=4 SCAR=14.1
Pod undocumented
416 5 my $self = shift;
417 5 ▶ my $data = (ref $self) ? $self : shift();
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 5 0 5
418 5 my $extension_name = shift;
419 5 my $value = shift;
420
421 5 $extension_name =~ s/::/./g;
422 5 my $key = "[$extension_name]";
423
424 5 ▶ if ($self->_pb_fields_by_name->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_pb_fields_by_name->{$key}) { } 4 1 5
425 4 $data->{$key} = $value;
426 } else {
427 1 ▶ my $class_name = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
428 1 die "There is no extension '$extension_name' in '$class_name'";
429 }
430 }
431
432 ##
433 ## Overide the Class::Accessor new to handle oneof fields.
434 ##
435 1 ▶sub new {
Subroutine new: called CC=5 SCAR=20.9
Pod undocumented
436 1 my ($proto, $fields) = @_;
437 1 ▶ my ($class) = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
438  
439 1 ▶ $fields = {} unless defined $fields;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fields 1 0 1
440  
441 1 my $self = bless {}, $class;
442  
443 ## Set the fields
444 1 ▶ while (my ($key, $value) = each %$fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$fields) 0 1 1
445 0 ▶ if (!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value) { } 0 0 0
446 0 $self->{$key} = undef;
447 }
448 else {
449 0 $self->set($key, $value);
450 }
451 }
452  
453 1 return $self;
454 }
455  
456 ##
457 ## Return which field in a oneof is set
458 ##
459 3 ▶sub which_oneof {
Subroutine which_oneof: called CC=5 SCAR=16.5
Pod undocumented
460 3 my $self = shift;
461 3 my $oneof = shift;
462  
463 return undef unless $self->can('_pb_oneofs') &&
464 3 ▶ exists($self->_pb_oneofs->{$oneof});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("_pb_oneofs") and exists $self->_pb_oneofs->{$oneof} 0 3 3
Condition33%1/3
$self->can("_pb_oneofs") and exists $self->_pb_oneofs->{$oneof}
!ll&&!rl&&r
003
MC/DC0%0/2
$self->can("_pb_oneofs") and exists $self->_pb_oneofs->{$oneof}
$self->can("_pb_oneofs") exists $self->_pb_oneofs->{$oneof}
Truth table33%1/3
A and B
A= $self->can("_pb_oneofs")B= exists $self->_pb_oneofs->{$oneof}
ABresult
0X0
100
111
465  
466 3 foreach my $f (@{$self->_pb_oneofs->{$oneof}}) {
467 5 ▶ if (defined($self->{$f})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$f}) 2 3 5
468 2 return $f;
469 }
470 }
471  
472 1 return undef;
473 }
474  
475 ##
476 ## This is for Class::Accessor read-accessors, will be
477 ## copied to classes from Message/Group.
478 ## If no value is set, the default one will be returned.
479 ##
480 56 ▶sub get {
Subroutine get: called CC=7 SCAR=30.7
Pod undocumented
481 56 my $self = shift;
482
483 56 ▶ if (@_==1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 56 0 56
elsif (@_ > 1) { } 0 0 0
484 ## checking that $self->{$_[0]} exists is not enough,
485 ## since undef value may be set via Class::Accessor's new, e.g:
486 ## my $data = My::Message->new({ name => undef })
487 56 ▶ return $self->{$_[0]} if defined $self->{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$self{$_[0]} 25 31 56
488 31 my $field = $self->_pb_fields_by_name->{$_[0]};
489 31 return $field->[F_DEFAULT];
490 } elsif (@_>1) {
491 0 my @rv;
492 my $fields;
493 0 foreach my $key (@_) {
494 0 ▶ if (defined $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$key}) { } 0 0 0
495 0 push @rv, $self->{$key};
496 } else {
497 0 ▶ $fields ||= $self->_pb_fields_by_name;
Condition0%0/3
$fields ||= $self->_pb_fields_by_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$fields ||= $self->_pb_fields_by_name
$fields $self->_pb_fields_by_name
Truth table0%0/3
A ||= B
A= $fieldsB= $self->_pb_fields_by_name
ABresult
1X1
011
000
498 0 push @rv, $fields->{$key}->[F_DEFAULT];
499 }
500 }
501 0 return @rv;
502 } else {
503 0 Carp::confess("Wrong number of arguments received.");
504 }
505 }
506
507 17 ▶sub set {
Subroutine set: called CC=8 SCAR=21.8
Pod undocumented
508 17 my $self = shift;
509 17 my $key = shift;
510
511 17 ▶ if (@_==1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 17 0 17
elsif (@_ > 1) { } 0 0 0
512 17 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 16 1 17
513 16 $self->{$key} = $_[0];
514 } else {
515 1 delete $self->{$key};
516 }
517 } elsif (@_>1) {
518 0 $self->{$key} = [@_];
519 } else {
520 0 Carp::confess("Wrong number of arguments received.");
521 }
522  
523 # Is this a oneof field
524 17 ▶ if ($self->can('_pb_oneofs_rev') && exists($self->_pb_oneofs_rev->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("_pb_oneofs_rev") and exists $self->_pb_oneofs_rev->{$key}) 2 15 17
Condition66%2/3
$self->can("_pb_oneofs_rev") and exists $self->_pb_oneofs_rev->{$key}
!ll&&!rl&&r
1502
MC/DC50%1/2
$self->can("_pb_oneofs_rev") and exists $self->_pb_oneofs_rev->{$key}
$self->can("_pb_oneofs_rev") exists $self->_pb_oneofs_rev->{$key}
Truth table66%2/3
A and B
A= $self->can("_pb_oneofs_rev")B= exists $self->_pb_oneofs_rev->{$key}
ABresult
0X0
100
111
525 2 foreach my $f (@{$self->_pb_oneofs->{$self->_pb_oneofs_rev->{$key}}}) {
526 4 ▶ delete $self->{$f} unless $f eq $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $f eq $key 2 2 4
527 }
528 }
529 }
530
531 861 ▶sub _get_class_name_for{
Subroutine _get_class_name_for: called CC=3 SCAR=11.0
532 861 my $self = shift;
533 861 my $type_name = shift;
534 861 my $opts = shift;
535
536 861 ▶ if ($opts->{no_camel_case}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'no_camel_case'}) { } 1 860 861
537 1 my $class_name = $type_name;
538 1 $class_name =~ s/\./::/g;
539 1 return $class_name;
540 } else {
541 860 my @idents = split qr/\./, $type_name;
542 860 foreach (@idents) {
543 1950 s/_(.)/uc($1)/ge;
544 1950 $_ = "\u$_";
545 }
546 860 return join("::", @idents);
547 }
548 }
549
550 package Google::ProtocolBuffers::Message;
551 14 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
552 ## public
553 14 *encode = \&Google::ProtocolBuffers::Codec::encode;
554 14 *decode = \&Google::ProtocolBuffers::Codec::decode;
555 14 *setExtension = \&Google::ProtocolBuffers::setExtension;
556 14 *getExtension = \&Google::ProtocolBuffers::getExtension;
557 14 *getPerlCode = \&Google::ProtocolBuffers::CodeGen::generate_code_of_message_or_group;
558 ## internal
559 ## _pb_complex_type_kind can be removed and $class->isa('Google::ProtocolBuffers::Message')
560 ## can be used instead, but current implementation is faster
561 315 ▶sub _pb_complex_type_kind { Google::ProtocolBuffers::Constants::MESSAGE() }
Subroutine _pb_complex_type_kind: called CC=1 SCAR=0.0
562 # _pb_fields_list ## These 3 methods are created in
563 # _pb_fields_by_name ## namespace of derived class
564 # _pb_fields_by_number
565
566 package Google::ProtocolBuffers::Group;
567 14 *setExtension = \&Google::ProtocolBuffers::setExtension;
568 14 *getExtension = \&Google::ProtocolBuffers::getExtension;
569 14 *getPerlCode = \&Google::ProtocolBuffers::CodeGen::generate_code_of_message_or_group;
570 40 ▶sub _pb_complex_type_kind { Google::ProtocolBuffers::Constants::GROUP() }
Subroutine _pb_complex_type_kind: called CC=1 SCAR=0.0
571 #_pb_fields_list
572 #_pb_fields_by_name
573 #_pb_fields_by_number
574
575 package Google::ProtocolBuffers::Enum;
576 14 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
577 14 *getPerlCode = \&Google::ProtocolBuffers::CodeGen::generate_code_of_enum;
578 26 ▶sub _pb_complex_type_kind { Google::ProtocolBuffers::Constants::ENUM() }
Subroutine _pb_complex_type_kind: called CC=1 SCAR=0.0
579 #_pb_fields_list
580
581 14 1;
582
583 __END__
« ProtocolBuffers/CodecIV32.pm ↑ Summary ProtocolBuffers/CodeGen.pm »