/
encode_and_decode_test.v
255 lines (231 loc) · 5.04 KB
/
encode_and_decode_test.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
import toml
enum JobTitle {
worker
executive
manager
}
struct Pet {
name string
nicknames []string
age u64
income int
height f32
has_furr bool
title JobTitle
address Address
// *¹ Currently it is only possible to decode a single nested struct generically.
// As soon as we decode another nested struct (e.g. within this struct, like `contact` below)
// or only one nested struct within another struct, it results in wrong values or errors.
// Related issue: https://github.com/vlang/v/issues/18110
// contact Contact
}
struct Address {
street string
city string
}
// *¹
/*
struct Contact {
phone string
}*/
struct Employee {
mut:
name string
age int
salary f32
is_human bool
title JobTitle
}
struct Arrs {
strs []string
bools []bool
ints []int
i64s []i64
u64s []u64
f32s []f32
f64s []f64
dts []toml.DateTime
dates []toml.Date
times []toml.Time
}
fn test_encode_and_decode() {
// *¹
// p := Pet{'Mr. Scratchy McEvilPaws', ['Freddy', 'Fred', 'Charles'], 8, -1, 0.8, true, .manager, Address{'1428 Elm Street', 'Springwood'}, Contact{'123-456-7890'}}
p := Pet{'Mr. Scratchy McEvilPaws', ['Freddy', 'Fred', 'Charles'], 8, -1, 0.8, true, .manager, Address{'1428 Elm Street', 'Springwood'}}
s := 'name = "Mr. Scratchy McEvilPaws"
nicknames = [
"Freddy",
"Fred",
"Charles"
]
age = 8
income = -1
height = 0.8
has_furr = true
title = 2
address = { street = "1428 Elm Street", city = "Springwood" }'
// contact = { phone = "123-456-7890" }' // *¹
assert toml.encode[Pet](p) == s
assert toml.decode[Pet](s)! == p
}
pub fn (e Employee) to_toml() string {
mut mp := map[string]toml.Any{}
mp['name'] = toml.Any(e.name)
mp['age'] = toml.Any(e.age)
mp['is_human'] = toml.Any(e.is_human)
// Change some values to assert that the custom method is used instead of generic encoding.
mp['salary'] = toml.Any(f32(e.salary) + 5000.0)
mp['title'] = toml.Any(int(e.title) + 1)
return mp.to_toml()
}
pub fn (mut e Employee) from_toml(any toml.Any) {
mp := any.as_map()
e.name = mp['name'] or { toml.Any('') }.string()
e.age = mp['age'] or { toml.Any(0) }.int()
e.is_human = mp['is_human'] or { toml.Any(false) }.bool()
// Change some values to assert that the custom method is used instead of generic decoding.
e.salary = mp['salary'] or { toml.Any(0) }.f32() - 15000.0
e.title = unsafe { JobTitle(mp['title'] or { toml.Any(0) }.int() - 2) }
}
fn test_custom_encode_and_decode() {
x := Employee{'Peter', 28, 95000.5, true, .executive}
s := toml.encode[Employee](x)
eprintln('Employee x: ${s}')
assert s == r'name = "Peter"
age = 28
is_human = true
salary = 100000.5
title = 2'
y := toml.decode[Employee](s) or {
println(err)
assert false
return
}
eprintln('Employee y: ${y}')
assert y.name == 'Peter'
assert y.age == 28
assert y.salary == 85000.5
assert y.is_human == true
assert y.title == .worker
}
struct Example1 {
arr []Problem
}
struct Example2 {
arr []Problem
}
struct Problem {
x int
}
pub fn (example Example1) to_toml() string {
return '[This is Valid]'
}
pub fn (problem Problem) to_toml() string {
return 'a problem'
}
fn test_custom_encode_of_complex_struct() {
assert toml.encode(Example1{}) == '[This is Valid]'
assert toml.encode(Example2{[Problem{}, Problem{}]}) == 'arr = [
"a problem",
"a problem"
]'
}
fn test_array_encode_decode() {
a := Arrs{
strs: ['foo', 'bar']
bools: [true, false]
ints: [-1, 2]
i64s: [i64(-2)]
u64s: [u64(123)]
f32s: [f32(1.0), f32(2.5)]
f64s: [100000.5, -123.0]
dts: [toml.DateTime{'1979-05-27T07:32:00Z'}, toml.DateTime{'1979-05-27T07:32:00Z'}]
dates: [toml.Date{'1979-05-27'}, toml.Date{'2022-12-31'}]
times: [toml.Time{'07:32:59'}, toml.Time{'17:32:04'}]
}
s := 'strs = [
"foo",
"bar"
]
bools = [
true,
false
]
ints = [
-1,
2
]
i64s = [
-2
]
u64s = [
123
]
f32s = [
1.0,
2.5
]
f64s = [
100000.5,
-123.0
]
dts = [
1979-05-27T07:32:00Z,
1979-05-27T07:32:00Z
]
dates = [
1979-05-27,
2022-12-31
]
times = [
07:32:59,
17:32:04
]'
assert toml.encode[Arrs](a) == s
assert toml.decode[Arrs](s)! == a
}
fn test_decode_doc() {
doc := toml.parse_text('name = "Peter"
age = 28
is_human = true
salary = 100000.5
title = 2')!
e := doc.decode[Employee]()!
assert e.name == 'Peter'
assert e.age == 28
assert e.salary == 100000.5
assert e.is_human == true
assert e.title == .manager
}
fn test_unsupported_type() {
s := 'name = "Peter"'
err_msg := 'toml.decode: expected struct, found '
if _ := toml.decode[string](s) {
assert false
} else {
assert err.msg() == err_msg + 'string'
}
if _ := toml.decode[[]string](s) {
assert false
} else {
assert err.msg() == err_msg + '[]string'
}
if _ := toml.decode[int](s) {
assert false
} else {
assert err.msg() == err_msg + 'int'
}
if _ := toml.decode[[]f32](s) {
assert false
} else {
assert err.msg() == err_msg + '[]f32'
}
// ...
doc := toml.parse_text('name = "Peter"')!
assert doc.value('name').string() == 'Peter'
if _ := doc.decode[string]() {
assert false
} else {
assert err.msg() == 'Doc.decode: expected struct, found string'
}
}