Merge pull request #725 from weaveworks/654-omit-controls

Omit controls field from json if emtpy.
This commit is contained in:
Tom Wilkie
2015-12-04 13:15:36 +00:00
2 changed files with 35 additions and 4 deletions

View File

@@ -1,6 +1,8 @@
package report
import (
"bytes"
"encoding/json"
"time"
"github.com/weaveworks/scope/common/mtime"
@@ -43,8 +45,8 @@ func (cs Controls) AddControl(c Control) {
// node at a given point in time. Its is immutable. A zero-value for Timestamp
// indicated this NodeControls is 'not set'.
type NodeControls struct {
Timestamp time.Time `json:"timestamp"`
Controls StringSet `json:"controls,omitempty"`
Timestamp time.Time
Controls StringSet
}
// MakeNodeControls makes a new NodeControls
@@ -75,3 +77,32 @@ func (nc NodeControls) Add(ids ...string) NodeControls {
Controls: nc.Controls.Add(ids...),
}
}
// WireNodeControls is the intermediate type for json encoding.
type WireNodeControls struct {
Timestamp string `json:"timestamp,omitempty"`
Controls StringSet `json:"controls,omitempty"`
}
// MarshalJSON implements json.Marshaller
func (nc NodeControls) MarshalJSON() ([]byte, error) {
buf := bytes.Buffer{}
err := json.NewEncoder(&buf).Encode(WireNodeControls{
Timestamp: renderTime(nc.Timestamp),
Controls: nc.Controls,
})
return buf.Bytes(), err
}
// UnmarshalJSON implements json.Unmarshaler
func (nc *NodeControls) UnmarshalJSON(input []byte) error {
in := WireNodeControls{}
if err := json.NewDecoder(bytes.NewBuffer(input)).Decode(&in); err != nil {
return err
}
*nc = NodeControls{
Timestamp: parseTime(in.Timestamp),
Controls: in.Controls,
}
return nil
}

View File

@@ -12,8 +12,8 @@ import (
// EdgeMetadatas and Nodes respectively. Edges are directional, and embedded
// in the Node struct.
type Topology struct {
Nodes // TODO(pb): remove Nodes intermediate type
Controls
Nodes `json:"nodes"`
Controls `json:"controls,omitempty"`
}
// MakeTopology gives you a Topology.