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 new urls to metadata #795

Merged
merged 11 commits into from
Oct 19, 2023
Merged
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* Removed content and user caches for Connect altogether. Now, we look up
usernames and content on the Connect server every time (#793).

* Added new `urls` item to metadata for a pin (#795).

# pins 1.2.2

* Fixed how dots are checked in `pin_write()` to make user-facing messages more
Expand Down
2 changes: 2 additions & 0 deletions R/board_connect_bundle.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ rsc_bundle_preview_index <- function(board, name, x, metadata) {
pin_files = paste0("<a href=\"", metadata$file, "\">", metadata$file, "</a>", collapse = ", "),
data_preview = jsonlite::toJSON(data_preview, auto_unbox = TRUE),
data_preview_style = if (is.data.frame(x)) "" else "display:none",
urls = paste0("<a href=\"", metadata$urls, "\">", metadata$urls, "</a>", collapse = ", "),
url_preview_style = if (!is.null(metadata$urls)) "" else "display:none",
pin_name = paste0(owner, "/", name$name),
pin_metadata = list(
as_yaml = yaml::as.yaml(metadata),
Expand Down
4 changes: 3 additions & 1 deletion R/meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ standard_meta <- function(paths,
type,
title = NULL,
description = NULL,
tags = NULL) {
tags = NULL,
urls = NULL) {
list(
file = fs::path_file(paths),
file_size = as.integer(fs::file_size(paths)),
Expand All @@ -45,6 +46,7 @@ standard_meta <- function(paths,
title = title,
description = description,
tags = if (is.null(tags)) tags else as.list(tags),
urls = if (is.null(urls)) urls else as.list(urls),
created = format(Sys.time(), "%Y%m%dT%H%M%SZ", tz = "UTC"),
api_version = 1L
)
Expand Down
7 changes: 6 additions & 1 deletion R/pin-meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#' * `$title` - pin title
#' * `$description` - pin description
#' * `$tags` - pin tags
#' * `$urls` - URLs for more info on pin
#' * `$created` - date this (version of the pin) was created
#' * `$api_version` - API version used by pin
#'
Expand Down Expand Up @@ -97,12 +98,13 @@ empty_local_meta <- local_meta(x = NULL, name = NULL, dir = 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"), tags = c("tag1", "tag2"))
name <- local_pin(board, 1, title = "title", description = "desc", metadata = list(a = "a"), tags = c("tag1", "tag2"), urls = c("https://posit.co/"))
juliasilge marked this conversation as resolved.
Show resolved Hide resolved
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$urls, c("https://posit.co/"))
testthat::expect_equal(meta$user$a, "a")
})

Expand Down Expand Up @@ -133,6 +135,9 @@ test_api_meta <- function(board) {
testthat::expect_snapshot_error(
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this test could be run just once for pin_write() since the code doesn't vary across backends?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, great point, yep 👍

local_pin(board, 1, title = "title", tags = list(a = "a"))
)
testthat::expect_snapshot_error(
local_pin(board, 1, title = "title", urls = list(a = "a"))
)
testthat::expect_snapshot_error(
local_pin(board, 1, title = "title", metadata = c("tag1", "tag2"))
)
Expand Down
7 changes: 6 additions & 1 deletion R/pin-read-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ pin_read <- function(board, name, version = NULL, hash = NULL, ...) {
#' use the default for `board`
#' @param tags A character vector of tags for the pin; most important for
#' discoverability on shared boards.
#' @param urls A character vector of URLs for more info on the pin, such as a
#' link to a wiki or other documentation.
#' @param force_identical_write Store the pin even if the pin contents are
#' identical to the last version (compared using the hash). Only the pin
#' contents are compared, not the pin metadata. Defaults to `FALSE`.
Expand All @@ -78,6 +80,7 @@ pin_write <- function(board, x,
metadata = NULL,
versioned = NULL,
tags = NULL,
urls = NULL,
force_identical_write = FALSE) {
check_board(board, "pin_write", "pin")
dots <- list2(...)
Expand All @@ -96,6 +99,7 @@ pin_write <- function(board, x,
}
check_metadata(metadata)
check_character(tags, allow_null = TRUE)
check_character(urls, allow_null = TRUE)
if (!is_string(name)) {
abort("`name` must be a string")
}
Expand All @@ -114,7 +118,8 @@ pin_write <- function(board, x,
type = type,
title = title %||% default_title(name, data = x),
description = description,
tags = tags
tags = tags,
urls = urls
)
meta$user <- metadata

Expand Down
5 changes: 5 additions & 0 deletions inst/preview/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ <h3>{{{pin_name}}}</h3>
{{/pin_metadata}}
</section>

<section style="{{url_preview_style}}">
<h3>Learn more about this pin at:</h3>
<p>{{{urls}}}</p>
</section>
juliasilge marked this conversation as resolved.
Show resolved Hide resolved

<section>
<h3>R Code</h3>
<pre id="pin-r" class="pin-code"><code class="r">library(pins)
Expand Down
1 change: 1 addition & 0 deletions 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.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/board_azure_adls2.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

`tags` must be a character vector or `NULL`, not a list.

---

`urls` must be a character vector or `NULL`, not a list.

---

`metadata` must be a list or `NULL`, not a character vector.
Expand All @@ -24,6 +28,10 @@

`tags` must be a character vector or `NULL`, not a list.

---

`urls` must be a character vector or `NULL`, not a list.

---

`metadata` must be a list or `NULL`, not a character vector.
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/board_azure_blob.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

`tags` must be a character vector or `NULL`, not a list.

---

`urls` must be a character vector or `NULL`, not a list.

---

`metadata` must be a list or `NULL`, not a character vector.
Expand All @@ -24,6 +28,10 @@

`tags` must be a character vector or `NULL`, not a list.

---

`urls` must be a character vector or `NULL`, not a list.

---

`metadata` must be a list or `NULL`, not a character vector.
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/board_azure_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

`tags` must be a character vector or `NULL`, not a list.

---

`urls` must be a character vector or `NULL`, not a list.

---

`metadata` must be a list or `NULL`, not a character vector.
Expand All @@ -24,6 +28,10 @@

`tags` must be a character vector or `NULL`, not a list.

---

`urls` must be a character vector or `NULL`, not a list.

---

`metadata` must be a list or `NULL`, not a character vector.
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/_snaps/board_connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

`tags` must be a character vector or `NULL`, not a list.

---

`urls` must be a character vector or `NULL`, not a list.

---

`metadata` must be a list or `NULL`, not a character vector.
Expand Down
12 changes: 10 additions & 2 deletions tests/testthat/_snaps/board_connect_bundle.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<b>Format:</b> rds &bull;
<b>API:</b> v1.0
</p>
<p></p>
<p>Some simple data to test with</p>
<p>Download data: <a href="test.csv">test.csv</a></p>
<details>
<summary>Raw metadata</summary>
Expand All @@ -47,7 +47,10 @@
pin_hash: 77fee172a9275a62
type: rds
title: 'test: a pinned 2 x 2 data frame'
desctiption: Some simple data to test with
description: Some simple data to test with
urls:
- https://posit.co/
- https://www.tidyverse.org/
created: 20211111T113956Z
api_version: '1.0'
user:
Expand All @@ -56,6 +59,11 @@
</details>
</section>

<section style="">
<h3>Learn more about this pin at:</h3>
<p><a href="https://posit.co/">https://posit.co/</a>, <a href="https://www.tidyverse.org/">https://www.tidyverse.org/</a></p>
</section>

<section>
<h3>R Code</h3>
<pre id="pin-r" class="pin-code"><code class="r">library(pins)
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/_snaps/board_folder.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

`tags` must be a character vector or `NULL`, not a list.

---

`urls` must be a character vector or `NULL`, not a list.

---

`metadata` must be a list or `NULL`, not a character vector.
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/_snaps/board_gcs.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

`tags` must be a character vector or `NULL`, not a list.

---

`urls` must be a character vector or `NULL`, not a list.

---

`metadata` must be a list or `NULL`, not a character vector.
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/_snaps/board_gdrive.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

`tags` must be a character vector or `NULL`, not a list.

---

`urls` must be a character vector or `NULL`, not a list.

---

`metadata` must be a list or `NULL`, not a character vector.
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/_snaps/board_s3.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

`tags` must be a character vector or `NULL`, not a list.

---

`urls` must be a character vector or `NULL`, not a list.

---

`metadata` must be a list or `NULL`, not a character vector.
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/_snaps/meta.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# standard metadata is useful

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

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-board_connect_bundle.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ test_that("generates index files", {
pin_hash = "77fee172a9275a62",
type = "rds",
title = "test: a pinned 2 x 2 data frame",
desctiption = "Some simple data to test with",
description = "Some simple data to test with",
urls = c("https://posit.co/", "https://www.tidyverse.org/"),
created = "20211111T113956Z",
api_version = "1.0",
user = list(my_meta = "User defined metadata")
Expand Down
Loading