Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to remove labels with empty values #122

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions model/labelset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ import (
// match.
type LabelSet map[LabelName]LabelValue

// RemoveEmptyLabels removes labels that have empty values.
func (ls LabelSet) RemoveEmptyLabels() {
for k, v := range ls {
if string(v) == "" {
delete(ls, k)
}
}
}

// Validate checks whether all names and values in the label set
// are valid.
func (ls LabelSet) Validate() error {
Expand Down
21 changes: 21 additions & 0 deletions model/labelset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package model

import "testing"

func TestRemoveEmptyLabels(t *testing.T) {
ls := LabelSet{
LabelName("one"): LabelValue("two"),
LabelName("three"): LabelValue("four"),
LabelName("five"): LabelValue(""),
}

ls.RemoveEmptyLabels()

if _, prs := ls["five"]; prs {
t.Errorf(`key "five" not removed from map`)
}

if len(ls) != 2 {
t.Errorf("expected 2 keys in labelset; found %d:\n%v\n", len(ls), ls)
}
}