blib/lib/OPP.pm

filter: Statementstmt 18.1% 36 / 198 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 40.9% 9 / 22 Podpod 0.0% 0 / 10 totaltotal 14.3% 45 / 314 CC 56 SCAR 44.7
CC
56
Cov
13%
pipeline
272.0
__ANON__
90.0
unflatten
90.0
CRAP 87.5
« blib/lib/OPP/Proc/Lookup.pm ↑ Summary blib/lib/Onyphe.pm »
1 #
2 # $Id: OPP.pm,v 689580a0e0f8 2023/07/17 09:34:26 gomor $
3 #
4 package OPP;
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '1.00';
9  
10 2 our $debug = 0;
11  
12 2 ▶use Class::Gomor::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use base qw(Class::Gomor::Array);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our @AS = qw(
16 nested
17 state
18 output
19 );
20 2 __PACKAGE__->cgBuildIndices;
21 2 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
22  
23 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Text::ParseWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use JSON::XS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 #
30 # Check given field is of nested kind:
31 #
32 # $self->is_nested("domain"); # 0
33 # $self->is_nested("app.http.component"); # ( 'app.http.component', undef )
34 # $self->is_nested("app.http.component.product"); # ( 'app.http.component', 'product' )
35 #
36 sub is_nested {
37 0 ▶ my $self = shift;
Subroutine is_nested: not called CC=6 SCAR=37.4
Pod undocumented
38 0 my ($field) = @_;
39  
40 0 ▶ croak("is_nested: need field arg") unless defined($field);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field 0 0 0
41  
42 0 my $nested = { map { $_ => 1 } @{$self->nested} };
43  
44 0 my ($head, $leaf) = $field =~ m{^(.+)\.(\S+)$};
45  
46 0 my $is_nested = 0;
47 # Handle first case: app.http.component.product given as input:
48 # Will have head set to app.http.component and leaf to product:
49 0 ▶ if (defined($head) && $nested->{$head}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $head and $nested->{$head}) { } 0 0 0
elsif ($nested->{$field}) { } 0 0 0
Condition0%0/3
defined $head and $nested->{$head}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $head and $nested->{$head}
defined $head $nested->{$head}
Truth table0%0/3
A and B
A= defined $headB= $nested->{$head}
ABresult
0X0
100
111
50 0 $is_nested = 1;
51 }
52 # Handle second case: app.http.component given as input:
53 elsif ($nested->{$field}) {
54 0 $head = $field;
55 0 $leaf = undef;
56 0 $is_nested = 1;
57 }
58  
59 0 ▶ return $is_nested ? [ $head, $leaf ] : 0;
Branch: 1 branch
BranchTrueFalseTotal
$is_nested ? : 0 0 0
60 }
61  
62 #
63 # Flatten given doc so we can work with field names in 'a.b.c' format i/o of {a}{b}{c}:
64 #
65 sub flatten {
66 0 ▶ my $self = shift;
Subroutine flatten: not called CC=4 SCAR=30.0
Pod undocumented
67 0 my ($docs) = @_;
68  
69 #croak("flatten: need doc|docs argument") unless defined($docs);
70 0 ▶ return $docs unless defined($docs);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $docs 0 0 0
71  
72 0 ▶ $docs = ref($docs) eq 'ARRAY' ? $docs : [ $docs ];
Branch: 1 branch
BranchTrueFalseTotal
ref $docs eq 'ARRAY' ? : 0 0 0
73  
74 0 my @new = ();
75 0 for my $doc (@$docs) {
76 0 my $new = { __opp_flatten => 1 };
77 0 my $sub; $sub = sub {
78 0 ▶ my ($doc, $field) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
79  
80 0 for my $k (keys %$doc) {
81 0 ▶ my $this_field = defined($field) ? "$field.$k" : $k;
Branch: 1 branch
BranchTrueFalseTotal
defined $field ? : 0 0 0
82 0 ▶ if (ref($doc->{$k}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $doc->{$k} eq 'HASH') { } 0 0 0
83 0 $sub->($doc->{$k}, $this_field);
84 }
85 else {
86 0 $new->{$this_field} = $doc->{$k};
87 }
88 }
89  
90 0 return $new;
91 0 };
92  
93 #push @new, ($doc->{__opp_flatten} ? $doc : $sub->($doc));
94 0 push @new, $sub->($doc);
95 }
96  
97 0 return \@new;
98 }
99  
100 my $tie = sub {
101 0 ▶ my ($h) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 my $t = tie(my %res, 'Tie::IxHash');
103 0 %res = %$h;
104 0 $t->SortByKey;
105 0 return \%res;
106 2 };
107  
108 2 my $order; $order = sub {
109 0 ▶ my ($h) = @_;
Subroutine __ANON__: not called CC=9 SCAR=45.0
110  
111 0 my $tie = $tie->($h);
112  
113 0 for my $k (keys %$h) {
114 0 ▶ next unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 0 0
115 0 ▶ next unless defined $h->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h->{$k} 0 0 0
116 0 ▶ if (ref($h->{$k}) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $h->{$k} eq 'HASH') { } 0 0 0
elsif (ref($h->{$k} eq 'ARRAY')) { } 0 0 0
117 0 my $this_tie = $order->($h->{$k});
118 0 $tie->{$k} = $this_tie;
119 }
120 elsif (ref($h->{$k} eq 'ARRAY')) {
121 0 my @a = ();
122 0 for (@{$h->{$k}}) {
123 0 ▶ next unless ref($_) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "HASH" 0 0 0
124 0 my $this_tie = $order->($_);
125 0 push @a, $this_tie;
126 }
127 0 ▶ $h->{$k} = \@a if @a;
Branch: 1 branch
BranchTrueFalseTotal
if @a 0 0 0
128 }
129 }
130  
131 0 return $tie;
132 2 };
133  
134 sub order {
135 0 ▶ my $self = shift;
Subroutine order: not called CC=4 SCAR=30.0
Pod undocumented
136 0 my ($docs) = @_;
137  
138 0 ▶ $docs = ref($docs) eq 'ARRAY' ? $docs : [ $docs ];
Branch: 1 branch
BranchTrueFalseTotal
ref $docs eq 'ARRAY' ? : 0 0 0
139  
140 0 my @ordered = ();
141 0 for (@$docs) {
142 0 ▶ my $this = $order->($_) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $this = &$order($_) or next 0 0 0
143 0 push @ordered, $this;
144 }
145  
146 0 return \@ordered;
147 }
148  
149 sub unflatten {
150 0 ▶ my $self = shift;
Subroutine unflatten: not called CC=9 SCAR=45.0
Pod undocumented
151 0 my ($flats) = @_;
152  
153 0 ▶ croak("unflatten: need flat|flats argument") unless defined($flats);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flats 0 0 0
154  
155 0 ▶ $flats = ref($flats) eq 'ARRAY' ? $flats : [ $flats ];
Branch: 1 branch
BranchTrueFalseTotal
ref $flats eq 'ARRAY' ? : 0 0 0
156  
157 0 my @new = ();
158 0 for my $flat (@$flats) {
159 0 ▶ if ($flat->{_opp_nounflatten}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flat->{"_opp_nounflatten"}) 0 0 0
160 0 delete $flat->{_opp_nounflatten};
161 0 push @new, $flat;
162 0 next;
163 }
164  
165 0 my %new;
166 0 for my $k (keys %$flat) {
167 0 my @toks = split(/\./, $k);
168 0 my $value = $flat->{$k};
169  
170 0 my $current = \%new;
171 0 my $last = $#toks;
172 0 for my $idx (0..$#toks) {
173 0 ▶ if ($idx == $last) { # Last token
Branch: 1 branch
BranchTrueFalseTotal
if ($idx == $last) 0 0 0
174 0 $current->{$toks[$idx]} = $value;
175 0 last;
176 }
177  
178 # Create HASH key so we can iterate and create all subkeys
179 # Merge with existing or create empty HASH:
180 0 ▶ $current->{$toks[$idx]} = $current->{$toks[$idx]} || {};
Condition0%0/2
$$current{$toks[$idx]} || {}
l!l
00
MC/DC0%0/1
$$current{$toks[$idx]} || {}
$$current{$toks[$idx]}
Truth table0%0/2
A
A= $$current{$toks[$idx]}
Aresult
11
00
181 0 $current = $current->{$toks[$idx]};
182 }
183 }
184  
185 0 delete $new{__opp_flatten};
186 0 push @new, \%new;
187 }
188  
189 0 return \@new;
190 }
191  
192 sub pipeline {
193 0 ▶ my $self = shift;
Subroutine pipeline: not called CC=16 SCAR=56.1
Pod undocumented
194 0 my ($input, $opp) = @_;
195  
196 0 ▶ $input = ref($input) eq 'ARRAY' ? $input : [ $input ];
Branch: 1 branch
BranchTrueFalseTotal
ref $input eq 'ARRAY' ? : 0 0 0
197  
198 0 ▶ return $input unless defined($opp);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opp 0 0 0
199  
200 0 $opp =~ s{(?:^\s*|\s*$)}{}g;
201  
202 0 my @cmd = split(/\s*(?<!\\)\|\s*/, $opp);
203 0 ▶ croak("pipeline: no query, aborting") if @cmd == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @cmd == 0 0 0 0
204  
205 0 ▶ print STDERR "pipeline: cmdlist[@cmd] count[".scalar(@cmd)."]\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $OPP::debug 0 0 0
206  
207 0 my $idx = 0;
208 0 $self->output->add($self->flatten($input));
209 0 for my $this (@cmd) {
210 0 ▶ print STDERR "pipeline: cmd[$this]\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $OPP::debug 0 0 0
211 0 my @proc = $this =~ m{^(\S+)(?:\s+(.+))?$};
212 0 ▶ if (! defined($proc[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proc[0]) 0 0 0
213 0 ▶ print STDERR "pipeline: parse failed for [$this]\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $OPP::debug 0 0 0
214 0 return;
215 }
216  
217 # Load proc
218 0 my $module = 'OPP::Proc::'.ucfirst(lc($proc[0]));
219 0 eval("use $module;");
220 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
221 0 chomp($@);
222 0 print STDERR "pipeline: use proc failed [$proc[0]]: $@\n";
223 0 return;
224 }
225 0 my $proc = $module->new;
226 0 ▶ if (!defined($proc)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proc) 0 0 0
227 0 print STDERR "pipeline: load proc failed [$proc[0]]\n";
228 0 return;
229 }
230 0 $proc->idx($idx);
231 0 $proc->nested($self->nested);
232 0 $proc->state($self->state);
233 0 $proc->output($proc->clone($self->output)->init);
234  
235 0 my $argument = $proc[1];
236 0 my $options = $proc->parse($argument);
237 0 $proc->options($options);
238  
239 0 ▶ print STDERR "pipeline: proc[$proc]\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $OPP::debug 0 0 0
240  
241 0 for my $input (@{$self->output->docs}) {
242 0 $proc->process($input);
243 }
244 0 $self->output->docs($proc->output->docs);
245 0 $idx++;
246 }
247  
248 0 ▶ if (defined($self->output->docs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->output->docs) 0 0 0
249 0 for my $doc (@{$self->unflatten($self->output->docs)}) {
250 0 print "$_\n" for @{$self->to_json($doc)};
251 }
252 0 $self->output->flush; # Flush output when processed
253 }
254  
255 0 return 1;
256 }
257  
258 sub to_json {
259 0 ▶ my $self = shift;
Subroutine to_json: not called CC=5 SCAR=34.0
Pod undocumented
260 0 my ($doc) = @_;
261  
262 0 ▶ $doc = ref($doc) eq 'ARRAY' ? $doc : [ $doc ];
Branch: 1 branch
BranchTrueFalseTotal
ref $doc eq 'ARRAY' ? : 0 0 0
263  
264 0 my @json = ();
265 0 for (@$doc) {
266 0 ▶ my $docs = $self->order($_) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $docs = $self->order($_) or next 0 0 0
267 0 for my $doc (@$docs) {
268 0 push @json, encode_json($doc);
269 }
270 }
271  
272 0 return \@json;
273 }
274  
275 sub from_json {
276 0 ▶ my $self = shift;
Subroutine from_json: not called CC=3 SCAR=24.8
Pod undocumented
277 0 my ($doc) = @_;
278  
279 0 ▶ $doc = ref($doc) eq 'ARRAY' ? $doc : [ $doc ];
Branch: 1 branch
BranchTrueFalseTotal
ref $doc eq 'ARRAY' ? : 0 0 0
280  
281 0 my @json = ();
282 0 push @json, decode_json($_) for @$doc;
283  
284 0 return $self->order(\@json);
285 }
286  
287 sub add_output {
288 0 ▶ my $self = shift;
Subroutine add_output: not called CC=1 SCAR=6.9
Pod undocumented
289 0 my ($doc) = @_;
290  
291 0 return push @{$self->output}, $doc;
292 }
293  
294 sub process_as_json {
295 0 ▶ my $self = shift;
Subroutine process_as_json: not called CC=3 SCAR=24.8
Pod undocumented
296 0 my ($input, $opp) = @_;
297  
298 0 ▶ croak("process: need input argument") unless defined($input);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 0 0
299 0 ▶ croak("process: need opp argument") unless defined($opp);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opp 0 0 0
300  
301 0 $input = $self->from_json($input);
302  
303 0 return $self->pipeline($input, $opp);
304 }
305  
306 sub process_as_perl {
307 0 ▶ my $self = shift;
Subroutine process_as_perl: not called CC=3 SCAR=24.8
Pod undocumented
308 0 my ($input, $opp) = @_;
309  
310 0 ▶ croak("process: need input argument") unless defined($input);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 0 0
311 0 ▶ croak("process: need opp argument") unless defined($opp);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opp 0 0 0
312  
313 0 return $self->pipeline($input, $opp);
314 }
315  
316 2 1;
317  
318 __END__
« blib/lib/OPP/Proc/Lookup.pm ↑ Summary blib/lib/Onyphe.pm »