Modify PathConfig.PathSlice method and add tests

This commit is contained in:
Jingfang Liu
2018-09-27 14:02:52 -07:00
parent 19197a490e
commit d8b27ef8fe
8 changed files with 106 additions and 6 deletions

View File

@@ -30,7 +30,31 @@ type PathConfig struct {
CreateIfNotPresent bool `json:"create,omitempty" yaml:"create,omitempty"`
}
const (
escapedForwardSlash = "\\/"
tempSlashReplacement = "???"
)
// PathSlice converts the path string to a slice of strings, separated by "/"
// "/" can be contained in a fieldname.
// such as ingress.kubernetes.io/auth-secret in Ingress annotations.
// To deal with this special case, the path to this field should be formatted as
//
// metadata/annotations/ingress.kubernetes.io\/auth-secret
//
// Then PathSlice will return
//
// []string{"metadata", "annotations", "ingress.auth-secretkubernetes.io/auth-secret"}
func (p PathConfig) PathSlice() []string {
return strings.Split(p.Path, "/")
if !strings.Contains(p.Path, escapedForwardSlash) {
return strings.Split(p.Path, "/")
}
s := strings.Replace(p.Path, escapedForwardSlash, tempSlashReplacement, -1)
paths := strings.Split(s, "/")
var result []string
for _, path := range paths {
result = append(result, strings.Replace(path, tempSlashReplacement, "/", -1))
}
return result
}