Skip to content

Commit

Permalink
Merge pull request #35 from sailthru/force_tbl_json
Browse files Browse the repository at this point in the history
#31 remove the need to use as.tbl_json all the time
  • Loading branch information
Jeremy Stanley committed Apr 21, 2015
2 parents 5efb11b + d354614 commit 3a89d85
Show file tree
Hide file tree
Showing 22 changed files with 62 additions and 70 deletions.
2 changes: 1 addition & 1 deletion R/append_values.r
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ append_values_factory <- function(type, as.value) {

function(x, column.name = type, force=TRUE) {

assert_that(is.tbl_json(x))
if (!is.tbl_json(x)) x <- as.tbl_json(x)

# Extract json
json <- attr(x, "JSON")
Expand Down
1 change: 0 additions & 1 deletion R/data-commits.r
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#'
#' # Extract metadata for every commit
#' commits %>% # single json document of github commits from dplyr
#' as.tbl_json %>% # turn into a 'tbl_json'
#' gather_array %>% # stack as an array
#' spread_values(
#' sha = jstring("sha"),
Expand Down
1 change: 0 additions & 1 deletion R/data-companies.r
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#'
#' # Get the key employees data
#' key_employees <- companies %>%
#' as.tbl_json %>%
#' spread_values(
#' name = jstring("name")
#' ) %>%
Expand Down
5 changes: 2 additions & 3 deletions R/data-issues.r
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#'
#' # Extract metadata for every issue
#' issues %>% # single json document of github issues from dplyr
#' as.tbl_json %>% # turn into a 'tbl_json'
#' gather_array %>% # stack as an array
#' spread_values(
#' id = jnumber("id"),
Expand All @@ -27,7 +26,6 @@
#'
#' # Extract label content for issues with labels
#' issues %>% # single json document of github issues from dplyr
#' as.tbl_json %>% # turn into a 'tbl_json'
#' gather_array %>% # stack as an array
#' spread_values(id = jnumber("id")) %>% # capture issue id for relational purposes
#' enter_object("labels") %>% # filter just those with labels
Expand All @@ -39,7 +37,8 @@
#' )
#'
#' # Get all URLs at the top level of the JSON
#' issues %>% as.tbl_json %>% gather_array %>%
#' issues %>%
#' gather_array %>%
#' gather_keys %>%
#' append_values_string() %>%
#' filter(grepl("url", key))
Expand Down
1 change: 0 additions & 1 deletion R/data-worldbank.r
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#'
#' # Get the top 10 sectors by funded project in Africa
#' wb_sectors <- worldbank %>% # 500 Projects funded by the world bank
#' as.tbl_json %>% # turn into a tbl_json object
#' spread_values(
#' name = jstring("project_name"), # Spread name
#' region = jstring("regionname") # Spread region
Expand Down
2 changes: 1 addition & 1 deletion R/enter_object.r
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' @export
enter_object <- function(x, ...) {

assert_that(is.tbl_json(x))
if (!is.tbl_json(x)) x <- as.tbl_json(x)

# Prepare path
path <- prep_path(...)
Expand Down
2 changes: 1 addition & 1 deletion R/gather_array.r
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @export
gather_array <- function(x, column.name = "array.index") {

assert_that(is.tbl_json(x))
if (!is.tbl_json(x)) x <- as.tbl_json(x)

# Get JSON
json <- attr(x, "JSON")
Expand Down
2 changes: 1 addition & 1 deletion R/gather_keys.r
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @export
gather_keys <- function(x, column.name = "key") {

assert_that(is.tbl_json(x))
if (!is.tbl_json(x)) x <- as.tbl_json(x)

# Get JSON
json <- attr(x, "JSON")
Expand Down
2 changes: 1 addition & 1 deletion R/json_lengths.r
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @export
json_lengths <- function(x, column.name = "length") {

assert_that(is.tbl_json(x))
if (!is.tbl_json(x)) x <- as.tbl_json(x)

# Extract json
json <- attr(x, "JSON")
Expand Down
2 changes: 1 addition & 1 deletion R/json_types.r
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @export
json_types <- function(x, column.name = "type") {

assert_that(is.tbl_json(x))
if (!is.tbl_json(x)) x <- as.tbl_json(x)

# Extract json
json <- attr(x, "JSON")
Expand Down
2 changes: 1 addition & 1 deletion R/spread_values.r
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @export
spread_values <- function(x, ...) {

assert_that(is.tbl_json(x))
if (!is.tbl_json(x)) x <- as.tbl_json(x)

# Get JSON
json <- attr(x, "JSON")
Expand Down
1 change: 0 additions & 1 deletion man/commits.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ commits \%>\% substr(1, 2000) \%>\% writeLines

# Extract metadata for every commit
commits \%>\% # single json document of github commits from dplyr
as.tbl_json \%>\% # turn into a 'tbl_json'
gather_array \%>\% # stack as an array
spread_values(
sha = jstring("sha"),
Expand Down
1 change: 0 additions & 1 deletion man/companies.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ companies[[1]] \%>\% writeLines

# Get the key employees data
key_employees <- companies \%>\%
as.tbl_json \%>\%
spread_values(
name = jstring("name")
) \%>\%
Expand Down
5 changes: 2 additions & 3 deletions man/issues.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ issues \%>\% substr(1, 2000) \%>\% writeLines

# Extract metadata for every issue
issues \%>\% # single json document of github issues from dplyr
as.tbl_json \%>\% # turn into a 'tbl_json'
gather_array \%>\% # stack as an array
spread_values(
id = jnumber("id"),
Expand All @@ -33,7 +32,6 @@ issues \%>\% # single json document of github issues from dplyr

# Extract label content for issues with labels
issues \%>\% # single json document of github issues from dplyr
as.tbl_json \%>\% # turn into a 'tbl_json'
gather_array \%>\% # stack as an array
spread_values(id = jnumber("id")) \%>\% # capture issue id for relational purposes
enter_object("labels") \%>\% # filter just those with labels
Expand All @@ -45,7 +43,8 @@ issues \%>\% # single json document of github issues from dplyr
)

# Get all URLs at the top level of the JSON
issues \%>\% as.tbl_json \%>\% gather_array \%>\%
issues \%>\%
gather_array \%>\%
gather_keys \%>\%
append_values_string() \%>\%
filter(grepl("url", key))
Expand Down
1 change: 0 additions & 1 deletion man/worldbank.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ worldbank[[1]] \%>\% writeLines

# Get the top 10 sectors by funded project in Africa
wb_sectors <- worldbank \%>\% # 500 Projects funded by the world bank
as.tbl_json \%>\% # turn into a tbl_json object
spread_values(
name = jstring("project_name"), # Spread name
region = jstring("regionname") # Spread region
Expand Down
22 changes: 11 additions & 11 deletions tests/testthat/test-append_values.r
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test_that("has correct complete structure with simple input", {
json <- '[{"name": "anne"}, {"name": "bob"}, {"name": "george"}]'

expect_identical(
json %>% as.tbl_json %>% gather_array %>% gather_keys %>%
json %>% gather_array %>% gather_keys %>%
append_values_string,
tbl_json(
data.frame(
Expand All @@ -27,7 +27,7 @@ test_that("string works with value array", {
json <- '["a", "b"]'

expect_identical(
json %>% as.tbl_json %>% gather_array %>% append_values_string,
json %>% gather_array %>% append_values_string,
tbl_json(
data.frame(
document.id = c(1L, 1L),
Expand All @@ -47,7 +47,7 @@ test_that("string works with simple input", {
json <- '["a", "b", null]'

expect_identical(
(json %>% as.tbl_json %>% gather_array %>% append_values_string)$string,
(json %>% gather_array %>% append_values_string)$string,
c("a", "b", NA_character_)
)

Expand All @@ -59,7 +59,7 @@ test_that("number works with simple input", {
json <- '[1, 2, null]'

expect_identical(
(json %>% as.tbl_json %>% gather_array %>% append_values_number)$number,
(json %>% gather_array %>% append_values_number)$number,
c(1, 2, NA_real_)
)

Expand All @@ -71,7 +71,7 @@ test_that("logical works with simple input", {
json <- '[true, false, null]'

expect_identical(
(json %>% as.tbl_json %>% gather_array %>% append_values_logical)$logical,
(json %>% gather_array %>% append_values_logical)$logical,
c(TRUE, FALSE, NA)
)

Expand All @@ -80,7 +80,7 @@ test_that("logical works with simple input", {

test_that("handles mixed input as appropriate NA", {

data <- '["a", 1, true, null]' %>% as.tbl_json %>% gather_array
data <- '["a", 1, true, null]' %>% gather_array
expect_identical(
(data %>% append_values_string)$string,
c("a", "1", "TRUE", NA_character_)
Expand Down Expand Up @@ -111,7 +111,7 @@ test_that("correctly handles character(0)", {
list())

expect_identical(
character(0) %>% as.tbl_json %>% append_values_string,
character(0) %>% append_values_string,
empty)

}
Expand All @@ -130,7 +130,7 @@ test_that("correctly handles {}", {
list(nl))

expect_identical(
'{}' %>% as.tbl_json %>% append_values_string,
'{}' %>% append_values_string,
empty)

}
Expand All @@ -146,15 +146,15 @@ test_that("correctly handles []", {
list(list()))

expect_identical(
'[]' %>% as.tbl_json %>% append_values_string,
'[]' %>% append_values_string,
empty)

}
)

test_that("correctly handles mixed types when force=FALSE", {

data <- '["a", 1, true, null]' %>% as.tbl_json %>% gather_array
data <- '["a", 1, true, null]' %>% gather_array

expect_identical(
(data %>% append_values_string(force=FALSE))$string,
Expand All @@ -173,7 +173,7 @@ test_that("correctly handles mixed types when force=FALSE", {

test_that("correctly handles append when trying to append an array", {

data <- '[["a", "b", "c"], "d", "e", "f"]' %>% as.tbl_json %>% gather_array
data <- '[["a", "b", "c"], "d", "e", "f"]' %>% gather_array

expect_identical(
(data %>% append_values_string())$string,
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-enter_object.r
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test_that("filter works with one path", {
json <- '{"name": "bob", "attributes": {"age": 32, "gender": "male"}}'

expect_identical(
json %>% as.tbl_json %>% enter_object("attributes"),
json %>% enter_object("attributes"),
tbl_json(
data.frame(document.id = 1L),
list(list(age = 32L, gender = "male"))
Expand All @@ -20,7 +20,7 @@ test_that("filter works with multiple depth paths", {
json <- '{"name": "bob", "attributes": { "demographics": {"age": 32, "gender": "male"}}}'

expect_identical(
json %>% as.tbl_json %>% enter_object("attributes", "demographics"),
json %>% enter_object("attributes", "demographics"),
tbl_json(
data.frame(document.id = 1L),
list(list(age = 32L, gender = "male"))
Expand All @@ -38,7 +38,7 @@ test_that("filter removes records with missing path", {
)

expect_identical(
json %>% as.tbl_json %>% spread_values(name = jstring("name")) %>%
json %>% spread_values(name = jstring("name")) %>%
enter_object("attributes"),
tbl_json(
data.frame(
Expand All @@ -57,7 +57,7 @@ test_that("works if no paths exist", {
json <- '{"name": "bob"}'

expect_identical(
json %>% as.tbl_json %>% spread_values(name = jstring("name")) %>%
json %>% spread_values(name = jstring("name")) %>%
enter_object("attributes"),
tbl_json(
data.frame(
Expand All @@ -80,15 +80,15 @@ test_that("correctly handles character(0), {}, []", {
list())

expect_identical(
character(0) %>% as.tbl_json %>% enter_object("key"),
character(0) %>% enter_object("key"),
empty)

expect_identical(
'{}' %>% as.tbl_json %>% enter_object("key"),
'{}' %>% enter_object("key"),
empty)

expect_identical(
'[]' %>% as.tbl_json %>% enter_object("key"),
'[]' %>% enter_object("key"),
empty)

}
Expand Down
Loading

0 comments on commit 3a89d85

Please sign in to comment.