-
Notifications
You must be signed in to change notification settings - Fork 57
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 .multi
in req_body_form()
#371
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
multi_dots <- function(..., | ||
.multi = c("error", "comma", "pipe", "explode"), | ||
error_arg = "...", | ||
error_call = caller_env()) { | ||
if (is.function(.multi)) { | ||
check_function2(.multi, call = error_call, arg = ".multi") | ||
} else { | ||
.multi <- arg_match(.multi, error_arg = ".multi", error_call = error_call) | ||
} | ||
|
||
dots <- list2(...) | ||
if (length(dots) == 0) { | ||
return(list()) | ||
} | ||
|
||
if (!is_named(dots)) { | ||
cli::cli_abort( | ||
"All components of {.arg {error_arg}} must be named.", | ||
call = error_call | ||
) | ||
} | ||
|
||
type_ok <- map_lgl(dots, function(x) is_atomic(x) || is.null(x)) | ||
if (any(!type_ok)) { | ||
cli::cli_abort( | ||
"All elements of {.arg {error_arg}} must be either an atomic vector or NULL.", | ||
call = error_call | ||
) | ||
} | ||
|
||
n <- lengths(dots) | ||
if (any(n > 1)) { | ||
if (is.function(.multi)) { | ||
dots[n > 1] <- lapply(dots[n > 1], format_query_param) | ||
dots[n > 1] <- lapply(dots[n > 1], .multi) | ||
dots[n > 1] <- lapply(dots[n > 1], I) | ||
} else if (.multi == "comma") { | ||
dots[n > 1] <- lapply(dots[n > 1], format_query_param) | ||
dots[n > 1] <- lapply(dots[n > 1], paste0, collapse = ",") | ||
dots[n > 1] <- lapply(dots[n > 1], I) | ||
} else if (.multi == "pipe") { | ||
dots[n > 1] <- lapply(dots[n > 1], format_query_param) | ||
dots[n > 1] <- lapply(dots[n > 1], paste0, collapse = "|") | ||
dots[n > 1] <- lapply(dots[n > 1], I) | ||
} else if (.multi == "explode") { | ||
dots <- explode(dots) | ||
dots[n > 1] <- lapply(dots[n > 1], format_query_param) | ||
dots[n > 1] <- lapply(dots[n > 1], I) | ||
} else if (.multi == "error") { | ||
cli::cli_abort( | ||
c( | ||
"All vector elements of {.arg {error_arg}} must be length 1.", | ||
i = "Use {.arg .multi} to choose a strategy for handling vectors." | ||
), | ||
call = error_call | ||
) | ||
} | ||
} | ||
|
||
# Format other params | ||
dots[n == 1] <- lapply(dots[n == 1], format_query_param, error_call = error_call) | ||
dots[n == 1] <- lapply(dots[n == 1], I) | ||
|
||
dots | ||
} | ||
|
||
explode <- function(x) { | ||
expanded <- map(x, function(x) { | ||
if (is.null(x)) { | ||
list(NULL) | ||
} else { | ||
map(seq_along(x), function(i) x[i]) | ||
} | ||
}) | ||
stats::setNames( | ||
unlist(expanded, recursive = FALSE, use.names = FALSE), | ||
rep(names(x), lengths(expanded)) | ||
) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# checks its inputs | ||
|
||
Code | ||
multi_dots(1) | ||
Condition | ||
Error: | ||
! All components of `...` must be named. | ||
Code | ||
multi_dots(x = I(1)) | ||
Condition | ||
Error: | ||
! Escaped query value must be a single string, not the number 1. | ||
Code | ||
multi_dots(x = 1:2) | ||
Condition | ||
Error: | ||
! All vector elements of `...` must be length 1. | ||
i Use `.multi` to choose a strategy for handling vectors. | ||
Code | ||
multi_dots(x = mean) | ||
Condition | ||
Error: | ||
! All elements of `...` must be either an atomic vector or NULL. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
test_that("can handle multi query params", { | ||
expect_equal( | ||
multi_dots(a = 1:2, .multi = "explode"), | ||
list(a = I("1"), a = I("2")) | ||
) | ||
expect_equal( | ||
multi_dots(a = 1:2, .multi = "comma"), | ||
list(a = I("1,2")) | ||
) | ||
expect_equal( | ||
multi_dots(a = 1:2, .multi = "pipe"), | ||
list(a = I("1|2")) | ||
) | ||
expect_equal( | ||
multi_dots(a = 1:2, .multi = function(x) "X"), | ||
list(a = I("X")) | ||
) | ||
}) | ||
|
||
test_that("can handle empty dots", { | ||
expect_equal(multi_dots(), list()) | ||
}) | ||
|
||
test_that("preserves NULL values", { | ||
expect_equal(multi_dots(x = NULL), list(x = NULL)) | ||
}) | ||
|
||
test_that("preserves duplicates values", { | ||
expect_equal(multi_dots(x = 1, x = 2), list(x = I("1"), x = I("2"))) | ||
}) | ||
|
||
test_that("leaves already escaped values alone", { | ||
x <- I("1 + 2") | ||
expect_equal(multi_dots(x = x), list(x = x)) | ||
}) | ||
|
||
test_that("checks its inputs", { | ||
expect_snapshot(error = TRUE, { | ||
multi_dots(1) | ||
multi_dots(x = I(1)) | ||
multi_dots(x = 1:2) | ||
multi_dots(x = mean) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I stumbled over this issue
Created on 2023-11-10 with reprex v2.0.2
You can see that the body is
a=ab,c%20
instead ofa=ab,c
because white spaces are appended.In
format_query_param()
we need to usejustify = "none"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, I did that in #355. Maybe that didn't get in this branch? Anyway I'll double check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No looks like a different problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ARGH you need
justify = "none"
for strings andtrim = TRUE
for numbers.