-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinfo.v
More file actions
100 lines (85 loc) · 2.1 KB
/
info.v
File metadata and controls
100 lines (85 loc) · 2.1 KB
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
module openapi
import x.json2 { Any }
import json
pub struct Info {
pub mut:
title string
version string
terms_of_service string
description string
contact Contact
license License
}
pub fn (mut info Info) from_json(json Any) ? {
object := json.as_map()
check_required<Info>(object, 'title', 'version')?
for key, value in object {
match key {
'title' {
info.title = value.str()
}
'version' {
info.version = value.str()
}
'termsOfService' {
info.terms_of_service = value.str()
}
'description' {
info.description = value.str()
}
'contact' {
info.contact = decode<Contact>(value.json_str())?
}
'license' {
info.license = decode<License>(value.json_str())?
}
else {}
}
}
if info.terms_of_service != '' && !check_url_regex(info.terms_of_service) {
return error('Failed Info decoding: "termsOfService" do not match url regex expression')
}
}
// ---------------------------------------- //
pub struct Contact {
pub mut:
name string
url string
email string
}
pub fn (mut contact Contact) from_json(json Any) ? {
for key, value in json.as_map() {
match key {
'name' { contact.name = value.str() }
'url' { contact.url = value.str() }
'email' { contact.email = value.str() }
else {}
}
}
if contact.url != '' && !check_url_regex(contact.url) {
return error('Failed Contact decoding: "url" do not match url regex expression')
}
if contact.email != '' && !check_email_regex(contact.email) {
return error('Failed Contact decoding: "email" do not match email regex expression')
}
}
// ---------------------------------------- //
pub struct License {
pub mut:
name string
url string
}
pub fn (mut license License) from_json(json Any) ? {
object := json.as_map()
check_required<License>(object, 'name')?
for key, value in object {
match key {
'name' { license.name = value.str() }
'url' { license.url = value.str() }
else {}
}
}
if license.url != '' && !check_url_regex(license.url) {
return error('Failed License decoding: "url" do not match url regex expression')
}
}