Skip to content

Commit

Permalink
fix daroczig#130, lack of unnamed arguments returns a 0-length string
Browse files Browse the repository at this point in the history
  • Loading branch information
r2evans committed Nov 20, 2023
1 parent 9947980 commit 3344fc1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# logger 0.2.9000 (development)

- fix `formatter_glue(foo="bar")` so that the lack of an unnamed
argument will still return a string instead of `character(0)`

# logger 0.2.2 (2021-10-10)

Maintenance release:
Expand Down
14 changes: 12 additions & 2 deletions R/formatters.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ formatter_sprintf <- structure(function(fmt, ..., .logcall = sys.call(), .topcal
#' @importFrom utils str
formatter_glue <- structure(function(..., .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) {
fail_on_missing_package('glue')
dots <- list(...)
if (!is.null(names(dots)) && !any(names(dots) == "")) {
dot1 <- paste0(names(dots), "={", names(dots), "}", collapse = " ")
dots <- c(dot1, dots)
}
as.character(
tryCatch(
glue::glue(..., .envir = .topenv),
do.call(glue::glue, c(dots, list(.envir = .topenv))),
error = function(e) {
stop(paste(
'`glue` failed in `formatter_glue` on:\n\n',
Expand All @@ -55,9 +60,14 @@ formatter_glue <- structure(function(..., .logcall = sys.call(), .topcall = sys.
#' @importFrom utils str
formatter_glue_safe <- structure(function(..., .logcall = sys.call(), .topcall = sys.call(-1), .topenv = parent.frame()) {
fail_on_missing_package('glue')
dots <- list(...)
if (!is.null(names(dots)) && !any(names(dots) == "")) {
dot1 <- paste0(names(dots), "={", names(dots), "}", collapse = " ")
dots <- c(dot1, dots)
}
as.character(
tryCatch(
glue::glue_safe(..., .envir = .topenv),
do.call(glue::glue_safe, c(dots, list(.envir = .topenv))),
error = function(e) {
stop(paste(
'`glue_safe` failed in `formatter_glue_safe` on:\n\n',
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-formatters.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ test_that('glue works', {

})

test_that('formatter_glue with no unnamed args is not empty', {
expect_true(nzchar(formatter_glue(foo = "foo", bar = 1)))
})

log_formatter(formatter_glue_safe)
test_that('glue_safe works', {

Expand Down

0 comments on commit 3344fc1

Please sign in to comment.