diff --git a/NAMESPACE b/NAMESPACE index 3819355b..83edd450 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,8 +1,11 @@ # Generated by roxygen2: do not edit by hand +S3method(append,datapackage) +S3method(append,default) S3method(print,datapackage) export("resource<-") export(add_resource) +export(append) export(check_package) export(create_package) export(create_schema) diff --git a/R/append.datapackage.R b/R/append.datapackage.R new file mode 100644 index 00000000..5b8a6965 --- /dev/null +++ b/R/append.datapackage.R @@ -0,0 +1,43 @@ +#' Append values to an object +#' +#' An S3 generic version of [base::append()]; see [append.datapackage()]. +#' +#' @param x the vector the values are to be appended to. +#' @param values to be included in the modified vector. +#' @param after a subscript, after which the values are to be appended. +#' +#' @returns A vector containing the values in `x` with the elements of `values` +#' appended after the specified element of `x`. +#' @export +append <- function(x, values, after = length(x)) { + # This function needs to be exported or base::append() will stop working when + # loading the package. Because base append() is not an S3 generic, we need to + # define one to dispatch to append.datapackage() or append.default(). + UseMethod("append") +} + +#' @exportS3Method +append.default <- function(x, values, after = length(x)) { + base::append(x, values, after = after) +} + +#' Append a Data Package +#' +#' Appends values to a Data Package object, preserving its attributes such as +#' directory and class. +#' +#' @param x Data Package object, as returned by [read_package()] or +#' [create_package()]. +#' @inheritParams append +#' @returns A Data Package object with the elements of `values` appended after +#' the specified element of `x`. +#' +#' @exportS3Method +#' +#' @examples +#' p <- example_package() +#' p_app <- append(p, list("$schema" = "just added"), after = 0) +#' p_app +#' attributes(p_app) +append.datapackage <- function(x, values, after = length(x)) { + append_with_attrs(x, values, after = after)} diff --git a/R/upgrade_descriptor.R b/R/upgrade_descriptor.R index 9030f366..3b3757f7 100644 --- a/R/upgrade_descriptor.R +++ b/R/upgrade_descriptor.R @@ -14,9 +14,12 @@ upgrade_descriptor <- function(package) { return(package) } - # Set $schema - purrr::pluck(package, "$schema") <- - "https://datapackage.org/profiles/2.0/datapackage.json" + # Set $schema as first property + package <- append( + package, + list("$schema" = "https://datapackage.org/profiles/2.0/datapackage.json"), + after = 0 + ) # Set $schema to profile if URL (to custom profile) profile <- package$profile %||% "undefined" diff --git a/R/upgrade_resource.R b/R/upgrade_resource.R index df132312..79614d6e 100644 --- a/R/upgrade_resource.R +++ b/R/upgrade_resource.R @@ -12,9 +12,12 @@ upgrade_resource <- function(resource) { return(resource) } - # Set $schema - purrr::pluck(resource, "$schema") <- - "https://datapackage.org/profiles/2.0/dataresource.json" + # Set $schema as first property + resource <- append_with_attrs( + resource, + list("$schema" = "https://datapackage.org/profiles/2.0/dataresource.json"), + after = 0 + ) # Set type to table if resource is tabular, see # https://datapackage.org/standard/data-resource/#type diff --git a/R/utils.R b/R/utils.R index 2d4a05df..34fe51c9 100644 --- a/R/utils.R +++ b/R/utils.R @@ -90,3 +90,28 @@ get_dot_names <- function(...) { # Return the names that are not an empty string (no name set) return(dot_names[dot_names != ""]) } + +#' Append an object, but retain attritutes +#' +#' Appends values to an object, preserving its attributes. `names` are removed +#' because they are length dependent. +#' @inheritParams base::append x values after +#' +#' @returns An object with the elements of `values` appended after the specified +#' element of `x`. +#' +#' @noRd +append_with_attrs <- function(x, values, after = length(x)){ + attrs <- attributes(x) + # Names will be the wrong length when appending + attrs[["names"]] <- NULL + p_app <- base::append(unclass(x), values, after = after) + + attributes(p_app) <- purrr::list_modify( + # Start from the newly appended attributes + attributes(p_app), + # Add our existing attributes: directory and class + val = !!!attrs + ) + p_app +} diff --git a/man/append.Rd b/man/append.Rd new file mode 100644 index 00000000..1803b74c --- /dev/null +++ b/man/append.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/append.datapackage.R +\name{append} +\alias{append} +\title{Append values to an object} +\usage{ +append(x, values, after = length(x)) +} +\arguments{ +\item{x}{the vector the values are to be appended to.} + +\item{values}{to be included in the modified vector.} + +\item{after}{a subscript, after which the values are to be appended.} +} +\value{ +A vector containing the values in \code{x} with the elements of \code{values} +appended after the specified element of \code{x}. +} +\description{ +An S3 generic version of \code{\link[base:append]{base::append()}}; see \code{\link[=append.datapackage]{append.datapackage()}}. +} diff --git a/man/append.datapackage.Rd b/man/append.datapackage.Rd new file mode 100644 index 00000000..424236b5 --- /dev/null +++ b/man/append.datapackage.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/append.datapackage.R +\name{append.datapackage} +\alias{append.datapackage} +\title{Append a Data Package} +\usage{ +\method{append}{datapackage}(x, values, after = length(x)) +} +\arguments{ +\item{x}{Data Package object, as returned by \code{\link[=read_package]{read_package()}} or +\code{\link[=create_package]{create_package()}}.} + +\item{values}{to be included in the modified vector.} + +\item{after}{a subscript, after which the values are to be appended.} +} +\value{ +A Data Package object with the elements of \code{values} appended after +the specified element of \code{x}. +} +\description{ +Appends values to a Data Package object, preserving its attributes such as +directory and class. +} +\examples{ +p <- example_package() +p_app <- append(p, list("$schema" = "just added"), after = 0) +p_app +attributes(p_app) +} diff --git a/tests/testthat/test-upgrade_descriptor.R b/tests/testthat/test-upgrade_descriptor.R index 17a57517..53b64def 100644 --- a/tests/testthat/test-upgrade_descriptor.R +++ b/tests/testthat/test-upgrade_descriptor.R @@ -53,6 +53,12 @@ test_that("upgrade_descriptor() sets $schema to default v2 value or custom expect_identical(upgrade_descriptor(p_v1)$`$schema`, v2_profile) }) +test_that("upgrade_descriptor() sets $schema to the first position", { + p_v1 <- example_package(version = "1.0") + p_upgraded <- upgrade_descriptor(p_v1) + expect_identical(names(p_upgraded)[[1]], "$schema") +}) + test_that("upgrade_descriptor() updates contributor role to roles", { p_v1 <- example_package(version = "1.0") diff --git a/tests/testthat/test-upgrade_resource.R b/tests/testthat/test-upgrade_resource.R index 4f6fe3f1..6669bb58 100644 --- a/tests/testthat/test-upgrade_resource.R +++ b/tests/testthat/test-upgrade_resource.R @@ -48,6 +48,12 @@ test_that("upgrade_resource() sets $schema to default v2 value", { expect_identical(upgrade_resource(resource_v1)$`$schema`, v2_profile) }) +test_that("upgrade_resource() sets $schema to the first position", { + resource_v1 <- resource(example_package(version = "1.0"), "deployments") + resource_upgraded <- upgrade_resource(resource_v1) + expect_identical(names(resource_upgraded)[[1]], "$schema") +}) + test_that("upgrade_resource() sets type for tabular resources", { resource_v1 <- resource(example_package(version = "1.0"), "deployments")