blib/lib/App/RecordStream/KeyGroups.pm

filter: Statementstmt 97.1% 138 / 142 Branchbran 88.1% 37 / 42 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 6 totaltotal 91.4% 203 / 222 CC 32 SCAR 18.5
CC
32
Cov
94%
_get_paths
12.1
parse_group
10.1
get_fields
4.0
CRAP 6.3
« blib/lib/App/RecordStream/Operation/totable.pm ↑ Summary blib/lib/App/RecordStream/DomainLanguage/Snippet.pm »
1 package App::RecordStream::KeyGroups;
2 115 our $VERSION = "4.0.23";
3  
4 115 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 115 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 sub new {
8 95 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
9 95 my @args = @_;
10  
11 95 my $this = {
12 KEY_GROUPS => [],
13 };
14  
15 95 bless $this, $class;
16  
17 95 $this->add_groups($_) foreach @args;
18  
19 95 return $this;
20 };
21  
22 sub has_any_group {
23 45 ▶ my $this = shift;
Subroutine has_any_group: called CC=1 SCAR=0.0
Pod undocumented
24 45 return (scalar @{$this->{'KEY_GROUPS'}}) > 0;
25 }
26  
27 sub add_groups {
28 72 ▶ my $this = shift;
Subroutine add_groups: called CC=3 SCAR=11.0
Pod undocumented
29 72 my $groups = shift;
30  
31 72 foreach my $group_spec (split(',', $groups)) {
32 90 my $group;
33 90 ▶ if ( $group_spec =~ m/^!/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_spec =~ /^!/) { } 31 59 90
34 31 $group = App::RecordStream::KeyGroups::Group->new($group_spec);
35 }
36 else {
37 59 $group = App::RecordStream::KeyGroups::KeySpec->new($group_spec);
38 }
39  
40 90 push @{$this->{'KEY_GROUPS'}}, $group;
41 }
42 }
43  
44 sub get_keyspecs_for_record {
45 764 ▶ my $this = shift;
Subroutine get_keyspecs_for_record: called CC=2 SCAR=6.9
Pod undocumented
46 764 my $record = shift;
47  
48 764 my @specs;
49  
50 764 foreach my $group ( @{$this->{'KEY_GROUPS'}} ) {
51 1202 push @specs, @{$group->get_fields($record)};
52 }
53  
54 764 return \@specs;
55 }
56  
57 # This is a cached version
58 sub get_keyspecs {
59 38 ▶ my $this = shift;
Subroutine get_keyspecs: called CC=2 SCAR=6.9
Pod undocumented
60 38 my $record = shift;
61  
62 38 ▶ if ( !$this->{'KEY_SPECS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"KEY_SPECS"}) 25 13 38
63 25 $this->{'KEY_SPECS'} = $this->get_keyspecs_for_record($record);
64 }
65  
66 38 return $this->{'KEY_SPECS'};
67 }
68  
69 sub usage {
70 12 ▶ return <<HELP;
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
71 KEY GROUPS
72 __FORMAT_TEXT__
73 SYNTAX: !regex!opt1!opt2...
74 Key groups are a way of specifying multiple fields to a recs command with a
75 single argument or function. They are generally regexes, and have several
76 options to control what fields they match. By default you give a regex, and
77 it will be matched against all first level keys of a record to come up with
78 the record list. For instance, in a record like this:
79 __FORMAT_TEXT__
80  
81 { 'zip': 1, 'zap': 2, 'foo': { 'bar': 3 } }
82  
83 __FORMAT_TEXT__
84 Key group: !z! would get the keys 'zip' and 'zap'
85  
86 You can have a literal '!' in your regex, just escape it with a \\.
87  
88 Normally, key groups will only match keys whose values are scalars. This
89 can be changed with the 'returnrefs' or rr flag.
90  
91 With the above record !f! would match no fields, but !f!rr would match foo
92 (which has a value of a hash ref)
93  
94 Options on KeyGroups:
95 __FORMAT_TEXT__
96 returnrefs, rr - Return keys that have reference values (default:off)
97 full, f - Regex should match against full keys (recurse fully)
98 depth=NUM,d=NUM - Only match keys at NUM depth (regex will match against
99 full keyspec)
100 sort, s - sort keyspecs lexically
101 HELP
102 }
103  
104 115 1;
105  
106 package App::RecordStream::KeyGroups::KeySpec;
107  
108 sub new {
109 59 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
110 59 my $spec = shift;
111  
112 59 my $this = {
113 SPEC => $spec,
114 };
115  
116 59 return bless $this, $class;
117 }
118  
119 sub get_fields {
120 1035 ▶ my $this = shift;
Subroutine get_fields: called CC=2 SCAR=7.2
121 1035 my $record = shift;
122  
123 1035 ▶ if ( $record->has_key_spec($this->{'SPEC'}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record->has_key_spec($this->{"SPEC"})) 1035 0 1035
124 1035 return [join('/', @{$record->get_key_list_for_spec($this->{'SPEC'})})];
125 }
126  
127 0 return [];
128 }
129  
130 115 1;
131  
132 package App::RecordStream::KeyGroups::Group;
133  
134 115 my $VALID_OPTIONS = {
135 d => 'depth',
136 depth => 'depth',
137 s => 'sort',
138 'sort' => 'sort',
139 f => 'full_match',
140 full => 'full_match',
141 rr => 'return_refs',
142 returnrefs => 'return_refs'
143 };
144  
145 sub new {
146 32 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
147 32 my $group_spec = shift;
148  
149 32 my $this = {
150 };
151  
152 32 bless $this, $class;
153  
154 32 $this->parse_group($group_spec);
155 32 return $this;
156 }
157  
158 sub get_fields {
159 175 ▶ my $this = shift;
Subroutine get_fields: called CC=4 SCAR=13.9
160 175 my $record = shift;
161  
162 175 my @specs;
163 175 my $regex = $this->{'REGEX'};
164 175 foreach my $spec (@{$this->get_specs($record)}) {
165 775 ▶ if ( $spec =~ m/$regex/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /$regex/) 295 480 775
166 295 push @specs, $spec;
167 }
168 }
169  
170 #TODO: deal with sorts
171 175 ▶ if ( $this->has_option('sort') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->has_option("sort")) 111 64 175
172 111 @specs = sort @specs;
173 }
174 175 return \@specs;
175 }
176  
177 sub get_specs {
178 175 ▶ my $this = shift;
Subroutine get_specs: called CC=3 SCAR=11.0
179 175 my $record = shift;
180  
181 175 my $min_depth = 1;
182 175 my $max_depth = 1;
183  
184 175 ▶ if ( $this->has_option('full_match') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($this->has_option('full_match')) { } 5 170 175
elsif ($this->has_option('depth')) { } 1 169 170
185 5 $max_depth = -1;
186  
187 }
188 elsif ( $this->has_option('depth') ) {
189 1 my $depth = $this->option_value('depth');
190 1 $min_depth = $depth;
191 1 $max_depth = $depth;
192 }
193  
194 175 my $paths = [];
195 175 $this->_get_paths({%$record}, 1, $min_depth, $max_depth, [], $paths);
196 175 return [map { join('/', @$_) } @$paths];
197 }
198  
199 sub _get_paths {
200 915 ▶ my $this = shift;
Subroutine _get_paths: called CC=12 SCAR=25.0
201 915 my $data = shift;
202 915 my $current_depth = shift;
203 915 my $min_depth = shift;
204 915 my $max_depth = shift;
205 915 my $current_keys = shift;
206 915 my $found_paths = shift;
207  
208 915 ▶ if ( $current_depth >= $min_depth ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_depth >= $min_depth) 914 1 915
209 914 ▶ if ( ref($data) eq '' || $this->has_option('return_refs') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "" or $this->has_option("return_refs")) 775 139 914
Condition100%3/3
ref $data eq "" or $this->has_option("return_refs")
l!l&&r!l&&!r
72649139
MC/DC100%2/2
ref $data eq "" or $this->has_option("return_refs")
ref $data eq "" $this->has_option("return_refs")
Truth table100%3/3
A or B
A= ref $data eq ""B= $this->has_option("return_refs")
ABresult
1X1
011
000
210 775 push @$found_paths, [@$current_keys];
211 }
212 }
213  
214 915 ▶ if ( ref($data) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "ARRAY") 2 913 915
215 2 my $index = -1;
216 2 foreach my $value ( @$data ) {
217 3 $index++;
218 3 ▶ if ( $current_depth <= $max_depth || $max_depth == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_depth <= $max_depth or $max_depth == -1) 0 3 3
Condition33%1/3
$current_depth <= $max_depth or $max_depth == -1
l!l&&r!l&&!r
003
MC/DC0%0/2
$current_depth <= $max_depth or $max_depth == -1
$current_depth <= $max_depth $max_depth == -1
Truth table33%1/3
A or B
A= $current_depth <= $max_depthB= $max_depth == -1
ABresult
1X1
011
000
219 0 $this->_get_paths($value,
220 $current_depth+1,
221 $min_depth,
222 $max_depth,
223 [@$current_keys, "\#index"],
224 $found_paths);
225 }
226 }
227 }
228 915 ▶ if ( ref($data) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH") 187 728 915
229 187 foreach my $key (keys %$data) {
230 745 ▶ if ( $current_depth <= $max_depth || $max_depth == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_depth <= $max_depth or $max_depth == -1) 740 5 745
Condition100%3/3
$current_depth <= $max_depth or $max_depth == -1
l!l&&r!l&&!r
720205
MC/DC100%2/2
$current_depth <= $max_depth or $max_depth == -1
$current_depth <= $max_depth $max_depth == -1
Truth table100%3/3
A or B
A= $current_depth <= $max_depthB= $max_depth == -1
ABresult
1X1
011
000
231 740 $this->_get_paths($data->{$key},
232 $current_depth+1,
233 $min_depth,
234 $max_depth,
235 [@$current_keys, $key],
236 $found_paths);
237 }
238 }
239 }
240 }
241  
242 sub parse_group {
243 45 ▶ my $this = shift;
Subroutine parse_group: called CC=10 SCAR=23.1
244 45 my $spec = shift;
245  
246 45 ▶ if ( '!' ne substr($spec, 0, 1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("!" ne substr($spec, 0, 1)) 2 43 45
247 2 die "Malformed group spec: '$spec', does not start with '!'\n";
248 }
249  
250 43 ▶ if ( length($spec) < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $spec < 2) 0 43 43
251 0 die "Malformed group spec: '$spec', does not have enough length\n";
252 }
253  
254 43 my $regex = '';
255 43 my $last_char = '';
256 43 my $found_end = 0;
257 43 my $start_option_index = 1;
258  
259 43 ▶ for (my $index = 1; $index < length($spec); $index++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < length $spec) 156 1 157
260 156 $start_option_index++;
261 156 my $current_char = substr($spec, $index, 1);
262  
263 156 ▶ if ( $current_char eq '!' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_char eq "!") 42 114 156
264 42 ▶ if ( $last_char ne '\\' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_char ne "\\") 42 0 42
265 42 $last_char = $current_char;
266 42 $found_end = 1;
267 42 last;
268 }
269 }
270 114 $last_char = $current_char;
271 114 $regex .= $current_char;
272 114 next;
273 }
274  
275 43 ▶ die "Malformed group spec: Did not find terminating '!' in '$spec'\n" if ( ! $found_end );
Branch: 1 branch
BranchTrueFalseTotal
unless $found_end 1 42 43
276  
277 42 my $options_string = substr($spec, $start_option_index);
278 42 my $options = {};
279  
280 42 foreach my $option_group (split('!', $options_string)) {
281 44 my ($option, $value) = split('=', $option_group);
282 44 ▶ if ( my $normalized_option = $VALID_OPTIONS->{$option} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $normalized_option = $VALID_OPTIONS->{$option}) { } 43 1 44
283 43 ▶ if ( exists $options->{$normalized_option} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{$normalized_option}) { } 0 43 43
284 0 die "Already specified option '$option'. Bad option: '$option_group' in '$spec'\n";
285 }
286 else {
287 43 $options->{$normalized_option} = $value;
288 }
289 }
290 else {
291 1 die "Malformed group spec: Unrecognized option: '$option' in '$spec'\n";
292 }
293 }
294  
295 41 $this->{'REGEX'} = $regex;
296 41 $this->{'OPTIONS'} = $options;
297 }
298  
299 sub has_option {
300 708 ▶ my $this = shift;
Subroutine has_option: called CC=1 SCAR=0.0
301 708 my $option = shift;
302  
303 708 return exists $this->{'OPTIONS'}->{$option};
304 }
305  
306 sub option_value {
307 1 ▶ my $this = shift;
Subroutine option_value: called CC=1 SCAR=0.0
308 1 my $option = shift;
309  
310 1 return $this->{'OPTIONS'}->{$option};
311 }
312  
313 115 1;
« blib/lib/App/RecordStream/Operation/totable.pm ↑ Summary blib/lib/App/RecordStream/DomainLanguage/Snippet.pm »