Skip to content

Commit

Permalink
✨ Use available metadata when creating a data package
Browse files Browse the repository at this point in the history
data_package() uses the metadata generated by update_metadata()
  • Loading branch information
ThierryO committed Dec 14, 2024
1 parent 2a12557 commit d677eaa
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions R/data_package.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ data_resource <- function(file, path = ".") {
clean_data_path(root = path, file = file)[2] |>
read_yaml() -> metadata
list(
name = file, path = file, "encoding" = "utf-8",
format = "csv", media_type = "text/csv",
name = coalesce(metadata[["..generic"]][["name"]], file), path = file,
"encoding" = "utf-8", format = "csv", media_type = "text/csv",
hash = paste0("sha1:", metadata[["..generic"]][["data_hash"]]),
schema = list(
fields = vapply(
Expand All @@ -62,12 +62,15 @@ data_resource <- function(file, path = ".") {
c(value = metadata[["..generic"]][["NA string"]], label = "missing")
)
)
) |>
) -> dr
extra <- c("title", "description")
metadata[["..generic"]][extra[extra %in% names(metadata[["..generic"]])]] |>
c(dr) |>
list()
}

field_schema <- function(x, metadata) {
list(switch(
switch(
metadata[[x]]$class,
"character" = list(name = x, type = "string"),
"Date" = list(name = x, type = "date"),
Expand All @@ -81,7 +84,24 @@ field_schema <- function(x, metadata) {
),
"integer" = list(name = x, type = "integer"),
"numeric" = list(name = x, type = "number"),
"POSIXct" = list(name = x, type = "datetime"),
"POSIXct" = list(
name = x, type = "datetime", format = "%Y-%m-%dT%H:%M:%SZ"
),
stop("field_schema() can't handle ", metadata[[x]]$class)
))
) -> fs
if ("description" %in% names(metadata[[x]])) {
fs$description <- metadata[[x]][["description"]]
}
return(list(fs))
}

coalesce <- function(...) {
dots <- list(...)
if (length(dots) == 0) {
return(NULL)
}
if (!is.null(dots[[1]])) {
return(dots[[1]])
}
coalesce(dots[-1])
}

0 comments on commit d677eaa

Please sign in to comment.