v

Зеркало из https://github.com/vlang/v
Форк
0
/x
/
decode_struct_test.v 
83 строки · 2.3 Кб
1
import x.json2.decoder2 as json
2
import time
3

4
const fixed_time = time.new(
5
	year:   2022
6
	month:  3
7
	day:    11
8
	hour:   13
9
	minute: 54
10
	second: 25
11
)
12

13
type StringAlias = string
14
type BoolAlias = bool
15
type IntAlias = int
16
type TimeAlias = time.Time
17
type StructAlias = StructType[int]
18
type EnumAlias = Enumerates
19

20
type SumTypes = StructType[string] | []SumTypes | []string | bool | string | time.Time | u32
21

22
enum Enumerates {
23
	a
24
	b
25
	c
26
	d
27
	e = 99
28
	f
29
}
30

31
struct StructType[T] {
32
mut:
33
	val T
34
}
35

36
struct StructTypeOption[T] {
37
mut:
38
	val ?T
39
}
40

41
struct StructTypePointer[T] {
42
mut:
43
	val &T
44
}
45

46
fn test_types() {
47
	assert json.decode[StructType[string]]('{"val": ""}')!.val == ''
48

49
	assert json.decode[StructType[string]]('{"val": "2"}')!.val == '2'
50

51
	assert json.decode[StructType[int]]('{"val": 2}')!.val == 2
52

53
	assert json.decode[StructType[map[string]string]]('{"val": {"val": "test"}}')!.val['val'] == 'test'
54

55
	assert json.decode[StructType[Enumerates]]('{"val": 0}')!.val == Enumerates.a
56
	assert json.decode[StructType[Enumerates]]('{"val": 1}')!.val == Enumerates.b
57

58
	assert json.decode[StructType[IntAlias]]('{"val": 2}')!.val == IntAlias(2)
59
	assert json.decode[StructType[StringAlias]]('{"val": "2"}')!.val == StringAlias('2')
60

61
	assert json.decode[StructType[time.Time]]('{"val": "2022-03-11T13:54:25.000Z"}')!.val.year == fixed_time.year
62
	assert json.decode[StructType[time.Time]]('{"val": "2022-03-11T13:54:25.000Z"}')!.val.month == fixed_time.month
63
	assert json.decode[StructType[time.Time]]('{"val": "2022-03-11T13:54:25.000Z"}')!.val.day == fixed_time.day
64
	assert json.decode[StructType[time.Time]]('{"val": "2022-03-11T13:54:25.000Z"}')!.val.hour == fixed_time.hour
65
	assert json.decode[StructType[time.Time]]('{"val": "2022-03-11T13:54:25.000Z"}')!.val.minute == fixed_time.minute
66
	assert json.decode[StructType[time.Time]]('{"val": "2022-03-11T13:54:25.000Z"}')!.val.second == fixed_time.second
67
	assert json.decode[StructType[time.Time]]('{"val": "2022-03-11T13:54:25.000Z"}')!.val.unix() == fixed_time.unix()
68
}
69

70
fn test_option_types() {
71
	if x := json.decode[StructTypeOption[string]]('{}')!.val {
72
		assert false, 'Should return none'
73
	} else {
74
		assert err.msg() == ''
75
		assert true
76
	}
77

78
	if x := json.decode[StructTypeOption[string]]('{"val": "2"}')!.val {
79
		assert x == '2'
80
	} else {
81
		assert false, 'Should not return none'
82
	}
83
}
84

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.