Skip to content
This repository was archived by the owner on May 3, 2023. It is now read-only.

Commit f28f7f3

Browse files
Merge pull request #5 from promiseofcake/ljk/fix-yaxis
Fix Yaxis unmarshalling
2 parents c292c1f + 25c0638 commit f28f7f3

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

convert/decoder_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,46 @@ func TestGraphDefinitionDecoder(t *testing.T) {
5050
assert.EqualValues(t, test.expected, gd)
5151
}
5252
}
53+
54+
func float64Pointer(v float64) *float64 {
55+
return &v
56+
}
57+
58+
func stringPointer(v string) *string {
59+
return &v
60+
}
61+
62+
func TestYaxisDecoder(t *testing.T) {
63+
tests := []struct {
64+
input string
65+
expected *Yaxis
66+
}{
67+
{
68+
input: `{"min":"auto", "max":"auto"}`,
69+
expected: &Yaxis{},
70+
},
71+
{
72+
input: `{"min":"0"}`,
73+
expected: &Yaxis{Min: float64Pointer(0)},
74+
},
75+
{
76+
input: `{"min":"1"}`,
77+
expected: &Yaxis{Min: float64Pointer(1)},
78+
},
79+
{
80+
input: `{"max":"100"}`,
81+
expected: &Yaxis{Max: float64Pointer(100)},
82+
},
83+
{
84+
input: `{"max":"100","scale":"log"}`,
85+
expected: &Yaxis{Max: float64Pointer(100), Scale: stringPointer("log")},
86+
},
87+
}
88+
89+
for _, test := range tests {
90+
y := &Yaxis{}
91+
err := json.Unmarshal([]byte(test.input), y)
92+
assert.NoError(t, err)
93+
assert.EqualValues(t, test.expected, y)
94+
}
95+
}

convert/types.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,56 @@ type GraphDefinition struct {
3535
Precision datadog.PrecisionT `json:"precision,omitempty" hcl:"precision" hcle:"omitempty"`
3636
Requests []Request `json:"requests" hcl:"request"`
3737
Events []Event `json:"events" hcl:"events" hcle:"omitempty"`
38+
Yaxis Yaxis `json:"yaxis,omitempty" hcl:"yaxis" hcle:"omitempty"`
39+
}
40+
41+
// copied from https://github.com/zorkian/go-datadog-api due to custom unmarshalling
42+
// we need to duplicate due to custom HCL marshalling
43+
// TODO: use zorkian/go-datadog-api entirely and build a "to our struct" function
44+
type Yaxis struct {
45+
Min *float64 `json:"min,omitempty" hcl:"min" hcle:"omitempty"`
46+
Max *float64 `json:"max,omitempty" hcl:"max" hcle:"omitempty"`
47+
Scale *string `json:"scale,omitempty" hcl:"scale" hcle:"omitempty"`
48+
}
49+
50+
func (y *Yaxis) UnmarshalJSON(data []byte) error {
51+
type Alias Yaxis
52+
wrapper := &struct {
53+
Min *json.Number `json:"min,omitempty"`
54+
Max *json.Number `json:"max,omitempty"`
55+
*Alias
56+
}{
57+
Alias: (*Alias)(y),
58+
}
59+
60+
if err := json.Unmarshal(data, &wrapper); err != nil {
61+
return err
62+
}
63+
64+
if wrapper.Min != nil {
65+
if *wrapper.Min == "auto" {
66+
y.Min = nil
67+
} else {
68+
f, err := wrapper.Min.Float64()
69+
if err != nil {
70+
return err
71+
}
72+
y.Min = &f
73+
}
74+
}
75+
76+
if wrapper.Max != nil {
77+
if *wrapper.Max == "auto" {
78+
y.Max = nil
79+
} else {
80+
f, err := wrapper.Max.Float64()
81+
if err != nil {
82+
return err
83+
}
84+
y.Max = &f
85+
}
86+
}
87+
return nil
3888
}
3989

4090
type GraphStyle struct {

0 commit comments

Comments
 (0)