-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from hovsep/tags
Tags
- Loading branch information
Showing
15 changed files
with
883 additions
and
274 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,15 @@ | ||
package common | ||
|
||
type DescribedEntity struct { | ||
description string | ||
} | ||
|
||
// NewDescribedEntity constructor | ||
func NewDescribedEntity(description string) DescribedEntity { | ||
return DescribedEntity{description: description} | ||
} | ||
|
||
// Description getter | ||
func (e DescribedEntity) Description() string { | ||
return e.description | ||
} |
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,65 @@ | ||
package common | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestNewDescribedEntity(t *testing.T) { | ||
type args struct { | ||
description string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want DescribedEntity | ||
}{ | ||
{ | ||
name: "empty description", | ||
args: args{ | ||
description: "", | ||
}, | ||
want: DescribedEntity{ | ||
description: "", | ||
}, | ||
}, | ||
{ | ||
name: "with description", | ||
args: args{ | ||
description: "component1 is used to generate logs", | ||
}, | ||
want: DescribedEntity{ | ||
description: "component1 is used to generate logs", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equal(t, tt.want, NewDescribedEntity(tt.args.description)) | ||
}) | ||
} | ||
} | ||
|
||
func TestDescribedEntity_Description(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
describedEntity DescribedEntity | ||
want string | ||
}{ | ||
{ | ||
name: "empty description", | ||
describedEntity: NewDescribedEntity(""), | ||
want: "", | ||
}, | ||
{ | ||
name: "with description", | ||
describedEntity: NewDescribedEntity("component2 is used to handle errors"), | ||
want: "component2 is used to handle errors", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equal(t, tt.want, tt.describedEntity.Description()) | ||
}) | ||
} | ||
} |
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,84 @@ | ||
package common | ||
|
||
import "errors" | ||
|
||
type LabelsCollection map[string]string | ||
|
||
type LabeledEntity struct { | ||
labels LabelsCollection | ||
} | ||
|
||
var errLabelNotFound = errors.New("label not found") | ||
|
||
// NewLabeledEntity constructor | ||
func NewLabeledEntity(labels LabelsCollection) LabeledEntity { | ||
|
||
return LabeledEntity{labels: labels} | ||
} | ||
|
||
// Labels getter | ||
func (e *LabeledEntity) Labels() LabelsCollection { | ||
return e.labels | ||
} | ||
|
||
// Label returns the value of single label or nil if it is not found | ||
func (e *LabeledEntity) Label(label string) (string, error) { | ||
value, ok := e.labels[label] | ||
|
||
if !ok { | ||
return "", errLabelNotFound | ||
} | ||
|
||
return value, nil | ||
} | ||
|
||
// SetLabels overwrites labels collection | ||
func (e *LabeledEntity) SetLabels(labels LabelsCollection) { | ||
e.labels = labels | ||
} | ||
|
||
// AddLabel adds or updates(if label already exists) single label | ||
func (e *LabeledEntity) AddLabel(label string, value string) { | ||
if e.labels == nil { | ||
e.labels = make(LabelsCollection) | ||
} | ||
e.labels[label] = value | ||
} | ||
|
||
// AddLabels adds or updates(if label already exists) multiple labels | ||
func (e *LabeledEntity) AddLabels(labels LabelsCollection) { | ||
for label, value := range labels { | ||
e.AddLabel(label, value) | ||
} | ||
} | ||
|
||
// DeleteLabel deletes given label | ||
func (e *LabeledEntity) DeleteLabel(label string) { | ||
delete(e.labels, label) | ||
} | ||
|
||
// HasLabel returns true when entity has given label or false otherwise | ||
func (e *LabeledEntity) HasLabel(label string) bool { | ||
_, ok := e.labels[label] | ||
return ok | ||
} | ||
|
||
// HasAllLabels checks if entity has all labels | ||
func (e *LabeledEntity) HasAllLabels(label ...string) bool { | ||
for _, l := range label { | ||
if !e.HasLabel(l) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// HasAnyLabel checks if entity has at least one of given labels | ||
func (e *LabeledEntity) HasAnyLabel(label ...string) bool { | ||
for _, l := range label { | ||
if e.HasLabel(l) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Oops, something went wrong.