config: fix JSON unmarshaling for HostPort (#2134)

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
Simon Pasquier 2019-12-11 08:55:59 +01:00 committed by GitHub
parent ed6434c7d4
commit 06adefbe59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 2 deletions

View File

@ -90,9 +90,18 @@ func TestAPI(t *testing.T) {
client := &fakeAPIClient{T: t, ch: make(chan fakeAPIResponse, 1)}
now := time.Now()
u, err := url.Parse("http://example.com")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
statusData := &ServerStatus{
ConfigYAML: "{}",
ConfigJSON: &config.Config{},
ConfigYAML: "{}",
ConfigJSON: &config.Config{
Global: &config.GlobalConfig{
PagerdutyURL: &config.URL{URL: u},
SMTPSmarthost: config.HostPort{Host: "localhost", Port: "25"},
},
},
VersionInfo: map[string]string{"version": "v1"},
Uptime: now,
ClusterStatus: &ClusterStatus{Peers: []PeerStatus{}},

View File

@ -516,6 +516,28 @@ func (hp *HostPort) UnmarshalYAML(unmarshal func(interface{}) error) error {
return nil
}
// UnmarshalJSON implements the json.Unmarshaler interface for HostPort.
func (hp *HostPort) UnmarshalJSON(data []byte) error {
var (
s string
err error
)
if err = json.Unmarshal(data, &s); err != nil {
return err
}
if s == "" {
return nil
}
hp.Host, hp.Port, err = net.SplitHostPort(s)
if err != nil {
return err
}
if hp.Port == "" {
return errors.Errorf("address %q: port cannot be empty", s)
}
return nil
}
// MarshalYAML implements the yaml.Marshaler interface for HostPort.
func (hp HostPort) MarshalYAML() (interface{}, error) {
return hp.String(), nil