diff --git a/component/identifiable.go b/component/identifiable.go index aebacf087e1..39d209da657 100644 --- a/component/identifiable.go +++ b/component/identifiable.go @@ -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) } diff --git a/confmap/provider/envprovider/provider.go b/confmap/provider/envprovider/provider.go index 8a0b87a9174..eab5e63f14b 100644 --- a/confmap/provider/envprovider/provider.go +++ b/confmap/provider/envprovider/provider.go @@ -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 } diff --git a/pipeline/pipeline.go b/pipeline/pipeline.go index aa2d3d0d0ad..1995e88ca36 100644 --- a/pipeline/pipeline.go +++ b/pipeline/pipeline.go @@ -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) }