| 1 | package Net::Async::Redis::Protocol; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '6.006'; # VERSION | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | Net::Async::Redis::Protocol - simple implementation of the Redis wire protocol | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | Used internally by L<Net::Async::Redis> and L<Net::Async::Redis::Server>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use Scalar::Util qw(blessed reftype looks_like_number); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use List::Util qw(min); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # Normal string interpolation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use constant CRLF => "\x0D\x0A"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # Regex usage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | my $CRLF = CRLF; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 4 | ▶ | sub new { bless { protocol => 'resp3', hashrefs => 0, @_[1..$#_] }, $_[0] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | =head2 encode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | Given a Perl data structure, will return data suitable for sending | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | back as a response as from a Redis server. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | Note that this is not the correct format for client requests, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | see L</encode_from_client> instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 9049 | ▶ | sub encode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode: called CC=15 SCAR=30.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 9049 | my ($self, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 9049 | ▶ | die 'blessed data is not ok' if blessed $data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 9049 | ▶ | if(my $type = reftype $data) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 2014 | ▶ | if($type eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 2014 | return '*' . (0 + @$data) . CRLF . join '', map $self->encode($_), @$data | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } elsif($type eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | return '%' . (0 + keys %$data) . CRLF . join '', map $self->encode($_), map { $_ => $data->{$_} } sort keys %$data | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | die 'no support for ' . $type | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 7035 | ▶ | if(!defined($data)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 5 | ▶ | return $self->{protocol} eq 'resp3' ? '_' . CRLF : '$-1' . CRLF; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } elsif(!length($data)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 4 | return '$0' . CRLF . CRLF; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | ▶ | } elsif(($data ^ $data) eq "0" and int(0+$data) eq $data and $data !~ /inf/i) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
($data ^ $data) eq 0 and int 0 + $data eq $data and not $data =~ /inf/i
($data ^ $data) eq 0 and int 0 + $data eq $data
MC/DC33%1/3
($data ^ $data) eq 0 and int 0 + $data eq $data and not $data =~ /inf/i
($data ^ $data) eq 0
int 0 + $data eq $data
not $data =~ /inf/i
Truth table50%2/4
A and B and not(C)
A= ($data ^ $data) eq 0B= int 0 + $data eq $dataC= not $data =~ /inf/i
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 1014 | return ':' . (0 + $data) . CRLF; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | ▶ | } elsif(($data ^ $data) eq "0" and 0+$data eq $data) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
($data ^ $data) eq 0 and 0 + $data eq $data
MC/DC0%0/2
($data ^ $data) eq 0 and 0 + $data eq $data
($data ^ $data) eq 0
0 + $data eq $data
Truth table33%1/3
A and B
A= ($data ^ $data) eq 0B= 0 + $data eq $data
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | return ',' . lc(0 + $data) . CRLF; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | ▶ | } elsif(length($data) < 100 and $data !~ /[$CRLF]/) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
length $data < 100 and not $data =~ /[$CRLF]/
MC/DC50%1/2
length $data < 100 and not $data =~ /[$CRLF]/
length $data < 100
not $data =~ /[$CRLF]/
Truth table66%2/3
A and not(B)
A= length $data < 100B= not $data =~ /[$CRLF]/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 6010 | return '+' . $data . CRLF; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | return '$' . length($data) . CRLF . $data . CRLF; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | =head2 encode_from_client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | Handles client format encoding. Expects a list of data items, and will | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | convert them into length-prefixed bulk strings as a single response item. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub encode_from_client { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_from_client: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my ($self, @data) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | return '*' . (0 + @data) . CRLF . join '', map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | '$' . length($_) . CRLF . $_ . CRLF | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } @data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | =head2 decode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | Decodes wire protocol data into Perl data structures. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | Expects to be called with a reference to a byte string, and will | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | extract as much as it can from that string (destructively). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | Likely to call L</item> or L</item_error> zero or more times. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 32 | ▶ | sub decode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decode: called CC=40 SCAR=58.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 32 | my ($self, $bytes) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 32 | my $len = $self->{parsing_bulk}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | ITEM: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 32 | for ($$bytes) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 9055 | ▶ | if($log->is_trace) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my $bytes = substr $_, 0, min(16, length($_)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $log->tracef('Next few bytes: %s (%v02x)', $bytes, $bytes); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 9055 | ▶ | if(defined($len)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 6 | ▶ | last ITEM unless length($_) >= $len + 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 6 | ▶ | die 'invalid bulk data, did not end in CRLF' unless substr($_, $len, 2, '') eq CRLF; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 6 | $self->item(substr $_, 0, delete $self->{parsing_bulk}, ''); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 6 | undef $len; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 6 | ▶ | last ITEM unless length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 9049 | ▶ | if(s{^\+([^\x0D]*)$CRLF}{}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 14 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 6010 | $self->item("$1"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | } elsif(s{^:([^\x0D]*)$CRLF}{}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 1014 | my $int = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 1014 | ▶ | die 'invalid integer value ' . $int unless looks_like_number($int) && int($int) eq $int; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&looks_like_number($int) and int $int eq $int
MC/DC0%0/2
&looks_like_number($int) and int $int eq $int
&looks_like_number($int)
int $int eq $int
Truth table33%1/3
A and B
A= &looks_like_number($int)B= int $int eq $int
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 1014 | $self->item(0 + $int); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } elsif(s{^,([^\x0D]*)$CRLF}{}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | my $num = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | die 'invalid numeric value ' . $num unless looks_like_number($num) && lc(0 + $num) eq lc($num); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
&looks_like_number($num) and lc 0 + $num eq lc $num
MC/DC0%0/2
&looks_like_number($num) and lc 0 + $num eq lc $num
&looks_like_number($num)
lc 0 + $num eq lc $num
Truth table0%0/3
A and B
A= &looks_like_number($num)B= lc 0 + $num eq lc $num
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | $self->item(0 + $num); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | } elsif(s{^#([tf])$CRLF}{}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $self->item($1 eq 't'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } elsif(s{^\$([0-9]+)$CRLF}{}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 6 | $len = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 6 | ▶ | die 'invalid numeric value for length ' . $len unless 0+$len eq $len; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 6 | $self->{parsing_bulk} = $len; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } elsif(s{^=([0-9]+)$CRLF}{}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | $len = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | die 'invalid numeric value for length ' . $len unless 0+$len eq $len; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $self->{parsing_bulk} = $len; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | ▶ | } elsif(s{^\$-1$CRLF}{} or s{^\*-1$CRLF}{} or s{^_$CRLF}{}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
s/^\$-1$CRLF// or s/^\*-1$CRLF// or s/^_$CRLF//
s/^\$-1$CRLF// or s/^\*-1$CRLF//
MC/DC66%2/3
s/^\$-1$CRLF// or s/^\*-1$CRLF// or s/^_$CRLF//
s/^\$-1$CRLF//
s/^\*-1$CRLF//
s/^_$CRLF//
Truth table75%3/4
A or B or C
A= s/^\$-1$CRLF//B= s/^\*-1$CRLF//C= s/^_$CRLF//
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 5 | $self->item(undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } elsif(s{^>([0-9]+)$CRLF}{}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | my $pending = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | die 'invalid numeric value for push ' . $pending unless 0+$pending eq $pending; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | push @{$self->{active}}, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | type => 'push', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | pending => $pending | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } if $pending; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } elsif(s{^\*([0-9]+)$CRLF}{}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 2014 | my $pending = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 2014 | ▶ | die 'invalid numeric value for array ' . $pending unless 0+$pending eq $pending; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 2014 | ▶ | if($pending) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 2012 | push @{$self->{active}}, { type => 'array', pending => $pending }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 2 | $self->item([ ]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | } elsif(s{^~([0-9]+)$CRLF}{}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | my $pending = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | die 'invalid numeric value for set ' . $pending unless 0+$pending eq $pending; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if($pending) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | push @{$self->{active}}, { type => 'set', pending => $pending }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | $self->item([ ]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | } elsif(s{^%([0-9]+)$CRLF}{}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | my $pending = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | die 'invalid numeric value for map ' . $pending unless 0+$pending eq $pending; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if($pending) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | # We provide 2x the count here, for key/value pairs, and expect | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | # the handler to convert to a hash once it's received sufficient | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # items to emit the full element | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | push @{$self->{active}}, { type => 'map', pending => 2 * $pending }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | $self->item({ }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | } elsif(s{^\|([0-9]+)$CRLF}{}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | my $pending = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | die 'invalid numeric value for attribute ' . $pending unless 0+$pending eq $pending; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | push @{$self->{active}}, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | type => 'attribute', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | pending => 2 * $pending | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | } if $pending; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | } elsif(s{^-([^\x0D]*)$CRLF}{}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | $self->item_error($1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | } elsif(s{^!([^\x0D]*)$CRLF}{}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | die 'cannot handle blob error yet'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | last ITEM; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 9049 | ▶ | redo ITEM if length; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | sub parse { $_[0]->decode($_[1]) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 7037 | ▶ | sub item { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
item: called CC=10 SCAR=25.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 7037 | my ($self, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 7037 | ▶ | $data = [ %$data ] if ref $data eq 'HASH' and not $self->{hashrefs}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $data eq "HASH" and not $self->{"hashrefs"}
MC/DC0%0/2
ref $data eq "HASH" and not $self->{"hashrefs"}
ref $data eq "HASH"
not $self->{"hashrefs"}
Truth table33%1/3
A and not(B)
A= ref $data eq "HASH"B= not $self->{"hashrefs"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 7037 | while(1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 9049 | ▶ | return $self->{handler}->($data) unless @{$self->{active} || []}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->{'active'} || []
MC/DC100%1/1
$self->{'active'} || []
$self->{'active'}
Truth table100%2/2
A
A= $self->{'active'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 9017 | push @{$self->{active}[-1]{items}}, $data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 9017 | ▶ | return if --$self->{active}[-1]{pending}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 2012 | my $active = pop @{$self->{active}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | $data = $active->{type} eq 'map' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | ? ($self->{hashrefs} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | ? { @{$active->{items}} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | : [ @{$active->{items}} ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 2012 | ▶ | : $active->{items}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | # Skip attributes entirely for now | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 2012 | ▶ | return if $active->{type} eq 'attribute'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 2012 | ▶ | return $self->item_pubsub($data) if $active->{type} eq 'push'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | sub item_error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
item_error: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | my ($self, $err) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | ▶ | $self->{error}->($err) if $self->{error}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | $self | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | sub item_pubsub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
item_pubsub: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | my ($self, $item) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | $self->{pubsub}->(@$item) if $self->{pubsub}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | $self | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||