mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-28 01:31:17 +00:00
Introduce intermediate type for on-the-wire controls encoding.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user