Add JSON marshaling to config.Matchers

I have found out that we still need this for the v1 API (and possibly
elsewhere).

Signed-off-by: beorn7 <beorn@grafana.com>
This commit is contained in:
beorn7 2021-01-22 16:44:23 +01:00
parent 4ffba65ff5
commit 7561e7643c

View File

@ -763,7 +763,7 @@ func (re Regexp) MarshalYAML() (interface{}, error) {
return nil, nil
}
// UnmarshalJSON implements the json.Marshaler interface for Regexp
// UnmarshalJSON implements the json.Unmarshaler interface for Regexp
func (re *Regexp) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
@ -815,3 +815,32 @@ func (m Matchers) MarshalYAML() (interface{}, error) {
}
return result, nil
}
// UnmarshalJSON implements the json.Unmarshaler interface for Matchers.
func (m *Matchers) UnmarshalJSON(data []byte) error {
var lines []string
if err := json.Unmarshal(data, &lines); err != nil {
return err
}
for _, line := range lines {
pm, err := labels.ParseMatchers(line)
if err != nil {
return err
}
*m = append(*m, pm...)
}
sort.Sort(labels.Matchers(*m))
return nil
}
// MarshalJSON implements the json.Marshaler interface for Matchers.
func (m Matchers) MarshalJSON() ([]byte, error) {
if len(m) == 0 {
return nil, nil
}
result := make([]string, len(m))
for i, matcher := range m {
result[i] = matcher.String()
}
return json.Marshal(result)
}