blib/lib/PDL/IO/Protobuf.pm

filter: Statementstmt 88.1% 82 / 93 Branchbran 43.1% 25 / 58 Conditioncond 32.0% 8 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 63.1% 132 / 209 CC 39 SCAR 28.5
CC
39
Cov
65%
from_protobuf
33.1
to_protobuf
19.1
wprotobuf
11.3
CRAP 17.2
↑ Summary
1 package PDL::IO::Protobuf;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '0.01';
10  
11 2 our @EXPORT = qw(
12 rprotobuf
13 wprotobuf
14 );
15  
16 2 our @EXPORT_OK = qw(
17 rprotobuf
18 wprotobuf
19 from_protobuf
20 to_protobuf
21 pdl_type_for_proto_type
22 );
23  
24 2 my %PROTO_TO_PDL_TYPE = (
25 double => 'double',
26 float => 'float',
27 int32 => 'long',
28 sint32 => 'long',
29 sfixed32 => 'long',
30 uint32 => 'ulong',
31 fixed32 => 'ulong',
32 int64 => 'longlong',
33 sint64 => 'longlong',
34 sfixed64 => 'longlong',
35 uint64 => 'ulonglong',
36 fixed64 => 'ulonglong',
37 bool => 'byte',
38 );
39  
40 9 ▶sub pdl_type_for_proto_type {
Subroutine pdl_type_for_proto_type: called CC=2 SCAR=7.1
Pod documented
41 9 my ($proto_type) = @_;
42 9 ▶ return unless defined $proto_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $proto_type 0 9 9
43 9 return $PROTO_TO_PDL_TYPE{ lc($proto_type) };
44 }
45  
46 2 ▶sub _require_pdl {
Subroutine _require_pdl: called CC=1 SCAR=0.0
47 2 require PDL::Core;
48 2 require PDL::Types;
49 2 require PDL::Slices;
50 2 return;
51 }
52  
53 1 ▶sub _pdl_type_obj {
Subroutine _pdl_type_obj: called CC=2 SCAR=7.0
54 1 my ($type_name) = @_;
55 1 _require_pdl();
56 1 my $type = eval { PDL::Type->new($type_name) };
57 1 ▶ croak 'Unsupported PDL type: ' . $type_name unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 1 1
58 1 return $type;
59 }
60  
61 1 ▶sub from_protobuf {
Subroutine from_protobuf: called CC=15 SCAR=35.0
Pod documented
62 1 my ($msg, $field, %opts) = @_;
63 1 ▶ croak 'Protobuf message object is required' unless defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 0 1 1
64 1 ▶ croak 'Field name is required' unless defined $field && length $field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field and length $field 0 1 1
Condition33%1/3
defined $field and length $field
!ll&&!rl&&r
001
MC/DC0%0/2
defined $field and length $field
defined $field length $field
Truth table33%1/3
A and B
A= defined $fieldB= length $field
ABresult
0X0
100
111
65  
66 1 _require_pdl();
67  
68 1 my $values;
69 1 ▶ if (ref($msg) && $msg->can($field)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $msg and $msg->can($field)) { } 1 0 1
elsif (ref $msg eq 'HASH' and exists $msg->{$field}) { } 0 0 0
Condition33%1/3
ref $msg and $msg->can($field)
!ll&&!rl&&r
001
MC/DC0%0/2
ref $msg and $msg->can($field)
ref $msg $msg->can($field)
Truth table33%1/3
A and B
A= ref $msgB= $msg->can($field)
ABresult
0X0
100
111
70 1 $values = $msg->$field();
71 ▶ } elsif (ref($msg) eq 'HASH' && exists $msg->{$field}) {
Condition0%0/3
ref $msg eq "HASH" and exists $msg->{$field}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $msg eq "HASH" and exists $msg->{$field}
ref $msg eq "HASH" exists $msg->{$field}
Truth table0%0/3
A and B
A= ref $msg eq "HASH"B= exists $msg->{$field}
ABresult
0X0
100
111
72 0 $values = $msg->{$field};
73 } else {
74 0 croak 'Field "' . $field . '" not found on message';
75 }
76  
77 1 ▶ my @arr = ref($values) eq 'ARRAY' ? @$values : ($values);
Branch: 1 branch
BranchTrueFalseTotal
ref $values eq 'ARRAY' ? : 1 0 1
78  
79 1 my $pdl_type_name = $opts{type};
80 1 ▶ if (!$pdl_type_name && $opts{proto_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $pdl_type_name and $opts{"proto_type"}) 1 0 1
Condition33%1/3
not $pdl_type_name and $opts{"proto_type"}
!ll&&!rl&&r
001
MC/DC0%0/2
not $pdl_type_name and $opts{"proto_type"}
not $pdl_type_name $opts{"proto_type"}
Truth table33%1/3
not(A) and B
A= not $pdl_type_nameB= $opts{"proto_type"}
ABresult
0X0
100
111
81 $pdl_type_name = pdl_type_for_proto_type($opts{proto_type})
82 1 ▶ or croak 'Unsupported proto_type: ' . $opts{proto_type};
Branch: 1 branch
BranchTrueFalseTotal
$pdl_type_name = pdl_type_for_proto_type($opts{"proto_type"}) or croak('Unsupported proto_type: ' . $opts{"proto_type"}) 1 0 1
83 }
84 1 ▶ $pdl_type_name ||= 'double';
Condition50%1/2
$pdl_type_name ||= "double"
l!l
10
MC/DC0%0/1
$pdl_type_name ||= "double"
$pdl_type_name
Truth table50%1/2
A
A= $pdl_type_name
Aresult
11
00
85  
86 1 my $pdl = PDL::Core::pdl(_pdl_type_obj($pdl_type_name), \@arr);
87  
88 1 ▶ if (my $dims = $opts{dims}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dims = $opts{"dims"}) 1 0 1
89 1 ▶ croak 'dims must be an arrayref' unless ref($dims) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $dims eq "ARRAY" 0 1 1
90 1 $pdl = $pdl->reshape(@$dims);
91 }
92  
93 1 return $pdl;
94 }
95  
96 1 ▶sub to_protobuf {
Subroutine to_protobuf: called CC=9 SCAR=29.5
Pod documented
97 1 my ($pdl, $class_or_msg, $field, %opts) = @_;
98 1 ▶ croak 'PDL ndarray is required' unless defined $pdl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pdl 0 1 1
99 1 ▶ croak 'Target Protobuf class or message is required' unless defined $class_or_msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class_or_msg 0 1 1
100 1 ▶ croak 'Field name is required' unless defined $field && length $field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field and length $field 0 1 1
Condition33%1/3
defined $field and length $field
!ll&&!rl&&r
001
MC/DC0%0/2
defined $field and length $field
defined $field length $field
Truth table33%1/3
A and B
A= defined $fieldB= length $field
ABresult
0X0
100
111
101  
102 1 my @flat = $pdl->flat->list;
103  
104 1 my $msg;
105 1 ▶ if (ref($class_or_msg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class_or_msg) { } 0 1 1
106 0 $msg = $class_or_msg;
107 0 ▶ if ($msg->can($field)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($msg->can($field)) { } 0 0 0
elsif (ref $msg eq 'HASH') { } 0 0 0
108 0 $msg->$field(\@flat);
109 } elsif (ref($msg) eq 'HASH') {
110 0 $msg->{$field} = \@flat;
111 } else {
112 0 croak 'Cannot set field "' . $field . '" on message';
113 }
114 } else {
115 1 ▶ $msg = $class_or_msg->new($field => \@flat, %{ $opts{extra_fields} || {} });
Condition50%1/2
$opts{'extra_fields'} || {}
l!l
01
MC/DC0%0/1
$opts{'extra_fields'} || {}
$opts{'extra_fields'}
Truth table50%1/2
A
A= $opts{'extra_fields'}
Aresult
11
00
116 }
117  
118 1 return $msg;
119 }
120  
121 1 ▶sub rprotobuf {
Subroutine rprotobuf: called CC=8 SCAR=24.1
Pod documented
122 1 my ($source, $class, $field, %opts) = @_;
123 1 ▶ croak 'Source file path or filehandle is required' unless defined $source;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source 0 1 1
124 1 ▶ croak 'Protobuf message class is required' unless defined $class && length $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class and length $class 0 1 1
Condition33%1/3
defined $class and length $class
!ll&&!rl&&r
001
MC/DC0%0/2
defined $class and length $class
defined $class length $class
Truth table33%1/3
A and B
A= defined $classB= length $class
ABresult
0X0
100
111
125 1 ▶ croak 'Field name is required' unless defined $field && length $field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field and length $field 0 1 1
Condition33%1/3
defined $field and length $field
!ll&&!rl&&r
001
MC/DC0%0/2
defined $field and length $field
defined $field length $field
Truth table33%1/3
A and B
A= defined $fieldB= length $field
ABresult
0X0
100
111
126  
127 1 my $bytes;
128 1 ▶ if (ref($source)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $source) { } 0 1 1
129 0 local $/;
130 0 $bytes = <$source>;
131 } else {
132 1 ▶ open my $fh, '<:raw', $source or croak 'Cannot open ' . $source . ': ' . $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $source or croak('Cannot open ' . $source . ': ' . $!) 1 0 1
133 1 local $/;
134 1 $bytes = <$fh>;
135 1 close $fh;
136 }
137  
138 1 my $msg = $class->parse($bytes);
139 1 return from_protobuf($msg, $field, %opts);
140 }
141  
142 1 ▶sub wprotobuf {
Subroutine wprotobuf: called CC=8 SCAR=24.2
Pod documented
143 1 my ($pdl, $dest, $class_or_msg, $field, %opts) = @_;
144 1 ▶ croak 'PDL ndarray is required' unless defined $pdl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pdl 0 1 1
145 1 ▶ croak 'Destination file path or filehandle is required' unless defined $dest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dest 0 1 1
146  
147 1 my $msg = to_protobuf($pdl, $class_or_msg, $field, %opts);
148 1 my $bytes = $msg->serialize();
149  
150 1 ▶ if (ref($dest)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dest) { } 0 1 1
151 0 ▶ print {$dest} $bytes or croak 'Cannot write to filehandle: ' . $!;
Branch: 1 branch
BranchTrueFalseTotal
print {$dest;} $bytes or croak('Cannot write to filehandle: ' . $!) 0 0 0
152 } else {
153 1 ▶ open my $fh, '>:raw', $dest or croak 'Cannot open ' . $dest . ' for writing: ' . $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $dest or croak('Cannot open ' . $dest . ' for writing: ' . $!) 1 0 1
154 1 ▶ print {$fh} $bytes or croak 'Cannot write ' . $dest . ': ' . $!;
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $bytes or croak('Cannot write ' . $dest . ': ' . $!) 1 0 1
155 1 ▶ close $fh or croak 'Cannot close ' . $dest . ': ' . $!;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak('Cannot close ' . $dest . ': ' . $!) 1 0 1
156 }
157  
158 1 return $msg;
159 }
160  
161 2 1;
162  
163 __END__
↑ Summary