-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathencoding_test.go
More file actions
103 lines (85 loc) · 1.96 KB
/
encoding_test.go
File metadata and controls
103 lines (85 loc) · 1.96 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package encoding_test
import (
"encoding/json"
"testing"
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/encoding"
"github.com/google/go-cmp/cmp"
"github.com/aws/aws-sdk-go/aws"
)
func TestEncoding(t *testing.T) {
type Nested struct {
SP *string `json:",omitempty"`
BP *bool `json:",omitempty"`
IP *int `json:"intField,omitempty"`
FP *float64 `json:"floatPointer,omitempty"`
S string `json:"stringValue,omitempty"`
B bool `json:",omitempty"`
I int
F float64 `json:",omitempty"`
}
type Main struct {
SP *string
BP *bool `json:",omitempty"`
IP *int `json:",omitempty"`
FP *float64 `json:",omitempty"`
NP *Nested `json:"nestedPointer,omitempty"`
S string `json:",omitempty"`
B bool `json:"boolValue,omitempty"`
I int `json:",omitempty"`
F float64
N Nested `json:",omitempty"`
}
m := Main{
SP: aws.String("foo"),
IP: aws.Int(42),
NP: &Nested{
BP: aws.Bool(true),
FP: aws.Float64(3.14),
},
B: true,
F: 2.72,
N: Nested{
S: "bar",
I: 54,
},
}
stringMap := map[string]interface{}{
"SP": "foo",
"IP": "42",
"nestedPointer": map[string]interface{}{
"BP": "true",
"I": "0",
"floatPointer": "3.14",
},
"boolValue": "true",
"F": "2.72",
"N": map[string]interface{}{
"stringValue": "bar",
"I": "54",
},
}
var err error
rep, err := encoding.Marshal(m)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
// Test that rep can be unmarshalled as regular JSON
var jsonTest map[string]interface{}
err = json.Unmarshal(rep, &jsonTest)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
// And check it matches the expected form
if diff := cmp.Diff(jsonTest, stringMap); diff != "" {
t.Errorf(diff)
}
// Now check we can get the original struct back
var b Main
err = encoding.Unmarshal(rep, &b)
if err != nil {
panic(err)
}
if diff := cmp.Diff(m, b); diff != "" {
t.Errorf(diff)
}
}