Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(657)

Unified Diff: logdog/common/types/streamaddr.go

Issue 2899993002: [logdog/common/types] custom JSON encoding to allow streamaddr to be zero. (Closed)
Patch Set: support embedded struct Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | logdog/common/types/streamaddr_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.
//
« no previous file with comments | « no previous file | logdog/common/types/streamaddr_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698