Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: remove support for JSON marshaling #2086

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,6 @@ func (u URL) MarshalJSON() ([]byte, error) {
return nil, nil
}

// UnmarshalJSON implements the json.Marshaler interface for URL.
func (u *URL) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
urlp, err := parseURL(s)
if err != nil {
return err
}
u.URL = urlp.URL
return nil
}

// SecretURL is a URL that must not be revealed on marshaling.
type SecretURL URL

Expand Down Expand Up @@ -151,18 +137,6 @@ func (s SecretURL) MarshalJSON() ([]byte, error) {
return json.Marshal(secretToken)
}

// UnmarshalJSON implements the json.Marshaler interface for SecretURL.
func (s *SecretURL) UnmarshalJSON(data []byte) error {
// In order to deserialize a previously serialized configuration (eg from
// the Alertmanager API with amtool), `<secret>` needs to be treated
// specially, as it isn't a valid URL.
if string(data) == secretToken || string(data) == secretTokenJSON {
s.URL = &url.URL{}
return nil
}
return json.Unmarshal(data, (*URL)(s))
}

// Load parses the YAML input s into a Config.
func Load(s string) (*Config, error) {
cfg := &Config{}
Expand Down Expand Up @@ -752,26 +726,3 @@ func (re Regexp) MarshalYAML() (interface{}, error) {
}
return nil, nil
}

// UnmarshalJSON implements the json.Marshaler interface for Regexp
func (re *Regexp) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
regex, err := regexp.Compile("^(?:" + s + ")$")
if err != nil {
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.original != "" {
return json.Marshal(re.original)
}
return nil, nil
}
60 changes: 6 additions & 54 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,27 +366,13 @@ func TestMarshalSecretURL(t *testing.T) {
}
u := &SecretURL{urlp}

c, err := json.Marshal(u)
if err != nil {
t.Fatal(err)
}
// u003c -> "<"
// u003e -> ">"
require.Equal(t, "\"\\u003csecret\\u003e\"", string(c), "SecretURL not properly elided in JSON.")
// Check that the marshaled data can be unmarshaled again.
out := &SecretURL{}
err = json.Unmarshal(c, out)
if err != nil {
t.Fatal(err)
}

c, err = yaml.Marshal(u)
c, err := yaml.Marshal(u)
if err != nil {
t.Fatal(err)
}
require.Equal(t, "<secret>\n", string(c), "SecretURL not properly elided in YAML.")
// Check that the marshaled data can be unmarshaled again.
out = &SecretURL{}
out := &SecretURL{}
err = yaml.Unmarshal(c, &out)
if err != nil {
t.Fatal(err)
Expand All @@ -397,13 +383,7 @@ func TestUnmarshalSecretURL(t *testing.T) {
b := []byte(`"http://example.com/se cret"`)
var u SecretURL

err := json.Unmarshal(b, &u)
if err != nil {
t.Fatal(err)
}
require.Equal(t, "http://example.com/se%20cret", u.String(), "SecretURL not properly unmarshalled in JSON.")

err = yaml.Unmarshal(b, &u)
err := yaml.Unmarshal(b, &u)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -435,13 +415,7 @@ func TestUnmarshalURL(t *testing.T) {
b := []byte(`"http://example.com/a b"`)
var u URL

err := json.Unmarshal(b, &u)
if err != nil {
t.Fatal(err)
}
require.Equal(t, "http://example.com/a%20b", u.String(), "URL not properly unmarshalled in JSON.")

err = yaml.Unmarshal(b, &u)
err := yaml.Unmarshal(b, &u)
if err != nil {
t.Fatal(err)
}
Expand All @@ -456,12 +430,7 @@ func TestUnmarshalInvalidURL(t *testing.T) {
} {
var u URL

err := json.Unmarshal(b, &u)
if err == nil {
t.Errorf("Expected an error unmarshalling %q from JSON", string(b))
}

err = yaml.Unmarshal(b, &u)
err := yaml.Unmarshal(b, &u)
if err == nil {
t.Errorf("Expected an error unmarshalling %q from YAML", string(b))
}
Expand All @@ -473,27 +442,10 @@ func TestUnmarshalRelativeURL(t *testing.T) {
b := []byte(`"/home"`)
var u URL

err := json.Unmarshal(b, &u)
err := yaml.Unmarshal(b, &u)
if err == nil {
t.Errorf("Expected an error parsing URL")
}

err = yaml.Unmarshal(b, &u)
if err == nil {
t.Errorf("Expected an error parsing URL")
}
}

func TestJSONUnmarshal(t *testing.T) {
c, err := LoadFile("testdata/conf.good.yml")
if err != nil {
t.Errorf("Error parsing %s: %s", "testdata/conf.good.yml", err)
}

_, err = json.Marshal(c)
if err != nil {
t.Fatal("JSON Marshaling failed:", err)
}
}

func TestMarshalIdempotency(t *testing.T) {
Expand Down
24 changes: 0 additions & 24 deletions dispatch/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package dispatch

import (
"encoding/json"
"fmt"
"sort"
"strings"
Expand Down Expand Up @@ -182,26 +181,3 @@ func (ro *RouteOpts) String() string {
return fmt.Sprintf("<RouteOpts send_to:%q group_by:%q group_by_all:%t timers:%q|%q>",
ro.Receiver, labels, ro.GroupByAll, ro.GroupWait, ro.GroupInterval)
}

// MarshalJSON returns a JSON representation of the routing options.
func (ro *RouteOpts) MarshalJSON() ([]byte, error) {
v := struct {
Receiver string `json:"receiver"`
GroupBy model.LabelNames `json:"groupBy"`
GroupByAll bool `json:"groupByAll"`
GroupWait time.Duration `json:"groupWait"`
GroupInterval time.Duration `json:"groupInterval"`
RepeatInterval time.Duration `json:"repeatInterval"`
}{
Receiver: ro.Receiver,
GroupByAll: ro.GroupByAll,
GroupWait: ro.GroupWait,
GroupInterval: ro.GroupInterval,
RepeatInterval: ro.RepeatInterval,
}
for ln := range ro.GroupBy {
v.GroupBy = append(v.GroupBy, ln)
}

return json.Marshal(&v)
}