Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/2 config #12

Merged
merged 21 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: pacta.workflow.utils
Title: Utility functions for PACTA workflows
Version: 0.0.0.9004
Version: 0.0.0.9005
Authors@R:
c(person(given = "Alex",
family = "Axthelm",
Expand All @@ -25,6 +25,7 @@ Imports:
Suggests:
covr,
devtools,
jsonvalidate,
pak,
testthat (>= 3.0.0),
withr
Expand Down
124 changes: 124 additions & 0 deletions R/parse_json_params.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
parse_params <- function(
json,
inheritence_search_paths = NULL,
schema_file = NULL
) {
log_trace("Parsing params.")
if (file.exists(json)) {
log_trace("Reading params from file: {json}.}")
} else {
log_trace("Reading params from string.")
}
raw_params <- jsonlite::fromJSON(json)
full_params <- inherit_params(
raw_params,
inheritence_search_paths
)

if (!is.null(schema_file)) {
if (requireNamespace("jsonvalidate", quietly = TRUE)) {
log_trace("Validating parameters.")
validation_results <- jsonvalidate::json_validate(
json = jsonlite::toJSON(full_params, auto_unbox = TRUE),
schema = schema_file,
verbose = TRUE
)
if (validation_results) {
log_trace("Validation successful.")
} else {
log_error("Validation against JSON Schema failed.")
log_error("Schema file: {schema_file}")
pretty_log_jsonvalidate_errors(validation_results)
stop("JSON Validation failed.")
}
} else {
log_error("jsonvalidate package not found.")
stop("jsonvalidate package not found.")
}
} else {
log_trace("No JSON Schema provided. Skipping validation.")
}

return(full_params)
}

inherit_params <- function(
params,
inheritence_search_paths
) {
inherit_key <- "inherit"

inherited_files <- NULL
while (inherit_key %in% names(params)) {

# check for multiple inheritence keys
if (sum(names(params) == inherit_key) > 1L) {
log_error("Multiple inheritence keys found.")
stop("Multiple inheritence keys found.")
}

log_trace(
"Key \"{inherit_key}\" found in parameters. Inheriting parameters."
)

to_inherit <- params[[inherit_key]]
if (length(to_inherit) > 1L) {
log_error("Multiple values in inherit key.")
stop("Multiple values in inherit key.")
}
params[[inherit_key]] <- NULL # remove inherit key

possible_paths <- file.path(
inheritence_search_paths,
paste0(to_inherit, ".json")
)
candidate_file <- possible_paths[file.exists(possible_paths)]
if (length(candidate_file) == 0L) {
log_error("Inheritence file not found: {possible_paths}.")
stop("Inheritence file not found.")
} else {
if (length(candidate_file) > 1L) {
log_warn("Multiple files matching inheritence pattern found:")
log_warn("{candidate_file}.")
warning("Multiple inheritence files found.")
candidate_file <- candidate_file[[1L]]
log_warn("Using first file: {candidate_file}.")
}
}
if (candidate_file %in% inherited_files) {
log_error(
"Inheritence loop detected while inheriting from {candidate_file}."
)
log_error("Inherited file: {inherited_files}.")
stop("Inheritence loop detected.")
}
inherited_files <- c(inherited_files, candidate_file)
log_trace("Inheriting parameters from file: {candidate_file}.")
inherit_params <- jsonlite::fromJSON(candidate_file)
params <- merge_lists(
base_list = inherit_params,
overlay_list = params
)
}

log_trace("No inheritence key (\"{inherit_key}\") found.")
return(params)
}

pretty_log_jsonvalidate_errors <- function(
validation_object,
logging_function = log_error
) {
errors <- attr(validation_object, "errors")
if (length(errors) == 0L) {
return(NULL)
}
for (row in seq(1L, nrow(errors))) {
logging_function("JSON Validation ({row} / {nrow(errors)}):")
logging_function(" Keyword: {errors[[row, 'keyword']]}")
logging_function(" instancePath: {errors[[row, 'instancePath']]}")
logging_function(" schemaPath: {errors[[row, 'schemaPath']]}")
logging_function(" Message: {errors[[row, 'message']]}")
}
return(errors)
}
Loading
Loading