Skip to content
Merged
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
^pkgdown$
^README\.Rmd$
^CRAN-SUBMISSION$
^\.positai$
^\.claude$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ docs

# Mac OS
.DS_Store
.positai
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export(remove_resource)
export(resource_names)
export(resources)
export(schema)
export(version)
export(write_package)
import(rlang)
importFrom(readr,problems)
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## For users

* New `version()` determines what version of the Data Package standard is used by a Data Package (e.g. `"1.0"`, `"2.0"`, `">=2.0"`). This based on the presence and value of the `$schema` property (#299).
* `read_resource()` now supports reading from remote zip files, thanks to support in `{vroom}` (1.3.0) (#291).
* `write_package()` will now print multiple `resource$path` and `resource$schema$missingValues` on multiple lines (#297).
* `resources()` is soft-deprecated, please use `resource_names()` instead (#282).
Expand Down
43 changes: 43 additions & 0 deletions R/version.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#' Get Data Package version
#'
#' Determines what version of the [Data Package standard](
#' https://datapackage.org/) is used by a Data Package, based on the
#' [`$schema`](https://datapackage.org/standard/data-package/#dollar-schema)
#' property.
Comment thread
peterdesmet marked this conversation as resolved.
#' Version `"1.0"` is assumed if `$schema` is missing, version `">=2.0"` is
#' assumed for custom values (e.g. [extensions](
#' https://datapackage.org/standard/extensions/)).
#'
#' @inheritParams read_resource
#' @returns Data Package version number (e.g. `"1.0"`).
#' @family version functions
#' @export
#' @examples
#' package <- example_package()
#' version(package)
version <- function(package) {
dollar_schema <- purrr::pluck(package, "$schema")

if (is.null(dollar_schema)) {
return("1.0") # Assume 1.0 if $schema is undefined
}
if (!is.character(dollar_schema)) {
return(">=2.0") # Assume 2.0 or higher if $schema is present but invalid
}

# Extract version from e.g.
# "https://datapackage.org/profiles/<version>/datapackage.json"
pattern = "^https://datapackage\\.org/profiles/((?:[0-9A-Za-z]|\\.|-)+)/.*"
extracted_version <- sub(
pattern,
"\\1",
dollar_schema,
perl = TRUE
)

if (extracted_version == dollar_schema) {
return(">=2.0") # Assume 2.0 or higher if $schema is a custom value
}

extracted_version
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ reference:
- has_concept("accessor functions")
- has_concept("print functions")
- has_concept("check functions")
- has_concept("version functions")
- has_concept("sample data")
27 changes: 27 additions & 0 deletions man/version.Rd

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

44 changes: 44 additions & 0 deletions tests/testthat/test-version.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
test_that("version() returns version based on $schema", {
p <- create_package()

# Undefined
p$`$schema` <- NULL
expect_equal(version(p), "1.0")

# Defined default
p$`$schema` <- "https://datapackage.org/profiles/1.0/datapackage.json"
expect_equal(version(p), "1.0")

# 2.0
p$`$schema` <- "https://datapackage.org/profiles/2.0/datapackage.json"
expect_equal(version(p), "2.0")

# Future versions
p$`$schema` <- "https://datapackage.org/profiles/2.1/datapackage.json"
expect_equal(version(p), "2.1")
p$`$schema` <- "https://datapackage.org/profiles/2.2-rc.1/datapackage.json"
expect_equal(version(p), "2.2-rc.1")
p$`$schema` <- "https://datapackage.org/profiles/3.0/datapackage.json"
expect_equal(version(p), "3.0")

# Custom extensions
p$`$schema` <- "https://spatial.datapackage.org/profiles/1.0/datapackage.json"
expect_equal(version(p), ">=2.0")
p$`$schema` <- "http://rs.tdwg.org/dwc-dp/1.0/dwc-dp-profile.json"
expect_equal(version(p), ">=2.0")
})

test_that("version() returns >=2.0 for invalid $schema", {
p <- create_package()
p$`$schema` <- list()
expect_equal(version(p), ">=2.0")
p$`$schema` <- 3.0
expect_equal(version(p), ">=2.0")
})

test_that("version() returns correct version for example packages", {
p_1.0 <- example_package(version = "1.0")
expect_equal(version(p_1.0), "1.0")
p_2.0 <- example_package(version = "2.0")
expect_equal(version(p_2.0), "2.0")
})
Loading