Skip to content

Commit

Permalink
Add check_if_parser_and_preprocessor_are_identical
Browse files Browse the repository at this point in the history
  • Loading branch information
robertzk committed Jan 23, 2017
1 parent 8a5d2a8 commit 5832ddc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions R/director-parsers.R
Expand Up @@ -49,5 +49,8 @@ register_parser <- function(path, parser = function() { }, overwrite = FALSE, ca
## the latter has a longer length and will be preferred when selecting the
## function used for parsing resources in the `"models/ensembles"` directory.
self$.parsers <<- self$.parsers[names(self$.parsers)[rev(order(sapply(names(self$.parsers), nchar)))]]

check_if_parser_and_preprocessor_are_identical(self, path)
invisible(TRUE)
}

4 changes: 4 additions & 0 deletions R/director-preprocessors.R
Expand Up @@ -42,8 +42,12 @@ register_preprocessor <- function(path, preprocessor, overwrite = FALSE) {
## the `"models/ensembles"` directory.
self$.preprocessors <<- self$.preprocessors[
names(self$.preprocessors)[rev(order(sapply(names(self$.preprocessors), nchar)))]]

check_if_parser_and_preprocessor_are_identical(self, path)
invisible(TRUE)
}


#' Whether there exists a preprocessor for a resource.
#'
#' @param resource_path character. The resource name.
Expand Down
21 changes: 21 additions & 0 deletions R/director.R
Expand Up @@ -146,3 +146,24 @@ NULL
#' @export
is.director <- function(x) is(x, 'director')

#' If the parser and preprocessor for a path is the same, the user has probably made a mistake.
#'
#' @param director director. The director object.
#' @param path character. The path to check for whether the preprocessor and
#' parser are identical.
#'
#' @return Nothing, but issue a warning in red crayon if they are identical,
#' since it likely means the user forgot to specify a parser.
check_if_parser_and_preprocessor_are_identical <- function(director, path) {
has_same_body <- function(fn1, fn2) {
is.function(fn1) && is.function(fn2) &&
isTRUE(all.equal(body(fn1), body(fn2)))
}
if (has_same_body(director$.parsers[[paste0("/", path)]],
director$.preprocessors[[paste0("/", path)]])) {
warning(crayon::red("The path at ", sQuote(path), " has the same ",
"preprocessor and parser -- are you sure you ",
"included a parser?"))
}
}

0 comments on commit 5832ddc

Please sign in to comment.