From 7932638d58d4cb6c6a8718057b855c356db6b432 Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Mon, 23 Oct 2023 11:45:41 -0500 Subject: [PATCH] Rename .as_class to as_rapid_class. --- NAMESPACE | 1 + R/as.R | 54 +++++++++++++------ R/components-security_scheme-api_key.R | 4 +- ...ty_scheme-oauth2-authorization_code_flow.R | 2 +- ...nts-security_scheme-oauth2-implicit_flow.R | 2 +- ...onents-security_scheme-oauth2-token_flow.R | 2 +- R/components.R | 2 +- R/info-contact.R | 2 +- R/info-license.R | 2 +- R/info-origin.R | 2 +- R/info.R | 2 +- R/zz-rapid.R | 2 +- man/as_rapid_class.Rd | 40 ++++++++++++++ 13 files changed, 90 insertions(+), 27 deletions(-) create mode 100644 man/as_rapid_class.Rd diff --git a/NAMESPACE b/NAMESPACE index 3be6ff5..8436755 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/as.R b/R/as.R index 3dde035..523c944 100644 --- a/R/as.R +++ b/R/as.R @@ -1,18 +1,40 @@ -.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)) { @@ -20,7 +42,7 @@ } 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)) { @@ -28,13 +50,13 @@ 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)) { diff --git a/R/components-security_scheme-api_key.R b/R/components-security_scheme-api_key.R index e36dc8a..1671c6c 100644 --- a/R/components-security_scheme-api_key.R +++ b/R/components-security_scheme-api_key.R @@ -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") ) } diff --git a/R/components-security_scheme-oauth2-authorization_code_flow.R b/R/components-security_scheme-oauth2-authorization_code_flow.R index 1c8ac06..6870c59 100644 --- a/R/components-security_scheme-oauth2-authorization_code_flow.R +++ b/R/components-security_scheme-oauth2-authorization_code_flow.R @@ -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( diff --git a/R/components-security_scheme-oauth2-implicit_flow.R b/R/components-security_scheme-oauth2-implicit_flow.R index c4497ff..6e989ad 100644 --- a/R/components-security_scheme-oauth2-implicit_flow.R +++ b/R/components-security_scheme-oauth2-implicit_flow.R @@ -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( diff --git a/R/components-security_scheme-oauth2-token_flow.R b/R/components-security_scheme-oauth2-token_flow.R index 386c40d..9894f76 100644 --- a/R/components-security_scheme-oauth2-token_flow.R +++ b/R/components-security_scheme-oauth2-token_flow.R @@ -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( diff --git a/R/components.R b/R/components.R index bbd2e60..0585e3c 100644 --- a/R/components.R +++ b/R/components.R @@ -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) { diff --git a/R/info-contact.R b/R/info-contact.R index 2740e17..85137ba 100644 --- a/R/info-contact.R +++ b/R/info-contact.R @@ -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( diff --git a/R/info-license.R b/R/info-license.R index 538cd1a..1e9d942 100644 --- a/R/info-license.R +++ b/R/info-license.R @@ -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( diff --git a/R/info-origin.R b/R/info-origin.R index f3cb8c9..da94898 100644 --- a/R/info-origin.R +++ b/R/info-origin.R @@ -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) { diff --git a/R/info.R b/R/info.R index 3a4d827..4c2ed30 100644 --- a/R/info.R +++ b/R/info.R @@ -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( diff --git a/R/zz-rapid.R b/R/zz-rapid.R index c9212cd..f447847 100644 --- a/R/zz-rapid.R +++ b/R/zz-rapid.R @@ -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) { diff --git a/man/as_rapid_class.Rd b/man/as_rapid_class.Rd new file mode 100644 index 0000000..ff8711e --- /dev/null +++ b/man/as_rapid_class.Rd @@ -0,0 +1,40 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/as.R +\name{as_rapid_class} +\alias{as_rapid_class} +\title{Convert to a rapid-style class} +\usage{ +as_rapid_class( + x, + target_class, + alternate_names = NULL, + arg = rlang::caller_arg(x), + call = rlang::caller_env() +) +} +\arguments{ +\item{x}{The object to coerce. Must be empty or have names corresponding to +the parameter of the \code{target_class}, or names that can be coerced to those +names via \code{\link[snakecase:caseconverter]{snakecase::to_snake_case()}}. Extra names are ignored.} + +\item{target_class}{The S7 class to which the object should be converted.} + +\item{alternate_names}{Character vector (optional). An optional named +character vector, where the names are the names as they might appear in +\code{x}, and the values are the corresponding properties.} + +\item{arg}{An argument name as a string. This argument +will be mentioned in error messages as the input that is at the +origin of a problem.} + +\item{call}{The execution environment of a currently +running function, e.g. \code{caller_env()}. The function will be +mentioned in error messages as the source of the error. See the +\code{call} argument of \code{\link[rlang:abort]{abort()}} for more information.} +} +\value{ +An object with the specified \code{target_class}. +} +\description{ +Convert a named list into a rapid-style class. +}