-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
decoder.v
477 lines (446 loc) · 12.5 KB
/
decoder.v
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module json2
import time
fn format_message(msg string, line int, column int) string {
return '[x.json2] ${msg} (${line}:${column})'
}
pub struct DecodeError {
line int
column int
message string
}
// code returns the error code of DecodeError
pub fn (err DecodeError) code() int {
return 3
}
// msg returns the message of the DecodeError
pub fn (err DecodeError) msg() string {
return format_message(err.message, err.line, err.column)
}
pub struct InvalidTokenError {
DecodeError
token Token
expected TokenKind
}
// code returns the error code of the InvalidTokenError
pub fn (err InvalidTokenError) code() int {
return 2
}
// msg returns the message of the InvalidTokenError
pub fn (err InvalidTokenError) msg() string {
footer_text := if err.expected != .none_ { ', expecting `${err.expected}`' } else { '' }
return format_message('invalid token `${err.token.kind}`${footer_text}', err.token.line,
err.token.full_col())
}
pub struct UnknownTokenError {
DecodeError
token Token
kind ValueKind = .unknown
}
// code returns the error code of the UnknownTokenError
pub fn (err UnknownTokenError) code() int {
return 1
}
// msg returns the error message of the UnknownTokenError
pub fn (err UnknownTokenError) msg() string {
return format_message("unknown token '${err.token.lit}' when decoding ${err.kind}.",
err.token.line, err.token.full_col())
}
struct Parser {
pub mut:
scanner &Scanner = unsafe { nil }
prev_tok Token
tok Token
next_tok Token
n_level int
convert_type bool = true
}
fn (mut p Parser) next() {
p.prev_tok = p.tok
p.tok = p.next_tok
p.next_tok = p.scanner.scan()
}
fn (mut p Parser) next_with_err() ! {
p.next()
if p.tok.kind == .error {
return DecodeError{
line: p.tok.line
column: p.tok.full_col()
message: p.tok.lit.bytestr()
}
}
}
// TODO: copied from v.util to avoid the entire module and its functions
// from being imported. remove later once -skip-unused is enabled by default.
// skip_bom - skip Byte Order Mark (BOM)
// The UTF-8 BOM is a sequence of Bytes at the start of a text-stream (EF BB BF or \ufeff)
// that allows the reader to reliably determine if file is being encoded in UTF-8.
fn skip_bom(file_content string) string {
mut raw_text := file_content
// BOM check
if raw_text.len >= 3 {
unsafe {
c_text := raw_text.str
if c_text[0] == 0xEF && c_text[1] == 0xBB && c_text[2] == 0xBF {
// skip three BOM bytes
offset_from_begin := 3
raw_text = tos(c_text[offset_from_begin], vstrlen(c_text) - offset_from_begin)
}
}
}
return raw_text
}
// new_parser - create a instance of Parser{}
fn new_parser(srce string, convert_type bool) Parser {
src := skip_bom(srce)
return Parser{
scanner: &Scanner{
text: src.bytes()
}
convert_type: convert_type
}
}
// Decodes a JSON string into an `Any` type. Returns an option.
pub fn raw_decode(src string) !Any {
mut p := new_parser(src, true)
return p.decode()
}
// Same with `raw_decode`, but skips the type conversion for certain types when decoding a certain value.
pub fn fast_raw_decode(src string) !Any {
mut p := new_parser(src, false)
return p.decode()
}
// decode is a generic function that decodes a JSON string into the target type.
pub fn decode[T](src string) !T {
res := raw_decode(src)!.as_map()
return decode_struct[T](T{}, res)
}
// decode_array is a generic function that decodes a JSON string into the array target type.
pub fn decode_array[T](src string) ![]T {
res := raw_decode(src)!.as_map()
return decode_struct_array(T{}, res)
}
// decode_struct_array is a generic function that decodes a JSON map into array struct T.
fn decode_struct_array[T](_ T, res map[string]Any) ![]T {
$if T is $struct {
mut arr := []T{}
for v in res.values() {
arr << decode_struct[T](T{}, v.as_map())!
}
return arr
} $else {
return error("The type `${T.name}` can't be decoded.")
}
}
// decode_struct is a generic function that decodes a JSON map into the struct T.
fn decode_struct[T](_ T, res map[string]Any) !T {
mut typ := T{}
$if T is $struct {
$for field in T.fields {
mut skip_field := false
mut json_name := field.name
for attr in field.attrs {
if attr.contains('json: ') {
json_name = attr.replace('json: ', '')
if json_name == '-' {
skip_field = true
}
break
}
}
if !skip_field {
$if field.is_enum {
if v := res[json_name] {
typ.$(field.name) = v.int()
} else {
$if field.is_option {
typ.$(field.name) = none
}
}
} $else $if field.typ is u8 {
typ.$(field.name) = res[json_name]!.u64()
} $else $if field.typ is u16 {
typ.$(field.name) = res[json_name]!.u64()
} $else $if field.typ is u32 {
typ.$(field.name) = res[json_name]!.u64()
} $else $if field.typ is u64 {
typ.$(field.name) = res[json_name]!.u64()
} $else $if field.typ is int {
typ.$(field.name) = res[json_name]!.int()
} $else $if field.typ is i8 {
typ.$(field.name) = res[json_name]!.int()
} $else $if field.typ is i16 {
typ.$(field.name) = res[json_name]!.int()
} $else $if field.typ is i32 {
typ.$(field.name) = i32(res[field.name]!.i32())
} $else $if field.typ is i64 {
typ.$(field.name) = res[json_name]!.i64()
} $else $if field.typ is ?u8 {
if json_name in res {
typ.$(field.name) = ?u8(res[json_name]!.i64())
}
} $else $if field.typ is ?i8 {
if json_name in res {
typ.$(field.name) = ?i8(res[json_name]!.i64())
}
} $else $if field.typ is ?u16 {
if json_name in res {
typ.$(field.name) = ?u16(res[json_name]!.i64())
}
} $else $if field.typ is ?i16 {
if json_name in res {
typ.$(field.name) = ?i16(res[json_name]!.i64())
}
} $else $if field.typ is ?u32 {
if json_name in res {
typ.$(field.name) = ?u32(res[json_name]!.i64())
}
} $else $if field.typ is ?i32 {
if json_name in res {
typ.$(field.name) = ?i32(res[json_name]!.i64())
}
} $else $if field.typ is ?u64 {
if json_name in res {
typ.$(field.name) = ?u64(res[json_name]!.i64())
}
} $else $if field.typ is ?i64 {
if json_name in res {
typ.$(field.name) = ?i64(res[json_name]!.i64())
}
} $else $if field.typ is ?int {
if json_name in res {
typ.$(field.name) = ?int(res[json_name]!.i64())
}
} $else $if field.typ is f32 {
typ.$(field.name) = res[json_name]!.f32()
} $else $if field.typ is ?f32 {
if json_name in res {
typ.$(field.name) = res[json_name]!.f32()
}
} $else $if field.typ is f64 {
typ.$(field.name) = res[json_name]!.f64()
} $else $if field.typ is ?f64 {
if json_name in res {
typ.$(field.name) = res[json_name]!.f64()
}
} $else $if field.typ is bool {
typ.$(field.name) = res[json_name]!.bool()
} $else $if field.typ is ?bool {
if json_name in res {
typ.$(field.name) = res[json_name]!.bool()
}
} $else $if field.typ is string {
typ.$(field.name) = res[json_name]!.str()
} $else $if field.typ is ?string {
if json_name in res {
typ.$(field.name) = res[json_name]!.str()
}
} $else $if field.typ is time.Time {
typ.$(field.name) = res[json_name]!.to_time()!
} $else $if field.typ is ?time.Time {
if json_name in res {
typ.$(field.name) = res[json_name]!.to_time()!
}
} $else $if field.is_array {
arr := res[field.name]! as []Any
// vfmt off
match field.typ {
[]bool { typ.$(field.name) = arr.map(it.bool()) }
[]?bool { typ.$(field.name) = arr.map(?bool(it.bool())) }
[]f32 { typ.$(field.name) = arr.map(it.f32()) }
[]?f32 { typ.$(field.name) = arr.map(?f32(it.f32())) }
[]f64 { typ.$(field.name) = arr.map(it.f64()) }
[]?f64 { typ.$(field.name) = arr.map(?f64(it.f64())) }
[]i8 { typ.$(field.name) = arr.map(it.i8()) }
[]?i8 { typ.$(field.name) = arr.map(?i8(it.i8())) }
[]i16 { typ.$(field.name) = arr.map(it.i16()) }
[]?i16 { typ.$(field.name) = arr.map(?i16(it.i16())) }
[]i32 { typ.$(field.name) = arr.map(it.i32()) }
[]?i32 { typ.$(field.name) = arr.map(?i32(it.i32())) }
[]i64 { typ.$(field.name) = arr.map(it.i64()) }
[]?i64 { typ.$(field.name) = arr.map(?i64(it.i64())) }
[]int { typ.$(field.name) = arr.map(it.int()) }
[]?int { typ.$(field.name) = arr.map(?int(it.int())) }
[]string { typ.$(field.name) = arr.map(it.str()) }
[]?string { typ.$(field.name) = arr.map(?string(it.str())) }
// NOTE: Using `!` on `to_time()` inside the array method causes a builder error - 2024/04/01.
[]time.Time { typ.$(field.name) = arr.map(it.to_time() or { time.Time{} }) }
[]?time.Time { typ.$(field.name) = arr.map(?time.Time(it.to_time() or { time.Time{} })) }
[]u8 { typ.$(field.name) = arr.map(it.u64()) }
[]?u8 { typ.$(field.name) = arr.map(?u8(it.u64())) }
[]u16 { typ.$(field.name) = arr.map(it.u64()) }
[]?u16 { typ.$(field.name) = arr.map(?u16(it.u64())) }
[]u32 { typ.$(field.name) = arr.map(it.u64()) }
[]?u32 { typ.$(field.name) = arr.map(?u32(it.u64())) }
[]u64 { typ.$(field.name) = arr.map(it.u64()) }
[]?u64 { typ.$(field.name) = arr.map(?u64(it.u64())) }
else {}
}
// vfmt on
} $else $if field.is_struct {
typ.$(field.name) = decode_struct(typ.$(field.name), res[field.name]!.as_map())!
} $else $if field.is_alias {
} $else $if field.is_map {
} $else {
return error("The type of `${field.name}` can't be decoded. Please open an issue at https://github.com/vlang/v/issues/new/choose")
}
}
}
} $else $if T is $map {
for k, v in res {
// // TODO: make this work to decode types like `map[string]StructType[bool]`
// $if typeof(typ[k]).idx is string {
// typ[k] = v.str()
// } $else $if typeof(typ[k]).idx is $struct {
// }
match v {
string {
typ[k] = v.str()
}
else {}
}
}
} $else {
return error("The type `${T.name}` can't be decoded.")
}
return typ
}
// decode - decodes provided JSON
pub fn (mut p Parser) decode() !Any {
p.next()
p.next_with_err()!
fi := p.decode_value()!
if p.tok.kind != .eof {
return InvalidTokenError{
token: p.tok
}
}
return fi
}
fn (mut p Parser) decode_value() !Any {
if p.n_level + 1 == 500 {
return DecodeError{
message: 'reached maximum nesting level of 500'
}
}
match p.tok.kind {
// `[`
.lsbr {
return p.decode_array()
}
// `{`
.lcbr {
return p.decode_object()
}
.int_, .float {
tl := p.tok.lit.bytestr()
kind := p.tok.kind
p.next_with_err()!
if p.convert_type {
$if !nofloat ? {
if kind == .float {
return Any(tl.f64())
}
}
return Any(tl.i64())
}
return Any(tl)
}
.bool_ {
lit := p.tok.lit.bytestr()
p.next_with_err()!
if p.convert_type {
return Any(lit.bool())
}
return Any(lit)
}
.null {
p.next_with_err()!
if p.convert_type {
return Any(null)
}
return Any('null')
}
.str_ {
str := p.tok.lit.bytestr()
p.next_with_err()!
return Any(str)
}
else {
return InvalidTokenError{
token: p.tok
}
}
}
return InvalidTokenError{
token: p.tok
}
}
@[manualfree]
fn (mut p Parser) decode_array() !Any {
mut items := []Any{}
p.next_with_err()!
p.n_level++
// `]`
for p.tok.kind != .rsbr {
item := p.decode_value()!
items << item
if p.tok.kind == .comma {
p.next_with_err()!
if p.tok.kind == .rsbr {
return InvalidTokenError{
token: p.tok
}
}
} else if p.tok.kind != .rsbr {
return UnknownTokenError{
token: p.tok
kind: .array
}
}
}
p.next_with_err()!
p.n_level--
return Any(items)
}
fn (mut p Parser) decode_object() !Any {
mut fields := map[string]Any{}
p.next_with_err()!
p.n_level++
// `}`
for p.tok.kind != .rcbr {
// step 1 -> key
if p.tok.kind != .str_ {
return InvalidTokenError{
token: p.tok
expected: .str_
}
}
cur_key := p.tok.lit.bytestr()
p.next_with_err()!
// step 2 -> colon separator
if p.tok.kind != .colon {
return InvalidTokenError{
token: p.tok
expected: .colon
}
}
p.next_with_err()!
// step 3 -> value
fields[cur_key] = p.decode_value()!
if p.tok.kind !in [.comma, .rcbr] {
return InvalidTokenError{
token: p.tok
expected: .comma
}
} else if p.tok.kind == .comma {
p.next_with_err()!
}
}
p.next_with_err()!
// step 4 -> eof (end)
p.n_level--
return Any(fields)
}