Chromium Code Reviews| Index: logdog/common/types/streamaddr.go |
| diff --git a/logdog/common/types/streamaddr.go b/logdog/common/types/streamaddr.go |
| index f952ff1bd4ffbaf419c2421935561cef70cc1a44..193e3d51801821b85a2bb686dca612222deb2c02 100644 |
| --- a/logdog/common/types/streamaddr.go |
| +++ b/logdog/common/types/streamaddr.go |
| @@ -5,6 +5,8 @@ |
| package types |
| import ( |
| + "bytes" |
| + "encoding/json" |
| "flag" |
| "net/url" |
| "strings" |
| @@ -18,13 +20,13 @@ const logDogURLScheme = "logdog" |
| // StreamAddr is a fully-qualified LogDog stream address. |
| type StreamAddr struct { |
| // Host is the LogDog host. |
| - Host string |
| + Host string `json:"host"` |
| // Project is the LUCI project name that this log belongs to. |
| - Project cfgtypes.ProjectName |
| + Project cfgtypes.ProjectName `json:"project"` |
| // Path is the LogDog stream path. |
| - Path StreamPath |
| + Path StreamPath `json:"path"` |
| } |
| var _ flag.Value = (*StreamAddr)(nil) |
| @@ -70,6 +72,45 @@ func (s *StreamAddr) URL() *url.URL { |
| } |
| } |
| +// jsonStreamAddr is used to allow type checking during deserialization, but |
| +// special casing IsZero == 'null'. |
| +type jsonStreamAddr struct { |
|
dnj
2017/05/23 15:28:19
This is unfortunate. Are you opposed to using "omi
|
| + Host string `json:"host"` |
| + Project cfgtypes.ProjectName `json:"project"` |
| + Path StreamPath `json:"path"` |
| +} |
| + |
| +// MarshalJSON implements json.Marshaler |
| +func (s StreamAddr) MarshalJSON() ([]byte, error) { |
| + if s.IsZero() { |
| + return []byte(`null`), nil |
| + } |
| + // TODO(iannucci): when we support go 1.8 on appengine, this can be written as |
| + // json.Marshal((*jsonStreamAddr)(s)). |
| + jsa := jsonStreamAddr{s.Host, s.Project, s.Path} |
| + return json.Marshal(jsa) |
| +} |
| + |
| +// UnmarshalJSON implements json.Marshaler |
| +func (s *StreamAddr) UnmarshalJSON(d []byte) error { |
| + if bytes.Equal(d, []byte(`null`)) { |
| + s.Host = "" |
| + s.Project = "" |
| + s.Path = "" |
| + return nil |
| + } |
| + // TODO(iannucci): when we support go 1.8 on appengine, this can be written as |
| + // return json.Unmarshal(d, (*jsonStreamAddr)(s)) |
| + jsa := jsonStreamAddr{} |
| + if err := json.Unmarshal(d, &jsa); err != nil { |
| + return err |
| + } |
| + s.Host = jsa.Host |
| + s.Project = jsa.Project |
| + s.Path = jsa.Path |
| + return nil |
| +} |
| + |
| // ParseURL parses a LogDog URL into a Stream. If the URL is malformed, or |
| // if the host, project, or path is invalid, an error will be returned. |
| // |