config: fix JSON unmarshaling for HostPort (#2134)
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
parent
ed6434c7d4
commit
06adefbe59
|
@ -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{},
|
||||
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{}},
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue