Skip to content

Commit

Permalink
colearendt#26 make path(...) work with enter object as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy committed Sep 17, 2016
1 parent 64ecd15 commit e6790da
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 31 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ importFrom(purrr,flatten_chr)
importFrom(purrr,flatten_dbl)
importFrom(purrr,flatten_int)
importFrom(purrr,invoke_map)
importFrom(purrr,lift_vl)
importFrom(purrr,list_along)
importFrom(purrr,map)
importFrom(purrr,map2)
Expand Down
19 changes: 13 additions & 6 deletions R/enter_object.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#' entered into, \code{\link{gather_array}} to gather an array in an object
#' and \code{\link{spread_all}} to spread values in an object.
#' @param .x a json string or tbl_json object
#' @param ... a sequence of character strings designating the object name or
#' sequences of names you wish to enter
#' @param ... a quoted or unquoted sequence of strings designating the object
#' name or sequences of names you wish to enter
#' @return a \code{\link{tbl_json}} object
#' @export
#' @examples
Expand All @@ -35,21 +35,28 @@
#' # Let's capture the parent first and then enter in the children object
#' json %>% spread_all %>% enter_object("children")
#'
#' # No need to quote the path "children"
#' json %>% spread_all %>% enter_object(children)
#'
#' # Notice that "anne" was discarded, as she has no children
#'
#' # We can now use gather array to stack the array
#' json %>% spread_all %>% enter_object("children") %>%
#' json %>% spread_all %>% enter_object(children) %>%
#' gather_array("child.num")
#'
#' # And append_values_string to add the children names
#' json %>% spread_all %>% enter_object("children") %>%
#' json %>% spread_all %>% enter_object(children) %>%
#' gather_array("child.num") %>%
#' append_values_string("child")
#'
#' # The path can be comma delimited to go deep into a nested object
#' json <- '{"name": "bob", "attributes": {"age": 32, "gender": "male"}}'
#' json %>% enter_object(attributes, age)
#'
#' # A more realistc example with companies data
#' library(dplyr)
#' companies %>%
#' enter_object("acquisitions") %>%
#' enter_object(acquisitions) %>%
#' gather_array %>%
#' spread_all %>%
#' glimpse
Expand All @@ -58,7 +65,7 @@ enter_object <- function(.x, ...) {
if (!is.tbl_json(.x)) .x <- as.tbl_json(.x)

# Prepare path
path <- list(...)
path <- path(...) %>% as.list

# Extract json
json <- attr(.x, "JSON")
Expand Down
2 changes: 1 addition & 1 deletion R/tidyjson-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @import jsonlite
#' @importFrom purrr map map_lgl map_dbl map_int map_chr map2 %||% invoke_map
#' @importFrom purrr map_if at_depth flatten_chr flatten_dbl flatten_int
#' @importFrom purrr compose partial map2_chr list_along every
#' @importFrom purrr compose partial map2_chr list_along every lift_vl
#' @import dplyr
#' @import tidyr
#' @importFrom magrittr extract2 is_greater_than
Expand Down
17 changes: 12 additions & 5 deletions man/enter_object.Rd

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

50 changes: 31 additions & 19 deletions tests/testthat/test-enter_object.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
context("enter_object")

test_that("filter works with one path", {

json <- '{"name": "bob", "attributes": {"age": 32, "gender": "male"}}'

expect_identical(
Expand All @@ -11,12 +11,24 @@ test_that("filter works with one path", {
list(list(age = 32L, gender = "male"))
)
)

}
)

test_that("works with quoted or unquoted", {

json <- '{"name": "bob", "attributes": {"age": 32, "gender": "male"}}'

expect_identical(
json %>% enter_object("attributes", "age"),
json %>% enter_object(attributes, age)
)

}
)

test_that("filter works with multiple depth paths", {

json <- '{"name": "bob", "attributes": { "demographics": {"age": 32, "gender": "male"}}}'

expect_identical(
Expand All @@ -26,67 +38,67 @@ test_that("filter works with multiple depth paths", {
list(list(age = 32L, gender = "male"))
)
)

}
)

test_that("filter removes records with missing path", {

json <- c(
'{"name": "bob", "attributes": {"age": 32, "gender": "male"}}',
'{"name": "susan"}'
)

expect_identical(
json %>% spread_values(name = jstring("name")) %>%
enter_object("attributes"),
tbl_json(
data.frame(
document.id = 1L,
name = 'bob',
document.id = 1L,
name = 'bob',
stringsAsFactors = FALSE),
list(list(age = 32L, gender = "male"))
)
)

}
)

test_that("works if no paths exist", {

json <- '{"name": "bob"}'

expect_identical(
json %>% spread_values(name = jstring("name")) %>%
enter_object("attributes"),
tbl_json(
data.frame(
document.id = integer(0),
name = character(0),
document.id = integer(0),
name = character(0),
stringsAsFactors = FALSE),
list()
)
)

}
)

test_that("correctly handles character(0), {}, []", {

empty <- tbl_json(
data.frame(
document.id = integer(0),
document.id = integer(0),
stringsAsFactors = FALSE),
list())

expect_identical(
character(0) %>% enter_object("name"),
empty)

expect_identical(
'{}' %>% enter_object("name"),
empty)

expect_identical(
'[]' %>% enter_object("name"),
empty)
Expand Down

0 comments on commit e6790da

Please sign in to comment.