Skip to content

Commit

Permalink
allow consecutive copies
Browse files Browse the repository at this point in the history
  • Loading branch information
MasslessParticle committed Jan 24, 2024
1 parent e131ea8 commit 893e3af
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
6 changes: 3 additions & 3 deletions pkg/logql/log/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,9 @@ func (b *LabelsBuilder) IntoMap(m map[string]string) {
if !b.hasDel() && !b.hasAdd() && !b.HasErr() {
if b.baseMap == nil {
b.baseMap = b.base.Map()
for k, v := range b.baseMap {
m[k] = v
}
}
for k, v := range b.baseMap {
m[k] = v
}
return
}
Expand Down
62 changes: 44 additions & 18 deletions pkg/logql/log/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,54 @@ func TestLabelsBuilder_IntoMap(t *testing.T) {
"ToReplace", "text",
}
lbs := labels.FromStrings(strs...)
b := NewBaseLabelsBuilder().ForLabels(lbs, lbs.Hash())

m := map[string]string{}
b.IntoMap(m)
t.Run("it still copies the map after a Reset", func(t *testing.T) {
b := NewBaseLabelsBuilder().ForLabels(lbs, lbs.Hash())

require.Equal(t, map[string]string{
"namespace": "loki",
"job": "us-central1/loki",
"cluster": "us-central1",
"ToReplace": "text",
}, m)
m := map[string]string{}
b.IntoMap(m)

b.Reset()
require.Equal(t, map[string]string{
"namespace": "loki",
"job": "us-central1/loki",
"cluster": "us-central1",
"ToReplace": "text",
}, m)

b.Reset()

m2 := map[string]string{}
b.IntoMap(m2)
require.Equal(t, map[string]string{
"namespace": "loki",
"job": "us-central1/loki",
"cluster": "us-central1",
"ToReplace": "text",
}, m2)
})

t.Run("it can copy the map several times", func(t *testing.T) {
b := NewBaseLabelsBuilder().ForLabels(lbs, lbs.Hash())

m := map[string]string{}
b.IntoMap(m)

require.Equal(t, map[string]string{
"namespace": "loki",
"job": "us-central1/loki",
"cluster": "us-central1",
"ToReplace": "text",
}, m)

m2 := map[string]string{}
b.IntoMap(m2)
require.Equal(t, map[string]string{
"namespace": "loki",
"job": "us-central1/loki",
"cluster": "us-central1",
"ToReplace": "text",
}, m2)
m2 := map[string]string{}
b.IntoMap(m2)
require.Equal(t, map[string]string{
"namespace": "loki",
"job": "us-central1/loki",
"cluster": "us-central1",
"ToReplace": "text",
}, m2)
})
}

func TestLabelsBuilder_LabelsResult(t *testing.T) {
Expand Down

0 comments on commit 893e3af

Please sign in to comment.