Skip to content

Use strings.Cut instead of strings.SplitN for parsing key=value pairs in envParams.go #2274

@coderabbitai

Description

@coderabbitai

Problem Description

The current implementation in pkg/deploy/envParams.go uses strings.SplitN to parse key=value pairs from environment files:

parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
    paramsEnvMap[parts[0]] = parts[1]
}

Proposed Improvement

Since Go 1.18, the strings.Cut function provides a more idiomatic and cleaner way to handle this exact use case:

key, value, found := strings.Cut(line, "=")
if found {
    paramsEnvMap[key] = value
}

Benefits

  • More idiomatic: strings.Cut is specifically designed for the "split at first delimiter" use case
  • Clearer intent: The function name and return values explicitly communicate the operation
  • Simpler code: Eliminates the need for array indexing and length checking
  • Better readability: The found boolean makes the control flow more explicit

Additional Context

This improvement was identified during a discussion about environment file parsing alignment between opendatahub-operator and opendatahub-io/notebooks repositories. Both repositories handle key=value parsing, and using strings.Cut would be a cleaner approach.

Acceptance Criteria

  • Replace strings.SplitN(line, "=", 2) with `strings.Cut(line, "=")" in envParams.go
  • Update the conditional logic to use the found boolean
  • Ensure tests pass with the new implementation
  • Verify no regression in functionality

References

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions