Skip to content

Commit

Permalink
feat: make spq_set() replace prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Dec 21, 2023
1 parent b7e7d51 commit dafddbc
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 26 deletions.
60 changes: 37 additions & 23 deletions R/spq_perform.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,7 @@ spq_perform = function(.query,
)

if (replace_prefixes) {
endpoint_url = .query[["endpoint_info"]][["endpoint_url"]]
endpoint_is_usual <- (endpoint_url %in% usual_endpoints$url)
if (endpoint_is_usual) {
endpoint_name = usual_endpoints[["name"]][usual_endpoints[["url"]] == endpoint_url]
prefixes = usual_prefixes[["name"]][usual_prefixes[["type"]] == endpoint_name]
} else {
prefixes = NULL
}
prefixes = c(prefixes, .query[["prefixes_used"]])
results = purrr::reduce(
prefixes,
\(results, x) replace_prefix(x, results, .query = .query),
.init = results
)
results = replace_prefixes(results, .query)
}

results
Expand All @@ -105,16 +92,43 @@ replace_prefix = function(prefix, results, .query) {
.query[["prefixes_provided"]]
)

dplyr::mutate(
results,
dplyr::across(
dplyr::where(is.character),
\(x) str_replace(
x,
pattern = prefixes[["url"]][prefixes[["name"]] == prefix],
replacement = sprintf("%s:", prefix))
)
pattern = str_replace(
prefixes[["url"]][prefixes[["name"]] == prefix],
"http:", "https?:"
)

replacement = sprintf("%s:", prefix)

if (is.data.frame(results)) {
dplyr::mutate(
results,
dplyr::across(
dplyr::where(is.character),
\(x) str_replace(x, pattern, replacement)
)
)
} else {
str_replace(results, pattern, replacement)
}


}

replace_prefixes <- function(results, .query) {
endpoint_url = .query[["endpoint_info"]][["endpoint_url"]]
endpoint_is_usual <- (endpoint_url %in% usual_endpoints$url)
if (endpoint_is_usual) {
endpoint_name = usual_endpoints[["name"]][usual_endpoints[["url"]] == endpoint_url]
prefixes = usual_prefixes[["name"]][usual_prefixes[["type"]] == endpoint_name]
} else {
prefixes = NULL
}
prefixes = c(prefixes, .query[["prefixes_used"]])
purrr::reduce(
prefixes,
\(results, x) replace_prefix(x, results, .query = .query),
.init = results
)
}

control_explanation <- function() {
Expand Down
10 changes: 7 additions & 3 deletions R/spq_set.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ spq_set = function(.query, ...) {
args = rlang::enquos(...)

subjects = names(args)
values = purrr::map_chr(args, treat_value)

values = purrr::map_chr(args, treat_value, .query = .query)

.query <- purrr::reduce2(
subjects,
Expand All @@ -41,8 +42,11 @@ spq_set = function(.query, ...) {
return(.query)
}

treat_value = function(value) {
sprintf("{%s}", paste0(rlang::eval_tidy(value), collapse = " "))
treat_value = function(value, .query) {
values <- rlang::eval_tidy(value) %>%
replace_prefixes(.query = .query) %>%
paste(collapse = " ")
sprintf("{%s}", values)
}

add_one_value_var <- function(query, var, values) {
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/spq_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
}

# spq_set() works with URLs

Code
spq_init() %>% spq_set(species = c("https://www.wikidata.org/entity/Q144",
"https://www.wikidata.org/entity/Q146",
"https://www.wikidata.org/entity/Q780"), mayorcode = "wd:Q30185")
Output
SELECT *
WHERE {
VALUES ?species {wd:Q144 wd:Q146 wd:Q780}
VALUES ?mayorcode {wd:Q30185}
}

# spq_set() in two examples

Code
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-spq_set.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ test_that("spq_set() works", {
)
})

test_that("spq_set() works with URLs", {
expect_snapshot(
spq_init() %>%
# dog, cat or chicken
spq_set(species = c(
'https://www.wikidata.org/entity/Q144',
'https://www.wikidata.org/entity/Q146',
'https://www.wikidata.org/entity/Q780'
), mayorcode = "wd:Q30185")
)
})


test_that("spq_set() in two examples", {
expect_snapshot(
spq_init() %>%
Expand Down

0 comments on commit dafddbc

Please sign in to comment.