Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
simplify methods
  • Loading branch information
gogonzo committed Nov 14, 2023
1 parent 5d4d1a6 commit ac63f64
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 39 deletions.
38 changes: 3 additions & 35 deletions R/join_keys.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ join_keys.TealData <- function(...) {
#' @rdname join_keys
#' @export
join_keys.default <- function(...) {
# Constructor using join_keys<-.xxx setter
result <- new_join_keys()
join_keys(result) <- rlang::list2(...)
result
c(new_join_keys(), ...)
}

#' @rdname join_keys
Expand Down Expand Up @@ -129,24 +126,8 @@ join_keys.default <- function(...) {
if (checkmate::test_class(value, classes = c("join_keys", "list"))) {
return(value)
}

if (inherits(value, "join_key_set")) value <- list(value)

join_keys_obj <- new_join_keys()

# check if any join_key_sets share the same datasets but different values
for (idx_1 in seq_along(value)) {
for (idx_2 in seq_along(value)[-seq(1, idx_1)]) {
assert_compatible_keys(value[[idx_1]], value[[idx_2]])
}

dataset_1 <- get_dataset_1(value[[idx_1]])
dataset_2 <- get_dataset_2(value[[idx_1]])
keys <- get_keys(value[[idx_1]])

join_keys_obj[[dataset_1]][[dataset_2]] <- keys
}
join_keys_obj
c(join_keys_obj, value)
}

#' @rdname join_keys
Expand Down Expand Up @@ -175,26 +156,13 @@ c.join_keys <- function(...) {
x <- rlang::list2(...)
checkmate::assert_class(x[[1]], c("join_keys", "list"))
checkmate::assert_list(x[-1], types = c("join_keys", "join_key_set"))
# todo: assert if ... contains incompatible keys

join_keys_obj <- x[[1]]
x <- x[-1]
if (
checkmate::test_class(x, "join_key_set") ||
checkmate::test_class(x, c("join_keys", "list"))
) {
x <- list(x)
}

if (checkmate::test_list(x, types = "join_key_set")) {
jk_temp <- new_join_keys()
join_keys(jk_temp) <- x
x <- list(jk_temp)
}

for (el in x) {
join_keys_obj <- utils::modifyList(join_keys_obj, el)
}

join_keys_obj
}

Expand Down
19 changes: 15 additions & 4 deletions tests/testthat/test-join_keys.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ testthat::test_that("[.join_keys returns join_keys object with keys for given da
my_keys <- join_keys(
join_key("d1", "d1", "a"),
join_key("d2", "d2", "b"),
join_key("d3", "d3", "c"),
join_key("d3", "d3", "c")
)
testthat::expect_identical(
my_keys[c("d1", "d2")],
Expand All @@ -116,7 +116,7 @@ testthat::test_that("[.join_keys returns join_keys object with keys for given in
my_keys <- join_keys(
join_key("d1", "d1", "a"),
join_key("d2", "d2", "b"),
join_key("d3", "d3", "c"),
join_key("d3", "d3", "c")
)
testthat::expect_identical(
my_keys[c(1, 2)],
Expand Down Expand Up @@ -400,7 +400,7 @@ testthat::test_that("names<-.join_keys will replace names at first and second le
join_key("a", "a", "a"),
join_key("a", "b", "ab"),
join_key("a", "c", "ac"),
join_key("d", "b", "db"),
join_key("d", "b", "db")
)

names(jk)[1:2] <- c("x", "y")
Expand All @@ -411,7 +411,7 @@ testthat::test_that("names<-.join_keys will replace names at first and second le
join_key("x", "x", "a"),
join_key("x", "y", "ab"),
join_key("x", "c", "ac"),
join_key("d", "y", "db"),
join_key("d", "y", "db")
)
)
})
Expand Down Expand Up @@ -478,6 +478,17 @@ testthat::test_that("c.join_keys doesn't throw when second object is empty join_
testthat::expect_no_error(c(x, y))
})

testthat::test_that("c.join_keys doesn't allow to specify the keys which are incompatible", {
obj <- join_keys()
testthat::expect_error(
c(
obj,
join_keys(join_key("a", "b", "aa")),
join_keys(join_key("b", "a", "bb"))
)
)
})

# -----------------------------------------------------------------------------
#
# print.join_keys
Expand Down

0 comments on commit ac63f64

Please sign in to comment.