-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.v
More file actions
122 lines (98 loc) · 2.63 KB
/
helpers.v
File metadata and controls
122 lines (98 loc) · 2.63 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
module openapi
import x.json2 { Any, raw_decode }
import json
import regex
pub fn decode<T>(src string) ?T {
res := raw_decode(src)?
mut typ := T{}
typ.from_json(res)?
return typ
}
// ---------------------------------------- //
pub fn decode_array<T>(src string) ?[]T {
json := raw_decode(src)?
mut typ := []T{}
for value in json.arr() {
typ << decode<T>(value.json_str())?
}
return typ
}
pub fn decode_array_string(src string) ?[]string {
json := raw_decode(src)?
return json.arr().map(fn (elt Any) string {
return elt.str()
})
}
pub fn decode_array_any(src string) ?[]Any {
json := raw_decode(src)?
return json.arr().map(fn (elt Any) Any {
return elt
})
}
// ---------------------------------------- //
pub fn decode_map<T>(src string) ?map[string]T {
json := raw_decode(src)?
mut typ := map[string]T{}
for key, value in json.as_map() {
typ[key] = decode<T>(value.json_str())?
}
return typ
}
pub fn decode_map_string(src string) ?map[string]string {
json := raw_decode(src)?
mut typ := map[string]string{}
for key, value in json.as_map() {
typ[key] = value.str()
}
return typ
}
pub fn decode_map_any(src string) ?map[string]Any {
json := raw_decode(src)?
mut typ := map[string]Any{}
for key, value in json.as_map() {
typ[key] = value
}
return typ
}
pub fn decode_map_sumtype<T>(src string, verif fn (string) bool) ?map[string]ObjectRef<T> {
json := raw_decode(src)?
mut typ := map[string]ObjectRef<T>{}
for key, value in json.as_map() {
typ[key] = from_json<T>(value)?
}
return typ
}
// ---------------------------------------- //
pub fn check_required<T>(object map[string]Any, required_fields ...string) ? {
for field in required_fields {
if field !in object {
return error('Failed $T.name decoding: "$field" not specified !')
}
}
}
// ---------------------------------------- //
fn fake_predicat(str string) bool {
return true
}
fn check_key_regex(str string) bool {
mut reg := regex.regex_opt(r'^[\w\.\-]+$') or { panic('Failed to initialize regex expression') }
return reg.matches_string(str)
}
fn check_url_regex(str string) bool {
mut reg := regex.regex_opt(r'^(https?://)?(www\.)?[\w\-@:%\+~#=]{2,256}\.[\a]{2,6}[\-\w@:%\+~#?&//=\.]*$') or {
panic('Failed to initialize regex expression')
}
return reg.matches_string(str)
}
fn check_email_regex(str string) bool {
mut reg := regex.regex_opt(r'^\S+@\S+\.\S+$') or {
panic('Failed to initialize regex expression')
}
return reg.matches_string(str)
}
fn check_http_code_regex(str string) bool {
mut reg := regex.regex_opt(r'^([1-5][\d][\d])|([1-5]XX)$') or {
panic('Failed to initialize regex expression')
}
return reg.matches_string(str)
}