Skip to content

Commit

Permalink
Added utility to unpack a Gobbler path to its project/asset/version d…
Browse files Browse the repository at this point in the history
…etails.
  • Loading branch information
LTLA committed Oct 2, 2024
1 parent f145d7f commit a847f7e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: gobbler
Version: 0.3.8
Date: 2024-09-18
Version: 0.3.9
Date: 2024-10-02
Title: Interface to the gobbler service
Description:
Friendly interface to the gobbler service.
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export(serviceInfo)
export(setPermissions)
export(startGobbler)
export(stopGobbler)
export(unpackPath)
export(uploadDirectory)
export(versionPath)
import(httr2)
Expand Down
27 changes: 27 additions & 0 deletions R/unpackPath.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#' Unpack a path to its project-asset-version combination
#'
#' Unpack a Gobbler path to its combination of project, asset, version, and (optionally) path,
#' for easier use in the various \pkg{gobbler} functions.
#'
#' @param path String containing a relative path within the Gobbler registry.
#'
#' @return List containing \code{project}, \code{asset}, \code{version} and \code{path}.
#' All are strings, except for \code{path}, which may be \code{NULL}.
#'
#' @author Aaron Lun
#'
#' @examples
#' unpackPath("project/asset/version/path")
#' unpackPath("project/asset/version")
#'
#' @export
unpackPath <- function(path) {
components <- strsplit(path, "/")[[1]]
stopifnot(length(components) >= 3L)
if (length(components) == 3L || components[4] == "") {
path <- NULL
} else {
path <- paste(tail(components, -3), collapse="/")
}
list(project=components[1], asset=components[2], version=components[3], path=path)
}
27 changes: 27 additions & 0 deletions man/unpackPath.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat/test-unpackPath.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# library(gobbler); library(testthat); source("test-unpackPath.R")

test_that("unpackPath works as expected", {
out <- unpackPath("project/asset/version/path")
expect_identical(out, list(project="project", asset="asset", version="version", path="path"))

out <- unpackPath("project/asset/version")
expect_identical(out, list(project="project", asset="asset", version="version", path=NULL))

out <- unpackPath("project/asset/version/")
expect_identical(out, list(project="project", asset="asset", version="version", path=NULL))
})

0 comments on commit a847f7e

Please sign in to comment.