From 7561e7643ca8efdfd013b5fff5ae7c74b2758c82 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Fri, 22 Jan 2021 16:44:23 +0100 Subject: [PATCH] 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 --- config/config.go | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index a88fcccd..c1a873cc 100644 --- a/config/config.go +++ b/config/config.go @@ -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) +}