-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Bruno Tremblay <meztez@neoxone.com> Co-authored-by: Barret Schloerke <barret@rstudio.com>
- Loading branch information
1 parent
e535922
commit 0de28bc
Showing
10 changed files
with
113 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#' @rdname serializers | ||
#' @export | ||
serializer_yaml <- function(...) { | ||
if (!requireNamespace("yaml", quietly = TRUE)) { | ||
stop("yaml must be installed for the yaml serializer to work") | ||
} | ||
function(val, req, res, errorHandler) { | ||
tryCatch({ | ||
yaml <- yaml::as.yaml(val, ...) | ||
res$setHeader("Content-Type", "application/x-yaml") | ||
res$body <- yaml | ||
|
||
return(res$toResponse()) | ||
}, error = function(e){ | ||
errorHandler(req, res, e) | ||
}) | ||
} | ||
} | ||
|
||
#' @include globals.R | ||
.globals$serializers[["yaml"]] <- serializer_yaml |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
context("YAML serializer") | ||
|
||
test_that("YAML serializes properly", { | ||
skip_if_not_installed("yaml") | ||
|
||
l <- list(a=1, b=2, c="hi") | ||
val <- serializer_yaml()(l, list(), PlumberResponse$new(), stop) | ||
expect_equal(val$status, 200L) | ||
expect_equal(val$headers$`Content-Type`, "application/x-yaml") | ||
expect_equal(val$body, yaml::as.yaml(l)) | ||
|
||
l <- list(a=1, b=2, c="hi", na=NA) | ||
val <- serializer_yaml()(l, list(), PlumberResponse$new(), stop) | ||
expect_equal(val$status, 200L) | ||
expect_equal(val$headers$`Content-Type`, "application/x-yaml") | ||
expect_equal(val$body, yaml::as.yaml(l)) | ||
|
||
l <- list(a=1, b=2, c="hi", na=NA) | ||
val <- serializer_yaml(indent = 4)(l, list(), PlumberResponse$new(), stop) | ||
expect_equal(val$status, 200L) | ||
expect_equal(val$headers$`Content-Type`, "application/x-yaml") | ||
expect_equal(val$body, yaml::as.yaml(l, indent = 4)) | ||
}) | ||
|
||
test_that("Errors call error handler", { | ||
skip_if_not_installed("yaml") | ||
|
||
errors <- 0 | ||
errHandler <- function(req, res, err){ | ||
errors <<- errors + 1 | ||
} | ||
|
||
expect_equal(errors, 0) | ||
serializer_yaml()(parse(text="hi"), list(), PlumberResponse$new("yaml"), err = errHandler) | ||
expect_equal(errors, 1) | ||
}) |