Labels: move Builder and Reset out of common

New internstrings implementation is different.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2023-11-24 12:17:28 +00:00
parent 5aa4473894
commit d1af84f6ee
3 changed files with 38 additions and 19 deletions

View File

@ -371,6 +371,25 @@ func (ls Labels) ReleaseStrings(release func(string)) {
} }
} }
// Builder allows modifying Labels.
type Builder struct {
base Labels
del []string
add []Label
}
// Reset clears all current state for the builder.
func (b *Builder) Reset(base Labels) {
b.base = base
b.del = b.del[:0]
b.add = b.add[:0]
b.base.Range(func(l Label) {
if l.Value == "" {
b.del = append(b.del, l.Name)
}
})
}
// Labels returns the labels from the builder. // Labels returns the labels from the builder.
// If no modifications were made, the original labels are returned. // If no modifications were made, the original labels are returned.
func (b *Builder) Labels() Labels { func (b *Builder) Labels() Labels {

View File

@ -123,13 +123,6 @@ func FromMap(m map[string]string) Labels {
return New(l...) return New(l...)
} }
// Builder allows modifying Labels.
type Builder struct {
base Labels
del []string
add []Label
}
// NewBuilder returns a new LabelsBuilder. // NewBuilder returns a new LabelsBuilder.
func NewBuilder(base Labels) *Builder { func NewBuilder(base Labels) *Builder {
b := &Builder{ b := &Builder{
@ -140,18 +133,6 @@ func NewBuilder(base Labels) *Builder {
return b return b
} }
// Reset clears all current state for the builder.
func (b *Builder) Reset(base Labels) {
b.base = base
b.del = b.del[:0]
b.add = b.add[:0]
b.base.Range(func(l Label) {
if l.Value == "" {
b.del = append(b.del, l.Name)
}
})
}
// Del deletes the label of the given name. // Del deletes the label of the given name.
func (b *Builder) Del(ns ...string) *Builder { func (b *Builder) Del(ns ...string) *Builder {
for _, n := range ns { for _, n := range ns {

View File

@ -458,6 +458,25 @@ func (ls *Labels) InternStrings(intern func(string) string) {
func (ls Labels) ReleaseStrings(release func(string)) { func (ls Labels) ReleaseStrings(release func(string)) {
} }
// Builder allows modifying Labels.
type Builder struct {
base Labels
del []string
add []Label
}
// Reset clears all current state for the builder.
func (b *Builder) Reset(base Labels) {
b.base = base
b.del = b.del[:0]
b.add = b.add[:0]
b.base.Range(func(l Label) {
if l.Value == "" {
b.del = append(b.del, l.Name)
}
})
}
// Labels returns the labels from the builder. // Labels returns the labels from the builder.
// If no modifications were made, the original labels are returned. // If no modifications were made, the original labels are returned.
func (b *Builder) Labels() Labels { func (b *Builder) Labels() Labels {