Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PROJECT_PATH and ELLIPSOID arguments when using JSON input #111

Merged
merged 6 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions R/qgis-arguments.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,27 @@ qgis_sanitize_arguments <- function(algorithm, ..., .algorithm_arguments = qgis_
args_sanitized
}


#' @keywords internal
unclass_recursive <- function(x) {
is_list <- vapply(x, is.list, logical(1))
x[is_list] <- lapply(x[is_list], unclass_recursive)
lapply(x, unclass)
}


# turn sanitized arguments into command-line arguments
qgis_serialize_arguments <- function(arguments, use_json_input = FALSE) {
if (use_json_input) {
unclass_recursive <- function(x) {
is_list <- vapply(x, is.list, logical(1))
x[is_list] <- lapply(x[is_list], unclass_recursive)
lapply(x, unclass)
}

jsonlite::toJSON(list(inputs = unclass_recursive(arguments)), auto_unbox = TRUE)
arguments <- unclass_recursive(arguments)
arglist <-
list(
inputs = arguments[!(names(arguments) %in% c("ELLIPSOID", "PROJECT_PATH"))],
ellipsoid = arguments$ELLIPSOID,
project_path = arguments$PROJECT_PATH
)
arglist <- arglist[!vapply(arglist, is.null, logical(1))]
jsonlite::toJSON(arglist, auto_unbox = TRUE)
} else {
args_dict <- vapply(arguments, inherits, logical(1), "qgis_dict_input")
if (any(args_dict)) {
Expand Down
60 changes: 60 additions & 0 deletions tests/testthat/test-qgis-arguments.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,66 @@ test_that("qgis_sanitize_arguments() accepts multiple input arguments", {
)
})

test_that("qgis_serialize_arguments() outputs correct JSON strings", {
arguments <- list(
LAYOUT = "Layout 1",
TEXT_FORMAT = 0,
OUTPUT = "output.pdf",
PROJECT_PATH = "test.qgs",
AGGREGATES = list(
list(
aggregate = "first_value",
delimiter = ",",
input = '"admin"',
length = 36,
name = "admin",
precision = 0,
type = 10
),
list(
aggregate = "concatenate",
delimiter = ",",
input = '"name"',
length = 3000,
name = "name",
precision = 0,
type = 10
)
)
)
json <- qgis_serialize_arguments(arguments = arguments, use_json_input = TRUE)
expect_identical(
jsonlite::fromJSON(json, simplifyVector = FALSE),
list(
inputs = list(
LAYOUT = "Layout 1",
TEXT_FORMAT = 0L,
OUTPUT = "output.pdf",
AGGREGATES = list(
list(
aggregate = "first_value",
delimiter = ",",
input = "\"admin\"",
length = 36L,
name = "admin",
precision = 0L,
type = 10L
),
list(
aggregate = "concatenate",
delimiter = ",",
input = "\"name\"",
length = 3000L,
name = "name",
precision = 0L,
type = 10L
)
)
),
project_path = "test.qgs")
)
florisvdh marked this conversation as resolved.
Show resolved Hide resolved
})

test_that("argument coercers work", {
expect_error(as_qgis_argument(list()), "Don't know how to convert object of type")
expect_identical(as_qgis_argument("chr value"), "chr value")
Expand Down