Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit controls field from json if emtpy. #725

Merged
merged 2 commits into from
Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions report/controls.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package report

import (
"bytes"
"encoding/json"
"time"

"github.com/weaveworks/scope/common/mtime"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions report/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down