Skip to content

Update check functions for clarity and add tests #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
70a9bbb
refactor: clarify and update arg check functions, add tests
dshemetov Apr 24, 2023
9cc818a
bug: replace purrr::map with lapply
dshemetov Apr 24, 2023
5e0bbda
docs: compile
dshemetov Apr 24, 2023
c707c16
bug: fix covidcast as_of arg #48
dshemetov Apr 24, 2023
4ed1a66
style: styler
dshemetov Apr 24, 2023
71068cf
refactor: rewrite check functions with checkmate
dshemetov Apr 28, 2023
7934202
docs: document
dshemetov Apr 28, 2023
f4f1d21
refactor: check functions, endpoints, epirange
dshemetov Apr 29, 2023
150c4b4
docs: document
dshemetov Apr 29, 2023
93e1727
bug: fix norostat bug
dshemetov Apr 29, 2023
796f07a
docs: shorten example data ranges
dshemetov Apr 29, 2023
6b76bab
bug: fix epirange bad formatting
dshemetov Apr 29, 2023
96307af
bug: fix issue column parsing #36
dshemetov Apr 29, 2023
5a81ce3
docs: update paho_dengue example
dshemetov Apr 29, 2023
30095bd
bug: fix paho_dengue pointing to wrong endpoint #20
dshemetov Apr 29, 2023
38d79cd
docs: document
dshemetov Apr 29, 2023
3a8a3bb
pkg: add lubridate to deps
dshemetov Apr 29, 2023
0965cc9
docs: update epirange docs
dshemetov Apr 29, 2023
fb5922e
docs: document
dshemetov Apr 29, 2023
83ab5f1
Merge branch 'dev' into ds/check
dshemetov Apr 29, 2023
415acf2
docs: document
dshemetov Apr 29, 2023
624fab7
docs: move slow demo from tests to comment
dshemetov Apr 29, 2023
9bde271
tests: test * in date check functions
dshemetov Apr 29, 2023
9ca1f7a
docs: document
dshemetov Apr 29, 2023
11a9212
styler: styler
dshemetov Apr 29, 2023
aa72062
refactor: remove lubridate
dshemetov May 1, 2023
dd92ca0
docs: update auth request example to use fixed fake key
dshemetov May 1, 2023
9afe368
refactor: timeset validation
dshemetov May 3, 2023
cfbf4c1
docs: document
dshemetov May 3, 2023
f7c9593
bug: fix date lists being broken
dshemetov May 3, 2023
0c465d6
Use `#'` + `@noRd` for internal function doc comments
lcbrooks May 3, 2023
dadcc68
Change "date" -> "timeset" in parse_timeset_input errors
lcbrooks May 3, 2023
50aaad3
Catch `c` EpiRange error (unclearly), don't `format_list` non-lists
lcbrooks May 3, 2023
5b07e13
refactor: disallow lists in timeset arguments
dshemetov May 3, 2023
d87f3f7
docs: document
dshemetov May 3, 2023
e72ebb5
tests: fix a few tests
dshemetov May 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
Imports:
checkmate,
cli,
httr,
jsonlite,
Expand Down
9 changes: 9 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ export(request_url)
export(wiki)
export(with_base_url)
importFrom(MMWRweek,MMWRweek2Date)
importFrom(checkmate,assert)
importFrom(checkmate,assert_character)
importFrom(checkmate,assert_integerish)
importFrom(checkmate,assert_string)
importFrom(checkmate,check_character)
importFrom(checkmate,check_class)
importFrom(checkmate,check_date)
importFrom(checkmate,check_integerish)
importFrom(checkmate,check_list)
importFrom(httr,RETRY)
importFrom(httr,content)
importFrom(httr,http_error)
Expand Down
143 changes: 66 additions & 77 deletions R/check.R
Original file line number Diff line number Diff line change
@@ -1,91 +1,80 @@
#' Allows string vectors of length 1
#' @importFrom checkmate assert_string
check_string_param <- function(name, value, required = TRUE) {
# string or string[]
if ((required &&
!(!is.null(value) &&
is.character(value))) ||
(!required && !(is.null(value) || is.character(value)))) {
rlang::abort(paste0("argument ", name, " is not a string"),
name = name, value = value, class = "invalid_argument"
)
}
null.ok <- !required
assert_string(value, null.ok = null.ok, .var.name = name)
}

