v / vlib / json / tests / json_decode_struct_with_default_test.v
34 lines · 30 sloc · 912 bytes · 98f13ef491af19b932c15b16dfe10e6c3cdc565f
Raw
1import json
2
3pub struct Response {
4pub:
5 results []Result = []Result{len: 0} @[json: list]
6 tags []string = []string{len: 0} @[json: tags]
7 kind string @[json: result_type]
8}
9
10pub struct Result {
11pub:
12 id int @[json: defid]
13 author string @[json: author]
14 definition string @[json: definition]
15 link string @[json: permalink]
16 thumbs_down int @[json: thumbs_down]
17 thumbs_up int @[json: thumbs_up]
18 word string @[json: word]
19 date string @[json: written_on]
20 audio_samples []string @[json: sound_urls]
21 example string @[json: example]
22}
23
24pub fn define(word string) !&Response {
25 resp := '{"list":[{"defid":3439287}]}'
26 response := json.decode(Response, resp) or { return err }
27 return &response
28}
29
30fn test_main() {
31 response := define('')!
32 assert response.results.len > 0
33 assert response.results[0].id == 3439287
34}
35