/
Decode.pm
193 lines (154 loc) · 5.34 KB
/
Decode.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use v6;
use JSON::Tiny;
use Avro::Auxiliary;
use Avro::Schema;
use experimental :pack;
package Avro{
#======================================
# Exceptions
#======================================
class X::Avro::DecodeFail is Avro::AvroException {
has Str $.note;
method message { "Failed to decode "~$!note }
}
#== Role ==============================
# * Decoder
#======================================
role Decoder {
method decode(Mu , Avro::Schema --> Mu:D) { ... };
# multi method decode(IO::Handle:D $handle, Avro::Schema $schema --> Mu:D) { ... }; see bug ticker #125658
# multi method decode(Mu:D, Avro::Schema --> Mu:D) { ... };
}
#== Class =============================
# * BinaryDecoder
#======================================
class BinaryDecoder does Decoder {
# integers and longs are encode as variable sized zigzag numbers
sub decode_long(Stream $stream){
my @arr = ();
my int $byte;
repeat {
$byte = $stream.read(1).unpack("C");
push(@arr,$byte);
} until (($byte +> 7) == 0);
return from_zigzag(from_varint(@arr));
}
multi submethod decode_schema(Avro::Record $schema,Stream $stream) {
my %hash;
for $schema.fields.list -> $field {
%hash{$field.name} = self.decode_schema($field.type,$stream);
}
return %hash;
}
multi submethod decode_schema(Avro::Array $schema, Stream $stream) {
my Int $size = decode_long($stream);
my @arr = ();
while $size {
for (1..$size) -> $i {
push(@arr,self.decode_schema($schema.items,$stream));
}
$size = decode_long($stream);
}
return @arr
}
multi submethod decode_schema(Avro::Map $schema, Stream $stream) {
my Int $size = decode_long($stream);
my %hash;
my Avro::Schema $keyschema = Avro::String.new();
while $size {
for (1..$size) -> $i {
my Str $key = self.decode_schema($keyschema,$stream);
my $data = self.decode_schema($schema.values,$stream);
%hash{$key} = $data;
}
$size = decode_long($stream);
}
return %hash;
}
multi submethod decode_schema(Avro::Enum $schema, Stream $stream) {
my int $result = decode_long($stream);
$schema.sym[$result];
}
multi submethod decode_schema(Avro::Union $schema, Stream $stream) {
my Int $num = decode_long($stream);
my Avro::Schema $type = $schema.types[$num];
self.decode_schema($type,$stream);
}
multi submethod decode_schema(Avro::Fixed $schema, Stream $stream) {
my @arr = ();
for (1..$schema.size) -> $i {
push(@arr,$stream.read(1).unpack("C").chr);
}
@arr.join("");
}
multi submethod decode_schema(Avro::Null $schema, Stream $stream) {
#my $r = $stream.read(1).unpack("C");
#if $r == 0 { Any }
#else { X::Avro::DecodeFail.new(:schema($schema)).throw() }
Any
}
multi submethod decode_schema(Avro::String $schema, Stream $stream) {
my int $size = decode_long($stream);
my Blob $r = $stream.read($size);
$r.decode()
}
multi submethod decode_schema(Avro::Bytes $schema, Stream $stream) {
my int $size = decode_long($stream);
my @arr = ();
for 1..$size -> $i {
push(@arr,$stream.read(1).unpack("C").chr);
}
@arr.join("");
}
multi submethod decode_schema(Avro::Boolean $schema, Stream $stream) {
my $r = $stream.read(1).unpack("C");
given $r {
when 0 { False }
when 1 { True }
default { X::Avro::DecodeFail.new(:schema($schema)).throw() }
}
}
multi submethod decode_schema(Avro::Integer $schema, Stream $stream) {
decode_long($stream);
}
multi submethod decode_schema(Avro::Long $schema, Stream $stream) {
decode_long($stream);
}
multi submethod decode_schema(Avro::Float $schema, Stream $stream) {
my @arr = ();
for 1..4 -> $i {
push(@arr,$stream.read(1).unpack("C"));
}
from_floatbits(int_from_bytes(@arr));
}
multi submethod decode_schema(Avro::Double $schema, Stream $stream) {
my @arr = ();
for 1..8 -> $i {
push(@arr,$stream.read(1).unpack("C"));
}
from_doublebits(int_from_bytes(@arr));
}
multi submethod dispatch(Stream $stream, Avro::Schema $schema) {
#X::Avro::DecodeFail.new(:note("Empty Stream")).throw() if $stream.eof;
return self.decode_schema($schema,$stream);
}
multi submethod dispatch(IO::Handle:D $handle, Avro::Schema $schema) {
my HandleStream $stream = HandleStream.new(:handle($handle));
self.dispatch($stream,$schema);
}
multi submethod dispatch(Blob $blob, Avro::Schema $schema) {
my BlobStream $stream = BlobStream.new(:blob($blob));
self.dispatch($stream,$schema);
}
method decode(Mu $input, Avro::Schema $schema) { # dispatch within class due to perl6 bug #125658
return self.dispatch($input,$schema);
CATCH { default { X::Avro::DecodeFail.new(:note($schema.type)).throw() }}
}
};
#== Class =============================
# * JSONDecoder
#======================================
class JSONDecoder does Decoder {
method decode(Mu:D , Avro::Schema --> Mu:D) { "TODO" };
};
}