Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tags to metadata #677

Merged
merged 9 commits into from
Dec 6, 2022
Merged
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
26 changes: 16 additions & 10 deletions R/meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ write_meta <- function(x, path) {

# pin metadata ------------------------------------------------------------

standard_meta <- function(paths, type, title = NULL, description = NULL) {
standard_meta <- function(paths,
type,
title = NULL,
description = NULL,
tags = NULL) {
list(
file = fs::path_file(paths),
file_size = as.integer(fs::file_size(paths)),
pin_hash = pin_hash(paths),
type = type,
title = title,
description = description,
tags = tags,
created = format(Sys.time(), "%Y%m%dT%H%M%SZ", tz = "UTC"),
api_version = 1
)
Expand Down Expand Up @@ -80,16 +85,17 @@ default_title <- function(name, data = NULL, path = NULL) {

paste0(name, ": ", desc)
}

friendly_type <- function(x) {
switch(typeof(x),
logical = "logical vector",
integer = "integer vector",
numeric = ,
double = "double vector",
complex = "complex vector",
character = "character vector",
raw = "raw vector",
list = "list",
typeof(x)
logical = "logical vector",
integer = "integer vector",
numeric = ,
double = "double vector",
complex = "complex vector",
character = "character vector",
raw = "raw vector",
list = "list",
typeof(x)
)
}
21 changes: 19 additions & 2 deletions R/pin-meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
#' * `$file` - names of files stored in the pin.
#' * `$file_size` - size of each file.
#' * `$pin_hash` - hash of pin contents.
#' * `$type` - type of pin, "rds", "csv", etc
#' * `$type` - type of pin: "rds", "csv", etc
#' * `$title` - pin title
#' * `$description` - pin description
#' * `$tags` - pin tags
#' * `$created` - date this (version of the pin) was created
#' * `$api_version` - API version used by pin
#'
Expand All @@ -35,6 +36,11 @@
#' b %>% pin_meta("mtcars")
#' # Get path to underlying data
#' b %>% pin_download("mtcars")
#'
#' # Use tags instead
#' b %>% pin_write(head(mtcars), "mtcars", tags = c("fuel-efficiency", "automotive"))
#' b %>% pin_meta("mtcars")
#'
pin_meta <- function(board, name, version = NULL, ...) {
check_board(board, "pin_meta()", "pin_info()")
UseMethod("pin_meta")
Expand Down Expand Up @@ -85,11 +91,12 @@ local_meta <- function(x, name, dir, url = NULL, version = NULL, ...) {

test_api_meta <- function(board) {
testthat::test_that("can round-trip pin metadata", {
name <- local_pin(board, 1, title = "title", description = "desc", metadata = list(a = "a"))
name <- local_pin(board, 1, title = "title", description = "desc", metadata = list(a = "a"), tags = c("tag1", "tag2"))
meta <- pin_meta(board, name)
testthat::expect_equal(meta$name, name)
testthat::expect_equal(meta$title, "title")
testthat::expect_equal(meta$description, "desc")
testthat::expect_equal(meta$tags, c("tag1", "tag2"))
testthat::expect_equal(meta$user$a, "a")
})

Expand All @@ -110,6 +117,15 @@ test_api_meta <- function(board) {
)
})

testthat::test_that("metadata checking functions give correct errors", {
testthat::expect_snapshot_error(
local_pin(board, 1, title = "title", tags = list(a = "a"))
)
testthat::expect_snapshot_error(
local_pin(board, 1, title = "title", metadata = c("tag1", "tag2"))
)
})

testthat::test_that("pin_meta() returns pins_meta object", {
name <- local_pin(board, 1)

Expand All @@ -127,6 +143,7 @@ test_api_meta <- function(board) {
testthat::expect_vector(meta$user, list())
testthat::expect_vector(meta$local, list())
})

}

#' @export
Expand Down
14 changes: 12 additions & 2 deletions R/pin-read-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pin_read <- function(board, name, version = NULL, hash = NULL, ...) {
#' lists and rds for everything else.
#' @param versioned Should the pin be versioned? The default, `NULL`, will
#' use the default for `board`
#' @param tags A character vector of tags for the pin; most important for
#' discoverability on shared boards.
#' @rdname pin_read
#' @export
pin_write <- function(board, x,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I was to design this function today, I would put ... after x.

Expand All @@ -69,6 +71,7 @@ pin_write <- function(board, x,
description = NULL,
metadata = NULL,
versioned = NULL,
tags = NULL,
...) {
ellipsis::check_dots_used()
check_board(board, "pin_write()", "pin()")
Expand All @@ -83,6 +86,7 @@ pin_write <- function(board, x,
}
}
check_metadata(metadata)
check_tags(tags)
if (!is_string(name)) {
abort("`name` must be a string")
}
Expand All @@ -100,7 +104,8 @@ pin_write <- function(board, x,
paths = path,
type = type,
title = title %||% default_title(name, data = x),
description = description
description = description,
tags = tags
)
meta$user <- metadata

Expand Down Expand Up @@ -248,7 +253,12 @@ check_name <- function(x) {
}
check_metadata <- function(x) {
if (!is.null(x) && !is_bare_list(x)) {
abort("`metadata` must be a list")
abort("`metadata` must be a list.")
}
}
check_tags <- function(x) {
if (!is.null(x) && !is_character(x)) {
abort("`tags` must be a character vector.")
}
}
check_hash <- function(meta, hash) {
Expand Down
8 changes: 7 additions & 1 deletion man/pin_meta.Rd

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

4 changes: 4 additions & 0 deletions man/pin_read.Rd

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

16 changes: 16 additions & 0 deletions tests/testthat/_snaps/board_azure_adls2.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
Output
[1] "AzureStor"

# metadata checking functions give correct errors

`tags` must be a character vector.

---

`metadata` must be a list.

---

`tags` must be a character vector.

---

`metadata` must be a list.

# can deparse

Code
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/board_azure_blob.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
Output
[1] "AzureStor"

# metadata checking functions give correct errors

`tags` must be a character vector.

---

`metadata` must be a list.

---

`tags` must be a character vector.

---

`metadata` must be a list.

# can deparse

Code
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/board_azure_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
Output
[1] "AzureStor"

# metadata checking functions give correct errors

`tags` must be a character vector.

---

`metadata` must be a list.

---

`tags` must be a character vector.

---

`metadata` must be a list.

# can deparse

Code
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/board_folder.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
Output
character(0)

# metadata checking functions give correct errors

`tags` must be a character vector.

---

`metadata` must be a list.

# has useful print method

Code
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/board_s3.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
Output
[1] "paws.storage"

# metadata checking functions give correct errors

`tags` must be a character vector.

---

`metadata` must be a list.

# can deparse

Code
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/meta.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# standard metadata is useful

List of 8
List of 9
$ file : chr "df.rds"
$ file_size : int 200
$ pin_hash : chr "db696042be80dbb4"
$ type : chr "arrow"
$ title : chr "title"
$ description: NULL
$ tags : NULL
$ created : chr "<TODAY>"
$ api_version: num 1

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/pin-read-write.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
pin_write(board, mtcars, name = "mtcars", metadata = 1)
Condition
Error in `check_metadata()`:
! `metadata` must be a list
! `metadata` must be a list.

# pin_write() noisily generates name and type

Expand Down