Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
43 changes: 43 additions & 0 deletions R/append.datapackage.R
Original file line number Diff line number Diff line change
@@ -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)}
9 changes: 6 additions & 3 deletions R/upgrade_descriptor.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 6 additions & 3 deletions R/upgrade_resource.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
22 changes: 22 additions & 0 deletions man/append.Rd

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

30 changes: 30 additions & 0 deletions man/append.datapackage.Rd

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

6 changes: 6 additions & 0 deletions tests/testthat/test-upgrade_descriptor.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-upgrade_resource.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Loading