lib/OpenSearch/PPLQuery.pm

filter: Statementstmt 87.2% 192 / 220 Branchbran 51.7% 59 / 114 Conditioncond 40.9% 25 / 61 MC/DCmcdc 5.5% 2 / 36 Subroutinesub 96.3% 26 / 27 Podpod 0.0% 0 / 19 totaltotal 63.7% 304 / 477 CC 89 SCAR 32.4
CC
89
Cov
70%
validate_options
81.3
execute
21.0
validate_result_shape
14.7
CRAP 25.6
↑ Summary script/pplquery »
1 package OpenSearch::PPLQuery;
2  
3 # HTTP client for the OpenSearch PPL plugin, and the renderers used by the
4 # bin/pplquery command. User documentation lives in PPLQuery.pod at the
5 # distribution root and is injected below at build time; this module's own
6 # interface is described in comments, because it exists to serve the command
7 # and is not intended as a general-purpose OpenSearch library.
8 #
9 # Interface, for maintainers:
10 #
11 # OpenSearch::PPLQuery->new(%options)
12 # url required base URL; validated by validate_options
13 # user Basic-auth username; requires password and an https url
14 # password Basic-auth password; requires user
15 # ca_file CA certificate path; https only, excludes insecure
16 # insecure disable certificate/hostname verification; https only
17 # timeout seconds, defaults to 60, must be positive
18 #
19 # $client->execute($query)
20 # Returns ($document, $error_status). $error_status is undef on success
21 # and the HTTP status otherwise, so a caller can render the cluster's own
22 # error document instead of discarding it. Transport failures and
23 # undecodable responses throw.
24 #
25 # render_json / render_table / render_csv ($document, %options)
26 # Plain functions, not methods. render_table takes max_width.
27 #
28 # Every string crossing this boundary is character data, never octets. The
29 # assert_unicode_* helpers enforce that at the edges so that an encoding fault
30 # is reported where it enters rather than as mojibake in the output.
31  
32 22 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 22 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 22 ▶use Encode qw(decode encode FB_CROAK LEAVE_SRC);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 22 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
37 22 ▶use Cpanel::JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
38 22 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 22 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 22 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 22 our $VERSION = '1.0.0';
43  
44 22 my $JSON_TEXT = Cpanel::JSON::XS->new->utf8(0)->canonical(1);
45 22 my $JSON_LWP = Cpanel::JSON::XS->new->utf8(1)->canonical(1);
46 22 my %NUMERIC_TYPE = map { $_ => 1 } qw(int integer long bigint short smallint byte float double);
47  
48 sub new {
49 14 ▶ my ($class, %options) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
50 14 ▶ $options{timeout} = 60 if !defined $options{timeout};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"timeout"} 0 14 14
51 14 validate_options(\%options);
52 14 return bless \%options, $class;
53 }
54  
55 sub execute {
56 14 ▶ my ($self, $query) = @_;
Subroutine execute: called CC=12 SCAR=30.4
Pod undocumented
57 14 assert_unicode_text($query, 'query');
58 14 ▶ die "Query is empty\n" if !defined($query) || $query !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query and $query =~ /\S/u 0 14 14
Condition33%1/3
defined $query and $query =~ /\S/u
!ll&&!rl&&r
0014
MC/DC0%0/2
defined $query and $query =~ /\S/u
defined $query $query =~ /\S/u
Truth table33%1/3
A and B
A= defined $queryB= $query =~ /\S/u
ABresult
0X0
100
111
59  
60 14 my $base_url = $self->{url};
61 14 $base_url =~ s{/\z}{};
62 14 my $endpoint = URI->new("$base_url/_plugins/_ppl");
63 14 $endpoint->query_form(format => 'jdbc');
64  
65 14 my $request = HTTP::Request->new(POST => $endpoint);
66 14 $request->header('Accept' => 'application/json');
67 14 $request->header('Content-Type' => 'application/json; charset=utf-8');
68 14 ▶ $request->authorization_basic($self->{user}, $self->{password}) if defined $self->{user};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"user"} 0 14 14
69  
70 14 $request->content($JSON_LWP->encode({query => $query}));
71  
72 14 my $ua = LWP::UserAgent->new(agent => "OpenSearch-PPLQuery/$VERSION", timeout => $self->{timeout});
73 14 $ua->requests_redirectable([]);
74 14 ▶ if ($endpoint->scheme eq 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($endpoint->scheme eq "https") 0 14 14
75 0 ▶ if ($self->{insecure}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'insecure'}) { } 0 0 0
76 0 $ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00);
77 } else {
78 0 $ua->ssl_opts(verify_hostname => 1);
79 0 ▶ $ua->ssl_opts(SSL_ca_file => encode('UTF-8', $self->{ca_file}, FB_CROAK | LEAVE_SRC)) if defined $self->{ca_file};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ca_file"} 0 0 0
80 }
81 }
82  
83 14 my $response;
84 {
85 14 local $@;
86 14 $response = eval { $ua->request($request) };
87 14 ▶ die "HTTP request failed: " . exception_text($@) if !defined $response;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $response 0 14 14
88 }
89  
90 14 my $response_octets = $response->content;
91 14 my $document;
92 {
93 14 local $@;
94 14 $document = eval { $JSON_LWP->decode($response_octets) };
95 14 ▶ if ($@ ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ ne "") 0 14 14
96 0 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
97 0 my $response_text = decode_utf8($response_octets, 'OpenSearch response');
98 0 $response_text =~ s/\s+\z//;
99 0 ▶ my $detail = $response_text eq '' ? 'empty non-JSON response' : terminal_text($response_text);
Branch: 1 branch
BranchTrueFalseTotal
$response_text eq '' ? : 0 0 0
100 0 die 'OpenSearch returned HTTP ' . $response->code . ": $detail\n";
101 }
102 0 die 'OpenSearch returned invalid JSON: ' . exception_text($@);
103 }
104 }
105  
106 14 assert_unicode_value($document, 'decoded OpenSearch response');
107 14 ▶ return ($document, $response->is_success ? undef : $response->code);
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success ? : 12 2 14
108 }
109  
110 sub render_json {
111 8 ▶ my ($document) = @_;
Subroutine render_json: called CC=1 SCAR=0.0
Pod undocumented
112 8 return Cpanel::JSON::XS->new->utf8(0)->canonical(1)->pretty(1)->encode($document);
113 }
114  
115 sub render_table {
116 3 ▶ my ($document, %options) = @_;
Subroutine render_table: called CC=12 SCAR=25.3
Pod undocumented
117 3 my $names = validate_result_shape($document);
118 3 ▶ my $max_width = $options{max_width} // 0;
Condition50%1/2
$options{"max_width"} // 0
l!l
30
MC/DC0%0/1
$options{"max_width"} // 0
$options{"max_width"}
Truth table50%1/2
A
A= $options{"max_width"}
Aresult
11
00
119  
120 3 my @headers = map { truncate_cell(display_scalar($_), $max_width) } @$names;
121 3 my @rows;
122 3 for my $row (@{$document->{datarows}}) {
123 3 push @rows, [map { truncate_cell(display_scalar($_), $max_width) } @$row];
124 }
125 3 ▶ return '(' . scalar(@rows) . (scalar(@rows) == 1 ? " row)\n" : " rows)\n") if !@headers;
Branch: 2 branches
BranchTrueFalseTotal
unless @headers 0 3 3
scalar @rows == 1 ? : 0 0 0
126  
127 3 my @widths = map { length($headers[$_]) } 0 .. $#headers;
128 3 for my $row (@rows) {
129 3 for my $index (0 .. $#headers) {
130 6 my $length = length($row->[$index]);
131 6 ▶ $widths[$index] = $length if $length > $widths[$index];
Branch: 1 branch
BranchTrueFalseTotal
if $length > $widths[$index] 4 2 6
132 }
133 }
134  
135 3 ▶ my @aligns = map { $NUMERIC_TYPE{$_->{type} // ''} ? 1 : 0 } @{$document->{schema}};
Branch: 1 branch
BranchTrueFalseTotal
$NUMERIC_TYPE{$_->{'type'} // ''} ? : 0 6 6
Condition50%1/2
$_->{"type"} // ""
l!l
60
MC/DC0%0/1
$_->{"type"} // ""
$_->{"type"}
Truth table50%1/2
A
A= $_->{"type"}
Aresult
11
00
136 3 my $separator = '+' . join('+', map { '-' x ($_ + 2) } @widths) . '+';
137 3 $separator .= "\n";
138 3 my $output = $separator;
139 3 $output .= render_table_row(\@headers, \@widths, \@aligns);
140 3 $output .= $separator;
141 3 for my $index (0 .. $#rows) {
142 3 $output .= render_table_row($rows[$index], \@widths, \@aligns);
143 }
144 3 $output .= $separator;
145 3 ▶ $output .= scalar(@rows) . (@rows == 1 ? " row\n" : " rows\n");
Branch: 1 branch
BranchTrueFalseTotal
@rows == 1 ? : 3 0 3
146 3 return $output;
147 }
148  
149 sub render_csv {
150 2 ▶ my ($document) = @_;
Subroutine render_csv: called CC=1 SCAR=0.0
Pod undocumented
151 2 my $names = validate_result_shape($document);
152 2 return csv_row($names) . join('', map { csv_row($_) } @{$document->{datarows}});
153 }
154  
155 sub format_error {
156 1 ▶ my ($status, $document) = @_;
Subroutine format_error: called CC=4 SCAR=15.9
Pod undocumented
157 1 my $detail;
158 1 ▶ if (ref($document) eq 'HASH' && exists $document->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $document eq 'HASH' and exists $document->{'error'}) { } 1 0 1
Condition33%1/3
ref $document eq "HASH" and exists $document->{"error"}
!ll&&!rl&&r
001
MC/DC0%0/2
ref $document eq "HASH" and exists $document->{"error"}
ref $document eq "HASH" exists $document->{"error"}
Truth table33%1/3
A and B
A= ref $document eq "HASH"B= exists $document->{"error"}
ABresult
0X0
100
111
159 1 ▶ $detail = ref($document->{error}) ? $JSON_TEXT->encode($document->{error}) : terminal_text("$document->{error}");
Branch: 1 branch
BranchTrueFalseTotal
ref $document->{'error'} ? : 1 0 1
160 } else {
161 0 $detail = $JSON_TEXT->encode($document);
162 }
163 1 return "OpenSearch returned HTTP $status: $detail\n";
164 }
165  
166 sub validate_options {
167 14 ▶ my ($options) = @_;
Subroutine validate_options: called CC=26 SCAR=44.0
Pod undocumented
168 14 for my $name (qw(url user password ca_file)) {
169 56 ▶ assert_unicode_text($options->{$name}, "option $name") if defined $options->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{$name} 14 42 56
170 }
171 14 ▶ die "OpenSearch URL is required\n" if !defined $options->{url};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"url"} 0 14 14
172 14 ▶ die "timeout must be greater than zero\n" if $options->{timeout} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"timeout"} <= 0 0 14 14
173 14 ▶ die "Basic-auth username and password must be set together\n" if defined($options->{user}) != defined($options->{password});
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"user"} != defined $options->{"password"} 0 14 14
174 14 require_ascii($options->{url}, 'OpenSearch URL');
175 14 ▶ require_ascii($options->{user}, 'Basic-auth username') if defined $options->{user};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"user"} 0 14 14
176 14 ▶ require_ascii($options->{password}, 'Basic-auth password') if defined $options->{password};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"password"} 0 14 14
177  
178 14 my $uri = URI->new($options->{url});
179 14 ▶ die "OpenSearch URL must use http or https\n" if !defined($uri->scheme) || ($uri->scheme ne 'http' && $uri->scheme ne 'https');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $uri->scheme or $uri->scheme ne "http" and $uri->scheme ne "https" 0 14 14
Condition33%2/6
not defined $uri->scheme or $uri->scheme ne "http" and $uri->scheme ne "https"
l!l&&r!l&&!r
0014
$uri->scheme ne "http" and $uri->scheme ne "https"
!ll&&!rl&&r
1400
MC/DC0%0/3
not defined $uri->scheme or $uri->scheme ne "http" and $uri->scheme ne "https"
not defined $uri->scheme $uri->scheme ne "http" $uri->scheme ne "https"
Truth table0%0/4
not(A) or B and C
A= not defined $uri->schemeB= $uri->scheme ne "http"C= $uri->scheme ne "https"
ABCresult
1XX1
0111
00X0
0100
180 14 ▶ die "OpenSearch URL must include a host\n" if !defined($uri->host) || $uri->host eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $uri->host or $uri->host eq "" 0 14 14
Condition33%1/3
not defined $uri->host or $uri->host eq ""
l!l&&r!l&&!r
0014
MC/DC0%0/2
not defined $uri->host or $uri->host eq ""
not defined $uri->host $uri->host eq ""
Truth table33%1/3
not(A) or B
A= not defined $uri->hostB= $uri->host eq ""
ABresult
1X1
011
000
181 14 ▶ die "OpenSearch URL must not include credentials, a query, or a fragment\n" if defined($uri->userinfo) || defined($uri->query) || defined($uri->fragment);
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri->userinfo or defined $uri->query or defined $uri->fragment 0 14 14
Condition33%2/6
defined $uri->userinfo or defined $uri->query or defined $uri->fragment
l!l&&r!l&&!r
0014
defined $uri->userinfo or defined $uri->query
l!l&&r!l&&!r
0014
MC/DC0%0/3
defined $uri->userinfo or defined $uri->query or defined $uri->fragment
defined $uri->userinfo defined $uri->query defined $uri->fragment
Truth table25%1/4
A or B or C
A= defined $uri->userinfoB= defined $uri->queryC= defined $uri->fragment
ABCresult
1XX1
01X1
0011
0000
182 14 ▶ die "OpenSearch URL must be a base URL without a path\n" if $uri->path ne '' && $uri->path ne '/';
Branch: 1 branch
BranchTrueFalseTotal
if $uri->path ne "" and $uri->path ne "/" 0 14 14
Condition33%1/3
$uri->path ne "" and $uri->path ne "/"
!ll&&!rl&&r
1400
MC/DC0%0/2
$uri->path ne "" and $uri->path ne "/"
$uri->path ne "" $uri->path ne "/"
Truth table33%1/3
A and B
A= $uri->path ne ""B= $uri->path ne "/"
ABresult
0X0
100
111
183 14 ▶ die "Basic authentication requires an https URL\n" if defined($options->{user}) && $uri->scheme ne 'https';
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"user"} and $uri->scheme ne "https" 0 14 14
Condition33%1/3
defined $options->{"user"} and $uri->scheme ne "https"
!ll&&!rl&&r
1400
MC/DC0%0/2
defined $options->{"user"} and $uri->scheme ne "https"
defined $options->{"user"} $uri->scheme ne "https"
Truth table33%1/3
A and B
A= defined $options->{"user"}B= $uri->scheme ne "https"
ABresult
0X0
100
111
184 14 ▶ die "ca_file requires an https URL\n" if defined($options->{ca_file}) && $uri->scheme ne 'https';
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"ca_file"} and $uri->scheme ne "https" 0 14 14
Condition33%1/3
defined $options->{"ca_file"} and $uri->scheme ne "https"
!ll&&!rl&&r
1400
MC/DC0%0/2
defined $options->{"ca_file"} and $uri->scheme ne "https"
defined $options->{"ca_file"} $uri->scheme ne "https"
Truth table33%1/3
A and B
A= defined $options->{"ca_file"}B= $uri->scheme ne "https"
ABresult
0X0
100
111
185 14 ▶ die "insecure requires an https URL\n" if $options->{insecure} && $uri->scheme ne 'https';
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"insecure"} and $uri->scheme ne "https" 0 14 14
Condition33%1/3
$options->{"insecure"} and $uri->scheme ne "https"
!ll&&!rl&&r
1400
MC/DC0%0/2
$options->{"insecure"} and $uri->scheme ne "https"
$options->{"insecure"} $uri->scheme ne "https"
Truth table33%1/3
A and B
A= $options->{"insecure"}B= $uri->scheme ne "https"
ABresult
0X0
100
111
186 14 ▶ die "ca_file and insecure cannot be used together\n" if defined($options->{ca_file}) && $options->{insecure};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{"ca_file"} and $options->{"insecure"} 0 14 14
Condition33%1/3
defined $options->{"ca_file"} and $options->{"insecure"}
!ll&&!rl&&r
1400
MC/DC0%0/2
defined $options->{"ca_file"} and $options->{"insecure"}
defined $options->{"ca_file"} $options->{"insecure"}
Truth table33%1/3
A and B
A= defined $options->{"ca_file"}B= $options->{"insecure"}
ABresult
0X0
100
111
187 }
188  
189 sub validate_result_shape {
190 5 ▶ my ($document) = @_;
Subroutine validate_result_shape: called CC=11 SCAR=26.9
Pod undocumented
191 5 ▶ die "OpenSearch response is not a JSON object\n" if ref($document) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $document ne "HASH" 0 5 5
192 5 ▶ die "OpenSearch response has no schema array\n" if ref($document->{schema}) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $document->{"schema"} ne "ARRAY" 0 5 5
193 5 ▶ die "OpenSearch response has no datarows array\n" if ref($document->{datarows}) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $document->{"datarows"} ne "ARRAY" 0 5 5
194 5 my @names;
195 5 for my $column (@{$document->{schema}}) {
196 10 ▶ die "OpenSearch schema entry is not an object with a name\n" if ref($column) ne 'HASH' || !defined($column->{name}) || ref($column->{name});
Branch: 1 branch
BranchTrueFalseTotal
if ref $column ne "HASH" or not defined $column->{"name"} or ref $column->{"name"} 0 10 10
Condition33%2/6
ref $column ne "HASH" or not defined $column->{"name"} or ref $column->{"name"}
l!l&&r!l&&!r
0010
ref $column ne "HASH" or not defined $column->{"name"}
l!l&&r!l&&!r
0010
MC/DC0%0/3
ref $column ne "HASH" or not defined $column->{"name"} or ref $column->{"name"}
ref $column ne "HASH" not defined $column->{"name"} ref $column->{"name"}
Truth table25%1/4
A or not(B) or C
A= ref $column ne "HASH"B= not defined $column->{"name"}C= ref $column->{"name"}
ABCresult
1XX1
01X1
0011
0000
197 10 push @names, $column->{name};
198 }
199 5 for my $row (@{$document->{datarows}}) {
200 5 ▶ die "OpenSearch data row is not an array\n" if ref($row) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $row ne "ARRAY" 0 5 5
201 5 ▶ die 'OpenSearch data row has ' . scalar(@$row) . ' values for ' . scalar(@names) . " columns\n" if @$row != @names;
Branch: 1 branch
BranchTrueFalseTotal
if @$row != @names 0 5 5
202 }
203 5 return \@names;
204 }
205  
206 sub render_table_row {
207 6 ▶ my ($row, $widths, $aligns) = @_;
Subroutine render_table_row: called CC=3 SCAR=11.0
Pod undocumented
208 6 my @cells;
209 6 for my $index (0 .. $#$row) {
210 12 my $text = $row->[$index];
211 12 my $pad = ' ' x ($widths->[$index] - length($text));
212 12 ▶ push @cells, $aligns->[$index] ? $pad . $text : $text . $pad;
Branch: 1 branch
BranchTrueFalseTotal
$aligns->[$index] ? : 0 12 12
213 }
214 6 return '| ' . join(' | ', @cells) . " |\n";
215 }
216  
217 sub truncate_cell {
218 12 ▶ my ($text, $max_width) = @_;
Subroutine truncate_cell: called CC=3 SCAR=11.0
Pod undocumented
219 12 ▶ return $text if $max_width == 0 || length($text) <= $max_width;
Branch: 1 branch
BranchTrueFalseTotal
if $max_width == 0 or length $text <= $max_width 9 3 12
Condition100%3/3
$max_width == 0 or length $text <= $max_width
l!l&&r!l&&!r
813
MC/DC100%2/2
$max_width == 0 or length $text <= $max_width
$max_width == 0 length $text <= $max_width
Truth table100%3/3
A or B
A= $max_width == 0B= length $text <= $max_width
ABresult
1X1
011
000
220 3 return substr($text, 0, $max_width - 1) . "\x{2026}";
221 }
222  
223 4 ▶sub csv_row { return join(',', map { csv_field($_) } @{$_[0]}) . "\n"; }
Subroutine csv_row: called CC=1 SCAR=0.0
Pod undocumented
224  
225 sub csv_field {
226 8 ▶ my ($value) = @_;
Subroutine csv_field: called CC=4 SCAR=13.9
Pod undocumented
227 8 ▶ return '' if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 7 8
228 7 ▶ my $text = ref($value) ? $JSON_TEXT->encode($value) : "$value";
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 0 7 7
229 7 ▶ return $text if $text !~ /["\r\n,]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /["\r\n,]/u 6 1 7
230 1 $text =~ s/"/""/g;
231 1 return qq{"$text"};
232 }
233  
234 sub display_scalar {
235 12 ▶ my ($value) = @_;
Subroutine display_scalar: called CC=3 SCAR=11.3
Pod undocumented
236 12 ▶ return 'NULL' if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 12 12
237 12 ▶ my $text = ref($value) ? $JSON_TEXT->encode($value) : "$value";
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 0 12 12
238 12 $text =~ s/\r/\\r/g;
239 12 $text =~ s/\n/\\n/g;
240 12 $text =~ s/\t/\\t/g;
241 12 $text =~ s/([\x{00}-\x{08}\x{0b}\x{0c}\x{0e}-\x{1f}\x{7f}])/sprintf('\\u%04x', ord($1))/ge;
242 12 return $text;
243 }
244  
245 sub terminal_text {
246 0 ▶ my ($text) = @_;
Subroutine terminal_text: not called CC=1 SCAR=6.9
Pod undocumented
247 0 $text =~ s/\r/\\r/g;
248 0 $text =~ s/\n/\\n/g;
249 0 $text =~ s/\t/\\t/g;
250 0 $text =~ s/([\x{00}-\x{08}\x{0b}\x{0c}\x{0e}-\x{1f}\x{7f}])/sprintf('\\u%04x', ord($1))/ge;
251 0 return $text;
252 }
253  
254 sub decode_utf8 {
255 100 ▶ my ($octets, $label) = @_;
Subroutine decode_utf8: called CC=3 SCAR=11.1
Pod undocumented
256 100 ▶ return $octets if utf8::is_utf8($octets);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($octets) 0 100 100
257 100 my $text;
258 {
259 100 local $@;
260 100 $text = eval { decode('UTF-8', $octets, FB_CROAK | LEAVE_SRC) };
261 100 ▶ die "$label is not valid UTF-8\n" if $@ ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 100 100
262 }
263 100 return $text;
264 }
265  
266 sub exception_text {
267 6 ▶ my ($exception) = @_;
Subroutine exception_text: called CC=5 SCAR=24.2
Pod undocumented
268 6 ▶ $exception = "$exception" if ref($exception);
Branch: 1 branch
BranchTrueFalseTotal
if ref $exception 0 6 6
269 6 ▶ return $exception if utf8::is_utf8($exception) || $exception !~ /[^\x00-\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($exception) or not $exception =~ /[^\x00-\x7f]/u 6 0 6
Condition66%2/3
utf8::is_utf8($exception) or not $exception =~ /[^\x00-\x7f]/u
l!l&&r!l&&!r
420
MC/DC0%0/2
utf8::is_utf8($exception) or not $exception =~ /[^\x00-\x7f]/u
utf8::is_utf8($exception) not $exception =~ /[^\x00-\x7f]/u
Truth table66%2/3
A or not(B)
A= utf8::is_utf8($exception)B= not $exception =~ /[^\x00-\x7f]/u
ABresult
1X1
011
000
270 0 my $text;
271 {
272 0 local $@;
273 0 $text = eval { decode('UTF-8', $exception, FB_CROAK | LEAVE_SRC) };
274 0 ▶ return 'dependency returned a non-UTF-8 exception' if $@ ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 0 0
275 }
276 0 return $text;
277 }
278  
279 sub assert_unicode_value {
280 154 ▶ my ($value, $label) = @_;
Subroutine assert_unicode_value: called CC=9 SCAR=24.3
Pod undocumented
281 154 ▶ return if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 153 154
282 153 ▶ if (!ref($value)) { assert_unicode_text($value, $label); return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 77 76 153
283 76 ▶ if (ref($value) eq 'ARRAY') { assert_unicode_value($_, $label) for @$value; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 36 40 76
284 40 ▶ if (ref($value) eq 'HASH') { assert_unicode_text($_, $label), assert_unicode_value($value->{$_}, $label) for keys %$value; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 40 0 40
285 0 ▶ return if blessed($value) && Cpanel::JSON::XS::is_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) and &Cpanel::JSON::XS::is_bool($value) 0 0 0
Condition0%0/3
builtin::blessed($value) and &Cpanel::JSON::XS::is_bool($value)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and &Cpanel::JSON::XS::is_bool($value)
builtin::blessed($value) &Cpanel::JSON::XS::is_bool($value)
Truth table0%0/3
A and B
A= builtin::blessed($value)B= &Cpanel::JSON::XS::is_bool($value)
ABresult
0X0
100
111
286 0 die "$label contains an unsupported value\n";
287 }
288  
289 sub assert_unicode_text {
290 185 ▶ my ($text, $label) = @_;
Subroutine assert_unicode_text: called CC=4 SCAR=14.9
Pod undocumented
291 185 ▶ die "$label contains a downgraded non-ASCII string\n" if defined($text) && !utf8::is_utf8($text) && $text =~ /[^\x00-\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $text and not utf8::is_utf8($text) and $text =~ /[^\x00-\x7f]/u 0 185 185
Condition66%4/6
defined $text and not utf8::is_utf8($text) and $text =~ /[^\x00-\x7f]/u
!ll&&!rl&&r
441410
defined $text and not utf8::is_utf8($text)
!ll&&!rl&&r
044141
MC/DC0%0/3
defined $text and not utf8::is_utf8($text) and $text =~ /[^\x00-\x7f]/u
defined $text not utf8::is_utf8($text) $text =~ /[^\x00-\x7f]/u
Truth table50%2/4
A and not(B) and C
A= defined $textB= not utf8::is_utf8($text)C= $text =~ /[^\x00-\x7f]/u
ABCresult
0XX0
10X0
1100
1111
292 }
293  
294 sub require_ascii {
295 14 ▶ my ($text, $label) = @_;
Subroutine require_ascii: called CC=2 SCAR=7.2
Pod undocumented
296 14 ▶ die "$label must contain only ASCII characters\n" if $text =~ /[^\x00-\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /[^\x00-\x7f]/u 0 14 14
297 }
298  
299 22 1;
300  
301 __END__
↑ Summary script/pplquery »