-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatten_test.go
78 lines (72 loc) · 1.47 KB
/
flatten_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package tfconv_test
import (
"testing"
terra "github.com/nitrado/tfconv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/api/resource"
)
func TestConverter_Flatten(t *testing.T) {
c := terra.New("json")
c.Register(resource.Quantity{}, func(v any) (any, error) {
return resource.ParseQuantity(v.(string))
}, func(v any) (any, error) {
q := v.(resource.Quantity)
return (&q).String(), nil
})
obj := TestObject{
Str: "test-str",
Alias: StrAlias("test-alias"),
Int: 1,
Float: 2.3,
Bool: true,
Slice: []T{
{A: "test-t"},
{A: "test-t-also"},
},
Map: map[string]int{
"foo": 4,
},
MapConvert: map[likeAString]string{
"foo": "bar",
},
Struct: &T{
A: "test-ptr-t",
B: newInt(16),
C: nil,
},
Q: resource.MustParse("205m"),
QPtr: ptrOf(resource.MustParse("2Mi")),
}
got, err := c.Flatten(obj, testObjectSchema())
require.NoError(t, err)
want := []any{map[string]any{
"str": "test-str",
"alias": "test-alias",
"int": 1,
"float": 2.3,
"bool": true,
"slice": []any{map[string]any{
"a": "test-t",
}, map[string]any{
"a": "test-t-also",
}},
"map": map[string]any{
"foo": 4,
},
"map_convert": map[string]any{
"foo": "bar",
},
"struct": []any{map[string]any{
"a": "test-ptr-t",
"b": []any{map[string]any{
"value": 16,
}},
}},
"q": "205m",
"q_ptr": []any{map[string]any{
"value": "2Mi",
}},
}}
assert.Equal(t, want, got)
}