Avro/Schema.pm

filter: Statementstmt 91.1% 382 / 419 Branchbran 75.2% 155 / 206 Conditioncond 69.0% 87 / 126 MC/DCmcdc 47.3% 35 / 74 Subroutinesub 96.8% 61 / 63 Podpod 0.0% 0 / 4 totaltotal 80.7% 720 / 892 CC 159 SCAR 26.0
CC
159
Cov
83%
is_data_valid
42.6
match
31.3
parse_struct
16.1
CRAP 13.5
↑ Summary Avro/DataFileReader.pm »
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # https://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17  
18 package Avro::Schema;
19 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 6 our $VERSION = '1.12.2';
27  
28 6 my $json = JSON::MaybeXS->new->allow_nonref;
29  
30 80 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.1
Pod undocumented
31 80 my $schema = shift;
32 80 my $json_string = shift;
33 80 ▶ my $names = shift || {};
Condition50%1/2
shift() || {}
l!l
080
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
34 80 ▶ my $namespace = shift || "";
Condition50%1/2
shift() || ''
l!l
080
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
35  
36 80 ▶ my $struct = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 80 $json->decode($json_string);
38 }
39 4 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
40 4 throw Avro::Schema::Error::Parse(
41 "Cannot parse json string: $_"
42 );
43 80 };
44 76 return $schema->parse_struct($struct, $names, $namespace);
45 }
46  
47 10 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod undocumented
48 10 my $class = shift;
49 10 my $struct = shift;
50 10 return $json->encode($struct);
51 }
52  
53 237 ▶sub parse_struct {
Subroutine parse_struct: called CC=16 SCAR=27.8
Pod undocumented
54 237 my $schema = shift;
55 237 my $struct = shift;
56 237 ▶ my $names = shift || {};
Condition100%2/2
shift() || {}
l!l
2361
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
57 237 ▶ my $namespace = shift || "";
Condition100%2/2
shift() || ''
l!l
24213
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
58  
59 ## 1.3.2 A JSON object
60 237 ▶ if (ref $struct eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $struct eq 'HASH') { } 96 141 237
elsif (ref $struct eq 'ARRAY') { } 16 125 141
61 my $type = $struct->{type}
62 96 ▶ or throw Avro::Schema::Error::Parse("type is missing");
Branch: 1 branch
BranchTrueFalseTotal
my $type = $struct->{"type"} or "Avro::Schema::Error::Parse"->throw("type is missing") 96 0 96
63 96 ▶ if ( Avro::Schema::Primitive->is_type_valid($type) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Avro::Schema::Primitive"->is_type_valid($type)) 19 77 96
64 19 return Avro::Schema::Primitive->new(type => $type);
65 }
66 ## XXX technically we shouldn't allow error type other than in
67 ## a Protocol definition
68 77 ▶ if ($type eq 'record' or $type eq 'error') {
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'record' or $type eq 'error') { } 23 54 77
elsif ($type eq 'enum') { } 6 48 54
elsif ($type eq 'array') { } 19 29 48
elsif ($type eq 'map') { } 17 12 29
elsif ($type eq 'fixed') { } 12 0 12
Condition100%3/3
$type eq "record" or $type eq "error"
l!l&&r!l&&!r
22154
MC/DC100%2/2
$type eq "record" or $type eq "error"
$type eq "record" $type eq "error"
Truth table100%3/3
A or B
A= $type eq "record"B= $type eq "error"
ABresult
1X1
011
000
69 23 return Avro::Schema::Record->new(
70 struct => $struct,
71 names => $names,
72 namespace => $namespace,
73 );
74 }
75 elsif ($type eq 'enum') {
76 6 return Avro::Schema::Enum->new(
77 struct => $struct,
78 names => $names,
79 namespace => $namespace,
80 );
81 }
82 elsif ($type eq 'array') {
83 19 return Avro::Schema::Array->new(
84 struct => $struct,
85 names => $names,
86 namespace => $namespace,
87 );
88 }
89 elsif ($type eq 'map') {
90 17 return Avro::Schema::Map->new(
91 struct => $struct,
92 names => $names,
93 namespace => $namespace,
94 );
95 }
96 elsif ($type eq 'fixed') {
97 12 return Avro::Schema::Fixed->new(
98 struct => $struct,
99 names => $names,
100 namespace => $namespace,
101 );
102 }
103 else {
104 0 throw Avro::Schema::Error::Parse("unknown type: $type");
105 }
106 }
107 ## 1.3.2 A JSON array, representing a union of embedded types.
108 elsif (ref $struct eq 'ARRAY') {
109 16 return Avro::Schema::Union->new(
110 struct => $struct,
111 names => $names,
112 namespace => $namespace,
113 );
114 }
115 ## 1.3.2 A JSON string, naming a defined type.
116 else {
117 125 my $type = $struct;
118 ## It's one of our custom defined type
119
120 ## Short name provided, prepend the namespace
121 125 ▶ if ( $type !~ /\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /\./) 125 0 125
122 125 my $fulltype = $namespace . '.' . $type;
123 125 ▶ if (exists $names->{$fulltype}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $names->{$fulltype}) 1 124 125
124 1 return $names->{$fulltype};
125 }
126 }
127
128 ## Fully-qualified name
129 124 ▶ if (exists $names->{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $names->{$type}) 11 113 124
130 11 return $names->{$type};
131 }
132
133 ## It's a primitive type
134 113 return Avro::Schema::Primitive->new(type => $type);
135 }
136 }
137  
138 1915 ▶sub match {
Subroutine match: called CC=30 SCAR=34.4
Pod undocumented
139 1915 my $class = shift;
140 1915 my %param = @_;
141  
142 my $reader = $param{reader}
143 1915 ▶ or croak "missing reader schema";
Branch: 1 branch
BranchTrueFalseTotal
my $reader = $param{"reader"} or croak("missing reader schema") 1915 0 1915
144 my $writer = $param{writer}
145 1915 ▶ or croak "missing writer schema";
Branch: 1 branch
BranchTrueFalseTotal
my $writer = $param{"writer"} or croak("missing writer schema") 1915 0 1915
146  
147 1915 ▶ my $wtype = ref $writer ? $writer->type : $writer;
Branch: 1 branch
BranchTrueFalseTotal
ref $writer ? : 1915 0 1915
148 1915 ▶ my $rtype = ref $reader ? $reader->type : $reader;
Branch: 1 branch
BranchTrueFalseTotal
ref $reader ? : 1915 0 1915
149 ## 1.3.2 either schema is a union
150 1915 ▶ return $wtype if $wtype eq 'union' or $rtype eq 'union';
Branch: 1 branch
BranchTrueFalseTotal
if $wtype eq "union" or $rtype eq "union" 3 1912 1915
Condition66%2/3
$wtype eq "union" or $rtype eq "union"
l!l&&r!l&&!r
301912
MC/DC50%1/2
$wtype eq "union" or $rtype eq "union"
$wtype eq "union" $rtype eq "union"
Truth table66%2/3
A or B
A= $wtype eq "union"B= $rtype eq "union"
ABresult
1X1
011
000
151  
152 ## 1.3.2 both schemas have same primitive type
153 1912 ▶ return $wtype if $wtype eq $rtype
Branch: 1 branch
BranchTrueFalseTotal
if $wtype eq $rtype and "Avro::Schema::Primitive"->is_type_valid($wtype) 1316 596 1912
Condition100%3/3
$wtype eq $rtype and "Avro::Schema::Primitive"->is_type_valid($wtype)
!ll&&!rl&&r
175791316
MC/DC100%2/2
$wtype eq $rtype and "Avro::Schema::Primitive"->is_type_valid($wtype)
$wtype eq $rtype "Avro::Schema::Primitive"->is_type_valid($wtype)
Truth table100%3/3
A and B
A= $wtype eq $rtypeB= "Avro::Schema::Primitive"->is_type_valid($wtype)
ABresult
0X0
100
111
154 && Avro::Schema::Primitive->is_type_valid($wtype);
155  
156 ## 1.3.2
157 ## int is promotable to long, float, or double
158 596 ▶ if ($wtype eq 'int' && (
Branch: 1 branch
BranchTrueFalseTotal
if ($wtype eq "int" and $rtype eq "float" || $rtype eq "long" || $rtype eq "double") 3 593 596
Condition77%7/9
$wtype eq "int" and $rtype eq "float" || $rtype eq "long" || $rtype eq "double"
!ll&&!rl&&r
59303
$rtype eq "float" or $rtype eq "long" or $rtype eq "double"
l!l&&r!l&&!r
210
$rtype eq "float" or $rtype eq "long"
l!l&&r!l&&!r
111
MC/DC25%1/4
$wtype eq "int" and $rtype eq "float" || $rtype eq "long" || $rtype eq "double"
$wtype eq "int" $rtype eq "float" $rtype eq "long" $rtype eq "double"
Truth table80%4/5
A and B or C or D
A= $wtype eq "int"B= $rtype eq "float"C= $rtype eq "long"D= $rtype eq "double"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
159 $rtype eq 'float' or $rtype eq 'long' or $rtype eq 'double'
160 )) {
161 3 return $rtype;
162 }
163 ## long is promotable to float or double
164 593 ▶ if ($wtype eq 'long' && (
Branch: 1 branch
BranchTrueFalseTotal
if ($wtype eq "long" and $rtype eq "float" || $rtype eq "double") 1 592 593
Condition83%5/6
$wtype eq "long" and $rtype eq "float" || $rtype eq "double"
!ll&&!rl&&r
59021
$rtype eq "float" or $rtype eq "double"
l!l&&r!l&&!r
102
MC/DC66%2/3
$wtype eq "long" and $rtype eq "float" || $rtype eq "double"
$wtype eq "long" $rtype eq "float" $rtype eq "double"
Truth table75%3/4
A and B or C
A= $wtype eq "long"B= $rtype eq "float"C= $rtype eq "double"
ABCresult
0XX0
1000
11X1
1011
165 $rtype eq 'float' or $rtype eq 'double'
166 )) {
167 1 return $rtype;
168 }
169 ## float is promotable to double
170 592 ▶ if ($wtype eq 'float' && $rtype eq 'double') {
Branch: 1 branch
BranchTrueFalseTotal
if ($wtype eq "float" and $rtype eq "double") 1 591 592
Condition100%3/3
$wtype eq "float" and $rtype eq "double"
!ll&&!rl&&r
59011
MC/DC100%2/2
$wtype eq "float" and $rtype eq "double"
$wtype eq "float" $rtype eq "double"
Truth table100%3/3
A and B
A= $wtype eq "float"B= $rtype eq "double"
ABresult
0X0
100
111
171 1 return $rtype;
172 }
173 591 ▶ return 0 unless $rtype eq $wtype;
Branch: 1 branch
BranchTrueFalseTotal
unless $rtype eq $wtype 12 579 591
174  
175 ## 1.3.2 {subtype and/or names} match
176 579 ▶ if ($rtype eq 'array') {
Branch: 5 branches
BranchTrueFalseTotal
if ($rtype eq 'array') { } 427 152 579
elsif ($rtype eq 'record') { } 12 140 152
elsif ($rtype eq 'map') { } 116 24 140
elsif ($rtype eq 'fixed') { } 16 8 24
elsif ($rtype eq 'enum') { } 8 0 8
177 427 ▶ return $wtype if $class->match(
Branch: 1 branch
BranchTrueFalseTotal
if $class->match("reader", $reader->items, "writer", $writer->items) 425 2 427
178 reader => $reader->items,
179 writer => $writer->items,
180 );
181 }
182 elsif ($rtype eq 'record') {
183 12 ▶ return $wtype if $reader->fullname eq $writer->fullname;
Branch: 1 branch
BranchTrueFalseTotal
if $reader->fullname eq $writer->fullname 11 1 12
184 }
185 elsif ($rtype eq 'map') {
186 116 ▶ return $wtype if $class->match(
Branch: 1 branch
BranchTrueFalseTotal
if $class->match("reader", $reader->values, "writer", $writer->values) 114 2 116
187 reader => $reader->values,
188 writer => $writer->values,
189 );
190 }
191 elsif ($rtype eq 'fixed') {
192 16 ▶ return $wtype if $reader->size eq $writer->size
Branch: 1 branch
BranchTrueFalseTotal
if $reader->size eq $writer->size and $reader->fullname eq $writer->fullname 15 1 16
Condition66%2/3
$reader->size eq $writer->size and $reader->fullname eq $writer->fullname
!ll&&!rl&&r
1015
MC/DC50%1/2
$reader->size eq $writer->size and $reader->fullname eq $writer->fullname
$reader->size eq $writer->size $reader->fullname eq $writer->fullname
Truth table66%2/3
A and B
A= $reader->size eq $writer->sizeB= $reader->fullname eq $writer->fullname
ABresult
0X0
100
111
193 && $reader->fullname eq $writer->fullname;
194 }
195 elsif ($rtype eq 'enum') {
196 8 ▶ return $wtype if $reader->fullname eq $writer->fullname;
Branch: 1 branch
BranchTrueFalseTotal
if $reader->fullname eq $writer->fullname 7 1 8
197 }
198 7 return 0;
199 }
200  
201  
202 package Avro::Schema::Base;
203 6 our @ISA = qw/Avro::Schema/;
204 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
205  
206 227 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
207 227 my $class = shift;
208 227 my %param = @_;
209  
210 227 my $type = $param{type};
211 227 ▶ if (!$type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 205 22 227
212 205 my ($t) = $class =~ /::([^:]+)$/;
213 205 $type = lc ($t);
214 }
215 227 my $schema = bless {
216 type => $type,
217 }, $class;
218 227 return $schema;
219 }
220  
221 5266 ▶sub type {
Subroutine type: called CC=1 SCAR=0.0
222 5266 my $schema = shift;
223 5266 return $schema->{type};
224 }
225  
226 10 ▶sub to_string {
Subroutine to_string: called CC=2 SCAR=7.1
227 10 my $schema = shift;
228 10 ▶ my $known_names = shift || {};
Condition50%1/2
shift() || {}
l!l
010
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
229 10 return Avro::Schema->to_string($schema->to_struct($known_names));
230 }
231  
232 package Avro::Schema::Primitive;
233 6 our @ISA = qw/Avro::Schema::Base/;
234 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
235 6 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
236 6 ▶use Regexp::Common qw/number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
237  
238 6 my %PrimitiveType = map { $_ => 1 } qw/
239 null
240 boolean
241 int
242 long
243 float
244 double
245 bytes
246 string
247 /;
248  
249 6 my %Singleton = ( );
250  
251 ## FIXME: useless lazy generation
252 135 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
253 135 my $class = shift;
254 135 my %param = @_;
255  
256 my $type = $param{type}
257 135 ▶ or croak "Schema must have a type";
Branch: 1 branch
BranchTrueFalseTotal
my $type = $param{"type"} or croak("Schema must have a type") 135 0 135
258  
259 135 ▶ throw Avro::Schema::Error::Parse("Not a primitive type $type")
Branch: 1 branch
BranchTrueFalseTotal
unless $class->is_type_valid($type) 1 134 135
260 unless $class->is_type_valid($type);
261  
262 134 ▶ if (! exists $Singleton{ $type } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Singleton{$type}) 22 112 134
263 22 my $schema = $class->SUPER::new( type => $type );
264 22 $Singleton{ $type } = $schema;
265 }
266 134 return $Singleton{ $type };
267 }
268  
269 2126 ▶sub is_type_valid {
Subroutine is_type_valid: called CC=2 SCAR=7.6
270 2126 ▶ return $PrimitiveType{ $_[1] || "" };
Condition50%1/2
$_[1] || ''
l!l
21260
MC/DC0%0/1
$_[1] || ''
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
271 }
272  
273 ## Returns true or false wheter the given data is valid for
274 ## this schema
275 44 ▶sub is_data_valid {
Subroutine is_data_valid: called CC=24 SCAR=37.5
276 44 my $schema = shift;
277 44 my $data = shift;
278 44 my $type = $schema->{type};
279  
280 44 ▶ if ($type eq 'null') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "null") 3 41 44
281 3 return ! defined $data;
282 }
283  
284 # undef isn't valid for any other types
285 41 ▶ return 0 unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 9 32 41
286  
287 32 ▶ if ($type eq 'int') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "int") 13 19 32
288 6 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
289 13 my $packed_int = pack "l", $data;
290 13 my $unpacked_int = unpack "l", $packed_int;
291 13 ▶ return $unpacked_int eq $data ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$unpacked_int eq $data ? : 5 8 13
292 }
293 19 ▶ if ($type eq 'long') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "long") 10 9 19
294 10 ▶ if ($Config{use64bitint}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Avro::Schema::Primitive::Config{'use64bitint'}) { } 10 0 10
295 10 ▶ return 0 unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 10 10
296 10 my $packed_int = pack "q", $data;
297 10 my $unpacked_int = unpack "q", $packed_int;
298 10 ▶ return $unpacked_int eq $data ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$unpacked_int eq $data ? : 6 4 10
299  
300 }
301 else {
302 0 require Math::BigInt;
303 0 my $int = eval { Math::BigInt->new($data) };
304 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
305 0 warn "probably a unblessed ref: $@";
306 0 return 0;
307 }
308 0 ▶ return 0 if $int->is_nan;
Branch: 1 branch
BranchTrueFalseTotal
if $int->is_nan 0 0 0
309 0 my $max = Math::BigInt->new( "0x7FFF_FFFF_FFFF_FFFF" );
310 0 ▶ return $int->bcmp($max) <= 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$int->bcmp($max) <= 0 ? : 0 0 0
311 }
312 }
313 9 ▶ if ($type eq 'float' or $type eq 'double') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "float" or $type eq "double") 1 8 9
Condition66%2/3
$type eq "float" or $type eq "double"
l!l&&r!l&&!r
108
MC/DC50%1/2
$type eq "float" or $type eq "double"
$type eq "float" $type eq "double"
Truth table66%2/3
A or B
A= $type eq "float"B= $type eq "double"
ABresult
1X1
011
000
314 1 ▶ $data =~ /^$RE{num}{real}$/ ? return 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /^$Avro::Schema::Primitive::RE{'num'}{'real'}$/ ? : 1 0 1
315 }
316 8 ▶ if ($type eq 'bytes') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "bytes") 5 3 8
317 5 ▶ return 0 if ref $data;
Branch: 1 branch
BranchTrueFalseTotal
if ref $data 0 5 5
318 5 ▶ return 1 unless utf8::is_utf8($data) and $data =~ /[^\x00-\xFF]/;
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::is_utf8($data) and $data =~ /[^\x00-\xFF]/ 4 1 5
Condition100%3/3
utf8::is_utf8($data) and $data =~ /[^\x00-\xFF]/
!ll&&!rl&&r
311
MC/DC100%2/2
utf8::is_utf8($data) and $data =~ /[^\x00-\xFF]/
utf8::is_utf8($data) $data =~ /[^\x00-\xFF]/
Truth table100%3/3
A and B
A= utf8::is_utf8($data)B= $data =~ /[^\x00-\xFF]/
ABresult
0X0
100
111
319 }
320 4 ▶ if ($type eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "string") 3 1 4
321 3 ▶ return 1 unless ref $data;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 2 1 3
322 }
323 2 ▶ if ($type eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "boolean") 0 2 2
324 0 ▶ return 1 if JSON::PP::is_bool($data);
Branch: 1 branch
BranchTrueFalseTotal
if JSON::PP::is_bool($data) 0 0 0
325 0 ▶ return 0 if ref $data; # sometimes risky
Branch: 1 branch
BranchTrueFalseTotal
if ref $data 0 0 0
326 0 return $data =~ m{^(?:yes|no|y|n|t|f|true|false|0|1)$}i;
327 }
328 2 return 0;
329 }
330  
331 10 ▶sub to_struct {
Subroutine to_struct: called CC=1 SCAR=0.0
332 10 my $schema = shift;
333 10 return $schema->type;
334 }
335  
336 package Avro::Schema::Named;
337 6 our @ISA = qw/Avro::Schema::Base/;
338 6 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
339  
340 6 my %NamedType = map { $_ => 1 } qw/
341 record
342 enum
343 fixed
344 /;
345  
346 153 ▶sub new {
Subroutine new: called CC=7 SCAR=20.5
347 153 my $class = shift;
348 153 my %param = @_;
349  
350 153 my $schema = $class->SUPER::new(%param);
351  
352 153 ▶ my $names = $param{names} || {};
Condition50%1/2
$param{'names'} || {}
l!l
1530
MC/DC0%0/1
$param{'names'} || {}
$param{'names'}
Truth table50%1/2
A
A= $param{'names'}
Aresult
11
00
353 153 ▶ my $struct = $param{struct} || {};
Condition50%1/2
$param{'struct'} || {}
l!l
1530
MC/DC0%0/1
$param{'struct'} || {}
$param{'struct'}
Truth table50%1/2
A
A= $param{'struct'}
Aresult
11
00
354 153 my $name = $struct->{name};
355 153 ▶ unless (defined $name && length $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name and length $name) 0 153 153
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
00153
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
356 0 throw Avro::Schema::Error::Parse( "Missing name for $class" );
357 }
358 153 my $namespace = $struct->{namespace};
359 153 ▶ unless (defined $namespace && length $namespace) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $namespace and length $namespace) 112 41 153
Condition66%2/3
defined $namespace and length $namespace
!ll&&!rl&&r
112041
MC/DC50%1/2
defined $namespace and length $namespace
defined $namespace length $namespace
Truth table66%2/3
A and B
A= defined $namespaceB= length $namespace
ABresult
0X0
100
111
360 112 $namespace = $param{namespace};
361 }
362  
363 153 $schema->set_names($namespace, $name);
364 81 $schema->add_name($names);
365  
366 81 return $schema;
367 }
368  
369 36 ▶sub is_type_valid {
Subroutine is_type_valid: called CC=2 SCAR=7.6
370 36 ▶ return $NamedType{ $_[1] || "" };
Condition50%1/2
$_[1] || ''
l!l
360
MC/DC0%0/1
$_[1] || ''
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
371 }
372  
373 153 ▶sub set_names {
Subroutine set_names: called CC=11 SCAR=24.0
374 153 my $schema = shift;
375 153 my ($namespace, $name) = @_;
376  
377 153 ▶ my @parts = split /\./, ($name || ""), -1;
Condition100%2/2
$name || ''
l!l
1521
MC/DC100%1/1
$name || ''
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
378 153 ▶ if (@parts > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts > 1) 13 140 153
379 13 $name = pop @parts;
380 13 $namespace = join ".", @parts;
381 13 ▶ if (grep { ! length $_ } @parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not length $_;} @parts) 5 8 13
382 5 throw Avro::Schema::Error::Name(
383 "name '$name' is not a valid name"
384 );
385 }
386 }
387  
388 ## 1.3.2 The name portion of a fullname, and record field names must:
389 ## * start with [A-Za-z_]
390 ## * subsequently contain only [A-Za-z0-9_]
391 148 my $type = $schema->{type};
392 148 ▶ unless (length $name && $name =~ m/^[A-Za-z_][A-Za-z0-9_]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $name and $name =~ /^[A-Za-z_][A-Za-z0-9_]*$/) 31 117 148
Condition100%3/3
length $name and $name =~ /^[A-Za-z_][A-Za-z0-9_]*$/
!ll&&!rl&&r
130117
MC/DC100%2/2
length $name and $name =~ /^[A-Za-z_][A-Za-z0-9_]*$/
length $name $name =~ /^[A-Za-z_][A-Za-z0-9_]*$/
Truth table100%3/3
A and B
A= length $nameB= $name =~ /^[A-Za-z_][A-Za-z0-9_]*$/
ABresult
0X0
100
111
393 31 throw Avro::Schema::Error::Name(
394 "name '$name' is not valid for $type"
395 );
396 }
397 117 ▶ if (defined $namespace && length $namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $namespace and length $namespace) 52 65 117
Condition100%3/3
defined $namespace and length $namespace
!ll&&!rl&&r
333252
MC/DC100%2/2
defined $namespace and length $namespace
defined $namespace length $namespace
Truth table100%3/3
A and B
A= defined $namespaceB= length $namespace
ABresult
0X0
100
111
398 52 for (split /\./, $namespace, -1) {
399 68 ▶ unless ($_ && /^[A-Za-z_][A-Za-z0-9_]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_ and /^[A-Za-z_][A-Za-z0-9_]*$/) 36 32 68
Condition100%3/3
$_ and /^[A-Za-z_][A-Za-z0-9_]*$/
!ll&&!rl&&r
72932
MC/DC100%2/2
$_ and /^[A-Za-z_][A-Za-z0-9_]*$/
$_ /^[A-Za-z_][A-Za-z0-9_]*$/
Truth table100%3/3
A and B
A= $_B= /^[A-Za-z_][A-Za-z0-9_]*$/
ABresult
0X0
100
111
400 36 throw Avro::Schema::Error::Name(
401 "namespace '$namespace' is not valid for $type"
402 );
403 }
404 }
405 }
406 81 $schema->{name} = $name;
407 81 $schema->{namespace} = $namespace;
408 }
409  
410 81 ▶sub add_name {
Subroutine add_name: called CC=2 SCAR=7.1
411 81 my $schema = shift;
412 81 my ($names) = @_;
413  
414 81 my $name = $schema->fullname;
415 81 ▶ if ( exists $names->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $names->{$name}) 0 81 81
416 0 throw Avro::Schema::Error::Parse( "Name $name is already defined" );
417 }
418 81 $names->{$name} = $schema;
419 81 Scalar::Util::weaken( $names->{$name} );
420 81 return;
421 }
422  
423 191 ▶sub fullname {
Subroutine fullname: called CC=2 SCAR=6.9
424 191 my $schema = shift;
425 return join ".",
426 382 ▶ grep { defined $_ && length $_ }
Condition100%3/3
defined $_ && length $_
!ll&&!rl&&r
3983260
MC/DC100%2/2
defined $_ && length $_
defined $_ length $_
Truth table100%3/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
427 191 map { $schema->{$_ } }
428 qw/namespace name/;
429 }
430  
431 75 ▶sub namespace {
Subroutine namespace: called CC=1 SCAR=0.0
432 75 my $schema = shift;
433 75 return $schema->{namespace};
434 }
435  
436 package Avro::Schema::Record;
437 6 our @ISA = qw/Avro::Schema::Named/;
438 6 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
439  
440 6 my %ValidOrder = map { $_ => 1 } qw/ascending descending ignore/;
441  
442 135 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
443 135 my $class = shift;
444 135 my %param = @_;
445  
446 135 ▶ my $names = $param{names} ||= {};
Condition100%2/2
$param{"names"} ||= {}
l!l
23112
MC/DC100%1/1
$param{"names"} ||= {}
$param{"names"}
Truth table100%2/2
A
A= $param{"names"}
Aresult
11
00
447 135 my $schema = $class->SUPER::new(%param);
448  
449 my $fields = $param{struct}{fields}
450 63 ▶ or throw Avro::Schema::Error::Parse("Record must have Fields");
Branch: 1 branch
BranchTrueFalseTotal
my $fields = $param{"struct"}{"fields"} or "Avro::Schema::Error::Parse"->throw("Record must have Fields") 63 0 63
451  
452 63 ▶ throw Avro::Schema::Error::Parse("Record.Fields must me an array")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fields eq "ARRAY" 0 63 63
453 unless ref $fields eq 'ARRAY';
454  
455 63 my $namespace = $schema->namespace;
456  
457 63 my @fields;
458 63 for my $field (@$fields) {
459 83 my $f = Avro::Schema::Field->new($field, $names, $namespace);
460 65 push @fields, $f;
461 }
462 45 $schema->{fields} = \@fields;
463 45 return $schema;
464 }
465  
466 6 ▶sub to_struct {
Subroutine to_struct: called CC=3 SCAR=11.0
467 6 my $schema = shift;
468 6 ▶ my $known_names = shift || {};
Condition100%2/2
shift() || {}
l!l
42
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
469 ## consider that this record type is now known (will serialize differently)
470 6 my $fullname = $schema->fullname;
471 6 ▶ if ($known_names->{ $fullname }++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($known_names->{$fullname}++) 3 3 6
472 3 return $fullname;
473 }
474 return {
475 type => $schema->{type},
476 name => $fullname,
477 fields => [
478 3 map { $_->to_struct($known_names) } @{ $schema->{fields} }
479 ],
480 };
481 }
482  
483 53 ▶sub fields {
Subroutine fields: called CC=1 SCAR=0.0
484 53 my $schema = shift;
485 53 return $schema->{fields};
486 }
487  
488 11 ▶sub fields_as_hash {
Subroutine fields_as_hash: called CC=2 SCAR=6.9
489 11 my $schema = shift;
490 11 ▶ unless (exists $schema->{_fields_as_hash}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $schema->{"_fields_as_hash"}) 4 7 11
491 $schema->{_fields_as_hash} = {
492 4 map { $_->{name} => $_ } @{ $schema->{fields} }
493 };
494 }
495 11 return $schema->{_fields_as_hash};
496 }
497  
498 2 ▶sub is_data_valid {
Subroutine is_data_valid: called CC=3 SCAR=11.0
499 2 my $schema = shift;
500 2 my $data = shift;
501 2 for my $field (@{ $schema->{fields} }) {
502 2 my $key = $field->{name};
503 2 ▶ return 0 unless $field->is_data_valid($data->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless $field->is_data_valid($data->{$key}) 1 1 2
504 }
505 1 return 1;
506 }
507  
508 package Avro::Schema::Field;
509  
510 6 ▶sub to_struct {
Subroutine to_struct: called CC=2 SCAR=7.0
511 6 my $field = shift;
512 6 ▶ my $known_names = shift || {};
Condition50%1/2
shift() || {}
l!l
60
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
513 6 my $type = $field->{type}->to_struct($known_names);
514 6 return { name => $field->{name}, type => $type };
515 }
516  
517 84 ▶sub new {
Subroutine new: called CC=11 SCAR=25.5
518 84 my $class = shift;
519 84 my ($struct, $names, $namespace) = @_;
520  
521 84 my $name = $struct->{name};
522 84 ▶ throw Avro::Schema::Error::Parse("Record.Field.name is required")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 84 84
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
0084
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
523 unless defined $name && length $name;
524  
525 84 my $type = $struct->{type};
526 84 ▶ throw Avro::Schema::Error::Parse("Record.Field.name is required")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and length $type 0 84 84
Condition33%1/3
defined $type and length $type
!ll&&!rl&&r
0084
MC/DC0%0/2
defined $type and length $type
defined $type length $type
Truth table33%1/3
A and B
A= defined $typeB= length $type
ABresult
0X0
100
111
527 unless defined $type && length $type;
528  
529 84 $type = Avro::Schema->parse_struct($type, $names, $namespace);
530 82 my $field = { name => $name, type => $type };
531 #TODO: find where to weaken precisely
532 #Scalar::Util::weaken($struct->{type});
533  
534 82 ▶ if (exists $struct->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $struct->{"default"}) 28 54 82
535 28 my $is_valid = $type->is_data_valid($struct->{default});
536 28 my $t = $type->type;
537 28 ▶ throw Avro::Schema::Error::Parse(
Branch: 1 branch
BranchTrueFalseTotal
unless $is_valid 12 16 28
538 "default value doesn't validate $t: '$struct->{default}'"
539 ) unless $is_valid;
540  
541 ## small Perlish special case
542 16 ▶ if ($type eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'boolean') { } 0 16 16
543 0 ▶ $field->{default} = $struct->{default} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$struct->{'default'} ? : 0 0 0
544 }
545 else {
546 16 $field->{default} = $struct->{default};
547 }
548 }
549 70 ▶ if (my $order = $struct->{order}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $order = $struct->{"order"}) 7 63 70
550 throw Avro::Schema::Error::Parse(
551 "Order '$order' is not valid'"
552 7 ▶ ) unless $ValidOrder{$order};
Branch: 1 branch
BranchTrueFalseTotal
unless $ValidOrder{$order} 4 3 7
553 3 $field->{order} = $order;
554 }
555 66 return bless $field, $class;
556 }
557  
558 2 ▶sub is_data_valid {
Subroutine is_data_valid: called CC=2 SCAR=6.9
559 2 my $field = shift;
560 2 my $data = shift;
561 2 ▶ return 1 if $field->{type}->is_data_valid($data);
Branch: 1 branch
BranchTrueFalseTotal
if $field->{"type"}->is_data_valid($data) 1 1 2
562 1 return 0;
563 }
564  
565 package Avro::Schema::Enum;
566 6 our @ISA = qw/Avro::Schema::Named/;
567  
568 6 ▶sub new {
Subroutine new: called CC=7 SCAR=20.7
569 6 my $class = shift;
570 6 my %param = @_;
571 6 my $schema = $class->SUPER::new(%param);
572 my $struct = $param{struct}
573 6 ▶ or throw Avro::Schema::Error::Parse("Enum instantiation");
Branch: 1 branch
BranchTrueFalseTotal
my $struct = $param{"struct"} or "Avro::Schema::Error::Parse"->throw("Enum instantiation") 6 0 6
574 6 ▶ my $symbols = $struct->{symbols} || [];
Condition50%1/2
$struct->{'symbols'} || []
l!l
60
MC/DC0%0/1
$struct->{'symbols'} || []
$struct->{'symbols'}
Truth table50%1/2
A
A= $struct->{'symbols'}
Aresult
11
00
575  
576 6 ▶ unless (@$symbols) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$symbols) 0 6 6
577 0 throw Avro::Schema::Error::Parse("Enum needs at least one symbol");
578 }
579 6 my %symbols;
580 6 my $pos = 0;
581 6 for (@$symbols) {
582 15 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) 0 15 15
583 0 throw Avro::Schema::Error::Parse(
584 "Enum.symbol should be a string"
585 );
586 }
587 throw Avro::Schema::Error::Parse("Duplicate symbol in Enum")
588 15 ▶ if exists $symbols{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $symbols{$_} 0 15 15
589  
590 15 $symbols{$_} = $pos++;
591 }
592 6 $schema->{hash_symbols} = \%symbols;
593 6 return $schema;
594 }
595  
596 5 ▶sub is_data_valid {
Subroutine is_data_valid: called CC=3 SCAR=11.0
597 5 my $schema = shift;
598 5 my $data = shift;
599 5 ▶ return 1 if defined $data && exists $schema->{hash_symbols}{$data};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data and exists $schema->{"hash_symbols"}{$data} 3 2 5
Condition66%2/3
defined $data and exists $schema->{"hash_symbols"}{$data}
!ll&&!rl&&r
023
MC/DC50%1/2
defined $data and exists $schema->{"hash_symbols"}{$data}
defined $data exists $schema->{"hash_symbols"}{$data}
Truth table66%2/3
A and B
A= defined $dataB= exists $schema->{"hash_symbols"}{$data}
ABresult
0X0
100
111
600 2 return 0;
601 }
602  
603 10 ▶sub symbols {
Subroutine symbols: called CC=2 SCAR=6.9
604 10 my $schema = shift;
605 10 ▶ unless (exists $schema->{symbols}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $schema->{"symbols"}) 3 7 10
606 3 my $sym = $schema->{hash_symbols};
607 3 $schema->{symbols} = [ sort { $sym->{$a} <=> $sym->{$b} } keys %$sym ];
608 }
609 10 return $schema->{symbols};
610 }
611  
612 5 ▶sub symbols_as_hash {
Subroutine symbols_as_hash: called CC=2 SCAR=7.2
613 5 my $schema = shift;
614 5 ▶ return $schema->{hash_symbols} || {};
Condition50%1/2
$schema->{'hash_symbols'} || {}
l!l
50
MC/DC0%0/1
$schema->{'hash_symbols'} || {}
$schema->{'hash_symbols'}
Truth table50%1/2
A
A= $schema->{'hash_symbols'}
Aresult
11
00
615 }
616  
617 3 ▶sub to_struct {
Subroutine to_struct: called CC=3 SCAR=11.2
618 3 my $schema = shift;
619 3 ▶ my $known_names = shift || {};
Condition100%2/2
shift() || {}
l!l
12
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
620  
621 3 my $fullname = $schema->fullname;
622 3 ▶ if ($known_names->{ $fullname }++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($known_names->{$fullname}++) 0 3 3
623 0 return $fullname;
624 }
625 return {
626 type => 'enum',
627 name => $schema->fullname,
628 3 symbols => [ @{ $schema->symbols } ],
629 };
630 }
631  
632 package Avro::Schema::Array;
633 6 our @ISA = qw/Avro::Schema::Base/;
634  
635 19 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
636 19 my $class = shift;
637 19 my %param = @_;
638 19 my $schema = $class->SUPER::new(%param);
639  
640 my $struct = $param{struct}
641 19 ▶ or throw Avro::Schema::Error::Parse("Enum instantiation");
Branch: 1 branch
BranchTrueFalseTotal
my $struct = $param{"struct"} or "Avro::Schema::Error::Parse"->throw("Enum instantiation") 19 0 19
642  
643 my $items = $struct->{items}
644 19 ▶ or throw Avro::Schema::Error::Parse("Array must declare 'items'");
Branch: 1 branch
BranchTrueFalseTotal
my $items = $struct->{"items"} or "Avro::Schema::Error::Parse"->throw("Array must declare 'items'") 19 0 19
645  
646 19 $items = Avro::Schema->parse_struct($items, $param{names}, $param{namespace});
647 19 $schema->{items} = $items;
648 19 return $schema;
649 }
650  
651 2 ▶sub is_data_valid {
Subroutine is_data_valid: called CC=3 SCAR=13.3
652 2 my $schema = shift;
653 2 my $default = shift;
654 2 ▶ return 1 if $default && ref $default eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $default and ref $default eq "ARRAY" 2 0 2
Condition33%1/3
$default and ref $default eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
$default and ref $default eq "ARRAY"
$default ref $default eq "ARRAY"
Truth table33%1/3
A and B
A= $defaultB= ref $default eq "ARRAY"
ABresult
0X0
100
111
655 0 return 0;
656 }
657  
658 2340 ▶sub items {
Subroutine items: called CC=1 SCAR=0.0
659 2340 my $schema = shift;
660 2340 return $schema->{items};
661 }
662  
663 6 ▶sub to_struct {
Subroutine to_struct: called CC=2 SCAR=7.1
664 6 my $schema = shift;
665 6 ▶ my $known_names = shift || {};
Condition50%1/2
shift() || {}
l!l
60
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
666  
667 return {
668 type => 'array',
669 6 items => $schema->{items}->to_struct($known_names),
670 };
671 }
672  
673 package Avro::Schema::Map;
674 6 our @ISA = qw/Avro::Schema::Base/;
675  
676 17 ▶sub new {
Subroutine new: called CC=4 SCAR=14.8
677 17 my $class = shift;
678 17 my %param = @_;
679 17 my $schema = $class->SUPER::new(%param);
680  
681 my $struct = $param{struct}
682 17 ▶ or throw Avro::Schema::Error::Parse("Map instantiation");
Branch: 1 branch
BranchTrueFalseTotal
my $struct = $param{"struct"} or "Avro::Schema::Error::Parse"->throw("Map instantiation") 17 0 17
683  
684 17 my $values = $struct->{values};
685 17 ▶ unless (defined $values && length $values) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $values and length $values) 0 17 17
Condition33%1/3
defined $values and length $values
!ll&&!rl&&r
0017
MC/DC0%0/2
defined $values and length $values
defined $values length $values
Truth table33%1/3
A and B
A= defined $valuesB= length $values
ABresult
0X0
100
111
686 0 throw Avro::Schema::Error::Parse("Map must declare 'values'");
687 }
688 17 $values = Avro::Schema->parse_struct($values, $param{names}, $param{namespace});
689 17 $schema->{values} = $values;
690  
691 17 return $schema;
692 }
693  
694 1 ▶sub is_data_valid {
Subroutine is_data_valid: called CC=3 SCAR=13.3
695 1 my $schema = shift;
696 1 my $default = shift;
697 1 ▶ return 1 if $default && ref $default eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $default and ref $default eq "HASH" 1 0 1
Condition33%1/3
$default and ref $default eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
$default and ref $default eq "HASH"
$default ref $default eq "HASH"
Truth table33%1/3
A and B
A= $defaultB= ref $default eq "HASH"
ABresult
0X0
100
111
698 0 return 0;
699 }
700  
701 790 ▶sub values {
Subroutine values: called CC=1 SCAR=0.0
702 790 my $schema = shift;
703 790 return $schema->{values};
704 }
705  
706 6 ▶sub to_struct {
Subroutine to_struct: called CC=2 SCAR=7.1
707 6 my $schema = shift;
708 6 ▶ my $known_names = shift || {};
Condition50%1/2
shift() || {}
l!l
60
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
709  
710 return {
711 type => 'map',
712 6 values => $schema->{values}->to_struct($known_names),
713 };
714 }
715  
716 package Avro::Schema::Union;
717 6 our @ISA = qw/Avro::Schema::Base/;
718  
719 16 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
720 16 my $class = shift;
721 16 my %param = @_;
722 16 my $schema = $class->SUPER::new(%param);
723 my $union = $param{struct}
724 16 ▶ or throw Avro::Schema::Error::Parse("Union.new needs a struct");
Branch: 1 branch
BranchTrueFalseTotal
my $union = $param{"struct"} or "Avro::Schema::Error::Parse"->throw("Union.new needs a struct") 16 0 16
725  
726 16 ▶ my $names = $param{names} ||= {};
Condition50%1/2
$param{"names"} ||= {}
l!l
160
MC/DC0%0/1
$param{"names"} ||= {}
$param{"names"}
Truth table50%1/2
A
A= $param{"names"}
Aresult
11
00
727  
728 16 my @schemas;
729 my %seen_types;
730 16 for my $struct (@$union) {
731 36 my $sch = Avro::Schema->parse_struct($struct, $names, $param{namespace});
732 36 my $type = $sch->type;
733  
734 ## 1.3.2 Unions may not contain more than one schema with the same
735 ## type, except for the named types record, fixed and enum. For
736 ## example, unions containing two array types or two map types are not
737 ## permitted, but two types with different names are permitted.
738 36 ▶ if (Avro::Schema::Named->is_type_valid($type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Avro::Schema::Named"->is_type_valid($type)) 9 27 36
739 9 $type = $sch->fullname; # resolve Named types to their name
740 }
741 ## XXX: I could define &type_name doing the correct resolution for all classes
742 36 ▶ if ($seen_types{ $type }++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_types{$type}++) 3 33 36
743 3 throw Avro::Schema::Error::Parse(
744 "$type is present more than once in the union"
745 )
746 }
747 ## 1.3.2 Unions may not immediately contain other unions.
748 33 ▶ if ($type eq 'union') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "union") 1 32 33
749 1 throw Avro::Schema::Error::Parse(
750 "Cannot embed unions in union"
751 );
752 }
753 32 push @schemas, $sch;
754 }
755 12 $schema->{schemas} = \@schemas;
756  
757 12 return $schema;
758 }
759  
760 7 ▶sub schemas {
Subroutine schemas: called CC=1 SCAR=0.0
761 7 my $schema = shift;
762 7 return $schema->{schemas};
763 }
764  
765 0 ▶sub is_data_valid {
Subroutine is_data_valid: not called CC=3 SCAR=24.8
766 0 my $schema = shift;
767 0 my $data = shift;
768 0 for my $type ( @{ $schema->{schemas} } ) {
769 0 ▶ if ( $type->is_data_valid($data) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->is_data_valid($data)) 0 0 0
770 0 return 1;
771 }
772 }
773 0 return 0;
774 }
775  
776 0 ▶sub to_struct {
Subroutine to_struct: not called CC=2 SCAR=17.9
777 0 my $schema = shift;
778 0 ▶ my $known_names = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
779 0 return [ map { $_->to_struct($known_names) } @{$schema->{schemas}} ];
780 }
781  
782 package Avro::Schema::Fixed;
783 6 our @ISA = qw/Avro::Schema::Named/;
784  
785 12 ▶sub new {
Subroutine new: called CC=7 SCAR=20.6
786 12 my $class = shift;
787 12 my %param = @_;
788 12 my $schema = $class->SUPER::new(%param);
789  
790 my $struct = $param{struct}
791 12 ▶ or throw Avro::Schema::Error::Parse("Fixed instantiation");
Branch: 1 branch
BranchTrueFalseTotal
my $struct = $param{"struct"} or "Avro::Schema::Error::Parse"->throw("Fixed instantiation") 12 0 12
792  
793 12 my $size = $struct->{size};
794 12 ▶ unless (defined $size && length $size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $size and length $size) 0 12 12
Condition33%1/3
defined $size and length $size
!ll&&!rl&&r
0012
MC/DC0%0/2
defined $size and length $size
defined $size length $size
Truth table33%1/3
A and B
A= defined $sizeB= length $size
ABresult
0X0
100
111
795 0 throw Avro::Schema::Error::Parse("Fixed must declare 'size'");
796 }
797 12 ▶ if (ref $size) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $size) 0 12 12
798 0 throw Avro::Schema::Error::Parse(
799 "Fixed.size should be a scalar"
800 );
801 }
802 12 ▶ unless ($size =~ m{^\d+$} && $size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($size =~ /^\d+$/ and $size > 0) 4 8 12
Condition100%3/3
$size =~ /^\d+$/ and $size > 0
!ll&&!rl&&r
318
MC/DC100%2/2
$size =~ /^\d+$/ and $size > 0
$size =~ /^\d+$/ $size > 0
Truth table100%3/3
A and B
A= $size =~ /^\d+$/B= $size > 0
ABresult
0X0
100
111
803 4 throw Avro::Schema::Error::Parse(
804 "Fixed.size should be a positive integer"
805 );
806 }
807 # Cast into numeric so that it will be encoded as a JSON number
808 8 $schema->{size} = $size + 0;
809  
810 8 return $schema;
811 }
812  
813 4 ▶sub is_data_valid {
Subroutine is_data_valid: called CC=4 SCAR=13.9
814 4 my ( $schema, $data ) = @_;
815  
816 4 ▶ return 0 if utf8::is_utf8($data) && $data =~ /[^\x00-\xFF]/;
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($data) and $data =~ /[^\x00-\xFF]/ 1 3 4
Condition100%3/3
utf8::is_utf8($data) and $data =~ /[^\x00-\xFF]/
!ll&&!rl&&r
211
MC/DC100%2/2
utf8::is_utf8($data) and $data =~ /[^\x00-\xFF]/
utf8::is_utf8($data) $data =~ /[^\x00-\xFF]/
Truth table100%3/3
A and B
A= utf8::is_utf8($data)B= $data =~ /[^\x00-\xFF]/
ABresult
0X0
100
111
817 3 ▶ return $data && length($data) == $schema->{size};
Condition66%2/3
$data && length $data == $schema->{'size'}
!ll&&!rl&&r
102
MC/DC50%1/2
$data && length $data == $schema->{'size'}
$data length $data == $schema->{'size'}
Truth table66%2/3
A && B
A= $dataB= length $data == $schema->{'size'}
ABresult
0X0
100
111
818 }
819  
820 57 ▶sub size {
Subroutine size: called CC=1 SCAR=0.0
821 57 my $schema = shift;
822 57 return $schema->{size};
823 }
824  
825 1 ▶sub to_struct {
Subroutine to_struct: called CC=3 SCAR=11.8
826 1 my $schema = shift;
827 1 ▶ my $known_names = shift || {};
Condition50%1/2
shift() || {}
l!l
10
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
828  
829 1 my $fullname = $schema->fullname;
830 1 ▶ if ($known_names->{ $fullname }++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($known_names->{$fullname}++) 0 1 1
831 0 return $fullname;
832 }
833  
834 return {
835 type => 'fixed',
836 name => $fullname,
837 size => $schema->{size},
838 1 };
839 }
840  
841 package Avro::Schema::Error::Parse;
842 6 ▶use parent 'Error::Simple';
Subroutine BEGIN: called CC=1 SCAR=0.0
843  
844 package Avro::Schema::Error::Name;
845 6 ▶use parent 'Error::Simple';
Subroutine BEGIN: called CC=1 SCAR=0.0
846  
847 package Avro::Schema::Error::Mismatch;
848 6 ▶use parent 'Error::Simple';
Subroutine BEGIN: called CC=1 SCAR=0.0
849  
850 6 1;
↑ Summary Avro/DataFileReader.pm »