Skip to content

Commit

Permalink
Merge pull request #58 from hovsep/tags
Browse files Browse the repository at this point in the history
Tags
  • Loading branch information
hovsep authored Oct 9, 2024
2 parents 7b3d912 + 8a7349e commit 38ab21f
Show file tree
Hide file tree
Showing 15 changed files with 883 additions and 274 deletions.
15 changes: 15 additions & 0 deletions common/described_entity.go
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
}
65 changes: 65 additions & 0 deletions common/described_entity_test.go
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())
})
}
}
84 changes: 84 additions & 0 deletions common/labeled_entity.go
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
}
Loading

0 comments on commit 38ab21f

Please sign in to comment.