Skip to content

Commit

Permalink
update ram_yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
rcannood committed Jul 14, 2023
1 parent 33e609e commit ae31ddb
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions src/common/helper_functions/read_and_merge_yaml.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ read_and_merge_yaml <- function(path, project_path = .ram_find_project(path)) {
}, error = function(e) {
stop("Could not read ", path, ". Error: ", e)
})
.ram_process_merge(data, path, project_path)
.ram_process_merge(data, data, path, project_path)
}

.ram_find_project <- function(path) {
Expand All @@ -31,41 +31,51 @@ read_and_merge_yaml <- function(path, project_path = .ram_find_project(path)) {
is.null(obj) || (is.list(obj) && (length(obj) == 0 || !is.null(names(obj))))
}

.ram_process_merge <- function(data, path, project_path) {
.ram_process_merge <- function(data, root_data, path, project_path) {
if (.ram_is_named_list(data)) {
# check whether children have `__merge__` entries
processed_data <- lapply(data, function(dat) {
.ram_process_merge(dat, path, project_path)
.ram_process_merge(dat, root_data, path, project_path)
})
processed_data <- lapply(names(data), function(nm) {
dat <- data[[nm]]
.ram_process_merge(dat, root_data, path, project_path)
})
names(processed_data) <- names(data)

# if current element has __merge__, read list2 yaml and combine with data
new_data <-
if ("__merge__" %in% names(processed_data)) {
if ("__merge__" %in% names(processed_data) && !.ram_is_named_list(processed_data$`__merge__`)) {
new_data_path <- .ram_resolve_path(
path = processed_data$`__merge__`,
project_path = project_path,
parent_path = dirname(path)
)
read_and_merge_yaml(new_data_path, project_path)
} else if ("$ref" %in% names(processed_data)) {
} else if ("$ref" %in% names(processed_data) && !.ram_is_named_list(processed_data$`$ref`)) {
ref_parts <- strsplit(processed_data$`$ref`, "#")[[1]]

# resolve the path in $ref
new_data_path <- .ram_resolve_path(
path = ref_parts[[1]],
project_path = project_path,
parent_path = dirname(path)
)
new_data_path <- normalizePath(new_data_path, mustWork = FALSE)

# read in the new data
x <-
tryCatch({
suppressWarnings(yaml::read_yaml(new_data_path))
}, error = function(e) {
stop("Could not read ", new_data_path, ". Error: ", e)
})
if (ref_parts[[1]] == "") {
root_data
} else {
new_data_path <- .ram_resolve_path(
path = ref_parts[[1]],
project_path = project_path,
parent_path = dirname(path)
)
new_data_path <- normalizePath(new_data_path, mustWork = FALSE)

# read in the new data
tryCatch({
suppressWarnings(yaml::read_yaml(new_data_path))
}, error = function(e) {
stop("Could not read ", new_data_path, ". Error: ", e)
})
}
x_root <- x


# Navigate the path and retrieve the referenced data
ref_path_parts <- unlist(strsplit(ref_parts[[2]], "/"))
Expand All @@ -75,20 +85,24 @@ read_and_merge_yaml <- function(path, project_path = .ram_find_project(path)) {
} else if (part %in% names(x)) {
x <- x[[part]]
} else {
stop("Could not find ", part, " in ", new_data_path, "#", ref_parts[[2]])
stop("Could not find ", processed_data$`$ref`, " in ", path)
}
}

# postprocess the new data
.ram_process_merge(x, new_data_path, project_path)
if (ref_parts[[1]] == "") {
x
} else {
.ram_process_merge(x, x_root, new_data_path, project_path)
}
} else {
list()
}

.ram_deep_merge(new_data, processed_data)
} else if (is.list(data)) {
lapply(data, function(dat) {
.ram_process_merge(dat, path, project_path)
.ram_process_merge(dat, root_data, path, project_path)
})
} else {
data
Expand Down

0 comments on commit ae31ddb

Please sign in to comment.