Skip to content

Commit

Permalink
#11 rename key.seq to seq
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy committed Aug 26, 2016
1 parent f654e32 commit ad41449
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
22 changes: 11 additions & 11 deletions R/json_structure.r
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
#' as <parent>.<index> where <parent> is the ID for the parent and <index>
#' is this index.
#'
#' \code{key.seq} the sequence of keys that led to this child (parents that
#' are arrays are excluded) as a list, where character strings denote
#' objects and integers denote array positions
#' \code{seq} the sequence of keys / indices that led to this child
#' (parents that are arrays are excluded) as a list, where character strings
#' denote objects and integers denote array positions
#'
#' \code{key} if this is the value of an object, what was the key that it
#' is listed under (from \code{gather_keys}).
Expand Down Expand Up @@ -80,7 +80,7 @@ json_structure_init <- function(x) {
level = 0L,
index = 1L,
child.id = "1",
key.seq = replicate(n(), list()),
seq = replicate(n(), list()),
key = NA_character_
) %>%
json_types %>%
Expand All @@ -107,7 +107,7 @@ json_structure_empty <- function() {
level = integer(0),
index = integer(0),
child.id = character(0),
key.seq = list(),
seq = list(),
key = character(0),
type = factor(character(0), levels = allowed_json_types),
length = integer(0)
Expand Down Expand Up @@ -148,7 +148,7 @@ json_structure_objects <- function(s) {
transmute(
document.id,
parent.id = child.id,
key.seq,
seq,
level = level + 1L
) %>%
gather_keys %>%
Expand All @@ -162,10 +162,10 @@ json_structure_objects <- function(s) {
ungroup %>%
mutate(
child.id = paste(parent.id, index, sep = "."),
key.seq = map2(key.seq, key, c)
seq = map2(seq, key, c)
) %>%
select(
document.id, parent.id, level, index, child.id, key.seq, key, type, length
document.id, parent.id, level, index, child.id, seq, key, type, length
)

# Reconstruct tbl_json object
Expand All @@ -180,19 +180,19 @@ json_structure_arrays <- function(s) {
transmute(
document.id,
parent.id = child.id,
key.seq,
seq,
level = level + 1L
) %>%
gather_array("index") %>%
json_types %>%
json_lengths %>%
mutate(
child.id = paste(parent.id, index, sep = "."),
key.seq = map2(key.seq, index, c)
seq = map2(seq, index, c)
) %>%
transmute(
document.id, parent.id, level, index, child.id,
key.seq, key = NA_character_, type, length
seq, key = NA_character_, type, length
)

}
Expand Down
6 changes: 3 additions & 3 deletions man/json_structure.Rd

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

16 changes: 8 additions & 8 deletions tests/testthat/test-json_structure.r
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that("simple string works", {
level = 0L,
index = 1L,
child.id = "1",
key.seq = list(list()),
seq = list(list()),
key = NA_character_,
type = "string" %>% factor(levels = allowed_json_types),
length = 1L
Expand All @@ -33,7 +33,7 @@ test_that("simple object works", {
level = c(0L, 1L),
index = c(1L, 1L),
child.id = c("1", "1.1"),
key.seq = list(list(), list("key")),
seq = list(list(), list("key")),
key = c(NA_character_, "key"),
type = c("object", "string") %>% factor(levels = allowed_json_types),
length = c(1L, 1L)
Expand All @@ -55,7 +55,7 @@ test_that("simple array works", {
level = c(0L, 1L, 1L),
index = c(1L, 1L, 2L),
child.id = c("1", "1.1", "1.2"),
key.seq = list(list(), list(1L), list(2L)),
seq = list(list(), list(1L), list(2L)),
key = rep(NA_character_, 3),
type = c("array", "number", "number") %>% factor(levels = allowed_json_types),
length = c(2L, 1L, 1L)
Expand All @@ -77,7 +77,7 @@ test_that("nested object works", {
level = c(0L, 1L, 2L),
index = c(1L, 1L, 1L),
child.id = c("1", "1.1", "1.1.1"),
key.seq = list(list(), list("k1"), list("k1", "k2")),
seq = list(list(), list("k1"), list("k1", "k2")),
key = c(NA_character_, "k1", "k2"),
type = c("object", "object", "string") %>% factor(levels = allowed_json_types),
length = c(1L, 1L, 1L)
Expand All @@ -101,7 +101,7 @@ test_that("works with empty values appropriately", {
level = 0L,
index = 1L,
child.id = "1",
key.seq = list(list()),
seq = list(list()),
key = NA_character_,
type = "null" %>% factor(levels = allowed_json_types),
length = 0L
Expand All @@ -123,7 +123,7 @@ test_that("works with tbl_json already", {
level = rep(0L, 2),
index = rep(1L, 2),
child.id = rep("1", 2),
key.seq = list(list(), list()),
seq = list(list(), list()),
key = rep(NA_character_, 2),
type = rep("string", 2) %>% factor(levels = allowed_json_types),
length = rep(1L, 2)
Expand All @@ -134,12 +134,12 @@ test_that("works with tbl_json already", {

})

test_that("key.seq works", {
test_that("seq works for a deeply nested sequence", {

expect_identical(
'{"a": {"2": [1, {"3": "value"}] } }' %>%
json_structure %>%
`[[`("key.seq") %>%
`[[`("seq") %>%
`[[`(6),
list("a", "2", 2L, "3")
)
Expand Down

0 comments on commit ad41449

Please sign in to comment.