ProtocolBuffers/CodeGen.pm

filter: Statementstmt 87.1% 54 / 62 Branchbran 77.2% 17 / 22 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 2 totaltotal 78.5% 84 / 107 CC 19 SCAR 24.2
CC
19
Cov
82%
generate_code_of_message_or_group
17.8
_get_perl_literal
5.0
generate_code_of_enum
3.0
CRAP 11.2
« ProtocolBuffers.pm ↑ Summary ProtocolBuffers/Codec.pm »
1 package Google::ProtocolBuffers::CodeGen;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 1 ▶use Google::ProtocolBuffers::Constants qw/:types :labels :complex_types/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 1 my %primitive_types = reverse (
8 TYPE_DOUBLE => TYPE_DOUBLE,
9 TYPE_FLOAT => TYPE_FLOAT,
10 TYPE_INT64 => TYPE_INT64,
11 TYPE_UINT64 => TYPE_UINT64,
12 TYPE_INT32 => TYPE_INT32,
13 TYPE_FIXED64=> TYPE_FIXED64,
14 TYPE_FIXED32=> TYPE_FIXED32,
15 TYPE_BOOL => TYPE_BOOL,
16 TYPE_STRING => TYPE_STRING,
17 TYPE_GROUP => TYPE_GROUP, ##
18 TYPE_MESSAGE=> TYPE_MESSAGE, ## should never appear, because 'message' is a 'complex type'
19 TYPE_BYTES => TYPE_BYTES,
20 TYPE_UINT32 => TYPE_UINT32,
21 TYPE_ENUM => TYPE_ENUM, ##
22 TYPE_SFIXED32=>TYPE_SFIXED32,
23 TYPE_SFIXED64=>TYPE_SFIXED64,
24 TYPE_SINT32 => TYPE_SINT32,
25 TYPE_SINT64 => TYPE_SINT64,
26 );
27
28 1 my %labels = reverse (
29 LABEL_OPTIONAL => LABEL_OPTIONAL,
30 LABEL_REQUIRED => LABEL_REQUIRED,
31 LABEL_REPEATED => LABEL_REPEATED,
32 );
33
34 79 ▶sub _get_perl_literal {
Subroutine _get_perl_literal: called CC=5 SCAR=16.1
35 79 my $v = shift;
36 79 my $opts = shift;
37
38 79 ▶ if ($v =~ /^-?\d+$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v =~ /^-?\d+$/) { } 55 24 79
elsif ($v =~ /[-+]?\d*\.\d+([Ee][\+-]?\d+)?|[-+]?\d+[Ee][\+-]?\d+/i) { } 4 20 24
39 ## integer literal
40 55 ▶ if ($v>0x7fff_ffff || $v<-0x8000_0000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v > 2147483647 or $v < -2147483648) { } 3 52 55
Condition100%3/3
$v > 2147483647 or $v < '-2147483648'
l!l&&r!l&&!r
2152
MC/DC100%2/2
$v > 2147483647 or $v < '-2147483648'
$v > 2147483647 $v < '-2147483648'
Truth table100%3/3
A or B
A= $v > 2147483647B= $v < '-2147483648'
ABresult
1X1
011
000
41 3 return "Math::BigInt->new('$v')";
42 } else {
43 52 return "$v";
44 }
45 } elsif ($v =~ /[-+]?\d*\.\d+([Ee][\+-]?\d+)?|[-+]?\d+[Ee][\+-]?\d+/i) {
46 ## floating point literal
47 4 return "$v";
48 } else {
49 ## string literal
50 20 $v =~ s/([\x00-\x1f'"\\$@%\x80-\xff])/ '\\x{' . sprintf("%02x", ord($1)) . '}' /ge;
51 20 return qq["$v"];
52 }
53 }
54
55 5 ▶sub generate_code_of_enum {
Subroutine generate_code_of_enum: called CC=3 SCAR=11.1
Pod undocumented
56 5 my $self = shift;
57 5 my $opts = shift;
58
59 5 ▶ my $class_name = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
60 5 my $fields_text;
61 5 foreach my $f (@{ $self->_pb_fields_list }) {
62 21 my ($name, $value) = @$f;
63 21 $value = _get_perl_literal($value, $opts);
64 21 $fields_text .= " ['$name', $value],\n";
65 }
66
67 5 return <<"CODE";
68 unless ($class_name->can('_pb_fields_list')) {
69 Google::ProtocolBuffers->create_enum(
70 '$class_name',
71 [
72 $fields_text
73 ]
74 );
75 }
76
77 CODE
78 }
79
80
81 34 ▶sub generate_code_of_message_or_group {
Subroutine generate_code_of_message_or_group: called CC=13 SCAR=28.8
Pod undocumented
82 34 my $self = shift;
83 34 my $opts = shift;
84
85 34 ▶ my $create_what =
Branch: 2 branches
BranchTrueFalseTotal
$self->_pb_complex_type_kind == 1 ? : 28 6 34
$self->_pb_complex_type_kind == 2 ? : 6 0 6
86 ($self->_pb_complex_type_kind==MESSAGE) ? 'create_message' :
87 ($self->_pb_complex_type_kind==GROUP) ? 'create_group' : die;
88
89 34 ▶ my $class_name = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0340
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
90
91 34 my $fields_text = ''; # may be empty, as empty messages are allowed
92 34 foreach my $f (@{ $self->_pb_fields_list }) {
93 226 my ($label, $type, $name, $field_number, $default_value) = @$f;
94
95 226 ▶ die unless $labels{$label};
Branch: 1 branch
BranchTrueFalseTotal
unless $labels{$label} 0 226 226
96 226 $label = "Google::ProtocolBuffers::Constants::$labels{$label}()";
97
98 226 ▶ if ($primitive_types{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($primitive_types{$type}) { } 176 50 226
99 176 $type = "Google::ProtocolBuffers::Constants::$primitive_types{$type}()";
100 } else {
101 50 $type = "'$type'";
102 }
103
104 226 ▶ $default_value = (defined $default_value) ?
Branch: 1 branch
BranchTrueFalseTotal
defined $default_value ? : 58 168 226
105 _get_perl_literal($default_value, $opts) : 'undef';
106 226 $fields_text .= <<"FIELD";
107 [
108 $label,
109 $type,
110 '$name', $field_number, $default_value
111 ],
112 FIELD
113 }
114
115 34 my $oneofs_text = " undef,\n";
116 34 ▶ if ($self->can('_pb_oneofs')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("_pb_oneofs")) 0 34 34
117 0 $oneofs_text = " {\n";
118 0 ▶ while (my ($name, $fields) = each %{$self->_pb_oneofs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->_pb_oneofs;}) 0 0 0
119 0 $oneofs_text .= " '$name' => [\n";
120 0 foreach my $f (@$fields) {
121 0 $oneofs_text .= " '$f',\n";
122 }
123 0 $oneofs_text .= " ],\n";
124 }
125 0 $oneofs_text .= " },\n";
126 }
127  
128 34 my $options = '';
129 34 foreach my $opt_name (qw/create_accessors follow_best_practice/) {
130 68 ▶ if ($opts->{$opt_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{$opt_name}) 34 34 68
131 34 $options .= "'$opt_name' => 1, "
132 }
133 }
134
135 34 return <<"CODE";
136 unless ($class_name->can('_pb_fields_list')) {
137 Google::ProtocolBuffers->$create_what(
138 '$class_name',
139 [
140 $fields_text
141 ],
142 $oneofs_text
143 { $options }
144 );
145 }
146
147 CODE
148
149 }
150
151 1 1;
« ProtocolBuffers.pm ↑ Summary ProtocolBuffers/Codec.pm »