-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoperation.v
More file actions
135 lines (122 loc) · 3.11 KB
/
operation.v
File metadata and controls
135 lines (122 loc) · 3.11 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
123
124
125
126
127
128
129
130
131
132
133
134
135
module openapi
import x.json2 { Any }
import json
pub struct Operation {
pub mut:
external_docs ExternalDocumentation
operation_id string
request_body ObjectRef<RequestBody>
tags []string
summary string
description string
parameters []ObjectRef<Parameter>
responses Responses
callbacks map[string]ObjectRef<Callback>
deprecated bool
security []SecurityRequirement
servers []Server
}
pub fn (mut operation Operation) from_json(json Any) ? {
object := json.as_map()
check_required<Operation>(object, 'responses')?
for key, value in json.as_map() {
match key {
'externalDocs' {
operation.external_docs = decode<ExternalDocumentation>(value.json_str())?
}
'operationId' {
operation.operation_id = value.str()
}
'requestBody' {
operation.request_body = from_json<RequestBody>(value)?
}
'tags' {
operation.tags = decode_array_string(value.json_str())?
}
'summary' {
operation.summary = value.str()
}
'description' {
operation.description = value.str()
}
'parameters' {
operation.parameters = decode<[]ObjectRef<Parameter>>(value.json_str())?
}
'responses' {
operation.responses = decode<Responses>(value.json_str())?
}
'callbacks' {
operation.callbacks = decode_map_sumtype<Callback>(value.json_str(), fake_predicat)?
}
'deprecated' {
operation.deprecated = value.bool()
}
'security' {
operation.security = decode_array<SecurityRequirement>(value.json_str())?
}
'servers' {
operation.servers = decode_array<Server>(value.json_str())?
}
else {}
}
}
operation.validate(object)?
}
fn (mut operation Operation) validate(object map[string]Any) ? {
mut checked := map[string]string{}
for parameter in operation.parameters {
if parameter is Reference {
continue
}
param := parameter as Parameter
if param.name in checked && checked[param.name] == param.location {
return error('Failed Operation decoding: parameter with identical "name" and "in" found.')
}
checked[param.name] = param.location
}
}
pub fn (operation Operation) get_path_parameters() []Parameter {
mut parameters := []Parameter{}
for parameter in operation.parameters {
if parameter is Parameter {
param := Parameter{
...parameter
}
if param.location == 'path' {
parameters << param
}
}
}
return parameters
}
pub fn (operation Operation) get_request_body() RequestBody {
if operation.request_body is RequestBody {
return RequestBody{
...operation.request_body
}
}
return RequestBody{}
}
pub struct ExternalDocumentation {
pub mut:
description string
url string
}
pub fn (mut external_doc ExternalDocumentation) from_json(json Any) ? {
object := json.as_map()
check_required<ExternalDocumentation>(object, 'url')?
for key, value in object {
match key {
'description' {
external_doc.description = value.str()
}
'url' {
external_doc.url = value.str()
}
else {}
}
}
if external_doc.url != '' && !check_email_regex(external_doc.url) {
return error('Failed ExternalDocumentation decoding: "url" do not match url regex expression.')
}
}