Add original field to Regexp (#1757)

In similar vein to prometheus/prometheus/pkg/relabel/relabel.go, extend
Regexp to include the original regular expression string to faithfully
output what was read.

Update TestEmptyFieldsAndRegex.

Fixes: #1753

Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
Miek Gieben 2019-02-19 10:08:35 +00:00 committed by stuart nelson
parent 51eebbef85
commit 41c5b5cefa
2 changed files with 11 additions and 6 deletions

View File

@ -681,6 +681,7 @@ func (c *Receiver) UnmarshalYAML(unmarshal func(interface{}) error) error {
// Regexp encapsulates a regexp.Regexp and makes it YAML marshalable.
type Regexp struct {
*regexp.Regexp
original string
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for Regexp.
@ -694,13 +695,14 @@ func (re *Regexp) UnmarshalYAML(unmarshal func(interface{}) error) error {
return err
}
re.Regexp = regex
re.original = s
return nil
}
// MarshalYAML implements the yaml.Marshaler interface for Regexp.
func (re Regexp) MarshalYAML() (interface{}, error) {
if re.Regexp != nil {
return re.String(), nil
if re.original != "" {
return re.original, nil
}
return nil, nil
}
@ -716,13 +718,14 @@ func (re *Regexp) UnmarshalJSON(data []byte) error {
return err
}
re.Regexp = regex
re.original = s
return nil
}
// MarshalJSON implements the json.Marshaler interface for Regexp.
func (re Regexp) MarshalJSON() ([]byte, error) {
if re.Regexp != nil {
return json.Marshal(re.String())
if re.original != "" {
return json.Marshal(re.original)
}
return nil, nil
}

View File

@ -512,8 +512,10 @@ receivers:
func TestEmptyFieldsAndRegex(t *testing.T) {
boolFoo := true
var regexpFoo Regexp
regexpFoo.Regexp, _ = regexp.Compile("^(?:^(foo1|foo2|baz)$)$")
var regexpFoo = Regexp{
Regexp: regexp.MustCompile("^(?:^(foo1|foo2|baz)$)$"),
original: "^(foo1|foo2|baz)$",
}
var expectedConf = Config{