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

feat: make spq_set() replace prefixes #214

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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?:"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lvaudor c'est pour que ça marche avec https et http, is that ok?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oui parfait, c'est bien que ça fonctionne avec les deux

)

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

if (is.data.frame(results)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not my proudest moment

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