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 as.*_name functions #16

Open
andodet opened this issue Apr 11, 2022 · 0 comments
Open

Refactor as.*_name functions #16

andodet opened this issue Apr 11, 2022 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@andodet
Copy link
Owner

andodet commented Apr 11, 2022

As of now, as.*_name functions are a bit of a if/else mess that looks quite garbled and inaccessible. It would be good to go through a round of refactoring to get them more accessible and reduce code duplication. If too much effort, adding some comments to explain the logic might be another way to go.

The following function is how we currently check and format strings to make sure they're a valid subscription name. I am not proud of this:

as.sub_name <- function(x, project = ps_project_get()) {
  # Can it be done with a switch case?
  if (is.character(x) && x != "") {
    if (already_formatted(x)) {
      out <- x
    } else {
      out <- paste(c("projects", project, "subscriptions", x), collapse = "/")
    }
    return(out)
  } else if (inherits(x, "Subscription")) {
    if (already_formatted(x$name)) {
      out <- x$name
    } else {
      out <- paste(c("projects", project, "subscriptions", x$name), collapse = "/")
    }
    return(out)
  } else if (is.null(x)) {
    return(NULL)
  } else {
    stop("Subscription name is invalid!", call. = FALSE)
  }
}

An avenue that might be worth exploring is to have a nameValidator class that will handle name formatting for all entities (topics, subscriptions, schemas, snapshots).

Functions in need of refactoring:

  • as.sub_name
  • as.topic_name
  • as.schema_name
  • as.snapshot_name
@andodet andodet added enhancement New feature or request good first issue Good for newcomers labels Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant