LibTmux/Schema.pm

filter: Statementstmt 84.0% 84 / 100 Branchbran 56.9% 41 / 72 Conditioncond 41.4% 17 / 41 MC/DCmcdc 20.8% 5 / 24 Subroutinesub 94.7% 18 / 19 Podpod 8.3% 1 / 12 totaltotal 61.9% 166 / 268 CC 54 SCAR 28.2
CC
54
Cov
67%
register_adapter
56.0
normalize
26.1
assert_record
19.6
CRAP 16.8
« LibTmux/Snapshot.pm ↑ Summary LibTmux/Codec.pm »
1 package LibTmux::Schema;
2 4 ▶use v5.44;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Encode qw(encode decode FB_CROAK LEAVE_SRC);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use LibTmux::X;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 my ($catalog, %adapters);
9  
10 760 ▶sub catalog ($class) {
Subroutine catalog: called CC=3 SCAR=11.0
Pod undocumented
11 760 ▶ unless ($catalog) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($catalog) 4 756 760
12 4 ▶ open my $in, '<:raw', dirname(__FILE__) . '/schema.json'
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<:raw", dirname(__FILE__) . '/schema.json' or die "Read schema: $!" 4 0 4
13 or die "Read schema: $!";
14 4 local $/;
15 4 $catalog = JSON::PP->new->utf8->decode(<$in>);
16 }
17 760 return $catalog;
18 }
19  
20 158 ▶sub _version_at_least ($class, $version, $minimum) {
Subroutine _version_at_least: called CC=5 SCAR=17.0
21 158 my @got = $version =~ /(\d+)\.(\d+)([a-z]?)/;
22 158 my @want = $minimum =~ /(\d+)\.(\d+)([a-z]?)/;
23 158 ▶ return false unless @got == 3 && @want == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @got == 3 and @want == 3 0 158 158
Condition33%1/3
@got == 3 and @want == 3
!ll&&!rl&&r
00158
MC/DC0%0/2
@got == 3 and @want == 3
@got == 3 @want == 3
Truth table33%1/3
A and B
A= @got == 3B= @want == 3
ABresult
0X0
100
111
24 158 ▶ return $got[0] > $want[0] if $got[0] != $want[0];
Branch: 1 branch
BranchTrueFalseTotal
if $got[0] != $want[0] 0 158 158
25 158 ▶ return $got[1] > $want[1] if $got[1] != $want[1];
Branch: 1 branch
BranchTrueFalseTotal
if $got[1] != $want[1] 157 1 158
26 1 return $got[2] ge $want[2];
27 }
28  
29 723 ▶sub entity ($class, $kind) {
Subroutine entity: called CC=4 SCAR=15.6
Pod undocumented
30 723 ▶ LibTmux::X::InvalidFilter->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kind and not ref $kind 0 723 723
Condition33%1/3
defined $kind and not ref $kind
!ll&&!rl&&r
00723
MC/DC0%0/2
defined $kind and not ref $kind
defined $kind not ref $kind
Truth table33%1/3
A and not(B)
A= defined $kindB= not ref $kind
ABresult
0X0
100
111
31 message => 'entity kind must be a defined string')
32 unless defined($kind) && !ref($kind);
33 723 my $spec = $class->catalog->{entities}{$kind};
34 723 ▶ LibTmux::X::InvalidFilter->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $spec 0 723 723
35 message => 'unknown entity',
36 entity => $kind
37 ) unless $spec;
38 723 return $spec;
39 }
40 656 ▶sub field ($class, $kind, $field) { $class->entity($kind)->{fields}{$field} }
Subroutine field: called CC=1 SCAR=0.0
Pod undocumented
41  
42 330 ▶sub class_for ($class, $kind) {
Subroutine class_for: called CC=1 SCAR=0.0
Pod undocumented
43 330 'LibTmux::' . join('', map { ucfirst } split /_/, $kind);
44 }
45 86 ▶sub selection_for ($class, $kind) { $class->class_for($kind) . 'Selection' }
Subroutine selection_for: called CC=1 SCAR=0.0
Pod undocumented
46  
47 9 ▶sub type_name ($class, $value) {
Subroutine type_name: called CC=4 SCAR=13.9
Pod undocumented
48 9 ▶ defined($value) ? blessed($value) // reftype($value) // 'scalar' : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 9 0 9
Condition100%5/5
(builtin::blessed($value) // builtin::reftype($value)) // "scalar"
l!l
45
builtin::blessed($value) // builtin::reftype($value)
l!l&&r!l&&!r
135
MC/DC100%2/2
(builtin::blessed($value) // builtin::reftype($value)) // "scalar"
builtin::blessed($value) builtin::reftype($value)
Truth table100%3/3
A // B
A= builtin::blessed($value)B= builtin::reftype($value)
ABresult
1X1
011
000
49 }
50  
51 9 ▶sub invalid ($class, $path, $expected, $value, $type = 'InvalidFilter') {
Subroutine invalid: called CC=3 SCAR=11.1
Pod undocumented
52 9 my $error = "LibTmux::X::$type";
53 $error->throw(
54 path => $path,
55 9 entity => do {
56 9 my ($kind) = split /\./, $path;
57 9 ▶ exists($class->catalog->{entities}{$kind}) ? $kind : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $class->catalog->{'entities'}{$kind} ? : 9 0 9
58 },
59 expected => $expected,
60 received => $class->type_name($value),
61 message => "expected $expected"
62 );
63 }
64  
65 201 ▶sub normalize ($class, $field, $value, $path, $error = 'InvalidFilter') {
Subroutine normalize: called CC=19 SCAR=32.6
Pod undocumented
66 201 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 8 193 201
67 8 ▶ return undef if $field->{nullable};
Branch: 1 branch
BranchTrueFalseTotal
if $field->{"nullable"} 8 0 8
68 0 $class->invalid($path, 'defined ' . $field->{type}, $value, $error);
69 }
70 193 ▶ $class->invalid($path, $field->{type}, $value, $error) if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 193 193
71 193 my $type = $field->{type};
72 193 ▶ if ($type eq 'integer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "integer") 39 154 193
73 39 my $text = "$value";
74 39 ▶ $class->invalid($path, 'safe decimal integer', $value, $error)
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\A[+-]?[0-9]+\z/u 2 37 39
75 unless $text =~ /\A[+-]?[0-9]+\z/;
76 37 my $negative = $text =~ s/^-//;
77 37 $text =~ s/^\+//;
78 37 $text =~ s/^0+(?=[0-9])//;
79 37 ▶ $class->invalid($path, 'integer within +/-9007199254740991',
Branch: 1 branch
BranchTrueFalseTotal
if length $text > 16 or length $text == 16 and $text gt "9007199254740991" 0 37 37
Condition33%2/6
length $text > 16 or length $text == 16 and $text gt "9007199254740991"
l!l&&r!l&&!r
0037
length $text == 16 and $text gt "9007199254740991"
!ll&&!rl&&r
3700
MC/DC0%0/3
length $text > 16 or length $text == 16 and $text gt "9007199254740991"
length $text > 16 length $text == 16 $text gt "9007199254740991"
Truth table25%1/4
A or B and C
A= length $text > 16B= length $text == 16C= $text gt "9007199254740991"
ABCresult
1XX1
0111
00X0
0100
80 $value, $error)
81 if length($text) > 16
82 || (length($text) == 16 && $text gt '9007199254740991');
83 37 ▶ return 0 + ($negative ? "-$text" : $text);
Branch: 1 branch
BranchTrueFalseTotal
$negative ? : 0 37 37
84 }
85 154 ▶ if ($type eq 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "boolean") 51 103 154
86 51 ▶ $class->invalid($path, 'Boolean or 0/1', $value, $error)
Branch: 1 branch
BranchTrueFalseTotal
unless JSON::PP::is_bool($value) or "$value" eq 0 or "$value" eq 1 0 51 51
Condition33%2/6
JSON::PP::is_bool($value) or "$value" eq 0 or "$value" eq 1
l!l&&r!l&&!r
5100
JSON::PP::is_bool($value) or "$value" eq 0
l!l&&r!l&&!r
5100
MC/DC0%0/3
JSON::PP::is_bool($value) or "$value" eq 0 or "$value" eq 1
JSON::PP::is_bool($value) "$value" eq 0 "$value" eq 1
Truth table25%1/4
A or B or C
A= JSON::PP::is_bool($value)B= "$value" eq 0C= "$value" eq 1
ABCresult
1XX1
01X1
0011
0000
87 unless JSON::PP::is_bool($value)
88 || "$value" eq '0'
89 || "$value" eq '1';
90 51 ▶ return $value ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 33 18 51
91 }
92 103 my $text = "$value";
93 103 ▶ if ($type eq 'bytes') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "bytes") 11 92 103
94 11 ▶ return utf8::is_utf8($text)
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($text) ? : 0 11 11
95 ? encode('UTF-8', $text, FB_CROAK | LEAVE_SRC)
96 : $text;
97 }
98 92 ▶ if (!utf8::is_utf8($text)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::is_utf8($text)) 92 0 92
99 92 try { $text = decode('UTF-8', $text, FB_CROAK | LEAVE_SRC) }
100 catch ($e) {
101 0 $class->invalid($path, 'valid UTF-8 text', $value, $error)
102 }
103 }
104 92 return $text;
105 }
106  
107 5 ▶sub wire_scalar ($class, $field, $value, $path) {
Subroutine wire_scalar: called CC=3 SCAR=11.2
Pod undocumented
108 5 ▶ return $value unless defined $value && $field->{type} eq 'bytes';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $field->{"type"} eq "bytes" 4 1 5
Condition66%2/3
defined $value and $field->{"type"} eq "bytes"
!ll&&!rl&&r
041
MC/DC50%1/2
defined $value and $field->{"type"} eq "bytes"
defined $value $field->{"type"} eq "bytes"
Truth table66%2/3
A and B
A= defined $valueB= $field->{"type"} eq "bytes"
ABresult
0X0
100
111
109 1 my $text;
110 1 try { $text = decode('UTF-8', $value, FB_CROAK | LEAVE_SRC) }
111 catch ($e) {
112 1 LibTmux::X::UnsupportedValue->throw(
113 path => $path,
114 message =>
115 'non-UTF-8 octets have no string representation in libtmux.where/v1'
116 )
117 }
118 0 return $text;
119 }
120  
121 0 ▶sub register_adapter ($class, %args) {
Subroutine register_adapter: not called CC=7 SCAR=40.3
Pod documented
122 0 $class->entity($args{entity});
123 $class->invalid('adapter.class', 'class name', $args{class})
124 unless defined $args{class}
125 0 ▶ && $args{class} =~ /\A[A-Za-z_][A-Za-z0-9_:]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"class"} and $args{"class"} =~ /\A[A-Za-z_][A-Za-z0-9_:]*\z/u 0 0 0
Condition0%0/3
defined $args{"class"} and $args{"class"} =~ /\A[A-Za-z_][A-Za-z0-9_:]*\z/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"class"} and $args{"class"} =~ /\A[A-Za-z_][A-Za-z0-9_:]*\z/u
defined $args{"class"} $args{"class"} =~ /\A[A-Za-z_][A-Za-z0-9_:]*\z/u
Truth table0%0/3
A and B
A= defined $args{"class"}B= $args{"class"} =~ /\A[A-Za-z_][A-Za-z0-9_:]*\z/u
ABresult
0X0
100
111
126 $class->invalid('adapter.readers', 'unblessed hash reference',
127 $args{readers})
128 0 ▶ unless ref($args{readers}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"readers"} eq "HASH" 0 0 0
129 0 for my $field (keys $args{readers}->%*) {
130 $class->invalid(
131 "adapter.$field",
132 'registered field reader',
133 $args{readers}{$field}
134 )
135 unless $class->field($args{entity}, $field)
136 0 ▶ && ref($args{readers}{$field}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless $class->field($args{"entity"}, $field) and ref $args{"readers"}{$field} eq "CODE" 0 0 0
Condition0%0/3
$class->field($args{"entity"}, $field) and ref $args{"readers"}{$field} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$class->field($args{"entity"}, $field) and ref $args{"readers"}{$field} eq "CODE"
$class->field($args{"entity"}, $field) ref $args{"readers"}{$field} eq "CODE"
Truth table0%0/3
A and B
A= $class->field($args{"entity"}, $field)B= ref $args{"readers"}{$field} eq "CODE"
ABresult
0X0
100
111
137 }
138 $adapters{$args{class}} =
139 0 {entity => $args{entity}, readers => {%{$args{readers}}}};
140 }
141  
142 306 ▶sub assert_record ($class, $kind, $record, $path) {
Subroutine assert_record: called CC=8 SCAR=29.7
Pod undocumented
143 306 ▶ if (blessed($record) && $record->isa('LibTmux::Entity')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($record) and $record->isa("LibTmux::Entity")) 301 5 306
Condition66%2/3
builtin::blessed($record) and $record->isa("LibTmux::Entity")
!ll&&!rl&&r
50301
MC/DC50%1/2
builtin::blessed($record) and $record->isa("LibTmux::Entity")
builtin::blessed($record) $record->isa("LibTmux::Entity")
Truth table66%2/3
A and B
A= builtin::blessed($record)B= $record->isa("LibTmux::Entity")
ABresult
0X0
100
111
144 301 ▶ $class->invalid($path, "$kind entity", $record)
Branch: 1 branch
BranchTrueFalseTotal
unless $record->kind eq $kind 0 301 301
145 unless $record->kind eq $kind;
146 301 return;
147 }
148 5 ▶ return if ref($record) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $record eq "HASH" 5 0 5
149 0 ▶ my $adapter = blessed($record) ? $adapters{ref $record} : undef;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($record) ? : 0 0 0
150 0 ▶ return if $adapter && $adapter->{entity} eq $kind;
Branch: 1 branch
BranchTrueFalseTotal
if $adapter and $adapter->{"entity"} eq $kind 0 0 0
Condition0%0/3
$adapter and $adapter->{"entity"} eq $kind
!ll&&!rl&&r
000
MC/DC0%0/2
$adapter and $adapter->{"entity"} eq $kind
$adapter $adapter->{"entity"} eq $kind
Truth table0%0/3
A and B
A= $adapterB= $adapter->{"entity"} eq $kind
ABresult
0X0
100
111
151 0 $class->invalid($path, "$kind record or registered adapter", $record);
152 }
153  
154 144 ▶sub read ($class, $kind, $record, $field, $path) {
Subroutine read: called CC=7 SCAR=22.3
Pod undocumented
155 144 $class->assert_record($kind, $record, $path);
156 144 my $spec = $class->field($kind, $field);
157 144 ▶ return $record->_read($field)
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($record) and $record->isa("LibTmux::Entity") 141 3 144
Condition66%2/3
builtin::blessed($record) and $record->isa("LibTmux::Entity")
!ll&&!rl&&r
30141
MC/DC50%1/2
builtin::blessed($record) and $record->isa("LibTmux::Entity")
builtin::blessed($record) $record->isa("LibTmux::Entity")
Truth table66%2/3
A and B
A= builtin::blessed($record)B= $record->isa("LibTmux::Entity")
ABresult
0X0
100
111
158 if blessed($record) && $record->isa('LibTmux::Entity');
159 3 my $value;
160 3 ▶ if (ref($record) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $record eq 'HASH') { } 3 0 3
161 LibTmux::X::Unloaded->throw(
162 path => $path,
163 entity => $kind,
164 message => 'field or relation was not loaded'
165 3 ▶ ) unless exists $record->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $record->{$field} 0 3 3
166 3 $value = $record->{$field};
167 }
168 else {
169 0 my $reader = $adapters{ref $record}{readers}{$field};
170 0 ▶ LibTmux::X::Unloaded->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $reader 0 0 0
171 path => $path,
172 entity => $kind,
173 message => 'adapter has no reader for field'
174 ) unless $reader;
175 0 $value = $reader->($record);
176 }
177 3 ▶ return $value if $spec->{type} eq 'relation';
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"type"} eq "relation" 0 3 3
178 3 return $class->normalize($spec, $value, $path, 'Unloaded');
179 }
180  
181 =head1 NAME
182  
183 LibTmux::Schema - Catalog and explicit record adapters
184  
185 =head1 METHODS
186  
187 =head2 register_adapter
188  
189 C<register_adapter(class =E<gt> ..., entity =E<gt> ..., readers =E<gt> {...})>
190 registers trusted callbacks for declared fields. Unknown criteria keys never
191 become method names. Missing fields throw; nullable undef is known null.
192  
193 =cut
« LibTmux/Snapshot.pm ↑ Summary LibTmux/Codec.pm »