Skip to content

Commit f462226

Browse files
committed
➕ Add check_that helper function
1 parent 52db2c2 commit f462226

File tree

10 files changed

+75
-25
lines changed

10 files changed

+75
-25
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
export(check_container_class)
44
export(check_envvar)
5+
export(check_that)
56
export(get_auth_token)
67
export(get_container)
78
export(list_container_names)

R/azkit_helpers.R

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
#' An alternative to stopifnot/assert_that etc
2+
#'
3+
#' If the predicate function is true of `x` then `x` is returned. Otherwise,
4+
#' an error is thrown with a custom `message`.
5+
#'
6+
#' @param x The object to be checked
7+
#' @param predicate The predicate function used to check `x`
8+
#' @param message A custom error message, as a string. Will be shown to the
9+
#' user if the predicate check does not succeed. Can include `glue`d variables
10+
#' and `{cli}` semantic markup.
11+
#' @param pf Set as [parent.frame()] so variables in the caller environment can
12+
#' be used in the custom error message.
13+
#' @seealso [check_vec]
14+
#' @export
15+
check_that <- function(x, predicate, message, pf = parent.frame()) {
16+
if (predicate(x)) {
17+
x
18+
} else {
19+
cli::cli_abort(message, call = rlang::caller_call(), .envir = pf)
20+
}
21+
}
22+
23+
24+
#' @keywords internal
25+
ct_error_msg <- \(text) paste0("{.fn check_that}: ", text)
26+
127
#' An alternative to stopifnot/assert_that etc
228
#'
329
#' This function makes it easy to use the `{purrr}` functions `every()`,
@@ -6,10 +32,9 @@
632
#' Not suitable for checking if `length(x) == 1` as it will check vectors
733
#' element-wise, so will potentially return TRUE even if `length(x) > 1`
834
#'
9-
#' @param x The object to be checked
1035
#' @param predicate The predicate function used to check elements of `x`
1136
#' @param message A custom error message, as a string. Will be shown to the
12-
#' user if the predicate check does not succeed. Can include `glue` variables
37+
#' user if the predicate check does not succeed. Can include `glue`d variables
1338
#' and `{cli}` semantic markup. Variable values will be searched for in the
1439
#' environment of the caller function (not in the environment of `check_vec()`
1540
#' ). This makes it easier to include informative values in the message.
@@ -19,8 +44,7 @@
1944
#' predicate. "none" can be used to generate an inverse predicate, or the
2045
#' situation where success means that none of the elements of x satisfies the
2146
#' predicate. "some" is unlikely to be useful often, but it is available.
22-
#' @param pf Set as [parent.frame()] so variables in the caller environment can
23-
#' be used in the custom error message.
47+
#' @inheritParams check_that
2448
#' @seealso [check_scalar_type()]
2549
#' @keywords internal
2650
check_vec <- function(
@@ -50,8 +74,8 @@ cv_error_msg <- \(text) paste0("{.fn check_vec}: ", text)
5074
#' Possible values for the `type` parameter are: "character", "logical", "list",
5175
#' "integer", "double", "string", "bool", "bytes", "raw", "vector", "complex".
5276
# Supplying "string" or "bool" will additionally check that `x` is not missing.
53-
#' @seealso [check_vec()]
54-
#' @inheritParams check_vec
77+
#' @seealso [check_that]
78+
#' @inheritParams check_that
5579
#' @param type A string defining the R object type that `x` is checked to be
5680
#' @keywords internal
5781
check_scalar_type <- function(
@@ -91,7 +115,7 @@ cst_error_msg <- \(text) paste0("{.fn check_scalar_type}: ", text)
91115
#'
92116
#' Will error if x is equal to `""`, or if it is otherwise missing or invalid.
93117
#' With the exception that if x is NULL, then NULL will be passed through.
94-
#' @inheritParams check_vec
118+
#' @inheritParams check_that
95119
#' @param message A custom error message, as a string. Will be shown to the
96120
#' user if the check does not pass. Can include `glue` variables and `{cli}`
97121
#' semantic markup. Variable values will be searched for in the environment of

R/get_container.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ get_container <- function(container_name = NULL, ...) {
1919
token <- get_auth_token(...)
2020
endpoint <- get_default_endpoint(token)
2121
container_names <- list_container_names(token)
22-
not_found_msg <- cv_error_msg("Container {.val {cont_nm}} not found")
22+
not_found_msg <- ct_error_msg("Container {.val {cont_nm}} not found")
2323
cont_nm |>
24-
check_vec(\(x) x %in% container_names, not_found_msg) |>
24+
check_that(\(x) x %in% container_names, not_found_msg) |>
2525
AzureStor::blob_container(endpoint = endpoint)
2626
}
2727

R/list_files.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
list_files <- function(container, path = "", ext = "", recursive = TRUE) {
3232
stopifnot(rlang::is_character(c(path, ext), 2))
3333
stopifnot(rlang::is_bool(recursive))
34-
pnf_msg <- cv_error_msg("Path {.val {path}} not found")
35-
check_vec(path, \(x) AzureStor::blob_dir_exists(container, x), pnf_msg)
34+
pnf_msg <- ct_error_msg("Path {.val {path}} not found")
35+
check_that(path, \(x) AzureStor::blob_dir_exists(container, x), pnf_msg)
3636

3737
tbl <- AzureStor::list_blobs(container, path, recursive = recursive)
3838
if (nrow(tbl) > 0) {

R/read_azure_files.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' Read a parquet file from Azure storage
22
#'
3-
#' @param container An Azure container object, as returned by [get_container()]
3+
#' @param container An Azure container object, as returned by [get_container]
44
#' @param file The name of the file to be read, as a string. NB The file
55
#' extension does not need to be included (though it can be). The function
66
#' will error if multiple files are somehow matched.
@@ -14,8 +14,8 @@
1414
#' being read. Useful for checking the function is doing what is expected, but
1515
#' can be turned off with `FALSE`. Can be set persistently with the option
1616
#' "azkit.info". If `NULL` then it will default to the value of
17-
#' [rlang::is_interactive()] (ie `TRUE` for interactive sessions).
18-
#' @param ... optional arguments to be passed through to [arrow::read_parquet()]
17+
#' [rlang::is_interactive] (that is, `TRUE` for interactive sessions).
18+
#' @param ... optional arguments to be passed through to [arrow::read_parquet]
1919
#' @returns A tibble
2020
#' @examples \dontrun{
2121
#' # if a full filepath is available then path can be ignored
@@ -95,9 +95,9 @@ check_blob_exists <- function(container, file, ext, info, path) {
9595
dplyr::filter(dplyr::if_any("name", \(x) x == {{ file_path }})) |>
9696
dplyr::pull("name")
9797

98-
msg1 <- cv_error_msg("no matching {ext} file found")
98+
msg1 <- ct_error_msg("no matching {ext} file found")
9999
msg2 <- cst_error_msg("multiple matching {ext} files found")
100-
check_vec(filepath_out, rlang::is_character, msg1) # check length > 0
100+
check_that(filepath_out, \(x) length(x) > 0, msg1) # check length > 0
101101
check_scalar_type(filepath_out, "character", msg2) # check length == 1
102102

103103
info_option <- getOption("azkit.info")

man/check_blob_exists.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_scalar_type.Rd

Lines changed: 3 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_that.Rd

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/check_vec.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-read_azure_files.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ test_that("tdd of check_blob_exists", {
235235
dplyr::pull("name")
236236
stop_msg1 <- glue::glue("no matching {file_ext} file found")
237237
stop_msg2 <- glue::glue("multiple matching {file_ext} files found")
238-
check_vec(filepath, rlang::is_character, stop_msg1) # check length > 0
238+
check_that(filepath, \(x) length(x) > 0, stop_msg1) # check length > 0
239239
check_scalar_type(filepath, "character", stop_msg2) # check length == 1
240240
}
241241
expect_error(check_blob_exists(support_container, "unmatched"), "matching")
@@ -254,7 +254,7 @@ test_that("tdd of check_blob_exists", {
254254
dplyr::pull("name")
255255
stop_msg1 <- glue::glue("no matching {file_ext} file found")
256256
stop_msg2 <- glue::glue("multiple matching {file_ext} files found")
257-
check_vec(filepath, rlang::is_character, stop_msg1) # check length > 0
257+
check_that(filepath, \(x) length(x) > 0, stop_msg1) # check length > 0
258258
check_scalar_type(filepath, "character", stop_msg2) # check length == 1
259259
filepath
260260
}

0 commit comments

Comments
 (0)