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.1
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 115 our $VERSION = "4.0.23";
24  
25 115 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 115 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 115 ▶use App::RecordStream::KeySpec;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 115 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 115 my $registry = {};
33  
34 sub find_key {
35 4294 ▶ my ($data, $spec, $no_vivify, $throw_error) = @_;
Subroutine find_key: called CC=1 SCAR=0.0
Pod undocumented
36  
37 4294 my $spec_obj = __PACKAGE__->new($spec);
38 4294 return $spec_obj->guess_key($data, $no_vivify, $throw_error);
39 }
40  
41 sub new
42 {
43 9050 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
44 9050 my $spec = shift;
45  
46 9050 ▶ if ( exists $registry->{$spec} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $registry->{$spec}) 8836 214 9050
47 8836 return $registry->{$spec};
48 }
49  
50 214 my $this = {
51 SPEC => $spec,
52 };
53  
54 214 bless $this, $class;
55 214 $this->init();
56  
57 214 $registry->{$spec} = $this;
58 214 return $this;
59 }
60  
61 sub init
62 {
63 214 ▶ my $this = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
64 214 $this->_parse_key_spec();
65 }
66  
67 {
68 115 my $guessed_keys = {};
69  
70 sub _search_string_to_key {
71 39 ▶ my $key_chain = shift;
Subroutine _search_string_to_key: called CC=1 SCAR=0.0
72 39 my $string = shift;
73  
74 39 return $guessed_keys->{join('-', @$key_chain)}->{$string};
75 }
76  
77 sub _add_string_key_mapping {
78 33 ▶ my $key_chain = shift;
Subroutine _add_string_key_mapping: called CC=1 SCAR=0.0
79 33 my $string = shift;
80 33 my $key = shift;
81  
82 33 $guessed_keys->{join('-', @$key_chain)}->{$string} = $key;
83 }
84 }
85  
86 115 sub _guess_key_name_raw {
87 2312 ▶ my ($this, $data, $key_chain, $search_string) = @_;
Subroutine _guess_key_name_raw: called CC=12 SCAR=24.9
88  
89 2312 my $fuzzy = $this->{'FUZZY'};
90  
91 2312 ▶ if ( UNIVERSAL::isa($data, 'ARRAY') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($data, "ARRAY")) 14 2298 2312
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 2298 ▶ return $search_string if ( ! $fuzzy );
Branch: 1 branch
BranchTrueFalseTotal
unless $fuzzy 2259 39 2298
101  
102 39 my $found_key;
103  
104 39 ▶ 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 33 39
105 6 return $key;
106 }
107  
108 # First check exact match
109 33 ▶ if ( defined $data->{$search_string} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{$search_string}) { } 8 25 33
110 8 $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 33 ▶ if ( !$found_key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_key) 18 15 33
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 33 ▶ if ( !$found_key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_key) 14 19 33
131 14 $found_key = $search_string;
132 }
133  
134 33 _add_string_key_mapping($key_chain, $search_string, $found_key);
135  
136 33 return $found_key
137 }
138  
139 sub has_key_spec {
140 2697 ▶ my ($this, $data) = @_;
Subroutine has_key_spec: called CC=3 SCAR=11.2
Pod undocumented
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 sub get_key_list_for_spec {
155 2058 ▶ my ($this, $data) = @_;
Subroutine get_key_list_for_spec: called CC=1 SCAR=0.0
Pod undocumented
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 sub _parse_key_spec {
168 214 ▶ my ($this) = @_;
Subroutine _parse_key_spec: called CC=7 SCAR=19.5
169  
170 214 my $spec = $this->{'SPEC'};
171 214 my $fuzzy = 0;
172 214 my $spec_name = $spec;
173  
174 214 ▶ if ( substr($spec, 0, 1) eq '@' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($spec, 0, 1) eq "\@") 36 178 214
175 36 $fuzzy = 1;
176 36 $spec = substr($spec, 1);
177 }
178  
179 214 my $keys = [];
180 214 my $current_key = '';
181 214 my $last_char = '';
182  
183 214 ▶ for (my $index = 0; $index < length($spec); $index++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < length $spec) 827 214 1041
184 827 my $current_char = substr($spec, $index, 1);
185  
186 827 ▶ if ( $current_char eq '/' && $last_char ne '\\' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_char eq '/' and $last_char ne '\\') { } 30 797 827
Condition100%3/3
$current_char eq "/" and $last_char ne "\\"
!ll&&!rl&&r
796130
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 797 ▶ if ( $current_char eq '/' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_char eq "/") 1 796 797
194 1 chop $current_key;
195 }
196  
197 797 $current_key .= $current_char;
198 797 $last_char = $current_char;
199 797 next;
200 }
201 }
202  
203 214 ▶ if ( $current_key ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_key ne "") 214 0 214
204 214 push @$keys, $current_key;
205 }
206  
207 214 $this->{'PARSED_KEYS'} = $keys;
208 214 $this->{'FUZZY'} = $fuzzy;
209 }
210  
211 {
212 115 my $keylookup_hash = {};
213  
214 sub guess_key {
215 6992 ▶ my ($this, $data, $no_vivify, $throw_error) = @_;
Subroutine guess_key: called CC=4 SCAR=13.9
Pod undocumented
216  
217 6992 my @args = @{$this->{'PARSED_KEYS'}};
218  
219 6992 ▶ $no_vivify ||= 0;
Condition50%1/2
$no_vivify ||= 0
l!l
06992
MC/DC0%0/1
$no_vivify ||= 0
$no_vivify
Truth table50%1/2
A
A= $no_vivify
Aresult
11
00
220 6992 ▶ $throw_error ||= 0;
Condition100%2/2
$throw_error ||= 0
l!l
26994293
MC/DC100%1/1
$throw_error ||= 0
$throw_error
Truth table100%2/2
A
A= $throw_error
Aresult
11
00
221 6992 my $args_string = join('-', @args, $no_vivify, $throw_error);
222  
223 6992 ▶ if ( my $code = $keylookup_hash->{$args_string} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $keylookup_hash->{$args_string}) 6767 225 6992
224 6767 return $code->($data);
225 }
226  
227 225 my $keys = $this->_guess_key_recurse(
228 $data,
229 [],
230 $no_vivify,
231 $throw_error,
232 1,
233 @args,
234 );
235  
236 223 my $code = $this->_generate_keylookup_sub($keys, $no_vivify);
237 223 $keylookup_hash->{$args_string} = $code;
238  
239 223 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 115 sub _generate_keylookup_sub {
246 223 ▶ my $this = shift;
Subroutine _generate_keylookup_sub: called CC=9 SCAR=23.1
247 223 my $keys = shift;
248 223 my $no_vivify = shift;
249 223 my $throw_error = shift;
250  
251 223 ▶ if ( scalar @$keys == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$keys == 0) 0 223 223
252 0 return eval 'sub { if ( \$throw_error ) { die "NoSuchKey"; } return ""; }';
253 }
254  
255 223 my $code_string = 'sub { my $record = shift;';
256  
257 223 my $key_accessor = '$record';
258  
259 223 my $action = "return ''";
260 223 ▶ $action = "die 'NoSuchKey'" if ( $throw_error );
Branch: 1 branch
BranchTrueFalseTotal
if $throw_error 0 223 223
261  
262 223 my $check_actions = '';
263  
264 223 foreach my $key (@$keys) {
265 247 ▶ if ( $key =~ m/^#(\d+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^#(\d+)$/) { } 13 234 247
266 13 my $index = $1;
267 13 $key_accessor .= "->[$index]";
268 }
269 else {
270 234 my @hex_bytes = unpack('C*', $key);
271 234 my $hex_string = '';
272  
273 234 foreach my $byte (@hex_bytes) {
274 825 $hex_string .= "\\x" . sprintf ("%lx", $byte);
275 }
276  
277 234 $key_accessor .= "->{\"$hex_string\"}";
278 }
279  
280 247 $check_actions .= "$action if ( ! exists $key_accessor );";
281 }
282  
283 223 ▶ if ( $no_vivify || $throw_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_vivify or $throw_error) 0 223 223
Condition33%1/3
$no_vivify or $throw_error
l!l&&r!l&&!r
00223
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 223 $code_string .= "return \\($key_accessor)}";
288  
289 223 my $sub_ref = eval $code_string;
290 223 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 223 223
291 0 warn "Unexpected error in creating key lookup!\n";
292 0 die $@;
293 }
294 223 return $sub_ref;
295 }
296  
297 sub _guess_key_recurse {
298 2312 ▶ my ($this, $data, $key_chain, $no_vivify, $throw_error,
Subroutine _guess_key_recurse: called CC=14 SCAR=27.5
299 $return_key_chain, $search_string, @next_strings) = @_;
300  
301 2312 my $type = ref($data);
302  
303 2312 ▶ if ( $type eq 'SCALAR' || UNIVERSAL::isa(\$data, 'SCALAR') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "SCALAR" or UNIVERSAL::isa(\$data, "SCALAR")) 0 2312 2312
Condition33%1/3
$type eq "SCALAR" or UNIVERSAL::isa(\$data, "SCALAR")
l!l&&r!l&&!r
002312
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 2312 my $key = $this->_guess_key_name_raw($data, $key_chain, $search_string);
308  
309 2312 my $value;
310  
311 2312 ▶ if ( $type eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 14 2298 2312
312 14 $value = \($data->[$key]);
313 14 $key = "#$key";
314 }
315 else {
316 2298 ▶ if ( $no_vivify && (!exists $data->{$key}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_vivify and not exists $data->{$key}) 3 2295 2298
Condition100%3/3
$no_vivify and not exists $data->{$key}
!ll&&!rl&&r
23620593
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 2295 $value = \($data->{$key})
321 }
322  
323 2309 ▶ if ( scalar @next_strings > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @next_strings > 0) 31 2278 2309
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 2278 ▶ return $return_key_chain ? [@$key_chain, $key] : $value;
Branch: 1 branch
BranchTrueFalseTotal
$return_key_chain ? : 2278 0 2278
350 }
351  
352 sub keyspec_help {
353 25 ▶ return <<KEYSPECS_HELP;
Subroutine keyspec_help: called CC=1 SCAR=0.0
Pod undocumented
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 115 1;
« blib/lib/App/RecordStream/Operation/fromps.pm ↑ Summary blib/lib/App/RecordStream/Operation/totable.pm »