Skip to content

Commit

Permalink
🐪 DRY up key validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Patrick Kyle committed Feb 6, 2020
1 parent 79b0faa commit 39d52e0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
19 changes: 2 additions & 17 deletions R/dash.R
Original file line number Diff line number Diff line change
Expand Up @@ -815,15 +815,7 @@ Dash <- R6::R6Class(
# specify a custom index string
# ------------------------------------------------------------------------
index_string = function(string) {
required_keys <- c("app_entry", "config", "scripts")

keys_present <- vapply(required_keys, function(x) grepl(x, string), logical(1))

if (!all(keys_present)) {
stop(sprintf("Did you forget to include %s in your index string?",
paste(names(keys_present[keys_present==FALSE]), collapse = ", ")))
}
private$custom_index <- string
private$custom_index <- validate_keys(string)
},

# ------------------------------------------------------------------------
Expand All @@ -838,14 +830,7 @@ Dash <- R6::R6Class(
template = sub(key, kwargs[[name]], template)
}

required_keys <- c("app_entry", "config", "scripts")

checks <- sapply(requiredKeys, function(x) grepl(x, names(kwargs)))

if (FALSE %in% checks) {
stop(sprintf("Did you forget to include %s in your index string?",
paste(requiredKeys[!checks], collapse = ", ")))
}
validate_keys(names(kwargs))

private$template_index <- template
},
Expand Down
13 changes: 13 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -1287,3 +1287,16 @@ interpolate_str <- function(index_template, ...) {
}
return(template)
}

validate_keys <- function(string) {
required_keys <- c("app_entry", "config", "scripts")

keys_present <- vapply(required_keys, function(x) grepl(x, string), logical(1))

if (!all(keys_present)) {
stop(sprintf("Did you forget to include %s in your index string?",
paste(names(keys_present[keys_present==FALSE]), collapse = ", ")))
} else {
return(string)
}
}

0 comments on commit 39d52e0

Please sign in to comment.