@@ -7,138 +7,97 @@ import (
77 "github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/encoding"
88
99 "github.com/google/go-cmp/cmp"
10+
11+ "github.com/aws/aws-sdk-go/aws"
1012)
1113
12- type Model struct {
13- ModelString * encoding.String `json:"modelString,omitempty"`
14- ModelBool * encoding.Bool `json:"modelBool,omitempty"`
15- ModelInt * encoding.Int `json:"modelInt,omitempty"`
16- ModelFloat * encoding.Float `json:"modelFloat,omitempty"`
17- ModelSlice []Inner `json:"modelSlice,omitempty"`
18- ModelMap map [string ]Inner `json:"modelMap,omitempty"`
19- ModelNested json.RawMessage `json:"embedded,omitempty"`
20- }
14+ func TestEncoding (t * testing.T ) {
15+ type Nested struct {
16+ SP * string `json:",omitempty"`
17+ BP * bool `json:",omitempty"`
18+ IP * int `json:"intField,omitempty"`
19+ FP * float64 `json:"floatPointer,omitempty"`
20+
21+ S string `json:"stringValue,omitempty"`
22+ B bool `json:",omitempty"`
23+ I int
24+ F float64 `json:",omitempty"`
25+ }
2126
22- type Inner struct {
23- InnerString * encoding.String `json:"innerString,omitempty"`
24- InnerBool * encoding.Bool `json:"innerBool,omitempty"`
25- InnerInt * encoding.Int `json:"innerInt,omitempty"`
26- InnerFloat * encoding.Float `json:"innerFloat"` // No omitempty
27- }
27+ type Main struct {
28+ SP * string
29+ BP * bool `json:",omitempty"`
30+ IP * int `json:",omitempty"`
31+ FP * float64 `json:",omitempty"`
32+ NP * Nested `json:"nestedPointer,omitempty"`
33+
34+ S string `json:",omitempty"`
35+ B bool `json:"boolValue,omitempty"`
36+ I int `json:",omitempty"`
37+ F float64
38+ N Nested `json:",omitempty"`
39+ }
2840
29- var model = Model {
30- ModelBool : encoding .NewBool (false ),
31- ModelInt : encoding .NewInt (42 ),
32- ModelFloat : encoding .NewFloat (3.14 ),
33- ModelSlice : []Inner {
34- {
35- InnerString : encoding .NewString ("bar" ),
36- InnerInt : encoding .NewInt (43 ),
37- InnerFloat : encoding .NewFloat (6.28 ),
38- },
39- },
40- ModelMap : map [string ]Inner {
41- "ModelMapInner" : {
42- InnerString : encoding .NewString ("baz" ),
43- InnerBool : encoding .NewBool (false ),
44- InnerFloat : encoding .NewFloat (9.42 ),
41+ m := Main {
42+ SP : aws .String ("foo" ),
43+ IP : aws .Int (42 ),
44+ NP : & Nested {
45+ BP : aws .Bool (true ),
46+ FP : aws .Float64 (3.14 ),
4547 },
46- },
47- ModelNested : []byte (`{"innerBool":"true","innerFloat":null,"innerInt":"45"}` ),
48- }
4948
50- var stringified = map [string ]interface {}{
51- "modelBool" : "false" ,
52- "modelInt" : "42" ,
53- "modelFloat" : "3.14" ,
54- "modelSlice" : []interface {}{
55- map [string ]interface {}{
56- "innerString" : "bar" ,
57- "innerInt" : "43" ,
58- "innerFloat" : "6.28" ,
59- },
60- },
61- "modelMap" : map [string ]interface {}{
62- "ModelMapInner" : map [string ]interface {}{
63- "innerString" : "baz" ,
64- "innerBool" : "false" ,
65- "innerFloat" : "9.42" ,
49+ B : true ,
50+ F : 2.72 ,
51+ N : Nested {
52+ S : "bar" ,
53+ I : 54 ,
6654 },
67- },
68- "embedded" : map [string ]interface {}{
69- "innerBool" : "true" ,
70- "innerInt" : "45" ,
71- "innerFloat" : nil ,
72- },
73- }
74-
75- func TestString (t * testing.T ) {
76- v := "Hello, world!"
77- s := encoding .NewString (v )
78-
79- // Value
80- if * s .Value () != v {
81- t .Errorf ("Value failed: %v" , s .Value ())
8255 }
8356
84- // Marshal
85- data , err := json .Marshal (s )
86- if err != nil {
87- t .Error (err )
88- }
89-
90- if string (data ) != `"Hello, world!"` {
91- t .Error ("Marshal failed: " + string (data ))
92- }
93-
94- // Unmarshal value
95- v = "Unmarshal me"
96- data , err = json .Marshal (v )
97- if err != nil {
98- panic (err )
99- }
57+ stringMap := map [string ]interface {}{
58+ "SP" : "foo" ,
59+ "IP" : "42" ,
60+ "nestedPointer" : map [string ]interface {}{
61+ "BP" : "true" ,
62+ "I" : "0" ,
63+ "floatPointer" : "3.14" ,
64+ },
10065
101- err = json .Unmarshal (data , s )
102- if err != nil {
103- t .Error (err )
66+ "boolValue" : "true" ,
67+ "F" : "2.72" ,
68+ "N" : map [string ]interface {}{
69+ "stringValue" : "bar" ,
70+ "I" : "54" ,
71+ },
10472 }
10573
106- if * s .Value () != v {
107- t .Errorf ("Unmarshal value failed: %v" , s .Value ())
108- }
109- }
74+ var err error
11075
111- func TestMarshal (t * testing.T ) {
112- data , err := json .Marshal (model )
76+ rep , err := encoding .Marshal (m )
11377 if err != nil {
114- t .Error ( err )
78+ t .Errorf ( "Unexpected error: %v" , err )
11579 }
11680
117- actual := make (map [string ]interface {})
118- err = json .Unmarshal (data , & actual )
81+ // Test that rep can be unmarshalled as regular JSON
82+ var jsonTest map [string ]interface {}
83+ err = json .Unmarshal (rep , & jsonTest )
11984 if err != nil {
120- t .Error ( err )
85+ t .Errorf ( "Unexpected error: %v" , err )
12186 }
12287
123- if diff := cmp .Diff (stringified , actual ); diff != "" {
124- t .Error (diff )
88+ // And check it matches the expected form
89+ if diff := cmp .Diff (jsonTest , stringMap ); diff != "" {
90+ t .Errorf (diff )
12591 }
126- }
12792
128- func TestUnmarshal (t * testing.T ) {
129- data , err := json .Marshal (stringified )
93+ // Now check we can get the original struct back
94+ var b Main
95+ err = encoding .Unmarshal (rep , & b )
13096 if err != nil {
13197 panic (err )
13298 }
13399
134- actual := Model {}
135-
136- err = json .Unmarshal (data , & actual )
137- if err != nil {
138- t .Error (err )
139- }
140-
141- if diff := cmp .Diff (model , actual ); diff != "" {
142- t .Error (diff )
100+ if diff := cmp .Diff (m , b ); diff != "" {
101+ t .Errorf (diff )
143102 }
144103}
0 commit comments