LibTmux/Filter.pm

filter: Statementstmt 91.4% 172 / 188 Branchbran 75.3% 104 / 138 Conditioncond 61.2% 60 / 98 MC/DCmcdc 38.6% 22 / 57 Subroutinesub 95.2% 20 / 21 Podpod 100% 9 / 9 totaltotal 75.7% 387 / 511 CC 114 SCAR 37.4
CC
114
Cov
79%
_evaluate
66.1
_convert
53.7
_compile
30.7
CRAP 42.2
« LibTmux/QueryPlan.pm ↑ Summary LibTmux/ProcessJob.pm »
1 package LibTmux::Filter;
2 4 ▶use v5.44;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Scalar::Util qw(blessed refaddr reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use LibTmux::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use LibTmux::X;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 62 ▶sub new ($class, %args) {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
10 62 my $kind = $args{entity};
11 62 LibTmux::Schema->entity($kind);
12 62 my $state = {seen => {}, nodes => 0};
13 my ($tree, $where) =
14 62 _compile($kind, $args{where}, "$kind.where", $state, 0);
15 53 return bless {entity => $kind, tree => $tree, where => $where}, $class;
16 }
17 325 ▶sub entity ($self) { $self->{entity} }
Subroutine entity: called CC=1 SCAR=0.0
Pod documented
18 11 ▶sub normalized_where ($self) { dclone($self->{where}) }
Subroutine normalized_where: called CC=1 SCAR=0.0
Pod documented
19  
20 0 ▶sub register_adapter ($class, %args) {
Subroutine register_adapter: not called CC=1 SCAR=6.9
Pod documented
21 0 LibTmux::Schema->register_adapter(%args);
22 }
23  
24 7 ▶sub _bad ($path, $expected, $value) {
Subroutine _bad: called CC=1 SCAR=0.0
25 7 LibTmux::Schema->invalid($path, $expected, $value);
26 }
27  
28 81 ▶sub _compile ($kind, $input, $path, $state, $depth) {
Subroutine _compile: called CC=29 SCAR=34.2
29 81 ▶ _bad($path, 'unblessed criteria hash reference', $input)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input eq "HASH" 0 81 81
30 unless ref($input) eq 'HASH';
31 _bad($path, 'acyclic criteria of depth <=64 and <=10000 nodes', $input)
32 if $depth > 64
33 || ++$state->{nodes} > 10000
34 81 ▶ || $state->{seen}{refaddr $input};
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 64 or ++$state->{"nodes"} > 10000 or $state->{"seen"}{builtin::refaddr($input)} 1 80 81
Condition50%3/6
$depth > 64 or ++$state->{"nodes"} > 10000 or $state->{"seen"}{builtin::refaddr($input)}
l!l&&r!l&&!r
0180
$depth > 64 or ++$state->{"nodes"} > 10000
l!l&&r!l&&!r
0081
MC/DC33%1/3
$depth > 64 or ++$state->{"nodes"} > 10000 or $state->{"seen"}{builtin::refaddr($input)}
$depth > 64 ++$state->{"nodes"} > 10000 $state->{"seen"}{builtin::refaddr($input)}
Truth table50%2/4
A or B or C
A= $depth > 64B= ++$state->{"nodes"} > 10000C= $state->{"seen"}{builtin::refaddr($input)}
ABCresult
1XX1
01X1
0011
0000
35 80 local $state->{seen}{refaddr $input} = 1;
36 80 my (@children, %normalized);
37 80 for my $key (sort keys %$input) {
38 81 my $rhs = $input->{$key};
39 81 my $at = "$path.$key";
40 81 ▶ if ($key eq '-and' || $key eq '-or') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "-and" or $key eq "-or") 7 74 81
Condition100%3/3
$key eq "-and" or $key eq "-or"
l!l&&r!l&&!r
5274
MC/DC100%2/2
$key eq "-and" or $key eq "-or"
$key eq "-and" $key eq "-or"
Truth table100%3/3
A or B
A= $key eq "-and"B= $key eq "-or"
ABresult
1X1
011
000
41 7 ▶ _bad($at, 'array of criteria nodes', $rhs)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rhs eq "ARRAY" 1 6 7
42 unless ref($rhs) eq 'ARRAY';
43 6 my (@nodes, @values);
44 6 for my $i (0 .. $#$rhs) {
45 8 my ($node, $value) =
46 _compile($kind, $rhs->[$i], "$at.$i", $state, $depth + 1);
47 7 push @nodes, $node;
48 7 push @values, $value;
49 }
50 5 push @children, {tag => substr($key, 1), children => \@nodes};
51 5 $normalized{$key} = \@values;
52 5 next;
53 }
54 74 ▶ if ($key eq '-not') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "-not") 2 72 74
55 2 my ($node, $value) = _compile($kind, $rhs, $at, $state, $depth + 1);
56 1 push @children, {tag => 'not', child => $node};
57 1 $normalized{$key} = $value;
58 1 next;
59 }
60 72 my $field = LibTmux::Schema->field($kind, $key);
61 72 ▶ _bad($at, 'registered field', $key) unless $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 2 70 72
62 70 ▶ my $ops = ref($rhs) eq 'HASH' ? $rhs : {-eq => $rhs};
Branch: 1 branch
BranchTrueFalseTotal
ref $rhs eq 'HASH' ? : 32 38 70
63 70 ▶ _bad($at, 'scalar or operator hash reference', $rhs)
Branch: 1 branch
BranchTrueFalseTotal
if ref $rhs and ref $rhs ne "HASH" 2 68 70
Condition100%3/3
ref $rhs and ref $rhs ne "HASH"
!ll&&!rl&&r
36322
MC/DC100%2/2
ref $rhs and ref $rhs ne "HASH"
ref $rhs ref $rhs ne "HASH"
Truth table100%3/3
A and B
A= ref $rhsB= ref $rhs ne "HASH"
ABresult
0X0
100
111
64 if ref($rhs) && ref($rhs) ne 'HASH';
65 68 ▶ _bad($at, 'nonempty operator hash reference', $ops) unless keys %$ops;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$ops 0 68 68
66 68 my %values;
67 68 for my $op (sort keys %$ops) {
68 69 my $name = $op =~ s/^-//r;
69 _bad("$at.$op", 'allowed field operator', $op)
70 395 unless $op =~ /^-/ && CORE::grep { $_ eq $name }
71 69 ▶ $field->{operators}->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless $op =~ /^-/u and grep {$_ eq $name;} @{$$field{"operators"}} 1 68 69
Condition66%2/3
$op =~ /^-/u and grep {$_ eq $name;} @{$$field{"operators"}}
!ll&&!rl&&r
0168
MC/DC50%1/2
$op =~ /^-/u and grep {$_ eq $name;} @{$$field{"operators"}}
$op =~ /^-/u grep {$_ eq $name;} @{$$field{"operators"}}
Truth table66%2/3
A and B
A= $op =~ /^-/uB= grep {$_ eq $name;} @{$$field{"operators"}}
ABresult
0X0
100
111
72 68 my $value = $ops->{$op};
73 68 ▶ if ($field->{type} eq 'relation') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{"type"} eq "relation") 9 59 68
74 9 ▶ if (!defined($value) && !$field->{many} && $field->{nullable}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value || $field->{'many'} and $field->{'nullable'}) { } 0 9 9
Condition33%2/6
not defined $value || $field->{"many"} and $field->{"nullable"}
!ll&&!rl&&r
900
defined $value || $field->{'many'}
l!l&&r!l&&!r
900
MC/DC0%0/3
not defined $value || $field->{"many"} and $field->{"nullable"}
defined $value $field->{'many'} $field->{"nullable"}
Truth table25%1/4
not(A || B) and C
A= defined $valueB= $field->{'many'}C= $field->{"nullable"}
ABCresult
1XX0
01X0
0000
0011
75 0 push @children,
76 {
77 tag => 'relation',
78 kind => $kind,
79 field => $key,
80 spec => $field,
81 op => $name,
82 child => undef,
83 path => "$at.$op"
84 };
85 0 $values{$op} = undef;
86 }
87 else {
88 my ($node, $normal) =
89 9 _compile($field->{entity}, $value, "$at.$op", $state,
90 $depth + 1);
91 9 push @children,
92 {
93 tag => 'relation',
94 kind => $kind,
95 field => $key,
96 spec => $field,
97 op => $name,
98 child => $node,
99 path => "$at.$op"
100 };
101 9 $values{$op} = $normal;
102 }
103 9 next;
104 }
105 59 ▶ if ($name eq 'in' || $name eq 'not_in') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq 'in' or $name eq 'not_in') { } 4 55 59
Condition100%3/3
$name eq "in" or $name eq "not_in"
l!l&&r!l&&!r
3155
MC/DC100%2/2
$name eq "in" or $name eq "not_in"
$name eq "in" $name eq "not_in"
Truth table100%3/3
A or B
A= $name eq "in"B= $name eq "not_in"
ABresult
1X1
011
000
106 4 ▶ _bad("$at.$op", 'array of scalar operands', $value)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 0 4 4
107 unless ref($value) eq 'ARRAY';
108 $value =
109 4 [map { LibTmux::Schema->normalize($field, $_, "$at.$op") }
110 @$value];
111 }
112 else {
113 55 my %operand_spec = %$field;
114 55 ▶ $operand_spec{nullable} = false
Branch: 1 branch
BranchTrueFalseTotal
unless $name eq "eq" or $name eq "ne" 18 37 55
Condition100%3/3
$name eq "eq" or $name eq "ne"
l!l&&r!l&&!r
36118
MC/DC100%2/2
$name eq "eq" or $name eq "ne"
$name eq "eq" $name eq "ne"
Truth table100%3/3
A or B
A= $name eq "eq"B= $name eq "ne"
ABresult
1X1
011
000
115 unless $name eq 'eq' || $name eq 'ne';
116 55 $value = LibTmux::Schema->normalize(\%operand_spec, $value,
117 "$at.$op");
118 }
119 57 $values{$op} = $value;
120 57 push @children,
121 {
122 tag => 'scalar',
123 kind => $kind,
124 field => $key,
125 spec => $field,
126 op => $name,
127 value => $value,
128 path => "$at.$op"
129 };
130 }
131 65 ▶ $normalized{$key} = ref($rhs) eq 'HASH' ? \%values : $values{'-eq'};
Branch: 1 branch
BranchTrueFalseTotal
ref $rhs eq 'HASH' ? : 29 36 65
132 }
133 70 return ({tag => 'and', children => \@children}, \%normalized);
134 }
135  
136 140 ▶sub matches ($self, $record) {
Subroutine matches: called CC=2 SCAR=6.9
Pod documented
137 140 LibTmux::Schema->assert_record($self->entity, $record, $self->entity);
138 140 ▶ return _evaluate($self->{tree}, $record) ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
_evaluate($self->{'tree'}, $record) ? : 71 65 136
139 }
140  
141 89 ▶sub _equal ($spec, $left, $right) {
Subroutine _equal: called CC=6 SCAR=17.9
142 89 ▶ return !defined($left) && !defined($right)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $left and defined $right 6 83 89
Condition83%5/6
defined $left and defined $right
!ll&&!rl&&r
4283
defined $left || defined $right
l!l&&r!l&&!r
204
MC/DC100%2/2
defined $left and defined $right
defined $left defined $right
MC/DC50%1/2
defined $left || defined $right
defined $left defined $right
Truth table100%3/3
A and B
A= defined $leftB= defined $right
ABresult
0X0
100
111
Truth table66%2/3
A || B
A= defined $leftB= defined $right
ABresult
1X1
011
000
143 if !defined($left) || !defined($right);
144 return $left == $right
145 83 ▶ if $spec->{type} eq 'integer' || $spec->{type} eq 'boolean';
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"type"} eq "integer" or $spec->{"type"} eq "boolean" 61 22 83
Condition100%3/3
$spec->{"type"} eq "integer" or $spec->{"type"} eq "boolean"
l!l&&r!l&&!r
35822
MC/DC100%2/2
$spec->{"type"} eq "integer" or $spec->{"type"} eq "boolean"
$spec->{"type"} eq "integer" $spec->{"type"} eq "boolean"
Truth table100%3/3
A or B
A= $spec->{"type"} eq "integer"B= $spec->{"type"} eq "boolean"
ABresult
1X1
011
000
146 22 return $left eq $right;
147 }
148  
149 329 ▶sub _evaluate ($node, $record) {
Subroutine _evaluate: called CC=48 SCAR=41.9
150 329 my $tag = $node->{tag};
151 329 ▶ if ($tag eq 'and') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "and") 172 157 329
152 172 for my $child ($node->{children}->@*) {
153 169 ▶ return false unless _evaluate($child, $record);
Branch: 1 branch
BranchTrueFalseTotal
unless _evaluate($child, $record) 79 84 163
154 }
155 87 return true;
156 }
157 157 ▶ if ($tag eq 'or') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "or") 3 154 157
158 3 for my $child ($node->{children}->@*) {
159 0 ▶ return true if _evaluate($child, $record);
Branch: 1 branch
BranchTrueFalseTotal
if _evaluate($child, $record) 0 0 0
160 }
161 3 return false;
162 }
163 154 ▶ return !_evaluate($node->{child}, $record) if $tag eq 'not';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "not" 3 151 154
164 151 my $op = $node->{op};
165 151 ▶ if ( $tag eq 'scalar'
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "scalar" and $op eq "in" || $op eq "not_in" and not @{$node->{"value"}}) 7 144 151
Condition100%9/9
$tag eq "scalar" and $op eq "in" || $op eq "not_in" and not @{$node->{"value"}}
!ll&&!rl&&r
14317
$tag eq "scalar" and $op eq "in" || $op eq "not_in"
!ll&&!rl&&r
161278
$op eq "in" or $op eq "not_in"
l!l&&r!l&&!r
53127
MC/DC100%4/4
$tag eq "scalar" and $op eq "in" || $op eq "not_in" and not @{$node->{"value"}}
$tag eq "scalar" $op eq "in" $op eq "not_in" not @{$node->{"value"}}
Truth table83%5/6
A and B or C and not(D)
A= $tag eq "scalar"B= $op eq "in"C= $op eq "not_in"D= not @{$node->{"value"}}
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
166 && ($op eq 'in' || $op eq 'not_in')
167 && !$node->{value}->@*)
168 {
169 7 return $op eq 'not_in';
170 }
171 my $left = LibTmux::Schema->read($node->{kind}, $record, $node->{field},
172 144 $node->{path});
173 140 ▶ if ($tag eq 'relation') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "relation") 15 125 140
174 15 my $spec = $node->{spec};
175 15 ▶ if (!$spec->{many}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec->{"many"}) 6 9 15
176 _bad($node->{path}, 'non-null relation', $left)
177 6 ▶ if !defined($left) && !$spec->{nullable};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $left or $spec->{"nullable"} 0 6 6
Condition33%1/3
defined $left or $spec->{"nullable"}
l!l&&r!l&&!r
600
MC/DC0%0/2
defined $left or $spec->{"nullable"}
defined $left $spec->{"nullable"}
Truth table33%1/3
A or B
A= defined $leftB= $spec->{"nullable"}
ABresult
1X1
011
000
178 my $match =
179 !defined($node->{child})
180 ? !defined($left)
181 6 ▶ : defined($left) ? do {
Branch: 2 branches
BranchTrueFalseTotal
!defined($node->{'child'}) ? : 0 6 6
defined $left ? : 6 0 6
182 LibTmux::Schema->assert_record($spec->{entity}, $left,
183 6 $node->{path});
184 6 _evaluate($node->{child}, $left);
185 }
186 : false;
187 6 ▶ return $op eq 'is_not' ? !$match : !!$match;
Branch: 1 branch
BranchTrueFalseTotal
$op eq 'is_not' ? : 3 3 6
188 }
189 9 ▶ _bad($node->{path}, 'loaded relation array', $left)
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::reftype($left) // "") eq "ARRAY" 0 9 9
Condition50%1/2
builtin::reftype($left) // ""
l!l
90
MC/DC0%0/1
builtin::reftype($left) // ""
builtin::reftype($left)
Truth table50%1/2
A
A= builtin::reftype($left)
Aresult
11
00
190 unless (reftype($left) // '') eq 'ARRAY';
191 9 for my $i (0 .. $#$left) {
192 16 ▶ _bad($node->{path}, 'defined relation member', $left->[$i])
Branch: 1 branch
BranchTrueFalseTotal
unless exists $left->[$i] and defined $left->[$i] 0 16 16
Condition33%1/3
exists $left->[$i] and defined $left->[$i]
!ll&&!rl&&r
0016
MC/DC0%0/2
exists $left->[$i] and defined $left->[$i]
exists $left->[$i] defined $left->[$i]
Truth table33%1/3
A and B
A= exists $left->[$i]B= defined $left->[$i]
ABresult
0X0
100
111
193 unless exists($left->[$i]) && defined($left->[$i]);
194 LibTmux::Schema->assert_record($spec->{entity}, $left->[$i],
195 16 $node->{path});
196 }
197 9 for my $child (@$left) {
198 11 my $match = _evaluate($node->{child}, $child);
199 11 ▶ return true if $op eq 'some' && $match;
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "some" and $match 6 5 11
Condition66%2/3
$op eq "some" and $match
!ll&&!rl&&r
056
MC/DC50%1/2
$op eq "some" and $match
$op eq "some" $match
Truth table66%2/3
A and B
A= $op eq "some"B= $match
ABresult
0X0
100
111
200 5 ▶ return false if $op eq 'every' && !$match;
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "every" and not $match 0 5 5
Condition33%1/3
$op eq "every" and not $match
!ll&&!rl&&r
500
MC/DC0%0/2
$op eq "every" and not $match
$op eq "every" not $match
Truth table33%1/3
A and not(B)
A= $op eq "every"B= not $match
ABresult
0X0
100
111
201 5 ▶ return false if $op eq 'none' && $match;
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "none" and $match 0 5 5
Condition33%1/3
$op eq "none" and $match
!ll&&!rl&&r
500
MC/DC0%0/2
$op eq "none" and $match
$op eq "none" $match
Truth table33%1/3
A and B
A= $op eq "none"B= $match
ABresult
0X0
100
111
202 }
203 3 return $op ne 'some';
204 }
205 125 my $right = $node->{value};
206 125 ▶ return _equal($node->{spec}, $left, $right) if $op eq 'eq';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "eq" 85 40 125
207 40 ▶ return !_equal($node->{spec}, $left, $right) if $op eq 'ne';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "ne" 3 37 40
208 37 ▶ if ($op eq 'in' || $op eq 'not_in') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "in" or $op eq "not_in") 1 36 37
Condition66%2/3
$op eq "in" or $op eq "not_in"
l!l&&r!l&&!r
1036
MC/DC50%1/2
$op eq "in" or $op eq "not_in"
$op eq "in" $op eq "not_in"
Truth table66%2/3
A or B
A= $op eq "in"B= $op eq "not_in"
ABresult
1X1
011
000
209 1 my $found = false;
210 1 for my $item (@$right) {
211 1 ▶ if (_equal($node->{spec}, $left, $item)) { $found = true; last }
Branch: 1 branch
BranchTrueFalseTotal
if (_equal($node->{"spec"}, $left, $item)) 1 0 1
212 }
213 1 ▶ return $op eq 'in' ? $found : !$found;
Branch: 1 branch
BranchTrueFalseTotal
$op eq 'in' ? : 1 0 1
214 }
215 36 ▶ return false unless defined $left;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $left 2 34 36
216 34 ▶ return $left < $right if $op eq 'lt';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "lt" 0 34 34
217 34 ▶ return $left <= $right if $op eq 'lte';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "lte" 0 34 34
218 34 ▶ return $left > $right if $op eq 'gt';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "gt" 4 30 34
219 30 ▶ return $left >= $right if $op eq 'gte';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "gte" 4 26 30
220 26 ▶ return index($left, $right) >= 0 if $op eq 'contains';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "contains" 20 6 26
221 6 ▶ return index($left, $right) == 0 if $op eq 'starts_with';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "starts_with" 3 3 6
222 3 ▶ return !length($right)
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "ends_with" 3 0 3
Condition66%4/6
!length($right) || length $left >= length $right && substr($left, -length($right)) eq $right
l!l&&r!l&&!r
021
length $left >= length $right and substr($left, -length($right)) eq $right
!ll&&!rl&&r
102
MC/DC33%1/3
!length($right) || length $left >= length $right && substr($left, -length($right)) eq $right
!length($right) length $left >= length $right substr($left, -length($right)) eq $right
Truth table50%2/4
not(A) || B and C
A= !length($right)B= length $left >= length $rightC= substr($left, -length($right)) eq $right
ABCresult
1XX1
0111
00X0
0100
223 || (length($left) >= length($right)
224 && substr($left, -length($right)) eq $right)
225 if $op eq 'ends_with';
226 0 die 'unreachable compiled operator';
227 }
228 10 ▶sub _wire_op ($name) { $name =~ s/^-//; $name =~ s/_([a-z])/uc($1)/ge; $name }
Subroutine _wire_op: called CC=1 SCAR=0.0
229  
230 4 ▶sub _convert ($kind, $input, $decode, $path, $depth = 0) {
Subroutine _convert: called CC=26 SCAR=39.8
231 4 ▶ _bad($path, 'criteria object of depth <=64', $input)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input eq "HASH" and $depth <= 64 0 4 4
Condition33%1/3
ref $input eq "HASH" and $depth <= 64
!ll&&!rl&&r
004
MC/DC0%0/2
ref $input eq "HASH" and $depth <= 64
ref $input eq "HASH" $depth <= 64
Truth table33%1/3
A and B
A= ref $input eq "HASH"B= $depth <= 64
ABresult
0X0
100
111
232 unless ref($input) eq 'HASH' && $depth <= 64;
233 4 my %out;
234 4 my $fields = LibTmux::Schema->entity($kind)->{fields};
235 my %names =
236 4 ▶ map { ($decode ? $fields->{$_}{wire} : $_) => $_ } keys %$fields;
Branch: 1 branch
BranchTrueFalseTotal
$decode ? : 13 39 52
237 4 for my $key (sort keys %$input) {
238 7 ▶ my $logical =
Branch: 1 branch
BranchTrueFalseTotal
$decode && $key =~ /\A(and|or|not)\z/u ? : 0 7 7
Condition66%2/3
$decode and $key =~ /\A(and|or|not)\z/u
!ll&&!rl&&r
520
MC/DC0%0/2
$decode and $key =~ /\A(and|or|not)\z/u
$decode $key =~ /\A(and|or|not)\z/u
Truth table66%2/3
A and B
A= $decodeB= $key =~ /\A(and|or|not)\z/u
ABresult
0X0
100
111
239 $decode && $key =~ /\A(and|or|not)\z/ ? '-' . $key : undef;
240 7 ▶ $logical = $key if !$decode && $key =~ /\A-(and|or|not)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if not $decode and $key =~ /\A-(and|or|not)\z/u 0 7 7
Condition66%2/3
not $decode and $key =~ /\A-(and|or|not)\z/u
!ll&&!rl&&r
250
MC/DC0%0/2
not $decode and $key =~ /\A-(and|or|not)\z/u
not $decode $key =~ /\A-(and|or|not)\z/u
Truth table66%2/3
not(A) and B
A= not $decodeB= $key =~ /\A-(and|or|not)\z/u
ABresult
0X0
100
111
241 7 ▶ if (defined $logical) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $logical) 0 7 7
242 0 my $value = $input->{$key};
243 0 ▶ my $dest = $decode ? $logical : substr($logical, 1);
Branch: 1 branch
BranchTrueFalseTotal
$decode ? : 0 0 0
244 0 ▶ if ($logical eq '-not') {
Branch: 1 branch
BranchTrueFalseTotal
if ($logical eq '-not') { } 0 0 0
245 0 $out{$dest} =
246 _convert($kind, $value, $decode, "$path.$key", $depth + 1);
247 }
248 else {
249 0 ▶ _bad("$path.$key", 'array of criteria', $value)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 0 0 0
250 unless ref($value) eq 'ARRAY';
251 $out{$dest} = [
252 map {
253 0 _convert($kind, $_, $decode, "$path.$key", $depth + 1)
254 } @$value
255 ];
256 }
257 0 next;
258 }
259 7 my $fieldname = $names{$key};
260 7 ▶ _bad("$path.$key", 'registered wire field', $key)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fieldname 0 7 7
261 unless defined $fieldname;
262 7 my $field = $fields->{$fieldname};
263 7 ▶ my $dest = $decode ? $fieldname : $field->{wire};
Branch: 1 branch
BranchTrueFalseTotal
$decode ? : 2 5 7
264 7 my $value = $input->{$key};
265 7 ▶ if (ref($value) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 3 4 7
266 3 my %ops;
267 24 ▶ my %opnames = map { ($decode ? _wire_op($_) : '-' . $_) => $_ }
Branch: 1 branch
BranchTrueFalseTotal
$decode ? : 8 16 24
268 3 $field->{operators}->@*;
269 3 for my $operator (sort keys %$value) {
270 3 my $op = $opnames{$operator};
271 3 ▶ _bad("$path.$key.$operator", 'registered operator', $operator)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op 0 3 3
272 unless defined $op;
273 3 my $v = $value->{$operator};
274 3 ▶ if ($field->{type} eq 'relation' && defined $v) {
Branch: 2 branches
BranchTrueFalseTotal
if ($field->{'type'} eq 'relation' and defined $v) { } 0 3 3
elsif (not $decode) { } 2 1 3
Condition33%1/3
$field->{"type"} eq "relation" and defined $v
!ll&&!rl&&r
300
MC/DC0%0/2
$field->{"type"} eq "relation" and defined $v
$field->{"type"} eq "relation" defined $v
Truth table33%1/3
A and B
A= $field->{"type"} eq "relation"B= defined $v
ABresult
0X0
100
111
275 0 $v = _convert($field->{entity}, $v, $decode,
276 "$path.$key.$operator", $depth + 1);
277 }
278 elsif (!$decode) {
279 $v =
280 ref($v) eq 'ARRAY'
281 ? [
282 0 ▶ map { LibTmux::Schema->wire_scalar($field, $_, $path) }
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 0 2 2
283 @$v
284 ]
285 : LibTmux::Schema->wire_scalar($field, $v, $path);
286 }
287 3 ▶ $ops{$decode ? '-' . $op : _wire_op($op)} = $v;
Branch: 1 branch
BranchTrueFalseTotal
$decode ? : 1 2 3
288 }
289 3 $out{$dest} = \%ops;
290 }
291 else {
292 4 ▶ $out{$dest} =
Branch: 1 branch
BranchTrueFalseTotal
$decode ? : 1 3 4
293 $decode
294 ? $value
295 : LibTmux::Schema->wire_scalar($field, $value, $path);
296 }
297 }
298 3 return \%out;
299 }
300  
301 3 ▶sub to_data ($self) {
Subroutine to_data: called CC=1 SCAR=0.0
Pod documented
302 return {
303 schema => 'libtmux.where/v1',
304 entity => $self->entity,
305 where => _convert(
306 3 $self->entity, $self->{where}, false, $self->entity . '.where'
307 )
308 };
309 }
310  
311 2 ▶sub to_json ($self) {
Subroutine to_json: called CC=1 SCAR=0.0
Pod documented
312 2 JSON::PP->new->canonical->utf8->core_bools->encode($self->to_data);
313 }
314  
315 1 ▶sub from_data ($class, $data) {
Subroutine from_data: called CC=8 SCAR=28.4
Pod documented
316 _bad('wire', 'versioned filter envelope', $data)
317 unless ref($data) eq 'HASH'
318 && keys(%$data) == 3
319 && exists($data->{schema})
320 && exists($data->{entity})
321 1 ▶ && exists($data->{where});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" and keys %$data == 3 and exists $data->{"schema"} and exists $data->{"entity"} and exists $data->{"where"} 0 1 1
Condition33%4/12
ref $data eq "HASH" and keys %$data == 3 and exists $data->{"schema"} and exists $data->{"entity"} and exists $data->{"where"}
!ll&&!rl&&r
001
ref $data eq "HASH" and keys %$data == 3 and exists $data->{"schema"} and exists $data->{"entity"}
!ll&&!rl&&r
001
ref $data eq "HASH" and keys %$data == 3 and exists $data->{"schema"}
!ll&&!rl&&r
001
ref $data eq "HASH" and keys %$data == 3
!ll&&!rl&&r
001
MC/DC0%0/5
ref $data eq "HASH" and keys %$data == 3 and exists $data->{"schema"} and exists $data->{"entity"} and exists $data->{"where"}
ref $data eq "HASH" keys %$data == 3 exists $data->{"schema"} exists $data->{"entity"} exists $data->{"where"}
Truth table16%1/6
A and B and C and D and E
A= ref $data eq "HASH"B= keys %$data == 3C= exists $data->{"schema"}D= exists $data->{"entity"}E= exists $data->{"where"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
322 _bad('wire.schema', 'libtmux.where/v1', $data->{schema})
323 1 ▶ unless !ref($data->{schema}) && $data->{schema} eq 'libtmux.where/v1';
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $data->{"schema"} and $data->{"schema"} eq "libtmux.where/v1" 0 1 1
Condition33%1/3
not ref $data->{"schema"} and $data->{"schema"} eq "libtmux.where/v1"
!ll&&!rl&&r
001
MC/DC0%0/2
not ref $data->{"schema"} and $data->{"schema"} eq "libtmux.where/v1"
not ref $data->{"schema"} $data->{"schema"} eq "libtmux.where/v1"
Truth table33%1/3
not(A) and B
A= not ref $data->{"schema"}B= $data->{"schema"} eq "libtmux.where/v1"
ABresult
0X0
100
111
324 1 LibTmux::Schema->entity($data->{entity});
325 return $class->new(
326 entity => $data->{entity},
327 1 where => _convert($data->{entity}, $data->{where}, true, 'wire.where')
328 );
329 }
330  
331 1 ▶sub from_json ($class, $bytes) {
Subroutine from_json: called CC=1 SCAR=0.1
Pod documented
332 1 my $data;
333 1 try {
334 1 $data = JSON::PP->new->utf8->core_bools->max_depth(64)->decode($bytes)
335 }
336 0 catch ($e) { _bad('wire', 'valid JSON envelope', $bytes) }
337 1 return $class->from_data($data);
338 }
339  
340 =head1 NAME
341  
342 LibTmux::Filter - Validated, reusable local criteria and explicit wire data
343  
344 =head1 SYNOPSIS
345  
346 use v5.44;
347 use LibTmux::Filter;
348 my $filter = LibTmux::Filter->new(
349 entity => 'pane', where => {width => {-gte => 120}},
350 );
351 my $matches = $snapshot->panes->where($filter);
352  
353 =head1 CONTRACT
354  
355 Compilation validates the entire tree and copies operands. Hash keys evaluate
356 lexically; logical arrays retain input order. Missing reads throw; known-null
357 ordering is false. String operators are literal. Relations use some/every/none
358 and is/is_not with explicit dash prefixes in Perl data.
359  
360 C<matches> accepts entities, plain records, and registered adapters.
361 C<to_data>/C<to_json> export copies in libtmux.where/v1; C<from_data>/C<from_json>
362 validate input. Regex/coderefs/cycles and non-UTF-8 byte-path wire operands are
363 refused. Perl regex remains available through Selection C<grep>.
364  
365 =head1 METHODS
366  
367 =head2 new
368  
369 C<new(entity =E<gt> kind, where =E<gt> $hashref)> validates the complete criteria tree and owns a normalized copy. Schemas choose comparison types; no scalar-history coercion or arbitrary method dispatch occurs. Errors throw L<InvalidFilter|LibTmux::X/InvalidFilter> with precise paths, even for unreachable branches.
370  
371 =head2 entity
372  
373 Return the declared entity-kind string. It remains known for an empty L<LibTmux::Selection>.
374  
375 =head2 normalized_where
376  
377 Return an independent copy of normalized Perl criteria. Editing it does not change the compiled filter.
378  
379 =head2 matches
380  
381 C<matches($entity_or_record)> returns a native Boolean. Accept captured entities, plain hash records, or registered adapters of the declared kind. Missing required reads throw L<Unloaded|LibTmux::X/Unloaded>; they never become null or false through negation. Evaluation performs no I/O.
382  
383 =head2 register_adapter
384  
385 C<register_adapter(class =E<gt> name, entity =E<gt> kind, readers =E<gt> { field =E<gt> coderef })> registers trusted field readers without inheritance. Only declared fields are accepted. Readers are executable local code; they never enter portable criteria. See L<LibTmux::Schema>.
386  
387 =head2 to_data
388  
389 Return a fresh C<libtmux.where/v1> envelope using canonical wire field/operator names. Normalize strings, numbers, native booleans, null, objects, and arrays by schema. Aliases are currently absent; one canonical name is encoded.
390  
391 =head2 to_json
392  
393 Return canonical-key UTF-8 JSON octets using L<JSON::PP>. Non-UTF-8 opaque path bytes throw L<UnsupportedValue|LibTmux::X/UnsupportedValue>. This is reproducible key order, not semantic canonicalization of arbitrary Boolean trees.
394  
395 =head2 from_data
396  
397 C<from_data($envelope)> validates the version, entity, fields, operators, and operands, then returns a compiled filter. Reject executable values, unknown versions, cycles, and unsupported regex nodes.
398  
399 =head2 from_json
400  
401 C<from_json($json_octets)> decodes bounded JSON using core booleans and calls L</from_data>. Invalid JSON throws L<InvalidFilter|LibTmux::X/InvalidFilter>.
402  
403 =head1 EXECUTION
404  
405 All methods in this module operate on local captured data and perform no tmux
406 I/O. Ordering is deterministic; public query methods do not mutate membership.
407 Typed validation/evaluation errors propagate. See L<LibTmux::Server/snapshot>
408 for explicit acquisition and L<LibTmux::X> for machine-readable failures.
409  
410 =cut
« LibTmux/QueryPlan.pm ↑ Summary LibTmux/ProcessJob.pm »