Skip to content

Commit

Permalink
Try removing sampleHistogramPairPtr
Browse files Browse the repository at this point in the history
Signed-off-by: Jeanette Tan <[email protected]>
  • Loading branch information
zenador committed Jan 5, 2023
1 parent b079cf8 commit f451854
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions model/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,38 +98,21 @@ func (s Sample) MarshalJSON() ([]byte, error) {
return json.Marshal(&v)
}

type sampleHistogramPairPtr struct {
Timestamp Time
Histogram *SampleHistogram
}

func (s *sampleHistogramPairPtr) UnmarshalJSON(buf []byte) error {
tmp := []interface{}{&s.Timestamp, &s.Histogram}
wantLen := len(tmp)
if err := json.Unmarshal(buf, &tmp); err != nil {
return err
}
if gotLen := len(tmp); gotLen != wantLen {
return fmt.Errorf("wrong number of fields: %d != %d", gotLen, wantLen)
}
return nil
}

// UnmarshalJSON implements json.Unmarshaler.
func (s *Sample) UnmarshalJSON(b []byte) error {
v := struct {
Metric Metric `json:"metric"`
Value SamplePair `json:"value"`
Histogram sampleHistogramPairPtr `json:"histogram"`
Metric Metric `json:"metric"`
Value SamplePair `json:"value"`
Histogram *SampleHistogramPair `json:"histogram"`
}{
Metric: s.Metric,
Value: SamplePair{
Timestamp: s.Timestamp,
Value: s.Value,
},
Histogram: sampleHistogramPairPtr{
Histogram: &SampleHistogramPair{
Timestamp: s.Timestamp,
Histogram: s.Histogram,
Histogram: *s.Histogram,
},
}

Expand All @@ -138,9 +121,9 @@ func (s *Sample) UnmarshalJSON(b []byte) error {
}

s.Metric = v.Metric
if v.Histogram.Histogram != nil {
if v.Histogram != nil {
s.Timestamp = v.Histogram.Timestamp
s.Histogram = v.Histogram.Histogram
s.Histogram = &v.Histogram.Histogram
} else {
s.Timestamp = v.Value.Timestamp
s.Value = v.Value.Value
Expand Down

0 comments on commit f451854

Please sign in to comment.