Skip to content

Commit 48807bb

Browse files
committed
Make tests pass for paths
1 parent ffa95de commit 48807bb

5 files changed

Lines changed: 190 additions & 207 deletions

File tree

‎R/generate_pkg-paths.R‎

Lines changed: 115 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
paths,
33
api_abbr,
44
security_data,
5-
pagination_data,
5+
pagination_data = list(),
66
base_url
77
) {
88
paths_by_operation <- as_bk_data(paths)
@@ -31,7 +31,9 @@ S7::method(as_bk_data, class_paths) <- function(x) {
3131
return(list())
3232
}
3333
paths_df <- .paths_to_clean_df(x)
34-
return(tibble::deframe(paths_df))
34+
result <- purrr::pmap(paths_df, .path_row_to_list)
35+
names(result) <- paths_df$operation_id
36+
result
3537
}
3638

3739
.paths_to_clean_df <- function(x) {
@@ -50,34 +52,10 @@ S7::method(as_bk_data, class_paths) <- function(x) {
5052
x$endpoint,
5153
x$operation
5254
)
53-
x$description = .paths_fill_descriptions(x$description, x$summary)
55+
x$description <- .paths_fill_descriptions(x$description, x$summary)
5456
# TODO: Deal with x$global_parameters if present
5557
x$parameters <- purrr::map(x$parameters, .prepare_params_df)
56-
# TODO: add breakdown by location
57-
return(tidyr::nest(x, .by = "operation_id"))
58-
}
59-
60-
## to list ---------------------------------------------------------------------
61-
.paths_endpoints_to_lists <- function(endpoints) {
62-
pmap(
63-
list(
64-
operation_id = .paths_fill_operation_id(
65-
endpoints$operation_id,
66-
endpoints$endpoint,
67-
endpoints$operation
68-
),
69-
path = endpoints$endpoint,
70-
summary = .paths_fill_summary(
71-
endpoints$summary,
72-
endpoints$endpoint,
73-
endpoints$operation
74-
),
75-
description = .paths_fill_descriptions(endpoints$description),
76-
params_df = endpoints$parameters,
77-
method = endpoints$operation
78-
),
79-
.paths_endpoint_to_list
80-
)
58+
return(x)
8159
}
8260

8361
### fill data ------------------------------------------------------------------
@@ -100,40 +78,49 @@ S7::method(as_bk_data, class_paths) <- function(x) {
10078
)
10179
}
10280

103-
### create whisker data --------------------------------------------------------
81+
.paths_fill_descriptions <- function(descriptions, summaries) {
82+
descriptions[is.na(descriptions)] <- summaries[is.na(descriptions)]
83+
descriptions[is.na(descriptions)] <- ""
84+
return(stringr::str_squish(descriptions))
85+
}
86+
87+
### create template data -------------------------------------------------------
10488

