Skip to content

Commit

Permalink
check parameters
Browse files Browse the repository at this point in the history
ref #431
  • Loading branch information
wibeasley committed Oct 15, 2022
1 parent fd931b5 commit a5f9266
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions R/redcap-metadata-read.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,27 @@ redcap_metadata_read <- function(
#'
#' @param x A vector to convert to array format
#' @param arr_name A string containing the name of the API request parameter for
#' the array
#' the array. Must be either "fields" or "forms".
#'
#' @return
#' If \code{x} is not \code{NULL} a list is returned with one element for
#' each element of x in the format:
#' \code{list(`arr_name[0]` = x[1], `arr_name[1]` = x[2], ...)}. If \code{x} is
#' \code{NULL} then \code{NULL} is returned.
to_api_array <- function(x, arr_name) {
checkmate::assert_character(x , null.ok = TRUE, any.missing = FALSE)
checkmate::assert_character(arr_name, null.ok = TRUE, any.missing = FALSE, max.len = 1L, pattern = "^fields|forms$")

if (is.null(x)) {
return(NULL)
} else if (is.null(arr_name)){
rlang::abort("The `arr_name` parameter cannot be null if `x` is not null.")
} else if (length(x) != length(arr_name)) {
rlang::abort("The `x` and `arr_name` arrays must be the same length.")
}

res <- as.list(x)
names(res) <- paste0(arr_name, "[", seq_along(res) - 1, "]")
names(res) <- paste0(arr_name, "[", seq_along(res) - 1L, "]")

res
}
2 changes: 1 addition & 1 deletion man/to_api_array.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a5f9266

Please sign in to comment.