Skip to content

Commit 980a84c

Browse files
Copilotjonthegeek
andauthored
Use tib_spec attribute in untibblify() (#291)
* Initial plan * Use tib_spec attribute in untibblify() (#235) Agent-Logs-Url: https://github.com/wranglezone/tibblify/sessions/c08ef600-e052-40db-bdfd-d33c42350b00 Co-authored-by: jonthegeek <33983824+jonthegeek@users.noreply.github.com> * Use get_spec(x) as default for spec argument Agent-Logs-Url: https://github.com/wranglezone/tibblify/sessions/61afbd16-93bc-443e-bc51-11bd16d28253 Co-authored-by: jonthegeek <33983824+jonthegeek@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jonthegeek <33983824+jonthegeek@users.noreply.github.com>
1 parent f466238 commit 980a84c

File tree

5 files changed

+54
-14
lines changed

5 files changed

+54
-14
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# tibblify (development version)
22

3+
* `untibblify()` now automatically uses the `tib_spec` attribute when present, so tibblified objects can be round-tripped without explicitly passing the spec (#235).
34
* `parse_openapi_spec()` supports many more fields and works for many more APIs (#190, #200, @jonthegeek and @mgirlich).
45
* The underlying C implementation has been updated to better comply with R's C API. We also fixed various bugs during this update (#203, #204, #222).
56
* Documentation of all functions has been updated for clarity (#228, #245, #246).

R/untibblify.R

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#' into a nested list.
55
#'
66
#' @param x A data frame or an object.
7-
#' @param spec Optional. A spec object which was used to create `x`.
7+
#' @param spec Optional. A spec object which was used to create `x`. Defaults
8+
#' to the spec stored as the `tib_spec` attribute of `x`, if present.
89
#'
910
#' @return A nested list.
1011
#' @export
@@ -18,7 +19,7 @@
1819
#' )
1920
#' )
2021
#' untibblify(x)
21-
untibblify <- function(x, spec = NULL) {
22+
untibblify <- function(x, spec = get_spec(x)) {
2223
call <- current_call()
2324

2425
if (is.data.frame(x)) {
@@ -33,15 +34,6 @@ untibblify <- function(x, spec = NULL) {
3334
}
3435

3536
untibblify_df <- function(x, spec, call) {
36-
if (is.null(spec)) {
37-
idx <- seq_len(vctrs::vec_size(x))
38-
out <- purrr::map(
39-
idx,
40-
~ untibblify_row(vctrs::vec_slice(x, .x), spec, call)
41-
)
42-
return(out)
43-
}
44-
4537
idx <- seq_len(vctrs::vec_size(x))
4638
purrr::map(idx, ~ untibblify_row(vctrs::vec_slice(x, .x), spec, call))
4739
}
@@ -50,7 +42,6 @@ untibblify_row <- function(x, spec, call) {
5042
if (!is.null(spec)) {
5143
x <- apply_spec_renaming(x, spec)
5244
}
53-
# browser()
5445

5546
out <- as.list(x)
5647
fields <- spec$fields

man/dot-pkg_abort.Rd

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/untibblify.Rd

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-untibblify.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@ test_that("can rename according to spec (#49)", {
186186
)
187187
})
188188

189+
test_that("uses tib_spec attribute when no spec provided (#235)", {
190+
input <- list(
191+
list(x = 1, y = "a"),
192+
list(x = 2, y = "b")
193+
)
194+
spec <- tspec_df(x2 = tib_int("x"), y2 = tib_chr("y"))
195+
tibbed <- tibblify(input, spec)
196+
197+
expect_equal(untibblify(tibbed), input)
198+
})
199+
189200
test_that("checks input (#49)", {
190201
expect_snapshot({
191202
(expect_error(untibblify(1:3)))

0 commit comments

Comments
 (0)