check_int_param <- function(name, value, required = TRUE) {
# numeric or numeric[]
if ((required &&
!(!is.null(value) &&
is.numeric(value))) ||
(!required && !(is.null(value) || is.numeric(value)))) {
rlang::abort(paste0("argument ", name, " is not a number"),
name = name, value = value, class = "invalid_argument"
)
}
#' Allows string vectors of length > 1 and lists of string vectors of length 1
#' @importFrom checkmate assert_character
check_character_param <- function(name, value, required = TRUE) {
null.ok <- !required
assert_character(value, null.ok = null.ok, any.missing = FALSE, .var.name = name)
}

is_epirange_like <- function(value) {
# character or numeric or EpiRange or list(from=..., to=...)
is.character(value) ||
is.numeric(value) ||
inherits(value, "EpiRange") ||
(is.list(value) &&
"from" %in% names(value) && "to" %in% names(value))
#' Allows a single numeric value
#' @importFrom checkmate assert_integerish
check_scalar_integerish_param <- function(name, value, required = TRUE) {
null.ok <- !required
assert_integerish(value, len = 1, null.ok = null.ok, .var.name = name)
}

check_single_epirange_param <- function(name, value, required = TRUE) {
if ((required &&
!(!is.null(value) &&
is_epirange_like(value))) ||
(!required && !(is.null(value) || is_epirange_like(value)))) {
rlang::abort(paste0("argument ", name, " is not a epirange"),
name = name, value = value, class = "invalid_argument"
)
}
#' Allows a vector of numeric values or a list of numeric values
#' @importFrom checkmate assert_integerish
check_integerish_param <- function(name, value, required = TRUE) {
null.ok <- !required
assert_integerish(value, null.ok = null.ok, any.missing = FALSE, .var.name = name)
}

check_epirange_param <- function(name, value, required = TRUE) {
if (is.null(value)) {
if (required) {
rlang::abort(paste0("argument ", name, " is not a epirange"),
name = name, value = value, class = "invalid_argument"
)
}
return()
}
if (is_epirange_like(value)) {
return()
}
if (!is.list || !all(sapply(sapply(value, is_epirange_like)))) {
rlang::abort(paste0("argument ", name, " is not a epirange"),
name = name, value = value, class = "invalid_argument"
)
}
#' Allows a scalar date_like param: a single date, character, or integerish
#' @importFrom checkmate assert check_date check_character check_integerish
check_scalar_date_param <- function(name, value, required = TRUE) {
null.ok <- !required
assert(
# check_date(value, null.ok = null.ok, len = 1),
check_character(value, null.ok = null.ok, len = 1),
check_integerish(value, null.ok = null.ok, len = 1),
combine = "or",
.var.name = name
)
}

check_single_string_param <- function(name, value, required = TRUE) {
if ((required &&
!(!is.null(value) &&
is.character(value) &&
length(value) == 1)) ||
(!required &&
!(is.null(value) ||
(
is.character(value) && length(value) == 1
)))) {
rlang::abort(paste0("argument ", name, " is not a single string"),
name = name, value = value, class = "invalid_argument"
)
}
#' Allows a vector of date_like params: dates, characters, or integerishes
#' @importFrom checkmate check_date check_character check_integerish
check_date_param <- function(name, value, required = TRUE) {
null.ok <- !required
assert(
# check_date(value, null.ok = null.ok),
check_character(value, null.ok = null.ok),
check_integerish(value, null.ok = null.ok),
combine = "or",
.var.name = name
)
}

check_single_int_param <- function(name, value, required = TRUE) {
if ((required &&
!(!is.null(value) &&
is.numeric(value) &&
length(value) == 1)) ||
(!required &&
!(is.null(value) || (is.numeric(value) && length(value) == 1)))) {
rlang::abort(paste0("argument ", name, " is not a single integer"),
name = name, value = value, class = "invalid_argument"
)
}
#' Allows a scalar timeset param: a character, an integerish, or an EpiRange
#' @importFrom checkmate assert check_character check_integerish check_class
check_scalar_timeset_param <- function(name, value, required = TRUE) {
null.ok <- !required
assert(
check_character(value, null.ok = null.ok, len = 1),
check_integerish(value, null.ok = null.ok, len = 1),
check_class(value, "EpiRange", null.ok = null.ok),
combine = "or",
.var.name = name
)
}

#' Check if param is a character or numeric or EpiRange
#' @importFrom checkmate assert check_character check_integerish check_class check_list
check_timeset_param <- function(name, value, required = TRUE) {
null.ok <- !required
assert(
check_character(value, null.ok = null.ok),
check_integerish(value, null.ok = null.ok),
check_class(value, "EpiRange", null.ok = null.ok),
check_list(value, types = "EpiRange", null.ok = null.ok),
combine = "or",
.var.name = name
)
}
Loading