Add more tests and explain some strange quotes.

This commit is contained in:
monopole
2021-01-13 13:03:22 -08:00
parent cf8815b0a0
commit bb41d018b5
11 changed files with 111 additions and 92 deletions

View File

@@ -244,28 +244,26 @@ func copyStringSlice(s []string) []string {
// Implements ResCtx AddNamePrefix
func (r *Resource) AddNamePrefix(p string) {
annotations := r.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}
if _, ok := annotations[prefixAnnotation]; !ok {
annotations[prefixAnnotation] = p
} else {
annotations[prefixAnnotation] = annotations[prefixAnnotation] + "," + p
}
r.SetAnnotations(annotations)
r.addAdditiveAnnotation(prefixAnnotation, p)
}
// Implements ResCtx AddNameSuffix
func (r *Resource) AddNameSuffix(s string) {
r.addAdditiveAnnotation(suffixAnnotation, s)
}
func (r *Resource) addAdditiveAnnotation(name, value string) {
if value == "" {
return
}
annotations := r.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}
if _, ok := annotations[suffixAnnotation]; !ok {
annotations[suffixAnnotation] = s
if existing, ok := annotations[name]; ok {
annotations[name] = existing + "," + value
} else {
annotations[suffixAnnotation] = annotations[suffixAnnotation] + "," + s
annotations[name] = value
}
r.SetAnnotations(annotations)
}
@@ -347,50 +345,16 @@ func (r *Resource) PrefixesSuffixesEquals(o ResCtx) bool {
return sameEndingSubarray(r.GetNamePrefixes(), o.GetNamePrefixes()) && sameEndingSubarray(r.GetNameSuffixes(), o.GetNameSuffixes())
}
func (r *Resource) RemoveIdAnnotations() error {
func (r *Resource) RemoveIdAnnotations() {
annotations := r.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
if len(annotations) == 0 {
return
}
delete(annotations, nameAnnotation)
delete(annotations, prefixAnnotation)
delete(annotations, suffixAnnotation)
delete(annotations, namespaceAnnotation)
if len(annotations) == 0 {
json, err := r.MarshalJSON()
if err != nil {
return err
}
yml, err := yaml.JSONToYAML(json)
if err != nil {
return err
}
rnode, err := kyaml.Parse(string(yml))
if err != nil {
return err
}
err = rnode.SetAnnotations(nil)
if err != nil {
return err
}
b, err := rnode.MarshalJSON()
if err != nil {
return err
}
err = r.UnmarshalJSON(b)
if err != nil {
return err
}
return nil
}
r.SetAnnotations(annotations)
return nil
}
func (r *Resource) GetOriginalName() string {