Skip to content

Commit

Permalink
remove the parameter encoding since it's better to only live with UTF-8
Browse files Browse the repository at this point in the history
  • Loading branch information
shrektan committed Oct 5, 2018
1 parent 8a64760 commit 731704e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 25 deletions.
18 changes: 11 additions & 7 deletions R/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#' @import stringi
NULL

# Hard code UTF-8 file encoding
# Removes encoding headache at minor cost of setting encoding in editor
# https://github.com/trestletech/plumber/pull/312
utf8Encoding <- "UTF-8"

# used to identify annotation flags.
verbs <- c("GET", "PUT", "POST", "DELETE", "HEAD", "OPTIONS", "PATCH")
enumerateVerbs <- function(v){
Expand All @@ -13,7 +18,7 @@ enumerateVerbs <- function(v){

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

dirMode <- NULL

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

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

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

plumber$new(file, encoding=encoding)
plumber$new(file)
} else {
# Couldn't find the Plumber file nor an entrypoint
stop("File does not exist: ", file)
Expand Down Expand Up @@ -133,7 +138,6 @@ 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 @@ -145,7 +149,7 @@ plumber <- R6Class(
"plumber",
inherit = hookable,
public = list(
initialize = function(file=NULL, filters=defaultPlumberFilters, envir, encoding="UTF-8"){
initialize = function(file=NULL, filters=defaultPlumberFilters, envir){

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

if (!is.null(file)){
con <- file(file, encoding=encoding)
con <- file(file, encoding=utf8Encoding)
on.exit(close(con), add=TRUE)

private$lines <- readLines(con)
Encoding(private$lines) <- "UTF-8"
Encoding(private$lines) <- utf8Encoding

srcfile <- srcfilecopy(file, private$lines, file.info(file)[1, "mtime"],
isFile=TRUE)
Expand Down
4 changes: 1 addition & 3 deletions man/plumber.Rd

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

5 changes: 0 additions & 5 deletions tests/testthat/files/gb2312-encoded.R

This file was deleted.

4 changes: 4 additions & 0 deletions tests/testthat/files/multibytes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#* @get /echo
function() {
"中文消息"
}
10 changes: 0 additions & 10 deletions tests/testthat/test-encoding.R

This file was deleted.

12 changes: 12 additions & 0 deletions tests/testthat/test-multibytes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
context("multibytes source file")

test_that("support files with multibytes", {
# on Windows, the default encoding is not UTF-8. So, plumber has to
# tell R to use UTF-8 encoding to read the source file.
r <- plumber$new("files/multibytes.R")
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 731704e

Please sign in to comment.