-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add util function for processing namespaces/labels
Signed-off-by: Bradley Jones <[email protected]>
- Loading branch information
1 parent
9c58ceb
commit cc86aaa
Showing
2 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package inventory | ||
|
||
import "regexp" | ||
|
||
func checkForRegexMatch(regex string, value string) bool { | ||
return regexp.MustCompile(regex).MatchString(value) | ||
} | ||
|
||
func processAnnotationsOrLabels(annotationsOrLabels map[string]string, include []string) map[string]string { | ||
if len(include) == 0 { | ||
return annotationsOrLabels | ||
} | ||
toReturn := make(map[string]string) | ||
for key, val := range annotationsOrLabels { | ||
for _, includeKey := range include { | ||
if checkForRegexMatch(includeKey, key) { | ||
toReturn[key] = val | ||
} | ||
} | ||
} | ||
return toReturn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package inventory | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_processAnnotations(t *testing.T) { | ||
type args struct { | ||
annotationsOrLabels map[string]string | ||
include []string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want map[string]string | ||
}{ | ||
{ | ||
name: "Empty include", | ||
args: args{ | ||
annotationsOrLabels: map[string]string{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
}, | ||
include: []string{}, | ||
}, | ||
want: map[string]string{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
}, | ||
}, | ||
{ | ||
name: "Include explicit keys", | ||
args: args{ | ||
annotationsOrLabels: map[string]string{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
"key3": "value2", | ||
}, | ||
include: []string{"key1", "key3"}, | ||
}, | ||
want: map[string]string{ | ||
"key1": "value1", | ||
"key3": "value2", | ||
}, | ||
}, | ||
{ | ||
name: "Include non-existent keys", | ||
args: args{ | ||
annotationsOrLabels: map[string]string{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
"key3": "value2", | ||
}, | ||
include: []string{"key1", "key4"}, | ||
}, | ||
want: map[string]string{ | ||
"key1": "value1", | ||
}, | ||
}, | ||
{ | ||
name: "Empty annotationsOrLabels", | ||
args: args{ | ||
annotationsOrLabels: map[string]string{}, | ||
include: []string{"key1", "key4"}, | ||
}, | ||
want: map[string]string{}, | ||
}, | ||
{ | ||
name: "Include keys by regex pattern", | ||
args: args{ | ||
annotationsOrLabels: map[string]string{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
"key3": "value2", | ||
}, | ||
include: []string{"key[1-2]"}, | ||
}, | ||
want: map[string]string{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
}, | ||
}, | ||
{ | ||
name: "Include keys by regex pattern (all)", | ||
args: args{ | ||
annotationsOrLabels: map[string]string{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
"key3": "value2", | ||
}, | ||
include: []string{".*"}, | ||
}, | ||
want: map[string]string{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
"key3": "value2", | ||
}, | ||
}, | ||
{ | ||
name: "Include keys by regex pattern (non-existent)", | ||
args: args{ | ||
annotationsOrLabels: map[string]string{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
"key3": "value2", | ||
}, | ||
include: []string{"key[4-5]"}, | ||
}, | ||
want: map[string]string{}, | ||
}, | ||
{ | ||
name: "Include keys by regex pattern and explicit", | ||
args: args{ | ||
annotationsOrLabels: map[string]string{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
"key3": "value2", | ||
"explicit": "value2", | ||
}, | ||
include: []string{"key[1-2]", "explicit"}, | ||
}, | ||
want: map[string]string{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
"explicit": "value2", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := processAnnotationsOrLabels(tt.args.annotationsOrLabels, tt.args.include) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |