Skip to content

Commit

Permalink
Error on named NA, ignore named NULL
Browse files Browse the repository at this point in the history
Closes #21, closes #84.
  • Loading branch information
gaborcsardi committed Jan 20, 2020
1 parent 528380a commit 514f3cd
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
5 changes: 4 additions & 1 deletion R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
#' to `"GET"`.
#' @param ... Name-value pairs giving API parameters. Will be matched
#' into `endpoint` placeholders, sent as query parameters in GET
#' requests, and as a JSON body of POST requests.
#' requests, and as a JSON body of POST requests. Named `NULL` values
#' are silently dropped, and named `NA` values trigger an error.
#' @param per_page Number of items to return per page. If omitted,
#' will be substituted by `max(.limit, 100)` if `.limit` is set,
#' otherwise determined by the API (never greater than 100).
Expand Down Expand Up @@ -118,6 +119,8 @@ gh <- function(endpoint, ..., per_page = NULL, .token = NULL, .destfile = NULL,
) {

params <- list(...)
params <- drop_named_nulls(params)
check_named_nas(params)

if (is.null(per_page)) {
if (!is.null(.limit)) {
Expand Down
20 changes: 20 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,23 @@ probe <- function(.x, .p, ...) {
vapply(.x, .p, logical(1), ...)
}
}

drop_named_nulls <- function(x) {
if (has_no_names(x)) return(x)
named <- has_name(x)
null <- vapply(x, is.null, logical(1))
cleanse_names(x[! named | ! null])
}

check_named_nas <- function(x) {
if (has_no_names(x)) return(x)
named <- has_name(x)
na <- vapply(x, FUN.VALUE = logical(1), function(v) {
is.atomic(v) && anyNA(v)
})
bad <- which(named & na)
if (length(bad)) {
str <- paste0("`", names(x)[bad], "`", collapse = ", ")
stop("Named NA parameters are not allowed: ", str)
}
}
2 changes: 2 additions & 0 deletions inst/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
argument
- gh now sets `.Last.error` to the error object after an uncaught error,
and `.Last.error.trace` to the stack trace of the error.
- `gh()` now silently drops named `NULL` parameters, and throws an
error for named `NA` parameters (#21, #84).

# 1.0.1

Expand Down
3 changes: 2 additions & 1 deletion man/gh.Rd

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

43 changes: 43 additions & 0 deletions tests/testthat/test-na-null.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

test_that("named NULL is dropped", {

tcs <- list(
list(list(), list()),
list(list(a = 1), list(a = 1)),
list(list(NULL), list(NULL)),
list(list(a = NULL), list()),
list(list(NULL, a = NULL, 1), list(NULL, 1)),
list(list(a = NULL, b = 1, 5), list(b = 1, 5))
)

for (tc in tcs) {
expect_identical(
drop_named_nulls(tc[[1]]),
tc[[2]],
info = tc
)
}
})

test_that("named NA is error", {

goodtcs <- list(
list(),
list(NA),
list(NA, NA_integer_, a = 1)
)

badtcs <- list(
list(b = NULL, a = NA),
list(a = NA_integer_),
list(NA, c = NA_real_)
)

for (tc in goodtcs) {
expect_silent(check_named_nas(tc))
}

for (tc in badtcs) {
expect_error(check_named_nas(tc))
}
})

0 comments on commit 514f3cd

Please sign in to comment.