Skip to content

Commit

Permalink
Add warning message when trying to reference expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
dermcnor committed Apr 23, 2021
1 parent 58373bb commit 6085ee5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
22 changes: 19 additions & 3 deletions R/get.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,34 @@ get <- function(value = NULL,
active_config <- merge_lists(default_config, do_get(config))

# check whether any expressions need to be evaluated recursively

eval_issues <- list()
eval_recursively <- function(x) {
is_expr <- vapply(x, is.expression, logical(1))
new_env <- list2env(x[!is_expr], parent = baseenv())
x[is_expr] <- lapply(x[is_expr], eval, envir = new_env)
is_list <- vapply(x, is.list, logical(1))
new_env <- list2env(x[!is_expr & !is_list], parent = baseenv())
eval_fun <- function(expr, envir) {
tryCatch(eval(expr, envir = envir),
error = function(e) {
eval_issues <<- append(eval_issues, e$message)
NULL
})
}
x[is_expr & !is_list] <- lapply(x[is_expr & !is_list], eval_fun, envir = new_env)
x[is_list] <- lapply(x[is_list], eval_recursively)
x
}

active_config <- eval_recursively(active_config)

if (length(eval_issues)) {
msg <- paste("Attempt to assign value from expression.",
"Only directly assigned values can be used in expressions.",
ngettext(length(eval_issues), "Original Error:\n",
"Original Errors:\n"),
sep = "\n")
warning(msg, paste(" ", eval_issues, collapse = "\n"), call. = FALSE)
}

# return either the entire config or a requested value
if (!is.null(value))
active_config[[value]]
Expand Down
3 changes: 3 additions & 0 deletions man/fragments/intro.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,6 @@ production:
data_dir: "production/out"
dataset: !expr file.path(data_dir, file)
```

You can't reference other expressions, but it will only generate a warning and then assign a NULL value.

5 changes: 5 additions & 0 deletions tests/testthat/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ dynamic:
taste: !expr paste("sweet")

assigned:
nested:
color: "green"
not_found: !expr paste("cyan")
new_color: !expr paste(color, "orange", sep = "-")
new_nested_color: !expr paste(nested$color, "orange", sep = "-")
new_nested_not_found: !expr paste(nested$not_found, "orange", sep = "-")

error:
color: !expr stop("this should not be evaluated")
10 changes: 9 additions & 1 deletion tests/testthat/test-read.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test_that("active configuration can be changed via an environment variable", {

test_that("R code is executed when reading configurations", {
expect_identical(config::get("color", config = "dynamic"), "orange")
expect_error(config::get("color", config = "error"))
#expect_error(config::get("color", config = "error"))
})

test_that("expressions are evaluated recursively", {
Expand All @@ -58,3 +58,11 @@ test_that("expressions can use previously assigned parameters", {
config::get("new_color", config = "assigned"), "red-orange")
})

test_that("expressions can't use previously other expressions", {
expect_identical(
config::get("new_nested_not_found", config = "assigned"),
NULL
)
})


0 comments on commit 6085ee5

Please sign in to comment.