Skip to content

Commit

Permalink
Fix #458 (#461)
Browse files Browse the repository at this point in the history
* Fix #458

The S3 method can be dispatched. But the problem is that it can't be
tested with testthat. Why?

* Make test work

* Reduce line count

* Simplify .standardize_format
  • Loading branch information
chainsawriot authored Dec 17, 2024
1 parent 727d50f commit f1094bf
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: rio
Type: Package
Title: A Swiss-Army Knife for Data I/O
Version: 1.2.3
Version: 1.2.4
Authors@R: c(person("Jason", "Becker", role = "aut", email = "[email protected]"),
person("Chung-hong", "Chan", role = c("aut", "cre"), email = "[email protected]",
comment = c(ORCID = "0000-0002-6232-7530")),
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# rio 1.2.4

Bug fixes

* Fix #458, custom S3 import and export functions work again

# rio 1.2.3

* Fix #453, don't nudge the user to install all suggested packages
Expand Down
6 changes: 3 additions & 3 deletions R/extensions.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
}

.import.default <- function(file, ...) {
fileinfo <- get_info(file)
if (is.na(fileinfo$type) || is.na(fileinfo$import_function) || fileinfo$import_function == "") {
fileinfo <- get_info(file) ## S3 can't be dispatched
if (fileinfo$type == "unknown" || is.na(fileinfo$import_function) || fileinfo$import_function == "") {
stop("Format not supported", call. = FALSE)
}
if (fileinfo$type == "known") {
Expand All @@ -24,7 +24,7 @@

.export.default <- function(file, x, ...) {
fileinfo <- get_info(file)
if (is.na(fileinfo$type) || is.na(fileinfo$export_function) || fileinfo$export_function == "") {
if (fileinfo$type == "unknown" || is.na(fileinfo$export_function) || fileinfo$export_function == "") {
stop("Format not supported", call. = FALSE)
}
if (fileinfo$type == "known") {
Expand Down
8 changes: 2 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,15 @@ get_ext <- function(file) {
## TODO google sheets
matched_formats <- unique_rio_formats[unique_rio_formats$input == input, ]
if (nrow(matched_formats) == 0) {
return(list(input = input, format = NA, type = NA, format_name = NA, import_function = NA, export_function = NA, file = file))
return(list(input = input, format = input, type = "unknown", format_name = NA, import_function = NA, export_function = NA, file = file))
}
output <- as.list(matched_formats)
output$file <- file
return(output)
}

.standardize_format <- function(input) {
info <- .query_format(input, "")
if (is.na(info$format)) {
return(input)
}
info$format
.query_format(input, "")$format
}

twrap <- function(value, tag) {
Expand Down
21 changes: 13 additions & 8 deletions tests/testthat/test_extensions.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
library("datasets")
skip_on_cran()

test_that("S3 extension mechanism works for imports", {
withr::with_tempdir({
write.csv(iris, "iris.custom")
expect_error(import("iris.custom"))
expect_error(import("iris.custom"), "Format not supported")
.import.rio_custom <- function(file, ...) {
read.csv(file, ...)
}
##expect_true(is.data.frame(import('iris.custom')))
rm(.import.rio_custom)
expect_error(.import.rio_custom("iris.custom"), NA)
assign(".import.rio_custom", .import.rio_custom, envir = .GlobalEnv)
expect_true(is.data.frame(import("iris.custom")))
rm(list = ".import.rio_custom", envir = .GlobalEnv)
})
})

test_that("S3 extension mechanism works for exports", {
withr::with_tempdir({
expect_error(export("iris.custom"))
.export.rio_custom <- function(file, data, ...) {
write.csv(data, file, ...)
expect_error(export(iris, "iris.custom"))
.export.rio_custom <- function(file, x, ...) {
write.csv(x, file, ...)
invisible(file)
}
expect_error(is.character(export(iris, "iris.custom")))
rm(.export.rio_custom)
expect_error(.export.rio_custom("iris.custom", iris), NA)
assign(".export.rio_custom", .export.rio_custom, envir = .GlobalEnv)
expect_true(is.character(export(iris, "iris.custom")))
rm(list = ".export.rio_custom", envir = .GlobalEnv)
})
})

0 comments on commit f1094bf

Please sign in to comment.