1
0
mirror of https://github.com/rancher/steve.git synced 2025-06-29 00:07:22 +00:00
steve/pkg/stores/queryhelper/safesplit.go

17 lines
584 B
Go
Raw Normal View History

package queryhelper
import "strings"
// SafeSplit breaks a regular "a.b.c" path to the string slice ["a", "b", "c"]
// but if the final part is in square brackets, like "metadata.labels[cattleprod.io/moo]",
// it returns ["metadata", "labels", "cattleprod.io/moo"]
func SafeSplit(fieldPath string) []string {
squareBracketLocation := strings.Index(fieldPath, "[")
if squareBracketLocation == -1 {
return strings.Split(fieldPath, ".")
}
s := strings.Split(fieldPath[0:squareBracketLocation], ".")
s = append(s, fieldPath[squareBracketLocation+1:len(fieldPath)-1])
return s
}