Change Label and Annotate to accept map of string
The methods are useful when incoming data are not key value pairs, rather than iterating two times, one time when we call Annotate() or Label() over the map, and then iterate again inside the function, it would be better if we could skip iterating the first time, and let everything to be handled by Annotate or Label functions Relates-To: #162 Change-Id: Ia9d989a0534d15e4024317c4606a187ac021b751
This commit is contained in:
parent
992efae971
commit
b485f00c58
@ -11,7 +11,7 @@ type Factory struct {
|
||||
|
||||
// Document interface
|
||||
type Document interface {
|
||||
Annotate(key, value string)
|
||||
Annotate(map[string]string)
|
||||
AsYAML() ([]byte, error)
|
||||
GetAnnotations() map[string]string
|
||||
GetBool(path string) (bool, error)
|
||||
@ -26,31 +26,37 @@ type Document interface {
|
||||
GetString(path string) (string, error)
|
||||
GetStringMap(path string) (map[string]string, error)
|
||||
GetStringSlice(path string) ([]string, error)
|
||||
Label(key, value string)
|
||||
Label(map[string]string)
|
||||
MarshalJSON() ([]byte, error)
|
||||
}
|
||||
|
||||
// Factory implements Document
|
||||
var _ Document = &Factory{}
|
||||
|
||||
// Annotate document by applying annotation to it
|
||||
func (d *Factory) Annotate(key string, value string) {
|
||||
// Annotate document by applying annotations as map
|
||||
func (d *Factory) Annotate(newAnnotations map[string]string) {
|
||||
annotations := d.GetAnnotations()
|
||||
if annotations == nil {
|
||||
annotations = make(map[string]string)
|
||||
}
|
||||
annotations[key] = value
|
||||
// override Current labels
|
||||
for key, val := range newAnnotations {
|
||||
annotations[key] = val
|
||||
}
|
||||
d.SetAnnotations(annotations)
|
||||
}
|
||||
|
||||
// Label document by applying label on it
|
||||
func (d *Factory) Label(key string, value string) {
|
||||
labels := d.GetKustomizeResource().GetLabels()
|
||||
// Label document by applying labels as map
|
||||
func (d *Factory) Label(newLabels map[string]string) {
|
||||
labels := d.GetLabels()
|
||||
if labels == nil {
|
||||
labels = make(map[string]string)
|
||||
}
|
||||
labels[key] = value
|
||||
d.GetKustomizeResource().SetLabels(labels)
|
||||
// override Current labels
|
||||
for key, val := range newLabels {
|
||||
labels[key] = val
|
||||
}
|
||||
d.SetLabels(labels)
|
||||
}
|
||||
|
||||
// GetNamespace returns the namespace the resource thinks it's in.
|
||||
|
@ -97,25 +97,31 @@ func TestDocument(t *testing.T) {
|
||||
assert.Equal(s, gotSlice)
|
||||
})
|
||||
|
||||
t.Run("Label", func(t *testing.T) {
|
||||
docs, err := bundle.GetAllDocuments()
|
||||
require.NoError(err, "Unexpected error trying to GetAllDocuments")
|
||||
|
||||
for _, doc := range docs {
|
||||
doc.Label("test", "testval")
|
||||
labelList := doc.GetLabels()
|
||||
assert.Contains(labelList, "test")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Annotate", func(t *testing.T) {
|
||||
docs, err := bundle.GetAllDocuments()
|
||||
require.NoError(err, "Unexpected error trying to GetAllDocuments")
|
||||
annotationMap := map[string]string{
|
||||
"test-annotation": "test-annotaiton-value",
|
||||
}
|
||||
|
||||
for _, doc := range docs {
|
||||
doc.Annotate("test", "testval")
|
||||
doc.Annotate(annotationMap)
|
||||
annotationList := doc.GetAnnotations()
|
||||
assert.Contains(annotationList, "test")
|
||||
assert.Equal(annotationList["test-annotation"], "test-annotaiton-value")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Label", func(t *testing.T) {
|
||||
docs, err := bundle.GetAllDocuments()
|
||||
require.NoError(err, "Unexpected error trying to GetAllDocuments")
|
||||
labelMap := map[string]string{
|
||||
"test-label": "test-label-value",
|
||||
}
|
||||
|
||||
for _, doc := range docs {
|
||||
doc.Label(labelMap)
|
||||
labelList := doc.GetLabels()
|
||||
assert.Equal(labelList["test-label"], "test-label-value")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user