Reader/Merged.pm

filter: Statementstmt 95.3% 41 / 43 Branchbran 64.2% 9 / 14 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 79.7% 59 / 74 CC 12 SCAR 18.8
CC
12
Cov
83%
parse
9.2
new
4.8
BEGIN
1.0
CRAP 6.6
« Reader.pm ↑ Summary
1  
2 =head1 NAME
3  
4 TinyDNS::Reader::Merged - Read a TinyDNS file and merge records.
5  
6 =head1 DESCRIPTION
7  
8 The L<TinyDNS::Reader> module will allow you to parse a TinyDNS zonefile,
9 into a number of distinct records, one for each line.
10  
11 However there is no handling the case where you'd expect a single DNS
12 value to have multiple values.
13  
14 For example if you were to parse this file you would get two records:
15  
16 =for example begin
17  
18 db.example.com:1.2.3.4:300
19 db.example.com:10.20.30.40:300
20  
21 =for example end
22  
23 On a purely record-by-record basis that is as-expected, however when you
24 actually come to manipulate your DNS records you'd expect to have a single
25 logical object with values something like this:
26  
27 =for example begin
28  
29 {
30 'ttl' => '300',
31 'value' => [
32 '1.2.3.4',
33 '10.20.30.40'
34 ],
35 'name' => 'db.example.com',
36 'type' => 'A'
37 },
38  
39 =for example end
40  
41 This module takes care of that for you, by merging records which consist
42 of identical "name" + "type" pairs.
43  
44 Use it as a drop-in replacing for L<TinyDNS::Reader>.
45  
46 =cut
47  
48  
49 =head2 METHODS
50  
51 =cut
52  
53  
54 package TinyDNS::Reader::Merged;
55  
56 2 ▶use TinyDNS::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61  
62 =head2 new
63  
64 Constructor.
65  
66 This module expects to be given a C<file> parameter, pointing to a
67 file which can be parsed, or a C<text> parameter containing the text of the
68 records to parse.
69  
70 =cut
71  
72 sub new
73 4 ▶{
Subroutine new: called CC=4 SCAR=15.8
Pod documented
74 4 my ( $proto, %supplied ) = (@_);
75 4 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
76  
77 4 my $self = {};
78 4 bless( $self, $class );
79  
80 #
81 # Create our child object.
82 #
83 4 ▶ if ( $supplied{ 'file' } )
Branch: 2 branches
BranchTrueFalseTotal
if ($supplied{'file'}) { } 0 4 4
elsif ($supplied{'text'}) { } 4 0 4
84 {
85 0 $self->{ 'obj' } = TinyDNS::Reader->new( file => $supplied{ 'file' } );
86 }
87 elsif ( $supplied{ 'text' } )
88 {
89 4 $self->{ 'obj' } = TinyDNS::Reader->new( text => $supplied{ 'text' } );
90 }
91 else
92 {
93 0 die "Missing 'text' or 'file' argument";
94 }
95 4 return $self;
96 }
97  
98  
99 =head2 parse
100  
101 Parse the records and return a merged set.
102  
103 The parsing is delegated to L<TinyDNS::Reader>, so all supported record-types
104 work as expected.
105  
106 =cut
107  
108 sub parse
109 4 ▶{
Subroutine parse: called CC=9 SCAR=22.2
Pod documented
110 4 my ($self) = (@_);
111  
112 4 my $records = $self->{ 'obj' }->parse();
113  
114  
115 #
116 # Process each entry
117 #
118 4 my $res;
119  
120 4 my %seen = ();
121  
122 4 foreach my $r (@$records)
123 {
124  
125 # Test that the record was recognized.
126 5 ▶ next unless ( $r->valid() );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->valid 0 5 5
127  
128 5 my $name = $r->name();
129 5 my $type = $r->type();
130 5 my $val = $r->value();
131 5 my $ttl = $r->ttl();
132 5 my $hash = $r->hash();
133  
134 # skip if we've seen this name+type pair before.
135 5 ▶ next if ( $seen{ $name }{ $type } );
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$name}{$type} 1 4 5
136  
137 #
138 # Look for other values with the same type.
139 #
140 # NOTE: O(N^2) - needs improvement.
141 #
142 4 foreach my $x (@$records)
143 {
144  
145 # Test that the record was recognized.
146 5 ▶ next if ( !$x->valid() );
Branch: 1 branch
BranchTrueFalseTotal
unless $x->valid 0 5 5
147  
148 5 my $name2 = $x->name();
149 5 my $type2 = $x->type();
150 5 my $val2 = $x->value();
151 5 my $hash2 = $x->hash();
152  
153 5 ▶ next if ( $hash eq $hash2 );
Branch: 1 branch
BranchTrueFalseTotal
if $hash eq $hash2 4 1 5
154  
155 #
156 # If this record has the same name/type as the
157 # previous one then merge in the new value.
158 #
159 # NOTE: This means the TTL comes from the first
160 # of the records. Which is fine.
161 #
162 1 ▶ if ( ( $name eq $name2 ) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $name2 and $type eq $type2) 1 0 1
Condition33%1/3
$name eq $name2 and $type eq $type2
!ll&&!rl&&r
001
MC/DC0%0/2
$name eq $name2 and $type eq $type2
$name eq $name2 $type eq $type2
Truth table33%1/3
A and B
A= $name eq $name2B= $type eq $type2
ABresult
0X0
100
111
163 ( $type eq $type2 ) )
164 {
165 1 $r->add($val2);
166 }
167 }
168  
169 4 push( @$res,
170 { name => $name,
171 value => $r->value(),
172 ttl => $ttl,
173 type => $type
174 } );
175  
176  
177 #
178 # We've seen this name/type pair now.
179 #
180 4 $seen{ $name }{ $type } += 1;
181 }
182  
183 #
184 # Return the merged/updated results.
185 #
186 4 return ($res);
187 }
188  
189 2 1;
190  
191  
192 =head1 AUTHOR
193  
194 Steve Kemp <steve@steve.org.uk>
195  
196 =cut
197  
198 =head1 COPYRIGHT AND LICENSE
199  
200 Copyright (C) 2014-2015 Steve Kemp <steve@steve.org.uk>.
201  
202 This code was developed for an online Git-based DNS hosting solution,
203 which can be found at:
204  
205 =over 8
206  
207 =item *
208 https://dns-api.com/
209  
210 =back
211  
212 This library is free software. You can modify and or distribute it under
213 the same terms as Perl itself.
214  
215 =cut
« Reader.pm ↑ Summary