105-
.paths_endpoint_to_list <- function(
89+
.path_row_to_list <- function(
10690
operation_id,
107-
path,
91+
endpoint,
92+
operation,
10893
summary,
10994
description,
110-
params_df,
111-
method
95+
tags,
96+
parameters,
97+
...
11298
) {
113-
params_df <- .prepare_params_df(params_df)
114-
return(
115-
list(
116-
operation_id = operation_id,
117-
path = .path_as_arg(path, params_df),
118-
method = method,
119-
summary = summary,
120-
description = description,
121-
params = .params_to_list(params_df),
122-
params_query = .extract_params_by_location(params_df, "query"),
123-
params_header = .extract_params_by_location(params_df, "header"),
124-
params_cookie = .extract_params_by_location(params_df, "cookie")
125-
)
99+
list(
100+
operation_id = operation_id,
101+
tag = tags,
102+
path = .path_as_arg(endpoint, parameters),
103+
method = operation,
104+
summary = summary,
105+
description = description,
106+
params = .params_to_list(parameters),
107+
params_query_raw = .extract_params_by_location(parameters, "query"),
108+
params_header_raw = .extract_params_by_location(parameters, "header"),
109+
params_cookie_raw = .extract_params_by_location(parameters, "cookie")
126110
)
127111
}
128112

129113
.prepare_params_df <- function(params_df) {
130114
params_df <- .flatten_params_df(params_df)
131115
if (nrow(params_df)) {
132-
params_df$description <- .paths_complete_param_descriptions(
133-
descriptions = params_df$description,
134-
params_schema = params_df$schema,
135-
required = params_df$required,
136-
allow_empty = params_df$allowEmptyValue
116+
params_df$class <- .describe_param_classes(
117+
params_df$schema,
118+
params_df$allowEmptyValue,
119+
params_df$required
120+
)
121+
params_df$description <- .paths_fill_descriptions(
122+
params_df$description,
123+
params_df$schema$description
137124
)
138125
}
139126
params_df$schema <- NULL
@@ -153,15 +140,16 @@ S7::method(as_bk_data, class_paths) <- function(x) {
153140
if (!nrow(params_df)) {
154141
return(list())
155142
}
156-
# TODO: Deal with all the available data.
157-
params <- pmap(
143+
purrr::pmap(
158144
list(
159145
name = params_df$name,
146+
class = params_df$class,
160147
description = params_df$description
161148
),
162-
.paths_param_to_list
149+
function(name, class, description) {
150+
list(name = name, class = class, description = description)
151+
}
163152
)
164-
return(params)
165153
}
166154

167155
.extract_params_by_location <- function(params_df, filter_in) {
@@ -171,21 +159,20 @@ S7::method(as_bk_data, class_paths) <- function(x) {
171159
return(params_df$name[params_df$`in` == filter_in])
172160
}
173161

174-
.paths_fill_descriptions <- function(descriptions, summaries) {
175-
descriptions[is.na(descriptions)] <- summaries[is.na(descriptions)]
176-
descriptions[is.na(descriptions)] <- ""
177-
return(stringr::str_squish(descriptions))
178-
}
179-
180162
.describe_param_classes <- function(params_schema, allow_empty, required) {
181163
# TODO: Use enum and/or description when available.
182164
#
183-
# TODO: What should we do for `object` and `array`?
165+
# TODO: What should we do for `object`? Currently falls back to list, same as
166+
# array.
184167
type <- dplyr::left_join(
185168
dplyr::select(params_schema, "type", "format"),
186169
oas_format_registry,
187170
by = c("type", "format")
188171
)
172+
# Fall back to list for unknown types (array, object, etc.)
173+
type$r_class_name <- dplyr::coalesce(type$r_class_name, "list")
174+
type$r_class_package <- dplyr::coalesce(type$r_class_package, "base")
175+
type$r_class_link <- dplyr::coalesce(type$r_class_link, "list")
189176
type$r_class_name_display <- stringr::str_remove(
190177
glue::glue("{type$r_class_package}::{type$r_class_name}"),
191178
"^base::"
@@ -209,37 +196,6 @@ S7::method(as_bk_data, class_paths) <- function(x) {
209196
return(r_class_descriptions)
210197
}
211198

212-
.paths_complete_param_descriptions <- function(
213-
descriptions,
214-
params_schema,
215-
allow_empty,
216-
required
217-
) {
218-
r_class_descriptions <- .describe_param_classes(
219-
params_schema,
220-
allow_empty,
221-
required
222-
)
223-
224-
descriptions <- .paths_fill_descriptions(
225-
descriptions,
226-
params_schema$description
227-
)
228-
229-
return(
230-
stringr::str_trim(
231-
.glue_pipe_brace("(|{r_class_descriptions}|) |{descriptions}|")
232-
)
233-
)
234-
}
235-
236-
.paths_param_to_list <- function(name, description) {
237-
list(
238-
name = name,
239-
description = description
240-
)
241-
}
242-
243199
.path_as_arg <- function(path, params_df) {
244200
if (!nrow(params_df) || !any(params_df$`in` == "path")) {
245201
return(glue('"{path}"'))
@@ -253,78 +209,60 @@ S7::method(as_bk_data, class_paths) <- function(x) {
253209
.collapse_comma(glue("{x} = {x}"))
254210
}
255211

256-
# generate files ----------------------------------------------------------
212+
# generate files ---------------------------------------------------------------
257213

258214
.generate_paths_files <- function(
259215
paths_by_operation,
260216
api_abbr,
261217
security_data,
262218
pagination_data
263219
) {
264-
unlist(imap(
265-
paths_by_operation,
266-
function(path_operation, path_operation_id) {
267-
.generate_paths_operation_files(
268-
path_operation,
269-
path_operation_id,
270-
api_abbr,
271-
security_data,
272-
pagination_data
220+
security_arg_names <- security_data$security_arg_names %|0|% character()
221+
222+
# Prep each operation: remove security args, compile args strings
223+
prepped <- imap(paths_by_operation, function(op, op_id) {
224+
params <- .remove_security_args(op$params, security_arg_names)
225+
params_query <- .prep_param_args(op$params_query_raw, security_arg_names)
226+
params_header <- .prep_param_args(op$params_header_raw, security_arg_names)
227+
args <- .params_to_args(params)
228+
args_named <- .params_to_named_args(params)
229+
c(
230+
op,
231+
list(
232+
params = params,
233+
params_query = params_query,
234+
params_header = params_header,
235+
args = args,
236+
args_named = args_named,
237+
test_args = args
273238
)
274-
}
275-
))
276-
}
277-
278-
.generate_paths_operation_files <- function(
279-
path_operation,
280-
path_operation_id,
281-
api_abbr,
282-
security_data,
283-
pagination_data
284-
) {
285-
stop("Everything should be prepped before this, I think.")
286-
path_operation <- .prepare_path_operation(
287-
path_operation,
288-
security_data$security_arg_names
289-
)
290-
file_path <- .generate_paths_file(
291-
path_operation,
292-
path_operation_id,
293-
api_abbr,
294-
security_data,
295-
pagination_data
296-
)
297-
test_path <- .generate_paths_test_file(
298-
path_operation,
299-
path_operation_id,
300-
pagination_data,
301-
api_abbr
302-
)
303-
return(c(unname(file_path), unname(test_path)))
304-
}
305-
306-
.prepare_path_operation <- function(path_operation, security_args) {
307-
stop(
308-
"Do this all in initial parsing? Everything is by operation now, which means 1 path per file; no need to map."
309-
)
310-
path_operation <- map(
311-
path_operation,
312-
function(path) {
313-
path$params <- .remove_security_args(path$params, security_args)
314-
path$params_cookie <- .prep_param_args(path$params_cookie, security_args)
315-
path$params_header <- .prep_param_args(path$params_header, security_args)
316-
path$params_query <- .prep_param_args(path$params_query, security_args)
317-
path$args <- .params_to_args(path$params)
318-
path$test_args <- path$args
319-
return(path)
320-
}
321-
)
239+
)
240+
})
241+
242+
# One R file per operation
243+
r_files <- unname(unlist(imap(prepped, function(op, op_id) {
244+
.generate_paths_file(op, op_id, api_abbr, security_data)
245+
})))
246+
247+
# One test file per tag (operations grouped by tag, preserving encounter order)
248+
tags <- map_chr(prepped, "tag")
249+
unique_tags <- unique(tags)
250+
test_files <- unname(unlist(lapply(unique_tags, function(tag_name) {
251+
tag_ops <- prepped[tags == tag_name]
252+
.generate_paths_test_file(tag_ops, tag_name, api_abbr)
253+
})))
254+
255+
return(c(r_files, test_files))
322256
}
323257

324258
.params_to_args <- function(params) {
325259
.collapse_comma(map_chr(params, "name")) %|"|% character()
326260
}
327261
262+
.params_to_named_args <- function(params) {
263+
.collapse_comma_self_equal(map_chr(params, "name")) %|"|% character()
264+
}
265+
328266
.remove_security_args <- function(params, security_args) {
329267
discard(
330268
params,
@@ -338,29 +276,44 @@ S7::method(as_bk_data, class_paths) <- function(x) {
338276
.collapse_comma_self_equal(setdiff(params, security_args)) %|"|% character()
339277
}
340278
341-
.generate_paths_file <- function(path, path_tag_name, api_abbr, security_data) {
279+
.generate_paths_file <- function(
280+
path_operation,
281+
operation_id,
282+
api_abbr,
283+
security_data
284+
) {
342285
.bk_use_template(
343286
template = "paths.R",
344287
data = c(
345-
path,
346-
api_abbr = api_abbr,
347-
has_security = security_data$has_security,
348-
security_signature = security_data$security_signature,
349-
security_arg_list = security_data$security_arg_list
288+
path_operation,
289+
list(
290+
api_abbr = api_abbr,
291+
has_security = security_data$has_security %|0|% FALSE,
292+
security_signature = security_data$security_signature %|0|% "",
293+
security_arg_list = security_data$security_arg_list %|0|% "",
294+
pagination = FALSE,
295+
pagination_fn = ""
296+
)
350297
),
351-
target = glue("paths-{path_tag_name}-{path$operation_id}.R")
298+
target = glue("paths-{path_operation$tag}-{operation_id}.R")
352299
)
353300
}
354301
355-
.generate_paths_test_file <- function(path_tag, path_tag_name, api_abbr) {
302+
.generate_paths_test_file <- function(tag_operations, tag_name, api_abbr) {
303+
paths_list <- unname(imap(tag_operations, function(op, op_id) {
304+
list(
305+
operation_id = op_id,
306+
test_args = op$test_args %|0|% ""
307+
)
308+
}))
356309
.bk_use_template(
357310
template = "test-paths.R",
358311
data = list(
359-
paths = path_tag,
360-
tag = path_tag_name,
312+
paths = paths_list,
313+
tag = tag_name,
361314
api_abbr = api_abbr
362315
),
363316
dir = "tests/testthat",
364-
target = glue("test-paths-{path_tag_name}.R")
317+
target = glue("test-paths-{tag_name}.R")
365318
)
366319
}

0 commit comments

Comments
 (0)