Skip to content

Commit

Permalink
Prefer strings.Cut to strings.SplitN(..., 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed Nov 26, 2024
1 parent 44879de commit 61ee1b9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
21 changes: 9 additions & 12 deletions component/identifiable.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,20 @@ func (id ID) MarshalText() (text []byte, err error) {
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (id *ID) UnmarshalText(text []byte) error {
idStr := string(text)
items := strings.SplitN(idStr, typeAndNameSeparator, 2)
var typeStr, nameStr string
if len(items) >= 1 {
typeStr = strings.TrimSpace(items[0])
}

if len(items) == 1 && typeStr == "" {
return errors.New("id must not be empty")
}
typeStr, nameStr, hasName := strings.Cut(idStr, typeAndNameSeparator)
typeStr = strings.TrimSpace(typeStr)

if typeStr == "" {
return fmt.Errorf("in %q id: the part before %s should not be empty", idStr, typeAndNameSeparator)
if hasName {
return fmt.Errorf("in %q id: the part before %s should not be empty", idStr, typeAndNameSeparator)
} else {
return errors.New("id must not be empty")
}
}

if len(items) > 1 {
if hasName {
// "name" part is present.
nameStr = strings.TrimSpace(items[1])
nameStr = strings.TrimSpace(nameStr)
if nameStr == "" {
return fmt.Errorf("in %q id: the part after %s should not be empty", idStr, typeAndNameSeparator)
}
Expand Down
6 changes: 3 additions & 3 deletions confmap/provider/envprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func (*provider) Shutdown(context.Context) error {
// returns (var name, default value)
func parseEnvVarURI(uri string) (string, *string) {
const defaultSuffix = ":-"
if strings.Contains(uri, defaultSuffix) {
parts := strings.SplitN(uri, defaultSuffix, 2)
return parts[0], &parts[1]
name, defaultValue, hasDefault := strings.Cut(uri, defaultSuffix)
if hasDefault {
return name, &defaultValue
}
return uri, nil
}
21 changes: 9 additions & 12 deletions pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,20 @@ func (i ID) MarshalText() (text []byte, err error) {
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (i *ID) UnmarshalText(text []byte) error {
idStr := string(text)
items := strings.SplitN(idStr, typeAndNameSeparator, 2)
var signalStr, nameStr string
if len(items) >= 1 {
signalStr = strings.TrimSpace(items[0])
}

if len(items) == 1 && signalStr == "" {
return errors.New("id must not be empty")
}
signalStr, nameStr, hasName := strings.Cut(idStr, typeAndNameSeparator)
signalStr = strings.TrimSpace(signalStr)

if signalStr == "" {
return fmt.Errorf("in %q id: the part before %s should not be empty", idStr, typeAndNameSeparator)
if hasName {
return fmt.Errorf("in %q id: the part before %s should not be empty", idStr, typeAndNameSeparator)
} else {
return errors.New("id must not be empty")
}
}

if len(items) > 1 {
if hasName {
// "name" part is present.
nameStr = strings.TrimSpace(items[1])
nameStr = strings.TrimSpace(nameStr)
if nameStr == "" {
return fmt.Errorf("in %q id: the part after %s should not be empty", idStr, typeAndNameSeparator)
}
Expand Down

0 comments on commit 61ee1b9

Please sign in to comment.