mirror of
https://github.com/rancher/norman.git
synced 2025-08-31 14:51:57 +00:00
Some util methods for kv style string
This commit is contained in:
24
pkg/kv/split.go
Normal file
24
pkg/kv/split.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package kv
|
||||
|
||||
import "strings"
|
||||
|
||||
func Split(s, sep string) (string, string) {
|
||||
parts := strings.SplitN(s, sep, 2)
|
||||
return strings.TrimSpace(parts[0]), strings.TrimSpace(safeIndex(parts, 1))
|
||||
}
|
||||
|
||||
func SplitMap(s, sep string) map[string]string {
|
||||
result := map[string]string{}
|
||||
for _, part := range strings.Split(s, sep) {
|
||||
k, v := Split(part, "=")
|
||||
result[k] = v
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func safeIndex(parts []string, idx int) string {
|
||||
if len(parts) <= idx {
|
||||
return ""
|
||||
}
|
||||
return parts[idx]
|
||||
}
|
Reference in New Issue
Block a user