v2 / vlib / v / tests / options / option_if_option_test.v
40 lines · 36 sloc · 954 bytes · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1import maps
2import x.json2
3
4pub type Locale = string
5
6pub struct ApplicationCommandOptionChoice {
7pub:
8 name string
9 name_localizations ?map[Locale]string
10}
11
12pub fn ApplicationCommandOptionChoice.parse(j json2.Any) !ApplicationCommandOptionChoice {
13 match j {
14 map[string]json2.Any {
15 return ApplicationCommandOptionChoice{
16 name: j['name']! as string
17 name_localizations: if m := j['name_localizations'] {
18 maps.to_map[string, json2.Any, Locale, string](m as map[string]json2.Any, fn (k string, v json2.Any) (Locale, string) {
19 return k, v as string
20 })
21 } else {
22 none
23 }
24 }
25 }
26 else {
27 return error('expected application command option choice to be object, got ${j.type_name()}')
28 }
29 }
30}
31
32fn test_main() {
33 var := ApplicationCommandOptionChoice.parse({
34 'name': json2.Any('foo')
35 'name_localizations': {
36 'name': json2.Any('foo')
37 }
38 })!
39 assert dump(var) == var
40}
41