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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
let fail x = Printf.ksprintf failwith x
type arg = {id : string; name : string; descr : string option}
type 'a tree = Static of 'a static | Dynamic of Json.t
and 'a static = {
get_service : 'a option;
post_service : 'a option;
put_service : 'a option;
delete_service : 'a option;
patch_service : 'a option;
subdirs : 'a subdirs option;
}
and 'a subdirs =
| Suffixes of 'a suffix list
| Dynamic_dispatch of {arg : arg; tree : 'a tree}
and 'a suffix = {name : string; tree : 'a tree}
let opt_mandatory name json = function
| None -> Json.error json "missing mandatory value: %s" name
| Some x -> x
let parse_arg (json : Json.t) : arg =
Json.as_record json @@ fun get ->
{
id = get "id" |> opt_mandatory "id" json |> Json.as_string;
name = get "name" |> opt_mandatory "name" json |> Json.as_string;
descr = get "descr" |> Option.map Json.as_string;
}
let rec parse_tree (json : Json.t) : Json.t tree =
match Json.as_variant json with
| "static", static ->
Json.as_record static @@ fun get ->
Static
{
get_service = get "get_service";
post_service = get "post_service";
put_service = get "put_service";
delete_service = get "delete_service";
patch_service = get "patch_service";
subdirs = get "subdirs" |> Option.map parse_subdirs;
}
| "dynamic", dynamic -> Dynamic dynamic
| name, _ -> failwith ("parse_tree: don't know what to do with: " ^ name)
and parse_subdirs (json : Json.t) : Json.t subdirs =
match Json.as_variant json with
| "suffixes", suffixes ->
Suffixes (suffixes |> Json.as_list |> List.map parse_suffix)
| "dynamic_dispatch", dynamic_dispatch ->
Json.as_record dynamic_dispatch @@ fun get ->
Dynamic_dispatch
{
arg =
get "arg"
|> opt_mandatory "dynamic_dispatch.arg" dynamic_dispatch
|> parse_arg;
tree =
get "tree"
|> opt_mandatory "dynamic_dispatch.tree" dynamic_dispatch
|> parse_tree;
}
| name, _ -> failwith ("parse_subdir: don't know what to do with: " ^ name)
and parse_suffix (json : Json.t) : Json.t suffix =
Json.as_record json @@ fun get ->
{
name = get "name" |> opt_mandatory "suffixes.name" json |> Json.as_string;
tree = get "tree" |> opt_mandatory "suffixes.tree" json |> parse_tree;
}
type path_item = PI_static of string | PI_dynamic of arg
let show_path_item = function
| PI_static name -> name
| PI_dynamic arg -> "{" ^ arg.name ^ "}"
type path = path_item list
let show_path path = "/" ^ String.concat "/" (List.map show_path_item path)
type 'a endpoint = {
path : path;
get : 'a option;
post : 'a option;
put : 'a option;
delete : 'a option;
patch : 'a option;
}
let rec flatten_tree path acc tree =
match tree with
| Static static -> flatten_static path acc static
| Dynamic _ ->
acc
and flatten_static path acc static =
let acc =
match
( static.get_service,
static.post_service,
static.put_service,
static.delete_service,
static.patch_service )
with
| None, None, None, None, None -> acc
| _ ->
let endpoint =
{
path = List.rev path;
get = static.get_service;
post = static.post_service;
put = static.put_service;
delete = static.delete_service;
patch = static.patch_service;
}
in
endpoint :: acc
in
match static.subdirs with
| None -> acc
| Some subdirs -> flatten_subdirs path acc subdirs
and flatten_subdirs path acc subdirs =
match subdirs with
| Suffixes suffixes -> List.fold_left (flatten_suffix path) acc suffixes
| Dynamic_dispatch {arg; tree} ->
flatten_tree (PI_dynamic arg :: path) acc tree
and flatten_suffix path acc suffix =
flatten_tree (PI_static suffix.name :: path) acc suffix.tree
let flatten tree = flatten_tree [] [] tree |> List.rev
type schemas = {json_schema : Json.t; binary_schema : Json.t}
type query_parameter_kind =
| Optional of {name : string}
| Multi of {name : string}
| Single of {name : string}
| Flag
type query_parameter = {
id : string option;
name : string;
description : string option;
kind : query_parameter_kind;
}
type service = {
meth : Method.t;
path : path_item list;
description : string;
query : query_parameter list;
input : schemas option;
output : schemas option;
error : schemas option;
}
let parse_schemas (json : Json.t) : schemas =
Json.as_record json @@ fun get ->
{
json_schema = get "json_schema" |> opt_mandatory "json_schema" json;
binary_schema = get "binary_schema" |> opt_mandatory "binary_schema" json;
}
let parse_path_item (json : Json.t) : path_item =
match Json.as_string_opt json with
| Some s -> PI_static s
| None -> PI_dynamic (parse_arg json)
let parse_path (json : Json.t) : path =
json |> Json.as_list |> List.map parse_path_item
let parse_query_parameter (json : Json.t) : query_parameter =
Json.as_record json @@ fun get ->
let name = get "name" |> opt_mandatory "name" json |> Json.as_string in
let description = get "description" |> Option.map Json.as_string in
let kind, id, descr =
(get "kind" |> opt_mandatory "kind" json |> Json.as_record) @@ fun get ->
let parse_kind_with_name make record =
Json.as_record record @@ fun get ->
let name =
get "name" |> opt_mandatory "kind.name" json |> Json.as_string
in
( make name,
get "id" |> Option.map Json.as_string,
get "descr" |> Option.map Json.as_string )
in
match (get "optional", get "multi", get "single", get "flag") with
| Some optional, None, None, None ->
parse_kind_with_name (fun name -> Optional {name}) optional
| None, Some multi, None, None ->
parse_kind_with_name (fun name -> Multi {name}) multi
| None, None, Some single, None ->
parse_kind_with_name (fun name -> Single {name}) single
| None, None, None, Some flag ->
let () =
Json.as_record flag @@ fun _get ->
()
in
(Flag, None, None)
| _ -> fail "unsupported kind for query parameter %s" name
in
let description =
match (description, descr) with
| None, None -> None
| (Some _ as x), None | None, (Some _ as x) -> x
| Some x, Some y -> Some (y ^ " " ^ x)
in
{id; name; description; kind}
let parse_service (json : Json.t) : service =
Json.as_record json @@ fun get ->
{
meth =
get "meth" |> opt_mandatory "meth" json |> Json.as_string
|> Method.of_http_string;
path = get "path" |> opt_mandatory "path" json |> parse_path;
description =
get "description" |> Option.map Json.as_string
|> Option.value ~default:"(no description)";
query =
get "query" |> opt_mandatory "query" json |> Json.as_list
|> List.map parse_query_parameter;
input = get "input" |> Option.map parse_schemas;
output = get "output" |> Option.map parse_schemas;
error = get "error" |> Option.map parse_schemas;
}
let rec map_tree f tree =
match tree with
| Static static -> Static (map_static f static)
| Dynamic json -> Dynamic json
and map_static f static =
{
get_service = Option.map f static.get_service;
post_service = Option.map f static.post_service;
put_service = Option.map f static.put_service;
delete_service = Option.map f static.delete_service;
patch_service = Option.map f static.patch_service;
subdirs = Option.map (map_subdirs f) static.subdirs;
}
and map_subdirs f subdirs =
match subdirs with
| Suffixes suffixes -> Suffixes (List.map (map_suffix f) suffixes)
| Dynamic_dispatch {arg; tree} ->
Dynamic_dispatch {arg; tree = map_tree f tree}
and map_suffix f suffix = {name = suffix.name; tree = map_tree f suffix.tree}
let parse_services = map_tree parse_service