-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparameter.v
More file actions
113 lines (103 loc) · 2.4 KB
/
parameter.v
File metadata and controls
113 lines (103 loc) · 2.4 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
module openapi
import x.json2 { Any }
import json
pub struct Parameter {
pub mut:
name string
location string
description string
required bool
deprecated bool
allow_empty_value bool
style string
explode bool
allow_reserved bool
schema ObjectRef<Schema>
example Any
examples map[string]ObjectRef<Example>
content map[string]MediaType
}
pub fn (mut parameter Parameter) from_json(json Any) ? {
object := json.as_map()
for key, value in json.as_map() {
match key {
'name' {
parameter.name = value.str()
}
'in' {
parameter.location = value.str()
}
'description' {
parameter.description = value.str()
}
'required' {
parameter.required = value.bool()
}
'deprecated' {
parameter.deprecated = value.bool()
}
'allowEmptyValue' {
parameter.allow_empty_value = value.bool()
}
'style' {
parameter.style = value.str()
}
'explode' {
parameter.explode = value.bool()
}
'allowReserved' {
parameter.allow_reserved = value.bool()
}
'schema' {
parameter.schema = from_json<Schema>(value)?
}
'example' {
parameter.example = value
}
'examples' {
parameter.examples = decode_map_sumtype<Example>(value.json_str(), fake_predicat)?
}
'content' {
parameter.content = decode_map<MediaType>(value.json_str())?
}
else {}
}
}
parameter.validate(object)?
}
fn (mut parameter Parameter) validate(object map[string]Any) ? {
check_required<Parameter>(object, 'in', 'name')?
if 'example' in object && 'examples' in object {
return error('Failed Parameter decoding: "example" and "examples" are mutually exclusives')
}
mut default_style := ''
match parameter.location {
'query' {
default_style = 'form'
}
'header' {
default_style = 'simple'
}
'path' {
if !parameter.required {
return error('Failed Parameter decoding: "required" must be true in this case.')
}
default_style = 'simple'
}
'decode' {
default_style = 'form'
}
else {
return error('Failed Parameter decoding: "in" value not valid.')
}
}
if 'style' !in object {
parameter.style = default_style
}
if parameter.style == 'form' && 'explode' !in object {
parameter.explode = true
}
if parameter.content.len > 1 {
return error('Failed Parameter decoding: "content" must contain only one entry.')
}
}