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

refactor: add argument #208

Merged
merged 2 commits into from
Dec 21, 2023
Merged
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
9 changes: 6 additions & 3 deletions R/sparql-protection.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' expression.
#' @export
spq <- function(...) {
x <- c_character(...)
x <- c_character(..., call = rlang::caller_env())
structure(x, class = c("spq", "character"))
}

Expand All @@ -30,14 +30,17 @@ format.spq <- function(x, ...) {
#' @export
is.spq <- function(x) inherits(x, "spq")

c_character <- function(...) {
c_character <- function(..., call) {
x <- c(...)
if (length(x) == 0) {
return(character())
}

if (!is.character(x)) {
rlang::abort("Character input expected")
cli::cli_abort(
"Character input expected",
call = call
)
}

x
Expand Down
10 changes: 7 additions & 3 deletions R/spq_assemble.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ spq_assemble = function(.query, strict = TRUE) {
# prefixes -----
prefixes_known = .query[["prefixes_provided"]] %>%
dplyr::bind_rows(usual_prefixes)
check_prefixes(.query[["prefixes_used"]], prefixes_known = prefixes_known)
check_prefixes(
.query[["prefixes_used"]],
prefixes_known = prefixes_known,
call = rlang::caller_env()
)

part_prefixes <- if (nrow(.query[["prefixes_provided"]]) > 0) {
glue::glue(
Expand Down Expand Up @@ -249,6 +253,6 @@ add_label = function(vector, label, label_name, old_select) {

}

check_prefixes <- function(prefixes, prefixes_known) {
purrr::walk(prefixes, check_prefix, prefixes_known = prefixes_known)
check_prefixes <- function(prefixes, prefixes_known, call) {
purrr::walk(prefixes, check_prefix, prefixes_known = prefixes_known, call = call)
}
11 changes: 10 additions & 1 deletion R/spq_perform.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,17 @@ 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(
.query[["prefixes_used"]],
prefixes,
\(results, x) replace_prefix(x, results, .query = .query),
.init = results
)
Expand Down
8 changes: 4 additions & 4 deletions R/spq_select.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ spq_select = function(.query = NULL, ..., .spq_duplicate = NULL) {

if (length(plus_variables) > 0) {

check_variables_present(.query, plus_variables)
check_variables_present(.query, plus_variables, call = rlang::caller_env())

if (is.data.frame(.query[["structure"]])) {
.query[["structure"]][["selected"]] = FALSE
Expand All @@ -65,7 +65,7 @@ spq_select = function(.query = NULL, ..., .spq_duplicate = NULL) {
str_remove("\\-")

if (length(minus_variables) > 0) {
check_variables_present(.query, minus_variables)
check_variables_present(.query, minus_variables, call = rlang::caller_env())

.query = purrr::reduce(
minus_variables,
Expand All @@ -77,7 +77,7 @@ spq_select = function(.query = NULL, ..., .spq_duplicate = NULL) {
return(.query)
}

check_variables_present <- function(query, variables) {
check_variables_present <- function(query, variables, call) {

if (nzchar(Sys.getenv("GLITTER.TESTING.SELECT"))) {
return()
Expand All @@ -89,6 +89,6 @@ check_variables_present <- function(query, variables) {
cli::cli_abort(c(
"Can't use {.fun spq_select} on absent variables: {toString(absent_variables)}.",
i = "Did you forget a call to {.fun spq_add}, {.fun spq_mutate} or {.fun spq_label}?"
))
), call = call)
}
}
5 changes: 3 additions & 2 deletions R/treat-argument.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ spq_translate_dsl <- function(code) {
# Abort if not sparqlish
not_sparqlish = xml2::xml_find_all(code_data, ".//SYMBOL_FUNCTION_CALL[@sparqlish='false']")
if (length(not_sparqlish) > 0) {
rlang::abort(
cli::cli_abort(
c(
x = sprintf(
"Can't find SPARQL equivalent for %s().",
Expand All @@ -67,7 +67,8 @@ spq_translate_dsl <- function(code) {
)
),
i = "If you think there should be one, open an issue in https://github.com/lvaudor/glitter."
)
),
call = NULL
)
}

Expand Down
2 changes: 1 addition & 1 deletion R/utils-str.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ is_value = function(string){
#' @examples
#' check_prefix(prefixes_used=c("wd","wdt"), prefixes_known = usual_prefixes) # TRUE
#' check_prefix("blop:blabla", prefixes_known=usual_prefixes) #returns error message
check_prefix = function(prefixes_used, prefixes_known) {
check_prefix = function(prefixes_used, prefixes_known, call = NULL) {
unknown_prefixes <- prefixes_used[!(prefixes_used %in% prefixes_known$name)]
if (length(unknown_prefixes) == 0) {
return(TRUE)
Expand Down
14 changes: 7 additions & 7 deletions data-raw/usual_prefixes.csv
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
type,name,url
Wikidata,wd,http://www.wikidata.org/entity/
Wikidata,wdt,http://www.wikidata.org/prop/direct/
Wikidata,ps,http://www.wikidata.org/prop/statement/
Wikidata,psv,http://www.wikidata.org/prop/statement/value/
Wikidata,pq,http://www.wikidata.org/prop/qualifier/
Wikidata,p,http://www.wikidata.org/prop/
Wikidata,wikibase,http://wikiba.se/ontology#
wikidata,wd,http://www.wikidata.org/entity/
wikidata,wdt,http://www.wikidata.org/prop/direct/
wikidata,ps,http://www.wikidata.org/prop/statement/
wikidata,psv,http://www.wikidata.org/prop/statement/value/
wikidata,pq,http://www.wikidata.org/prop/qualifier/
wikidata,p,http://www.wikidata.org/prop/
wikidata,wikibase,http://wikiba.se/ontology#
dbpedia,dbo,http://dbpedia.org/ontology/
generic,foaf,http://xmlns.com/foaf/0.1/
generic,rdfs,http://www.w3.org/2000/01/rdf-schema#
Expand Down
Binary file modified data/usual_prefixes.rda
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/testthat/_snaps/build_parts.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
spq_mutate(coords = wdt::P625(city), .within_distance = list(center = c(long = 4.84,
lat = 45.76), radius = 5))
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?city (COALESCE(?city_labell,'') AS ?city_label) ?coords
WHERE {
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/_snaps/send_sparql.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,32 @@
The `user_agent` argument of `spq_perform()` is deprecated as of glitter 0.3.0.
i Please use the `user_agent` argument of `spq_request_control()` instead.
i Parameters controlling how the request is made have to be passed to `spq_init()`'s `request_control` argument.
i The deprecated feature was likely used in the glitter package.
Please report the issue at <https://github.com/lvaudor/glitter/issues>.
Warning:
The `max_tries` argument of `spq_perform()` is deprecated as of glitter 0.3.0.
i Please use the `max_tries` argument of `spq_request_control()` instead.
i Parameters controlling how the request is made have to be passed to `spq_init()`'s `request_control` argument.
i The deprecated feature was likely used in the glitter package.
Please report the issue at <https://github.com/lvaudor/glitter/issues>.
Warning:
The `max_seconds` argument of `spq_perform()` is deprecated as of glitter 0.3.0.
i Please use the `max_seconds` argument of `spq_request_control()` instead.
i Parameters controlling how the request is made have to be passed to `spq_init()`'s `request_control` argument.
i The deprecated feature was likely used in the glitter package.
Please report the issue at <https://github.com/lvaudor/glitter/issues>.
Warning:
The `timeout` argument of `spq_perform()` is deprecated as of glitter 0.3.0.
i Please use the `timeout` argument of `spq_request_control()` instead.
i Parameters controlling how the request is made have to be passed to `spq_init()`'s `request_control` argument.
i The deprecated feature was likely used in the glitter package.
Please report the issue at <https://github.com/lvaudor/glitter/issues>.
Warning:
The `request_type` argument of `spq_perform()` is deprecated as of glitter 0.3.0.
i Please use the `request_type` argument of `spq_request_control()` instead.
i Parameters controlling how the request is made have to be passed to `spq_init()`'s `request_control` argument.
i The deprecated feature was likely used in the glitter package.
Please report the issue at <https://github.com/lvaudor/glitter/issues>.
Code
req$method
Output
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/sparql-protection.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
Code
spq(1)
Condition
Error in `c_character()`:
Error:
! Character input expected

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/spq_add.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Code
spq_init() %>% spq_add(spq("?software p:P348/pq:P577 ?date"))
Output

PREFIX p: <http://www.wikidata.org/prop/>
SELECT ?date ?software
WHERE {

Expand All @@ -17,7 +17,7 @@
Code
spq_init() %>% spq_add(spq("?software pq:P577 ?date."))
Output

PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
SELECT ?date ?software
WHERE {

Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/_snaps/spq_assemble.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
spq_summarise(subject_label_concat = str_c(subject_label, sep = "; ")) %>%
spq_head(10)
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?film (GROUP_CONCAT(?subject_label;SEPARATOR="; ") AS ?subject_label_concat)
WHERE {
Expand All @@ -30,6 +32,8 @@
spq_init() %>% spq_add("?item wdt:P31 wd:Q13442814") %>% spq_label(item) %>%
spq_filter(str_detect(str_to_lower(item_label), "wikidata")) %>% spq_head(n = 5)
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?item (COALESCE(?item_labell,'') AS ?item_label)
WHERE {
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/_snaps/spq_filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"CONTAINS(LCASE(?itemTitle),'wikidata')")) %>% spq_filter(spq(
"LANG(?itemTitle)='en'"))
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?item ?itemTitle
WHERE {
Expand All @@ -24,6 +26,8 @@
"?item rdfs:label ?itemTitle") %>% spq_filter(str_detect(str_to_lower(
itemTitle), "wikidata")) %>% spq_filter(lang(itemTitle) == "en")
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?item ?itemTitle
WHERE {
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/_snaps/spq_init.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
narrative_location = wdt::P840(film)) %>% spq_label(film, narrative_location) %>%
spq_count(narrative_location_label, sort = TRUE, name = "n_films")
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?narrative_location_label (COUNT(*) AS ?n_films)
WHERE {
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/_snaps/spq_label.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
spq_add("?node ps:P39 wd:Q30185") %>% spq_add("?node pq:P642 ?place") %>%
spq_label(mayor, place, .languages = "en$")
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?mayor (COALESCE(?mayor_labell,'') AS ?mayor_label) ?node ?place (COALESCE(?place_labell,'') AS ?place_label) ?species
WHERE {
Expand Down Expand Up @@ -37,6 +42,11 @@
spq_add("?node ps:P39 wd:Q30185") %>% spq_add("?node pq:P642 ?place") %>%
spq_label(mayor, place, .languages = c("fr", "en"))
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?mayor (COALESCE(?mayor_labell,'') AS ?mayor_label) (lang(?mayor_labell) AS ?mayor_label_lang) ?node ?place (COALESCE(?place_labell,'') AS ?place_label) (lang(?place_labell) AS ?place_label_lang) ?species
WHERE {
Expand Down Expand Up @@ -92,6 +102,11 @@
spq_add("?node ps:P39 wd:Q30185") %>% spq_add("?node pq:P642 ?place") %>%
spq_label(mayor, place, .languages = "en$", .overwrite = TRUE)
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?node ?species (COALESCE(?mayor_labell,'') AS ?mayor) (COALESCE(?place_labell,'') AS ?place)
WHERE {
Expand Down Expand Up @@ -146,6 +161,8 @@
spq_add("?film wdt:P577 ?date") %>% spq_label(film, loc, subject) %>%
spq_head(10)
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?coords ?date ?film (COALESCE(?film_labell,'') AS ?film_label) ?image ?loc (COALESCE(?loc_labell,'') AS ?loc_label) ?subject (COALESCE(?subject_labell,'') AS ?subject_label)
WHERE {
Expand Down Expand Up @@ -188,6 +205,8 @@
spq_add("?film wdt:P577 ?date") %>% spq_label(film, loc, subject, .required = TRUE) %>%
spq_head(10)
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?coords ?date ?film (COALESCE(?film_labell,'') AS ?film_label) ?image ?loc (COALESCE(?loc_labell,'') AS ?loc_label) ?subject (COALESCE(?subject_labell,'') AS ?subject_label)
WHERE {
Expand Down
7 changes: 6 additions & 1 deletion tests/testthat/_snaps/spq_mutate.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
spq_init() %>% spq_mutate(statement = wdt::P1843(wd::Q331676)) %>% spq_mutate(
lang = lang(statement))
Output

PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT ?statement (lang(?statement) AS ?lang)
WHERE {

Expand All @@ -20,6 +21,8 @@
spq_add("?film wdt:P577 ?date") %>% spq_mutate(date = year(date)) %>%
spq_head(10)
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?film (COALESCE(?film_labell,'') AS ?film_label) (YEAR(?date0) AS ?date)
WHERE {
Expand All @@ -43,6 +46,8 @@
spq_add("?film wdt:P577 ?date") %>% spq_mutate(date = year(date)) %>%
spq_mutate(date = date - 2000)
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?film (COALESCE(?film_labell,'') AS ?film_label) (?date0-2000 AS ?date)
WHERE {
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/_snaps/spq_offset.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"?item wikibase:sitelinks ?linkcount") %>% spq_arrange(desc(linkcount)) %>%
spq_head(42) %>% spq_offset(11)
Output
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?item (COALESCE(?item_labell,'') AS ?item_label) ?linkcount
WHERE {
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/_snaps/spq_select.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@

i In index: 1.
i With name: birthyear.
Caused by error in `spq_translate_dsl()`:
! x Can't find SPARQL equivalent for collapse().
Caused by error:
x Can't find SPARQL equivalent for collapse().
i If you think there should be one, open an issue in https://github.com/lvaudor/glitter.

# spq_select can use DISTINCT and REDUCED
Expand Down Expand Up @@ -159,7 +159,7 @@
"?station wdt:P31 wd:Q928830") %>% spq_add("?station wdt:P625 ?coords") %>%
spq_select(station_label, blop)
Condition
Error in `check_variables_present()`:
Error:
! Can't use `spq_select()` on absent variables: ?station_label, ?blop.
i Did you forget a call to `spq_add()`, `spq_mutate()` or `spq_label()`?

Expand All @@ -170,7 +170,7 @@
"?station wdt:P31 wd:Q928830") %>% spq_add("?station wdt:P625 ?coords") %>%
spq_label(station) %>% spq_select(station_label, blop)
Condition
Error in `check_variables_present()`:
Error:
! Can't use `spq_select()` on absent variables: ?blop.
i Did you forget a call to `spq_add()`, `spq_mutate()` or `spq_label()`?

Loading
Loading