mirror of
https://github.com/golang/go
synced 2024-11-06 11:26:12 -07:00
2d3bb8ce05
This change adds a custom MarshalJSON method to BucketOptionsExplicit for marshalling parity with protobuf/jsonpb. This allows the OpenCensus service to correctly decode a distribution's BucketOptions. Updates golang/go#33819 Change-Id: Ia9dc868e1cbfc32a956f6a276dfd1591f7d4d31a Reviewed-on: https://go-review.googlesource.com/c/tools/+/208398 Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com> Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
77 lines
1.2 KiB
Go
77 lines
1.2 KiB
Go
package wire
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestMarshalJSON(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
pt *Point
|
|
want string
|
|
}{
|
|
{
|
|
"PointInt64",
|
|
&Point{
|
|
Value: PointInt64Value{
|
|
Int64Value: 5,
|
|
},
|
|
},
|
|
`{"int64Value":5}`,
|
|
},
|
|
{
|
|
"PointDouble",
|
|
&Point{
|
|
Value: PointDoubleValue{
|
|
DoubleValue: 3.14,
|
|
},
|
|
},
|
|
`{"doubleValue":3.14}`,
|
|
},
|
|
{
|
|
"PointDistribution",
|
|
&Point{
|
|
Value: PointDistributionValue{
|
|
DistributionValue: &DistributionValue{
|
|
Count: 3,
|
|
Sum: 10,
|
|
Buckets: []*Bucket{
|
|
{
|
|
Count: 1,
|
|
},
|
|
{
|
|
Count: 2,
|
|
},
|
|
},
|
|
BucketOptions: &BucketOptionsExplicit{
|
|
Bounds: []float64{
|
|
0, 5,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
`{"distributionValue":{"count":3,"sum":10,"bucket_options":{"explicit":{"bounds":[0,5]}},"buckets":[{"count":1},{"count":2}]}}`,
|
|
},
|
|
{
|
|
"nil point",
|
|
nil,
|
|
`null`,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
buf, err := tt.pt.MarshalJSON()
|
|
if err != nil {
|
|
t.Fatalf("Got:\n%v\nWant:\n%v", err, nil)
|
|
}
|
|
got := string(buf)
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Fatalf("Got:\n%s\nWant:\n%s", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|