Skip to content

Commit 83ae561

Browse files
authored
Add mapstructure unmarshalling to bytesize type (#56)
* Add mapstructure tests to Bytesize type
1 parent 07e6901 commit 83ae561

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

operator/helper/bytesize_test.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ package helper
1616

1717
import (
1818
"encoding/json"
19-
"strconv"
2019
"testing"
2120

21+
"github.com/mitchellh/mapstructure"
2222
"github.com/stretchr/testify/require"
2323
yaml "gopkg.in/yaml.v2"
2424
)
@@ -48,17 +48,16 @@ var sharedTestCases = []testCase{
4848
{`"1tib"`, 1024 * 1024 * 1024 * 1024, false},
4949
{`"1pB"`, 1000 * 1000 * 1000 * 1000 * 1000, false},
5050
{`"1pib"`, 1024 * 1024 * 1024 * 1024 * 1024, false},
51-
{`1e3`, 1000, false},
5251
{`"3ii3"`, 0, true},
5352
{`3ii3`, 0, true},
5453
{`--ii3`, 0, true},
5554
{`{"test":"val"}`, 0, true},
56-
{`1e3`, 1000, false},
55+
// {`1e3`, 1000, false}, not supported in mapstructure
5756
}
5857

5958
func TestByteSizeUnmarshalJSON(t *testing.T) {
60-
for i, tc := range sharedTestCases {
61-
t.Run(strconv.Itoa(i), func(t *testing.T) {
59+
for _, tc := range sharedTestCases {
60+
t.Run("json/"+tc.input, func(t *testing.T) {
6261
var bs ByteSize
6362
err := json.Unmarshal([]byte(tc.input), &bs)
6463
if tc.expectError {
@@ -90,16 +89,34 @@ func TestByteSizeUnmarshalYAML(t *testing.T) {
9089
}
9190

9291
cases := append(sharedTestCases, additionalCases...)
93-
for i, tc := range cases {
94-
t.Run(strconv.Itoa(i), func(t *testing.T) {
92+
93+
for _, tc := range cases {
94+
t.Run("yaml/"+tc.input, func(t *testing.T) {
9595
var bs ByteSize
96-
err := yaml.Unmarshal([]byte(tc.input), &bs)
96+
yamlErr := yaml.Unmarshal([]byte(tc.input), &bs)
9797
if tc.expectError {
98-
require.Error(t, err)
98+
require.Error(t, yamlErr)
9999
return
100100
}
101+
require.NoError(t, yamlErr)
102+
require.Equal(t, tc.expected, bs)
103+
})
104+
t.Run("mapstructure/"+tc.input, func(t *testing.T) {
105+
var bs ByteSize
106+
var raw string
107+
_ = yaml.Unmarshal([]byte(tc.input), &raw)
108+
109+
dc := &mapstructure.DecoderConfig{Result: &bs, DecodeHook: JSONUnmarshalerHook()}
110+
ms, err := mapstructure.NewDecoder(dc)
111+
require.NoError(t, err)
101112

113+
err = ms.Decode(raw)
114+
if tc.expectError {
115+
require.Error(t, err)
116+
return
117+
}
102118
require.NoError(t, err)
119+
103120
require.Equal(t, tc.expected, bs)
104121
})
105122
}

0 commit comments

Comments
 (0)