| 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::Protocol; | ||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use Avro::Protocol::Message; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use Avro::Schema; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use Object::Tiny qw{ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | name | ||||||||||||||||||||||||||||
| 30 | namespace | ||||||||||||||||||||||||||||
| 31 | doc | ||||||||||||||||||||||||||||
| 32 | types | ||||||||||||||||||||||||||||
| 33 | messages | ||||||||||||||||||||||||||||
| 34 | 1 | }; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | our $VERSION = '1.12.2'; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | my $json = JSON::MaybeXS->new->allow_nonref; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 42 | 1 | ▶ | my $enc_proto = shift | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | or throw Avro::Protocol::Error::Parse("protocol cannot be empty"); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | my $struct = try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 1 | $json->decode($enc_proto); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 49 | 0 | throw Avro::Protocol::Error::Parse( | |||||||||||||||||||||||||||
| 50 | "Cannot parse json string: $_" | ||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||
| 52 | 1 | }; | |||||||||||||||||||||||||||
| 53 | 1 | return $class->from_struct($struct); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 1 | ▶ | sub from_struct { | ||||||||||||||||||||||||||
|
Subroutine
from_struct: called CC=5 SCAR=17.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 58 | 1 | ▶ | my $struct = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 59 | 1 | my $name = $struct->{protocol}; | |||||||||||||||||||||||||||
| 60 | 1 | ▶ | unless (defined $name or length $name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $name or length $name
MC/DC0%0/2
defined $name or length $name
defined $name
length $name
Truth table33%1/3
A or B
A= defined $nameB= length $name
| |||||||||||||||||||||||||||||
| 61 | 0 | throw Avro::Protocol::Error::Parse("protocol name is required"); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | my $types = $class->parse_types($struct->{types}); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | my $messages = $struct->{messages} ? | ||||||||||||||||||||||||||||
| 67 | 1 | ▶ | $class->parse_messages($struct->{messages}, $types) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | my $protocol = $class->SUPER::new( | ||||||||||||||||||||||||||||
| 70 | name => $name, | ||||||||||||||||||||||||||||
| 71 | namespace => $struct->{namespace}, | ||||||||||||||||||||||||||||
| 72 | doc => $struct->{doc}, | ||||||||||||||||||||||||||||
| 73 | 1 | types => $types, | |||||||||||||||||||||||||||
| 74 | messages => $messages, | ||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||
| 76 | 1 | return $protocol; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | sub parse_types { | ||||||||||||||||||||||||||
|
Subroutine
parse_types: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 80 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 81 | 1 | ▶ | my $types = shift || []; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || []
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | my %types; | |||||||||||||||||||||||||||
| 84 | 1 | my $names = {}; | |||||||||||||||||||||||||||
| 85 | 1 | for (@$types) { | |||||||||||||||||||||||||||
| 86 | 2 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | 2 | my $schema = Avro::Schema->parse_struct($_, $names); | |||||||||||||||||||||||||||
| 88 | 2 | $types{ $schema->fullname } = $schema; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 91 | 0 | throw Avro::Protocol::Error::Parse("errors in parsing types: $_"); | |||||||||||||||||||||||||||
| 92 | 2 | }; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 1 | return \%types; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 1 | ▶ | sub parse_messages { | ||||||||||||||||||||||||||
|
Subroutine
parse_messages: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 99 | 1 | ▶ | my $messages = shift || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 100 | 1 | my $types = shift; | |||||||||||||||||||||||||||
| 101 | 1 | my $m = {}; | |||||||||||||||||||||||||||
| 102 | 1 | for my $name (keys %$messages) { | |||||||||||||||||||||||||||
| 103 | 1 | $m->{$name} = Avro::Protocol::Message->new($messages->{$name}, $types); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 1 | return $m; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | ▶ | sub fullname { | ||||||||||||||||||||||||||
|
Subroutine
fullname: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 1 | my $protocol = shift; | |||||||||||||||||||||||||||
| 110 | 1 | return join ".", grep { $_ } map { $protocol->$_ } qw{ namespace name }; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | package Avro::Protocol::Error::Parse; | ||||||||||||||||||||||||||||
| 114 | 1 | ▶ | use parent 'Error::Simple'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 1 | 1; | |||||||||||||||||||||||||||