Skip to content

Commit

Permalink
small basic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
natemcintosh committed Nov 25, 2024
1 parent 5cc92ec commit eefe9e9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ export(read_data)
export(read_disease_parameters)
export(read_exclusions)
export(read_interval_pmf)
export(run_pipeline)
export(read_json_into_config)
export(write_model_outputs)
export(write_output_dir_structure)
33 changes: 22 additions & 11 deletions R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,20 @@ Config <- S7::new_class(
)
)

#' Read JSON Configuration into Config Object
#'
#' Reads a JSON file from the specified path and converts it into a `Config`
#' object.
#'
#' @param config_path A string specifying the path to the JSON configuration
#' file.
#' @return An instance of the `Config` class populated with the data from the
#' JSON file.
#' @export
read_json_into_config <- function(config_path) {
# First, our hard coded, flattened, map from strings to Classes. If any new
# subclasses are added above, they will also need to be added here.
# subclasses are added above, they will also need to be added here. If we
# create a more automated way to do this, we can remove this.
str2class <- list(
data = Data,
priors = Priors,
Expand All @@ -245,6 +256,16 @@ read_json_into_config <- function(config_path) {
# First, read the JSON file into a list
raw_input <- jsonlite::read_json(config_path, simplifyVector = TRUE)

# Check what top level properties were not in the raw input
missing_properties <- setdiff(S7::prop_names(Config()), names(raw_input))
# Error out if missing any fields
if (length(missing_properties) > 0) {
cli::cli_abort(c(
"The following properties are missing from the config file:",
"{.var missing_properties}"
))
}

inner <- function(raw_data, class_to_fill) {
# For each property, check if it is a regular value, or an S7 object.
# If it is an S7 object, we need to create an instance of that class, and do
Expand All @@ -267,15 +288,5 @@ read_json_into_config <- function(config_path) {
config
}

# Check what top level properties were not in the raw input
missing_properties <- setdiff(S7::prop_names(Config()), names(raw_input))
# Error out if missing any fields
if (length(missing_properties) > 0) {
cli::cli_abort(c(
"The following properties are missing from the config file:",
"{.var missing_properties}"
))
}

inner(raw_input, Config)
}
20 changes: 20 additions & 0 deletions man/read_json_into_config.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eefe9e9

Please sign in to comment.