From ae31ddbf2c7511b389d234a0797ac7375906251b Mon Sep 17 00:00:00 2001 From: Robrecht Cannoodt Date: Fri, 14 Jul 2023 15:31:44 +0200 Subject: [PATCH] update ram_yaml --- .../helper_functions/read_and_merge_yaml.R | 56 ++++++++++++------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/src/common/helper_functions/read_and_merge_yaml.R b/src/common/helper_functions/read_and_merge_yaml.R index 2bc9a14ecb..932d3feb92 100644 --- a/src/common/helper_functions/read_and_merge_yaml.R +++ b/src/common/helper_functions/read_and_merge_yaml.R @@ -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) { @@ -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]], "/")) @@ -75,12 +85,16 @@ 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() } @@ -88,7 +102,7 @@ read_and_merge_yaml <- function(path, project_path = .ram_find_project(path)) { .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