File Coverage

blib/lib/Avro/Protocol.pm
Criterion Covered Total %
statement 63 66 95.4
branch 3 6 50.0
condition 4 9 44.4
subroutine 17 19 89.4
pod 0 5 0.0
total 87 105 82.8


line stmt bran cond sub pod time code
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             use strict;
19 1     1   663 use warnings;
  1         2  
  1         25  
20 1     1   4  
  1         2  
  1         24  
21             use Carp;
22 1     1   4 use JSON::XS();
  1         2  
  1         45  
23 1     1   559 use Try::Tiny;
  1         2794  
  1         20  
24 1     1   456 use Avro::Protocol::Message;
  1         1759  
  1         49  
25 1     1   387 use Avro::Schema;
  1         3  
  1         5  
26 1     1   26 use Error;
  1         2  
  1         15  
27 1     1   4 use Object::Tiny qw{
  1         1  
  1         4  
28 1         4 name
29             namespace
30             doc
31             types
32             messages
33             };
34 1     1   94  
  1         2  
35             our $VERSION = '1.11.1';
36              
37             my $json = JSON::XS->new->allow_nonref;
38              
39             my $class = shift;
40             my $enc_proto = shift
41 1     1 0 485 or throw Avro::Protocol::Error::Parse("protocol cannot be empty");
42 1 50       4  
43             my $struct = try {
44             $json->decode($enc_proto);
45             }
46 1     1   98 catch {
47             throw Avro::Protocol::Error::Parse(
48             "Cannot parse json string: $_"
49 0     0   0 );
50             };
51             return $class->from_struct($struct);
52 1         8 }
53 1         18  
54             my $class = shift;
55             my $struct = shift || {};
56             my $name = $struct->{protocol};
57 1     1 0 2 unless (defined $name or length $name) {
58 1   50     3 throw Avro::Protocol::Error::Parse("protocol name is required");
59 1         3 }
60 1 50 33     13  
61 0         0 my $types = $class->parse_types($struct->{types});
62              
63             my $messages = $struct->{messages} ?
64 1         4 $class->parse_messages($struct->{messages}, $types) : undef;
65              
66             my $protocol = $class->SUPER::new(
67 1 50       5 name => $name,
68             namespace => $struct->{namespace},
69             doc => $struct->{doc},
70             types => $types,
71             messages => $messages,
72             );
73 1         12 return $protocol;
74             }
75              
76 1         12 my $class = shift;
77             my $types = shift || [];
78              
79             my %types;
80 1     1 0 2 my $names = {};
81 1   50     3 for (@$types) {
82             try {
83 1         2 my $schema = Avro::Schema->parse_struct($_, $names);
84 1         6 $types{ $schema->fullname } = $schema;
85 1         3 }
86             catch {
87 2     2   110 throw Avro::Protocol::Error::Parse("errors in parsing types: $_");
88 2         4 };
89             }
90             return \%types;
91 0     0   0 }
92 2         23  
93             my $class = shift;
94 1         23 my $messages = shift || {};
95             my $types = shift;
96             my $m = {};
97             for my $name (keys %$messages) {
98 1     1 0 2 $m->{$name} = Avro::Protocol::Message->new($messages->{$name}, $types);
99 1   50     3 }
100 1         1 return $m;
101 1         1 }
102 1         3  
103 1         5 my $protocol = shift;
104             return join ".", grep { $_ } map { $protocol->$_ } qw{ namespace name };
105 1         10 }
106              
107             use parent 'Error::Simple';
108              
109 1     1 0 589 1;