lib/Langertha/Skeid/Protocol/RawBody.pm

filter: Statementstmt 97.5% 117 / 120 Branchbran 81.5% 62 / 76 Conditioncond 65.2% 15 / 23 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 86.5% 212 / 245 CC 53 SCAR 23.2
CC
53
Cov
89%
encode_with_schemas
18.2
_skip_value
9.1
_top_level_spans
9.0
CRAP 10.2
« lib/Langertha/Skeid/CustomerSource.pm ↑ Summary lib/Langertha/Skeid/Proxy/Realtime.pm »
1 package Langertha::Skeid::Protocol::RawBody;
2 86 our $VERSION = '0.004';
3 # ABSTRACT: The client's JSON body as it was sent, with one top-level value replaced
4 86 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 86 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 86 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 86 my $STRING = JSON::MaybeXS->new(utf8 => 1, allow_nonref => 1);
10  
11  
12 324 ▶sub with_top_level {
Subroutine with_top_level: called CC=7 SCAR=19.5
Pod documented
13 324 my ($body, $key, $value) = @_;
14 324 ▶ return undef unless defined($body) && defined($key) && defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body and defined $key and defined $value 1 323 324
Condition66%4/6
defined $body and defined $key and defined $value
!ll&&!rl&&r
10323
defined $body and defined $key
!ll&&!rl&&r
10323
MC/DC33%1/3
defined $body and defined $key and defined $value
defined $body defined $key defined $value
Truth table50%2/4
A and B and C
A= defined $bodyB= defined $keyC= defined $value
ABCresult
0XX0
10X0
1100
1111
15 323 ▶ my $spans = _top_level_spans($body) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $spans = _top_level_spans($body) or return undef 319 4 323
16 319 my @hits = grep { $_->{key} eq $key } @$spans;
17 319 ▶ return undef unless @hits;
Branch: 1 branch
BranchTrueFalseTotal
unless @hits 2 317 319
18 317 my $replacement = $STRING->encode("$value");
19 # Back to front, so the offsets of the earlier spans stay valid.
20 317 for my $hit (sort { $b->{start} <=> $a->{start} } @hits) {
21 319 substr($body, $hit->{start}, $hit->{length}, $replacement);
22 }
23 317 return $body;
24 }
25  
26 # The members of a JSON object: [{ key, start, length }], key decoded, start/length the byte span
27 # of the value. undef when the text is not one well-formed-enough object. This follows structure
28 # only -- strings, nesting, separators -- and leaves validating values to the decoder that
29 # already accepted the body.
30 755 ▶sub _top_level_spans {
Subroutine _top_level_spans: called CC=9 SCAR=22.0
31 755 my ($json) = @_;
32 755 my @spans;
33 755 pos($json) = 0;
34 755 ▶ $json =~ /\G\s*\{/gc or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$json =~ /\G\s*\{/cg or return undef 754 1 755
35 754 $json =~ /\G\s*/gc;
36 754 ▶ unless ($json =~ /\G\}/gc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json =~ /\G\}/cg) 753 1 754
37 753 while (1) {
38 2114 ▶ $json =~ /\G\s*("(?:[^"\\]|\\.)*")\s*:\s*/gcs or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$json =~ /\G\s*("(?:[^"\\]|\\.)*")\s*:\s*/cgs or return undef 2113 1 2114
39 2113 my $key = eval { $STRING->decode($1) };
40 2113 ▶ return undef unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 2113 2113
41 2113 my $start = pos($json);
42 2113 ▶ _skip_value(\$json) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
_skip_value(\$json) or return undef 2113 0 2113
43 2113 push @spans, { key => $key, start => $start, length => pos($json) - $start };
44 2113 $json =~ /\G\s*/gc;
45 2113 ▶ next if $json =~ /\G,/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $json =~ /\G,/cg 1361 752 2113
46 752 ▶ last if $json =~ /\G\}/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $json =~ /\G\}/cg 751 1 752
47 1 return undef;
48 }
49 }
50 752 ▶ $json =~ /\G\s*\z/gc or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$json =~ /\G\s*\z/cg or return undef 751 1 752
51 751 return \@spans;
52 }
53  
54 # The elements of a JSON array, as their texts. undef when the text is not one array.
55 7 ▶sub _elements {
Subroutine _elements: called CC=7 SCAR=19.8
56 7 my ($json) = @_;
57 7 my @elements;
58 7 pos($json) = 0;
59 7 ▶ $json =~ /\G\s*\[/gc or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$json =~ /\G\s*\[/cg or return undef 6 1 7
60 6 $json =~ /\G\s*/gc;
61 6 ▶ unless ($json =~ /\G\]/gc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json =~ /\G\]/cg) 6 0 6
62 6 while (1) {
63 7 $json =~ /\G\s*/gc;
64 7 my $start = pos($json);
65 7 ▶ _skip_value(\$json) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
_skip_value(\$json) or return undef 7 0 7
66 7 push @elements, substr($json, $start, pos($json) - $start);
67 7 $json =~ /\G\s*/gc;
68 7 ▶ next if $json =~ /\G,/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $json =~ /\G,/cg 1 6 7
69 6 ▶ last if $json =~ /\G\]/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $json =~ /\G\]/cg 6 0 6
70 0 return undef;
71 }
72 }
73 6 ▶ $json =~ /\G\s*\z/gc or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$json =~ /\G\s*\z/cg or return undef 6 0 6
74 6 return \@elements;
75 }
76  
77 # The text of one member of an object text -- the last one of that name, as decoders read it.
78 432 ▶sub _member {
Subroutine _member: called CC=3 SCAR=11.0
79 432 my ($json, $key) = @_;
80 432 ▶ my $spans = _top_level_spans($json) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $spans = _top_level_spans($json) or return undef 432 0 432
81 432 my ($hit) = reverse grep { $_->{key} eq $key } @$spans;
82 432 ▶ return $hit ? substr($json, $hit->{start}, $hit->{length}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$hit ? : 15 417 432
83 }
84  
85 # Moves pos() of the referenced string past one JSON value.
86 2120 ▶sub _skip_value {
Subroutine _skip_value: called CC=9 SCAR=22.1
87 2120 my ($ref) = @_;
88 2120 ▶ return 1 if $$ref =~ /\G"(?:[^"\\]|\\.)*"/gcs;
Branch: 1 branch
BranchTrueFalseTotal
if $$ref =~ /\G"(?:[^"\\]|\\.)*"/cgs 917 1203 2120
89 1203 ▶ if ($$ref =~ /\G[\[\{]/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ref =~ /\G[\[\{]/cg) 622 581 1203
90 622 my $depth = 1;
91 622 ▶ while ($depth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth) 8788 622 9410
92 8788 ▶ if ($$ref =~ /\G[^"\[\]\{\}]+/gc) { }
Branch: 4 branches
BranchTrueFalseTotal
if ($$ref =~ /\G[^"\[\]\{\}]+/cg) { } 2849 5939 8788
elsif ($$ref =~ /\G"(?:[^"\\]|\\.)*"/cgs) { } 3447 2492 5939
elsif ($$ref =~ /\G[\[\{]/cg) { } 935 1557 2492
elsif ($$ref =~ /\G[\]\}]/cg) { } 1557 0 1557
93 elsif ($$ref =~ /\G"(?:[^"\\]|\\.)*"/gcs) { }
94 935 elsif ($$ref =~ /\G[\[\{]/gc) { $depth++ }
95 1557 elsif ($$ref =~ /\G[\]\}]/gc) { $depth-- }
96 0 else { return 0 }
97 }
98 622 return 1;
99 }
100 581 ▶ return $$ref =~ /\G[^\s,\]\}"\[\{]+/gc ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$$ref =~ /\G[^\s,\]\}"\[\{]+/cg ? : 581 0 581
101 }
102  
103  
104 423 ▶sub tool_schemas {
Subroutine tool_schemas: called CC=7 SCAR=19.5
Pod documented
105 423 my ($body, @path) = @_;
106 423 ▶ return undef unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 423 423
107 423 my $tools = _member($body, 'tools');
108 423 ▶ my $elements = defined($tools) ? _elements($tools) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $tools ? : 7 416 423
109 423 ▶ return undef unless $elements;
Branch: 1 branch
BranchTrueFalseTotal
unless $elements 417 6 423
110 6 my @schemas;
111 6 for my $text (@$elements) {
112 7 for my $key (@path) {
113 9 ▶ $text = defined($text) ? _member($text, $key) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 9 0 9
114 }
115 7 push @schemas, $text;
116 }
117 6 return \@schemas;
118 }
119  
120  
121 86 my $CANONICAL = JSON::MaybeXS->new(utf8 => 1, allow_nonref => 1, canonical => 1);
122  
123 11 ▶sub encode_with_schemas {
Subroutine encode_with_schemas: called CC=17 SCAR=29.0
Pod documented
124 11 my ($body, $texts) = @_;
125 11 ▶ return undef unless ref($body) eq 'HASH' && ref($texts) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" and ref $texts eq "ARRAY" 0 11 11
Condition33%1/3
ref $body eq "HASH" and ref $texts eq "ARRAY"
!ll&&!rl&&r
0011
MC/DC0%0/2
ref $body eq "HASH" and ref $texts eq "ARRAY"
ref $body eq "HASH" ref $texts eq "ARRAY"
Truth table33%1/3
A and B
A= ref $body eq "HASH"B= ref $texts eq "ARRAY"
ABresult
0X0
100
111
126 11 my $tools = $body->{tools};
127 11 ▶ return undef unless ref($tools) eq 'ARRAY' && @$tools && @$tools == @$texts;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tools eq "ARRAY" and @$tools and @$tools == @$texts 2 9 11
Condition83%5/6
ref $tools eq "ARRAY" and @$tools and @$tools == @$texts
!ll&&!rl&&r
119
ref $tools eq "ARRAY" and @$tools
!ll&&!rl&&r
1010
MC/DC66%2/3
ref $tools eq "ARRAY" and @$tools and @$tools == @$texts
ref $tools eq "ARRAY" @$tools @$tools == @$texts
Truth table75%3/4
A and B and C
A= ref $tools eq "ARRAY"B= @$toolsC= @$tools == @$texts
ABCresult
0XX0
10X0
1100
1111
128  
129 9 my @spelled;
130 9 for my $i (0 .. $#$tools) {
131 9 my $tool = $tools->[$i];
132 9 ▶ return undef unless ref($tool) eq 'HASH' && ref($tool->{function}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tool eq "HASH" and ref $tool->{"function"} eq "HASH" 0 9 9
Condition33%1/3
ref $tool eq "HASH" and ref $tool->{"function"} eq "HASH"
!ll&&!rl&&r
009
MC/DC0%0/2
ref $tool eq "HASH" and ref $tool->{"function"} eq "HASH"
ref $tool eq "HASH" ref $tool->{"function"} eq "HASH"
Truth table33%1/3
A and B
A= ref $tool eq "HASH"B= ref $tool->{"function"} eq "HASH"
ABresult
0X0
100
111
133 9 my %function = %{ $tool->{function} };
134 9 my @members;
135 9 for my $key (qw(name description)) {
136 18 ▶ next unless exists $function{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $function{$key} 1 17 18
137 17 push @members, qq("$key":) . $CANONICAL->encode(delete $function{$key});
138 }
139 9 ▶ if (exists $function{parameters}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $function{"parameters"}) 9 0 9
140 9 my $parameters = delete $function{parameters};
141 9 my $text = $texts->[$i];
142 9 ▶ my $same = defined($text) && eval {
Condition100%3/3
defined $text && eval { do { $CANONICAL->encode($CANONICAL->decode($text)) eq $CANONICAL->encode($parameters) } }
!ll&&!rl&&r
117
MC/DC100%2/2
defined $text && eval { do { $CANONICAL->encode($CANONICAL->decode($text)) eq $CANONICAL->encode($parameters) } }
defined $text eval { do { $CANONICAL->encode($CANONICAL->decode($text)) eq $CANONICAL->encode($parameters) } }
Truth table100%3/3
A && B
A= defined $textB= eval { do { $CANONICAL->encode($CANONICAL->decode($text)) eq $CANONICAL->encode($parameters) } }
ABresult
0X0
100
111
143 8 $CANONICAL->encode($CANONICAL->decode($text)) eq $CANONICAL->encode($parameters);
144 };
145 9 ▶ push @members, '"parameters":' . ($same ? $text : $CANONICAL->encode($parameters));
Branch: 1 branch
BranchTrueFalseTotal
$same ? : 7 2 9
146 }
147 9 push @members, $STRING->encode($_) . ':' . $CANONICAL->encode($function{$_}) for sort keys %function;
148  
149 9 my %rest = %$tool;
150 9 delete $rest{function};
151 9 my $type = delete $rest{type};
152 push @spelled, join(',',
153 '{"type":' . $CANONICAL->encode($type // 'function'),
154 '"function":{' . join(',', @members) . '}',
155 0 ▶ map { $STRING->encode($_) . ':' . $CANONICAL->encode($rest{$_}) } sort keys %rest,
Condition50%1/2
$type // "function"
l!l
90
MC/DC0%0/1
$type // "function"
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
156 ) . '}';
157 }
158  
159 9 my %rest = %$body;
160 9 delete $rest{tools};
161 9 my $encoded = $CANONICAL->encode(\%rest);
162 9 my $tools_member = '"tools":[' . join(',', @spelled) . ']';
163 9 ▶ substr($encoded, -1, 1, (%rest ? ',' : '') . $tools_member . '}');
Branch: 1 branch
BranchTrueFalseTotal
%rest ? : 8 1 9
164 9 return $encoded;
165 }
166  
167 86 1;
168  
169 __END__
« lib/Langertha/Skeid/CustomerSource.pm ↑ Summary lib/Langertha/Skeid/Proxy/Realtime.pm »