Implement Selfers for all render types

To workaround https://github.com/ugorji/go/issues/141
This commit is contained in:
Alfonso Acosta
2016-02-16 20:26:12 +00:00
parent 9f02e20ac3
commit 9c368c4e84
7 changed files with 88 additions and 38 deletions

View File

@@ -1,7 +1,6 @@
package detailed
import (
"bytes"
"strconv"
"strings"
@@ -105,6 +104,7 @@ func (c Counter) MetadataRows(n report.Node) []MetadataRow {
}
// MetadataRow is a row for the metadata table.
// codecgen: skip
type MetadataRow struct {
ID string
Value string
@@ -119,23 +119,44 @@ func (m MetadataRow) Copy() MetadataRow {
}
}
// MarshalJSON marshals this MetadataRow to json. It adds a label before
// MarshalJSON shouldn't be used, use CodecEncodeSelf instead
func (MetadataRow) MarshalJSON() ([]byte, error) {
panic("MarshalJSON shouldn't be used, use CodecEncodeSelf instead")
}
// UnmarshalJSON shouldn't be used, use CodecDecodeSelf instead
func (*MetadataRow) UnmarshalJSON(b []byte) error {
panic("UnmarshalJSON shouldn't be used, use CodecDecodeSelf instead")
}
type labelledMetadataRow struct {
ID string `json:"id"`
Label string `json:"label"`
Value string `json:"value"`
Prime bool `json:"prime,omitempty"`
}
// CodecEncodeSelf marshals this MetadataRow. It adds a label before
// rendering.
func (m MetadataRow) MarshalJSON() ([]byte, error) {
buf := bytes.Buffer{}
encoder := codec.NewEncoder(&buf, &codec.JsonHandle{})
err := encoder.Encode(struct {
ID string `json:"id"`
Label string `json:"label"`
Value string `json:"value"`
Prime bool `json:"prime,omitempty"`
}{
func (m *MetadataRow) CodecEncodeSelf(encoder *codec.Encoder) {
in := labelledMetadataRow{
ID: m.ID,
Label: Label(m.ID),
Value: m.Value,
Prime: m.Prime,
})
return buf.Bytes(), err
}
encoder.Encode(in)
}
// CodecDecodeSelf implements codec.Selfer
func (m *MetadataRow) CodecDecodeSelf(decoder *codec.Decoder) {
var in labelledMetadataRow
decoder.Decode(&in)
*m = MetadataRow{
ID: in.ID,
Value: in.Value,
Prime: in.Prime,
}
}
// NodeMetadata produces a table (to be consumed directly by the UI) based on

View File

@@ -1,7 +1,6 @@
package detailed
import (
"bytes"
"math"
"github.com/ugorji/go/codec"
@@ -37,6 +36,7 @@ var (
// MetricRow is a tuple of data used to render a metric as a sparkline and
// accoutrements.
// codecgen: skip
type MetricRow struct {
ID string
Format string
@@ -60,19 +60,29 @@ func (m MetricRow) Copy() MetricRow {
return row
}
// MarshalJSON marshals this MetricRow to json. It takes the basic Metric
// MarshalJSON shouldn't be used, use CodecEncodeSelf instead
func (MetricRow) MarshalJSON() ([]byte, error) {
panic("MarshalJSON shouldn't be used, use CodecEncodeSelf instead")
}
// UnmarshalJSON shouldn't be used, use CodecDecodeSelf instead
func (*MetricRow) UnmarshalJSON(b []byte) error {
panic("UnmarshalJSON shouldn't be used, use CodecDecodeSelf instead")
}
type wiredMetricRow struct {
ID string `json:"id"`
Label string `json:"label"`
Format string `json:"format,omitempty"`
Group string `json:"group,omitempty"`
Value float64 `json:"value"`
report.WireMetrics
}
// CodecEncodeSelf marshals this MetricRow. It takes the basic Metric
// rendering, then adds some row-specific fields.
func (m MetricRow) MarshalJSON() ([]byte, error) {
buf := bytes.Buffer{}
encoder := codec.NewEncoder(&buf, &codec.JsonHandle{})
err := encoder.Encode(struct {
ID string `json:"id"`
Label string `json:"label"`
Format string `json:"format,omitempty"`
Group string `json:"group,omitempty"`
Value float64 `json:"value"`
report.WireMetrics
}{
func (m *MetricRow) CodecEncodeSelf(encoder *codec.Encoder) {
encoder.Encode(wiredMetricRow{
ID: m.ID,
Label: Label(m.ID),
Format: m.Format,
@@ -80,7 +90,21 @@ func (m MetricRow) MarshalJSON() ([]byte, error) {
Value: m.Value,
WireMetrics: m.Metric.ToIntermediate(),
})
return buf.Bytes(), err
}
// CodecDecodeSelf implements codec.Selfer
func (m *MetricRow) CodecDecodeSelf(decoder *codec.Decoder) {
var in wiredMetricRow
decoder.Decode(&in)
metric := in.WireMetrics.FromIntermediate()
*m = MetricRow{
ID: in.ID,
Format: in.Format,
Group: in.Group,
Value: in.Value,
Metric: &metric,
}
}
// NodeMetrics produces a table (to be consumed directly by the UI) based on

View File

@@ -36,6 +36,7 @@ func (g NodeSummaryGroup) Copy() NodeSummaryGroup {
// Column provides special json serialization for column ids, so they include
// their label for the frontend.
// codecgen: skip
type Column string
// CodecEncodeSelf implements codec.Selfer