Skip to content

Commit

Permalink
Rename .as_class to as_rapid_class.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonthegeek committed Oct 23, 2023
1 parent c343ec9 commit 7932638
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 27 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export(as_oauth2_security_scheme)
export(as_oauth2_token_flow)
export(as_origin)
export(as_rapid)
export(as_rapid_class)
export(as_scopes)
export(as_security_requirements)
export(as_security_scheme)
Expand Down
54 changes: 38 additions & 16 deletions R/as.R
Original file line number Diff line number Diff line change
@@ -1,40 +1,62 @@
.as_class <- function(x,
target_S7_class,
...,
arg = rlang::caller_arg(x),
call = rlang::caller_env()) {
#' Convert to a rapid-style class
#'
#' Convert a named list into a rapid-style class.
#'
#' @inheritParams rlang::args_dots_empty
#' @inheritParams rlang::args_error_context
#' @param x The object to coerce. Must be empty or have names corresponding to
#' the parameter of the `target_class`, or names that can be coerced to those
#' names via [snakecase::to_snake_case()]. Extra names are ignored.
#' @param target_class The S7 class to which the object should be converted.
#' @param alternate_names Character vector (optional). An optional named
#' character vector, where the names are the names as they might appear in
#' `x`, and the values are the corresponding properties.
#'
#' @return An object with the specified `target_class`.
#' @export
as_rapid_class <- function(x,
target_class,
alternate_names = NULL,
arg = rlang::caller_arg(x),
call = rlang::caller_env()) {
force(arg)
x <- .validate_for_as_class(x, target_S7_class, ..., x_arg = arg, call = call)
x <- .validate_for_as_class(
x,
target_class,
alternate_names = alternate_names,
x_arg = arg,
call = call
)
rlang::inject({
target_S7_class(!!!x)
target_class(!!!x)
})
}

.validate_for_as_class <- function(x,
target_S7_class,
extra_names = NULL,
target_class,
alternate_names = NULL,
x_arg = rlang::caller_arg(x),
call = rlang::caller_env()) {
if (!length(x)) {
return(NULL)
}

valid_names <- snakecase::to_snake_case(
c(S7::prop_names(target_S7_class()), names(extra_names))
c(S7::prop_names(target_class()), names(alternate_names))
)

if (rlang::is_named2(x)) {
force(x_arg)
x <- rlang::set_names(x, snakecase::to_snake_case)
ignored_names <- names(x)[!names(x) %in% valid_names]
x <- as.list(x)[names(x) %in% valid_names]
if (length(extra_names)) {
extra_names <- rlang::set_names(
snakecase::to_snake_case(extra_names),
snakecase::to_snake_case(names(extra_names))
if (length(alternate_names)) {
alternate_names <- rlang::set_names(
snakecase::to_snake_case(alternate_names),
snakecase::to_snake_case(names(alternate_names))
)
to_rename <- names(x) %in% names(extra_names)
names(x)[to_rename] <- extra_names[names(x)[to_rename]]
to_rename <- names(x) %in% names(alternate_names)
names(x)[to_rename] <- alternate_names[names(x)[to_rename]]
}
x <- x %|0|% NULL
if (length(ignored_names)) {
Expand Down
4 changes: 2 additions & 2 deletions R/components-security_scheme-api_key.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ S7::method(
as_api_key_security_scheme,
class_list | class_character
) <- function(x) {
.as_class(
as_rapid_class(
x,
api_key_security_scheme,
extra_names = c("in" = "location", "name" = "parameter_name")
alternate_names = c("in" = "location", "name" = "parameter_name")
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ S7::method(
as_oauth2_authorization_code_flow,
class_list | class_character
) <- function(x) {
.as_class(x, oauth2_authorization_code_flow)
as_rapid_class(x, oauth2_authorization_code_flow)
}

S7::method(
Expand Down
2 changes: 1 addition & 1 deletion R/components-security_scheme-oauth2-implicit_flow.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ S7::method(
as_oauth2_implicit_flow,
class_list | class_character
) <- function(x) {
.as_class(x, oauth2_implicit_flow)
as_rapid_class(x, oauth2_implicit_flow)
}

S7::method(
Expand Down
2 changes: 1 addition & 1 deletion R/components-security_scheme-oauth2-token_flow.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ S7::method(as_oauth2_token_flow, oauth2_token_flow) <- function(x) {
}

S7::method(as_oauth2_token_flow, class_list | class_character) <- function(x) {
.as_class(x, oauth2_token_flow)
as_rapid_class(x, oauth2_token_flow)
}

S7::method(
Expand Down
2 changes: 1 addition & 1 deletion R/components.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ S7::method(as_component_collection, component_collection) <- function(x) {
}

S7::method(as_component_collection, class_list) <- function(x) {
.as_class(x, component_collection)
as_rapid_class(x, component_collection)
}

S7::method(as_component_collection, class_missing | NULL) <- function(x) {
Expand Down
2 changes: 1 addition & 1 deletion R/info-contact.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ S7::method(as_contact, contact) <- function(x) {
}

S7::method(as_contact, class_list | class_character) <- function(x) {
.as_class(x, contact)
as_rapid_class(x, contact)
}

S7::method(
Expand Down
2 changes: 1 addition & 1 deletion R/info-license.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ S7::method(as_license, license) <- function(x) {
}

S7::method(as_license, class_list | class_character) <- function(x) {
.as_class(x, license)
as_rapid_class(x, license)
}

S7::method(
Expand Down
2 changes: 1 addition & 1 deletion R/info-origin.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ S7::method(as_origin, class_list | class_character) <- function(x) {
x <- x[[1]]
}

.as_class(x, class_origin)
as_rapid_class(x, class_origin)
}

S7::method(as_origin, class_missing | NULL) <- function(x) {
Expand Down
2 changes: 1 addition & 1 deletion R/info.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ S7::method(as_info, info) <- function(x) {
}

S7::method(as_info, class_list | class_character) <- function(x) {
.as_class(x, info, extra_names = c("x-origin" = "origin"))
as_rapid_class(x, info, alternate_names = c("x-origin" = "origin"))
}

S7::method(
Expand Down
2 changes: 1 addition & 1 deletion R/zz-rapid.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ S7::method(as_rapid, rapid) <- function(x) {
S7::method(as_rapid, class_list) <- function(x) {
rlang::try_fetch(
{
x <- .as_class(x, rapid)
x <- as_rapid_class(x, rapid)
expand_servers(x)
},
rapid_error_missing_names = function(cnd) {
Expand Down
40 changes: 40 additions & 0 deletions man/as_rapid_class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7932638

Please sign in to comment.