diff --git a/DESCRIPTION b/DESCRIPTION index 976a3a2..ae9f88c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,6 +31,7 @@ Suggests: rprojroot, spelling, testthat (>= 3.0.0), + vctrs, withr VignetteBuilder: knitr diff --git a/NAMESPACE b/NAMESPACE index 34f34ae..d45db07 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,8 @@ S3method(format,gh_pat) S3method(print,gh_pat) S3method(print,gh_response) S3method(str,gh_pat) +S3method(vctrs::vec_cast,list.gh_response) +S3method(vctrs::vec_ptype2,gh_response.gh_response) export(gh) export(gh_first) export(gh_gql) diff --git a/R/gh_response.R b/R/gh_response.R index 1566ea8..259d1d5 100644 --- a/R/gh_response.R +++ b/R/gh_response.R @@ -42,3 +42,17 @@ gh_process_response <- function(resp, gh_req) { remove_headers <- function(x) { x[names(x) != "headers"] } + +# Add vctrs methods that strip attributes from gh_response when combining, +# enabling rectangling via unnesting etc +# See for more details +#' @exportS3Method vctrs::vec_ptype2 +vec_ptype2.gh_response.gh_response <- function(x, y, ...) { + list() +} + +#' @exportS3Method vctrs::vec_cast +vec_cast.list.gh_response <- function(x, to, ...) { + attributes(x) <- NULL + x +} diff --git a/inst/WORDLIST b/inst/WORDLIST index f1f6286..2c0865c 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -23,3 +23,4 @@ pre programmatically repo usethis +wc diff --git a/man/gh-package.Rd b/man/gh-package.Rd index 3a8c833..765bd83 100644 --- a/man/gh-package.Rd +++ b/man/gh-package.Rd @@ -27,7 +27,7 @@ Authors: Other contributors: \itemize{ - \item Posit Software, PBC (\href{https://ror.org/03wc8by49}{ROR}) [copyright holder, funder] + \item Posit Software, PBC (03wc8by49) [copyright holder, funder] } } diff --git a/tests/testthat/test-gh_response.R b/tests/testthat/test-gh_response.R index 1fa260f..9153e78 100644 --- a/tests/testthat/test-gh_response.R +++ b/tests/testthat/test-gh_response.R @@ -83,3 +83,15 @@ test_that("output file is not overwritten on error", { expect_equal(readLines(tmp), "foo") expect_true(!is.null((err$response_content))) }) + + +test_that("gh_response objects can be combined via vctrs #161", { + skip_on_cran() + skip_if_not_installed("vctrs") + user_1 <- gh("/users", .limit = 1) + user_2 <- gh("/users", .limit = 1, ) + user_vec <- vctrs::vec_c(user_1, user_2) + user_df <- vctrs::vec_rbind(user_1[[1]], user_2[[1]]) + expect_equal(length(user_vec), 2) + expect_equal(nrow(user_df), 2) +})