Browse Source

Fix relabel.Regexp zero value marshalling (#14517)

Signed-off-by: Marco Pracucci <marco@pracucci.com>
pull/11474/head
Marco Pracucci 4 months ago committed by GitHub
parent
commit
d4f098ae80
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      model/relabel/relabel.go
  2. 13
      model/relabel/relabel_test.go

4
model/relabel/relabel.go

@ -213,6 +213,10 @@ func (re Regexp) IsZero() bool {
// String returns the original string used to compile the regular expression.
func (re Regexp) String() string {
if re.Regexp == nil {
return ""
}
str := re.Regexp.String()
// Trim the anchor `^(?:` prefix and `)$` suffix.
return str[4 : len(str)-2]

13
model/relabel/relabel_test.go

@ -900,3 +900,16 @@ action: replace
})
}
}
func TestRegexp_ShouldMarshalAndUnmarshalZeroValue(t *testing.T) {
var zero Regexp
marshalled, err := yaml.Marshal(&zero)
require.NoError(t, err)
require.Equal(t, "null\n", string(marshalled))
var unmarshalled Regexp
err = yaml.Unmarshal(marshalled, &unmarshalled)
require.NoError(t, err)
require.Nil(t, unmarshalled.Regexp)
}

Loading…
Cancel
Save