Skip to content

feat: add ParseComputePlatform and ComputePlatforms - #1187

Merged
panavenue merged 1 commit into
feature/client-side-metricsfrom
metrics-parse-compute-platform
Oct 22, 2026
Merged

panavenue merged 1 commit into
feature/client-side-metricsfrom
metrics-parse-compute-platform

Conversation

@panavenue

Copy link
Copy Markdown
Collaborator

Summary

Adds two small helpers around ComputePlatform so clients that receive the platform as a string (the Auth Proxy's upcoming --compute-platform flag, config files, env vars) don't have to duplicate the list of valid values.

// All valid values, stable order, for help text.
func ComputePlatforms() []ComputePlatform

// "ai_studio", "AI_STUDIO", " ai_studio " -> ComputePlatformAIStudio.
// Anything else -> error listing the valid values.
func ParseComputePlatform(s string) (ComputePlatform, error)

Intended proxy usage:

p, err := cloudsqlconn.ParseComputePlatform(flagValue)
if err != nil {
    return err // "invalid compute platform \"aws\"; must be one of: gce, gke, cloud_run, app_engine, cloud_functions, ai_studio"
}
opts = append(opts, cloudsqlconn.WithComputePlatform(p))

Why

ComputePlatform follows the idiomatic Go enum pattern (named type + typed constants). Go enums are not closed: ComputePlatform("anything") and even WithComputePlatform("anything") compile, and are only rejected when NewDialer validates. That runtime check keeps the label's value set closed, but a string-accepting client would otherwise have to hard-code the valid list and keep it in sync by hand. With these helpers the connector stays the single source of truth.

Changes

  • options.go: one unexported computePlatforms slice now backs ComputePlatforms(), ParseComputePlatform(), and WithComputePlatform's validation. WithComputePlatform stays strict (exact constants; "AI_STUDIO" is still rejected there) and its error now lists the valid values. ParseComputePlatform is the lenient entry point for user input (trim + case-insensitive).
  • README.md: one paragraph pointing string-accepting clients at the helpers.
  • Tests:
    • TestParseComputePlatform: every valid value, case/whitespace variants, rejection of "", unknown, ai-studio, aws; errors list all valid values; every parsed value is accepted by WithComputePlatform.
    • TestComputePlatformsMatchesInternalSet: the public list must equal internal/tel's allowed label values (excluding unknown), so adding a platform in one place but not the other fails CI (verified: removing ai_studio from the public list fails with a clear diff). Also checks ComputePlatforms() returns a copy.

Testing

go test -short -race ./... and golangci-lint v2.14.0 (0 issues).

Go "enums" (a named type plus typed constants) are not closed: a string
conversion such as ComputePlatform(s) compiles for any s and is only
rejected later by NewDialer. Clients that take the platform as a string,
notably the Auth Proxy's --compute-platform flag, should not have to copy
the list of valid values.

- ComputePlatforms() returns all valid values in a stable order (a copy),
  for help text.
- ParseComputePlatform(s) trims whitespace, matches case-insensitively,
  and returns an error listing the valid values otherwise.
- WithComputePlatform stays strict (exact constants only) and its error
  now lists the valid values too. All three read from one unexported
  slice.
- TestComputePlatformsMatchesInternalSet fails if the public list and
  internal/tel's allowed label values drift apart.
- README mentions the helpers.
@panavenue
panavenue requested a review from a team as a code owner October 21, 2026 18:58
@panavenue
panavenue merged commit f91dca7 into feature/client-side-metrics Oct 22, 2026
14 of 15 checks passed
@panavenue
panavenue deleted the metrics-parse-compute-platform branch October 22, 2026 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants