blib/lib/App/RecordStream/KeySpec.pm

filter: Statementstmt 95.0% 155 / 163 Branchbran 80.8% 55 / 68 Conditioncond 68.7% 11 / 16 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 7 totaltotal 86.4% 243 / 281 CC 45 SCAR 21.8
CC
45
Cov
89%
_guess_key_recurse
15.6
_guess_key_name_raw
12.0
_generate_keylookup_sub
10.0
CRAP 8.9
« blib/lib/App/RecordStream/Operation/fromps.pm ↑ Summary blib/lib/App/RecordStream/Operation/totable.pm »
1 package App::RecordStream::KeySpec;
2  
3 =head1 NAME
4  
5 App::RecordStream::KeySpec
6  
7 =head1 AUTHOR
8  
9 Benjamin Bernard <perlhacker@benjaminbernard.com>
10 Keith Amling <keith.amling@gmail.com>
11  
12 =head1 DESCRIPTION
13  
14 This class knows out to look up a keyspec in a datastructure
15  
16 =head1 SYNOPSIS
17  
18 use App::RecordStream::KeySpec;
19 my $data_ref = App::RecordStream::KeySpec::find_key($r, 'Foo/Bar');
20  
21 =cut
22  
23 116 our $VERSION = "4.0.25";
24  
25 116 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 116 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 116 ▶use App::RecordStream::KeySpec;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 116 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 116 my $registry = {};
33  
34 4317 ▶sub find_key {
Subroutine find_key: called CC=1 SCAR=0.0
Pod undocumented
35 4317 my ($data, $spec, $no_vivify, $throw_error) = @_;
36  
37 4317 my $spec_obj = __PACKAGE__->new($spec);
38 4317 return $spec_obj->guess_key($data, $no_vivify, $throw_error);
39 }
40  
41 sub new
42 9073 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
43 9073 my $class = shift;
44 9073 my $spec = shift;
45  
46 9073 ▶ if ( exists $registry->{$spec} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $registry->{$spec}) 8857 216 9073
47 8857 return $registry->{$spec};
48 }
49  
50 216 my $this = {
51 SPEC => $spec,
52 };
53  
54 216 bless $this, $class;
55 216 $this->init();
56  
57 216 $registry->{$spec} = $this;
58 216 return $this;
59 }
60  
61 sub init
62 216 ▶{
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
63 216 my $this = shift;
64 216 $this->_parse_key_spec();
65 }
66  
67 {
68 116 my $guessed_keys = {};
69  
70 40 ▶ sub _search_string_to_key {
Subroutine _search_string_to_key: called CC=1 SCAR=0.0
71 40 my $key_chain = shift;
72 40 my $string = shift;
73  
74 40 return $guessed_keys->{join('-', @$key_chain)}->{$string};
75 }
76  
77 34 ▶ sub _add_string_key_mapping {
Subroutine _add_string_key_mapping: called CC=1 SCAR=0.0
78 34 my $key_chain = shift;
79 34 my $string = shift;
80 34 my $key = shift;
81  
82 34 $guessed_keys->{join('-', @$key_chain)}->{$string} = $key;
83 }
84 }
85  
86 116 ▶sub _guess_key_name_raw {
Subroutine _guess_key_name_raw: called CC=12 SCAR=24.9
87 2314 my ($this, $data, $key_chain, $search_string) = @_;
88  
89 2314 my $fuzzy = $this->{'FUZZY'};
90  
91 2314 ▶ if ( UNIVERSAL::isa($data, 'ARRAY') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($data, "ARRAY")) 14 2300 2314
92 14 ▶ if ( $search_string =~ m/^#(\d+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search_string =~ /^#(\d+)$/) { } 14 0 14
93 14 return $1;
94 }
95 else {
96 0 die "Cannot select non-numeric index: $search_string (did you forget to prefix with a '#'?) for array: " . Dumper($data);
97 }
98 }
99  
100 2300 ▶ return $search_string if ( ! $fuzzy );
Branch: 1 branch
BranchTrueFalseTotal
unless $fuzzy 2260 40 2300
101  
102 40 my $found_key;
103  
104 40 ▶ if ( my $key = _search_string_to_key($key_chain, $search_string) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = _search_string_to_key($key_chain, $search_string)) 6 34 40
105 6 return $key;
106 }
107  
108 # First check exact match
109 34 ▶ if ( defined $data->{$search_string} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{$search_string}) { } 9 25 34
110 9 $found_key = $search_string;
111 }
112 else {
113 # Next check prefixes, no interpolation
114 25 foreach my $key ( CORE::sort(CORE::keys %$data) ) {
115 69 ▶ if ( $key =~ m/^\Q$search_string\E/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^\Q$search_string\E/i) 8 61 69
116 8 $found_key = $key;
117 }
118 }
119 }
120  
121 34 ▶ if ( !$found_key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_key) 18 16 34
122 # Check for match anywhere in the keys, allow regex interpolation
123 18 foreach my $key ( CORE::sort(CORE::keys %$data) ) {
124 44 ▶ if ( $key =~ m/$search_string/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /$search_string/i) 5 39 44
125 5 $found_key = $key;
126 }
127 }
128 }
129  
130 34 ▶ if ( !$found_key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_key) 14 20 34
131 14 $found_key = $search_string;
132 }
133  
134 34 _add_string_key_mapping($key_chain, $search_string, $found_key);
135  
136 34 return $found_key
137 }
138  
139 2697 ▶sub has_key_spec {
Subroutine has_key_spec: called CC=3 SCAR=11.2
Pod undocumented
140 2697 my ($this, $data) = @_;
141 2697 eval { $this->guess_key($data, 0, 1) };
142  
143 2697 ▶ if ( $@ =~ m/^NoSuchKey/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ =~ /^NoSuchKey/) { } 1 2696 2697
elsif ($@) { } 0 2696 2696
144 1 return 0;
145 }
146 elsif ( $@ ) {
147 #Rethrow if a different error
148 0 die $@;
149 }
150  
151 2696 return 1;
152 }
153  
154 2058 ▶sub get_key_list_for_spec {
Subroutine get_key_list_for_spec: called CC=1 SCAR=0.0
Pod undocumented
155 2058 my ($this, $data) = @_;
156  
157 return $this->_guess_key_recurse(
158 $data,
159 [],
160 1,
161 0,
162 1,
163 2058 @{$this->{'PARSED_KEYS'}},
164 );
165 }
166  
167 216 ▶sub _parse_key_spec {
Subroutine _parse_key_spec: called CC=7 SCAR=19.5
168 216 my ($this) = @_;
169  
170 216 my $spec = $this->{'SPEC'};
171 216 my $fuzzy = 0;
172 216 my $spec_name = $spec;
173  
174 216 ▶ if ( substr($spec, 0, 1) eq '@' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($spec, 0, 1) eq "\@") 37 179 216
175 37 $fuzzy = 1;
176 37 $spec = substr($spec, 1);
177 }
178  
179 216 my $keys = [];
180 216 my $current_key = '';
181 216 my $last_char = '';
182  
183 216 ▶ for (my $index = 0; $index < length($spec); $index++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < length $spec) 829 216 1045
184 829 my $current_char = substr($spec, $index, 1);
185  
186 829 ▶ if ( $current_char eq '/' && $last_char ne '\\' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_char eq '/' and $last_char ne '\\') { } 30 799 829
Condition100%3/3
$current_char eq "/" and $last_char ne "\\"
!ll&&!rl&&r
798130
MC/DC100%2/2
$current_char eq "/" and $last_char ne "\\"
$current_char eq "/" $last_char ne "\\"
Truth table100%3/3
A and B
A= $current_char eq "/"B= $last_char ne "\\"
ABresult
0X0
100
111
187 30 push @$keys, $current_key;
188 30 $current_key = '';
189 30 $last_char = '';
190 30 next;
191 }
192 else {
193 799 ▶ if ( $current_char eq '/' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_char eq "/") 1 798 799
194 1 chop $current_key;
195 }
196  
197 799 $current_key .= $current_char;
198 799 $last_char = $current_char;
199 799 next;
200 }
201 }
202  
203 216 ▶ if ( $current_key ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_key ne "") 216 0 216
204 216 push @$keys, $current_key;
205 }
206  
207 216 $this->{'PARSED_KEYS'} = $keys;
208 216 $this->{'FUZZY'} = $fuzzy;
209 }
210  
211 {
212 116 my $keylookup_hash = {};
213  
214 7015 ▶ sub guess_key {
Subroutine guess_key: called CC=4 SCAR=13.9
Pod undocumented
215 7015 my ($this, $data, $no_vivify, $throw_error) = @_;
216  
217 7015 my @args = @{$this->{'PARSED_KEYS'}};
218  
219 7015 ▶ $no_vivify ||= 0;
Condition50%1/2
$no_vivify ||= 0
l!l
07015
MC/DC0%0/1
$no_vivify ||= 0
$no_vivify
Truth table50%1/2
A
A= $no_vivify
Aresult
11
00
220 7015 ▶ $throw_error ||= 0;
Condition100%2/2
$throw_error ||= 0
l!l
26994316
MC/DC100%1/1
$throw_error ||= 0
$throw_error
Truth table100%2/2
A
A= $throw_error
Aresult
11
00
221 7015 my $args_string = join('-', @args, $no_vivify, $throw_error);
222  
223 7015 ▶ if ( my $code = $keylookup_hash->{$args_string} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $keylookup_hash->{$args_string}) 6788 227 7015
224 6788 return $code->($data);
225 }
226  
227 227 my $keys = $this->_guess_key_recurse(
228 $data,
229 [],
230 $no_vivify,
231 $throw_error,
232 1,
233 @args,
234 );
235  
236 225 my $code = $this->_generate_keylookup_sub($keys, $no_vivify);
237 225 $keylookup_hash->{$args_string} = $code;
238  
239 225 return $code->($data);
240 }
241 }
242  
243 # Performance! Oh god, performance. Generate a lookup subroutine that will
244 # lookup the passed keys, for execution later
245 116 ▶sub _generate_keylookup_sub {
Subroutine _generate_keylookup_sub: called CC=9 SCAR=23.0
246 225 my $this = shift;
247 225 my $keys = shift;
248 225 my $no_vivify = shift;
249 225 my $throw_error = shift;
250  
251 225 ▶ if ( scalar @$keys == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$keys == 0) 0 225 225
252 0 return eval 'sub { if ( \$throw_error ) { die "NoSuchKey"; } return ""; }';
253 }
254  
255 225 my $code_string = 'sub { my $record = shift;';
256  
257 225 my $key_accessor = '$record';
258  
259 225 my $action = "return ''";
260 225 ▶ $action = "die 'NoSuchKey'" if ( $throw_error );
Branch: 1 branch
BranchTrueFalseTotal
if $throw_error 0 225 225
261  
262 225 my $check_actions = '';
263  
264 225 foreach my $key (@$keys) {
265 249 ▶ if ( $key =~ m/^#(\d+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^#(\d+)$/) { } 13 236 249
266 13 my $index = $1;
267 13 $key_accessor .= "->[$index]";
268 }
269 else {
270 236 my @hex_bytes = unpack('C*', $key);
271 236 my $hex_string = '';
272  
273 236 foreach my $byte (@hex_bytes) {
274 827 $hex_string .= "\\x" . sprintf ("%lx", $byte);
275 }
276  
277 236 $key_accessor .= "->{\"$hex_string\"}";
278 }
279  
280 249 $check_actions .= "$action if ( ! exists $key_accessor );";
281 }
282  
283 225 ▶ if ( $no_vivify || $throw_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_vivify or $throw_error) 0 225 225
Condition33%1/3
$no_vivify or $throw_error
l!l&&r!l&&!r
00225
MC/DC0%0/2
$no_vivify or $throw_error
$no_vivify $throw_error
Truth table33%1/3
A or B
A= $no_vivifyB= $throw_error
ABresult
1X1
011
000
284 0 $code_string .= $check_actions;
285 }
286  
287 225 $code_string .= "return \\($key_accessor)}";
288  
289 225 my $sub_ref = eval $code_string;
290 225 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 225 225
291 0 warn "Unexpected error in creating key lookup!\n";
292 0 die $@;
293 }
294 225 return $sub_ref;
295 }
296  
297 2314 ▶sub _guess_key_recurse {
Subroutine _guess_key_recurse: called CC=14 SCAR=27.5
298 2314 my ($this, $data, $key_chain, $no_vivify, $throw_error,
299 $return_key_chain, $search_string, @next_strings) = @_;
300  
301 2314 my $type = ref($data);
302  
303 2314 ▶ if ( $type eq 'SCALAR' || UNIVERSAL::isa(\$data, 'SCALAR') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "SCALAR" or UNIVERSAL::isa(\$data, "SCALAR")) 0 2314 2314
Condition33%1/3
$type eq "SCALAR" or UNIVERSAL::isa(\$data, "SCALAR")
l!l&&r!l&&!r
002314
MC/DC0%0/2
$type eq "SCALAR" or UNIVERSAL::isa(\$data, "SCALAR")
$type eq "SCALAR" UNIVERSAL::isa(\$data, "SCALAR")
Truth table33%1/3
A or B
A= $type eq "SCALAR"B= UNIVERSAL::isa(\$data, "SCALAR")
ABresult
1X1
011
000
304 0 die "Cannot look for $search_string in scalar: " . Dumper($data);
305 }
306  
307 2314 my $key = $this->_guess_key_name_raw($data, $key_chain, $search_string);
308  
309 2314 my $value;
310  
311 2314 ▶ if ( $type eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 14 2300 2314
312 14 $value = \($data->[$key]);
313 14 $key = "#$key";
314 }
315 else {
316 2300 ▶ if ( $no_vivify && (!exists $data->{$key}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_vivify and not exists $data->{$key}) 3 2297 2300
Condition100%3/3
$no_vivify and not exists $data->{$key}
!ll&&!rl&&r
23820593
MC/DC100%2/2
$no_vivify and not exists $data->{$key}
$no_vivify not exists $data->{$key}
Truth table100%3/3
A and not(B)
A= $no_vivifyB= not exists $data->{$key}
ABresult
0X0
100
111
317 3 ▶ return $return_key_chain ? [] : '';
Branch: 1 branch
BranchTrueFalseTotal
$return_key_chain ? : 3 0 3
318 }
319  
320 2297 $value = \($data->{$key})
321 }
322  
323 2311 ▶ if ( scalar @next_strings > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @next_strings > 0) 31 2280 2311
324 31 ▶ if ( ! defined $$value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$value) 9 22 31
325 9 ▶ die "NoSuchKey" if ( $throw_error );
Branch: 1 branch
BranchTrueFalseTotal
if $throw_error 2 7 9
326  
327 7 ▶ if ( $no_vivify ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_vivify) 0 7 7
328 0 ▶ return $return_key_chain ? [] : '';
Branch: 1 branch
BranchTrueFalseTotal
$return_key_chain ? : 0 0 0
329 }
330  
331 7 ▶ if ( substr($next_strings[0], 0, 1) eq '#' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($next_strings[0], 0, 1) eq '#') { } 3 4 7
332 3 $$value = [];
333 }
334 else {
335 4 $$value = {};
336 }
337 }
338  
339 29 return $this->_guess_key_recurse(
340 $$value,
341 [@$key_chain, $key],
342 $no_vivify,
343 $throw_error,
344 $return_key_chain,
345 @next_strings,
346 );
347 }
348  
349 2280 ▶ return $return_key_chain ? [@$key_chain, $key] : $value;
Branch: 1 branch
BranchTrueFalseTotal
$return_key_chain ? : 2280 0 2280
350 }
351  
352 25 ▶sub keyspec_help {
Subroutine keyspec_help: called CC=1 SCAR=0.0
Pod undocumented
353 25 return <<KEYSPECS_HELP;
354 KEY SPECS
355 __FORMAT_TEXT__
356 A key spec is short way of specifying a field with prefixes or regular
357 expressions, it may also be nested into hashes and arrays. Use a '/' to nest
358 into a hash and a '#NUM' to index into an array (i.e. #2)
359  
360 An example is in order, take a record like this:
361 __FORMAT_TEXT__
362  
363 {"biz":["a","b","c"],"foo":{"bar 1":1},"zap":"blah1"}
364 {"biz":["a","b","c"],"foo":{"bar 1":2},"zap":"blah2"}
365 {"biz":["a","b","c"],"foo":{"bar 1":3},"zap":"blah3"}
366  
367 __FORMAT_TEXT__
368 In this case a key spec of 'foo/bar 1' would have the values 1,2, and 3
369 in the respective records.
370  
371 Similarly, 'biz/#0' would have the value of 'a' for all 3 records
372  
373 You can also prefix key specs with '\@' to engage the fuzzy matching logic
374 __FORMAT_TEXT__
375  
376 __FORMAT_TEXT__
377 Fuzzy matching works like this in order, first key to match wins
378 __FORMAT_TEXT__
379 1. Exact match ( eq )
380 2. Prefix match ( m/^/ )
381 3. Match anywehre in the key (m//)
382  
383 __FORMAT_TEXT__
384 So, in the above example '\@b/#2', the 'b' portion would expand to 'biz' and 2
385 would be the index into the array, so all records would have the value of 'c'
386  
387 Simiarly, \@f/b would have values 1, 2, and 3
388  
389 You can escape / with a \\. For example, if you have a record:
390 __FORMAT_TEXT__
391 {"foo/bar":2}
392  
393 __FORMAT_TEXT__
394 You can address that key with foo\\/bar
395 __FORMAT_TEXT__
396 KEYSPECS_HELP
397 }
398  
399 116 1;
« blib/lib/App/RecordStream/Operation/fromps.pm ↑ Summary blib/lib/App/RecordStream/Operation/totable.pm »