Skip to content

Commit

Permalink
add the encoding param for plumber() and Plumber$new() and enable the…
Browse files Browse the repository at this point in the history
… UTF-8 as the default

related to rstudio#296
  • Loading branch information
shrektan committed Oct 5, 2018
1 parent d8edd45 commit 8a64760
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
22 changes: 15 additions & 7 deletions R/plumber.R
Expand Up @@ -13,7 +13,7 @@ enumerateVerbs <- function(v){

#' @rdname plumber
#' @export
plumb <- function(file, dir="."){
plumb <- function(file, dir=".", encoding="UTF-8"){

dirMode <- NULL

Expand Down Expand Up @@ -50,7 +50,7 @@ plumb <- function(file, dir="."){
on.exit(setwd(old))

# Expect that entrypoint will provide us with the router
x <- source(entrypoint)
x <- source(entrypoint, encoding=encoding)

# source returns a list with value and visible elements, we want the (visible) value object.
pr <- x$value
Expand All @@ -62,7 +62,7 @@ plumb <- function(file, dir="."){
} else if (file.exists(file)) {
# Plumber file found

plumber$new(file)
plumber$new(file, encoding=encoding)
} else {
# Couldn't find the Plumber file nor an entrypoint
stop("File does not exist: ", file)
Expand Down Expand Up @@ -133,6 +133,7 @@ hookable <- R6Class(
#' plumber router definition. Alternatively, if an `entrypoint.R` file is
#' found, it will take precedence and be responsible for returning a runnable
#' Plumber router.
#' @param encoding Encoding of the input file; see [base::file()].
#' @include globals.R
#' @include serializer-json.R
#' @include parse-block.R
Expand All @@ -144,7 +145,7 @@ plumber <- R6Class(
"plumber",
inherit = hookable,
public = list(
initialize = function(file=NULL, filters=defaultPlumberFilters, envir){
initialize = function(file=NULL, filters=defaultPlumberFilters, envir, encoding="UTF-8"){

if (!is.null(file)){
if (!file.exists(file)){
Expand Down Expand Up @@ -177,10 +178,17 @@ plumber <- R6Class(
private$notFoundHandler <- default404Handler

if (!is.null(file)){
private$lines <- readLines(file)
private$parsed <- parse(file, keep.source=TRUE)
con <- file(file, encoding=encoding)
on.exit(close(con), add=TRUE)

source(file, local=private$envir, echo=FALSE, keep.source=TRUE)
private$lines <- readLines(con)
Encoding(private$lines) <- "UTF-8"

srcfile <- srcfilecopy(file, private$lines, file.info(file)[1, "mtime"],
isFile=TRUE)
private$parsed <- parse(text=private$lines, n =-1, srcfile=srcfile)

source(con, local=private$envir, echo=FALSE, keep.source=TRUE)

for (i in 1:length(private$parsed)){
e <- private$parsed[i]
Expand Down
4 changes: 3 additions & 1 deletion man/plumber.Rd

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

5 changes: 5 additions & 0 deletions tests/testthat/files/gb2312-encoded.R
@@ -0,0 +1,5 @@
# Note this file is saved with GB2312 encoding intentionally
#* @get /echo
function() {
"ÖÐÎÄÏûÏ¢"
}
10 changes: 10 additions & 0 deletions tests/testthat/test-encoding.R
@@ -0,0 +1,10 @@
context("encoding")

test_that("support other encoding", {
r <- plumber$new("files/gb2312-encoded.R", encoding = "GB2312")
req <- make_req("GET", "/echo")
res <- PlumberResponse$new()
out <- r$serve(req, res)$body
expect_identical(out, jsonlite::toJSON("\u4e2d\u6587\u6d88\u606f"))
expect_equal(Encoding(out), "UTF-8")
})

0 comments on commit 8a64760

Please sign in to comment.