Skip to content

Commit

Permalink
Merge pull request #7 from eodaGmbH/feature/rdantic
Browse files Browse the repository at this point in the history
Feature/rdantic
  • Loading branch information
crazycapivara authored Jan 11, 2024
2 parents 890b357 + 1f443ca commit 78fc57c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
21 changes: 12 additions & 9 deletions R/layer.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
#'
#' @export
Layer <- function(type, id, source = NULL, paint = NULL, layout = NULL, ...) {
list(
type = type,
id = id,
source = source,
paint = paint,
layout = layout
) |>
types <- list(type = is.character, id = is.character, source = is.list, paint = is.list, layout = is.list)
c(rdantic(
list(
type = type,
id = id,
source = source,
paint = paint,
layout = layout
), types
), ...) |>
purrr::compact() |>
set_maplibre_class("MapLibreLayer")
}
Expand Down Expand Up @@ -44,7 +47,7 @@ add_layer <- function(.map, layer) {
#' @export
#'
#' @example examples/layers.R
add_popup <- function(.map, layer_id, prop){
add_popup <- function(.map, layer_id, prop) {
.map |> add_call("addPopup", layer_id, prop)
}

Expand All @@ -60,6 +63,6 @@ add_popup <- function(.map, layer_id, prop){
#'
#' @examples
#' @example examples/layers.R
add_tooltip <- function(.map, layer_id, prop){
add_tooltip <- function(.map, layer_id, prop) {
.map |> add_call("addTooltip", layer_id, prop)
}
12 changes: 12 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,15 @@ set_maplibre_class <- function(.obj, class_name) {
class(.obj) <- c(class(.obj), class_name)
return(.obj)
}

rdantic <- function(.obj, types, test = 1L) {
for (k in names(.obj)) {
type_check <- types[[k]]
value <- .obj[[k]]
if (xor(!type_check(value), is.null(value))) {
stop(value, " is not ", deparse(substitute(type_check)) , call.=FALSE)
}
}

return(.obj)
}
12 changes: 12 additions & 0 deletions tests/testthat/test-rdantic.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test_that("rdantic", {
# Prepare
l <- list(a = 1, b = "test", d = NULL)
types <- list(a = is.numeric, b = is.character, d = is.numeric)

# Act
l <- rdantic(l, types)

# Assert
expect_equal(l$a, 1)
expect_equal(l$b, "test")
})

0 comments on commit 78fc57c

Please sign in to comment.