github.com/aws-cloudformation/cloudformation-cli-go-plugin@v1.2.0/cfn/encoding/unstringify_test.go (about) 1 package encoding_test 2 3 import ( 4 "testing" 5 6 "github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/encoding" 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/google/go-cmp/cmp" 9 ) 10 11 func TestUnstringifyStruct(t *testing.T) { 12 type Model struct { 13 S string 14 SP *string 15 B bool 16 BP *bool 17 I int 18 IP *int 19 F float64 20 FP *float64 21 } 22 23 expected := Model{ 24 S: "foo", 25 SP: aws.String("bar"), 26 B: true, 27 BP: aws.Bool(true), 28 I: 42, 29 IP: aws.Int(42), 30 F: 3.14, 31 FP: aws.Float64(22), 32 } 33 34 t.Run("Convert strings", func(t *testing.T) { 35 var actual Model 36 37 err := encoding.Unstringify(map[string]interface{}{ 38 "S": "foo", 39 "SP": "bar", 40 "B": "true", 41 "BP": "true", 42 "I": "42", 43 "IP": "42", 44 "F": "3.14", 45 "FP": "22", 46 }, &actual) 47 48 if err != nil { 49 t.Fatal(err) 50 } 51 52 if d := cmp.Diff(actual, expected); d != "" { 53 t.Error(d) 54 } 55 }) 56 57 t.Run("Original types", func(t *testing.T) { 58 var actual Model 59 60 err := encoding.Unstringify(map[string]interface{}{ 61 "S": "foo", 62 "SP": "bar", 63 "B": true, 64 "BP": true, 65 "I": 42, 66 "IP": 42, 67 "F": 3.14, 68 "FP": 22.0, 69 }, &actual) 70 71 if err != nil { 72 t.Fatal(err) 73 } 74 75 if d := cmp.Diff(actual, expected); d != "" { 76 t.Error(d) 77 } 78 }) 79 80 t.Run("Compatible types", func(t *testing.T) { 81 var actual Model 82 83 err := encoding.Unstringify(map[string]interface{}{ 84 "S": "foo", 85 "SP": "bar", 86 "B": true, 87 "BP": true, 88 "I": float64(42), 89 "IP": float64(42), 90 "F": 3.14, 91 "FP": int(22), 92 }, &actual) 93 94 if err != nil { 95 t.Fatal(err) 96 } 97 98 if d := cmp.Diff(actual, expected); d != "" { 99 t.Error(d) 100 } 101 }) 102 } 103 104 func TestUnstringifySlices(t *testing.T) { 105 type Model struct { 106 S []string 107 SP []*string 108 B []bool 109 BP []*bool 110 I []int 111 IP []*int 112 F []float64 113 FP []*float64 114 } 115 116 expected := Model{ 117 S: []string{"foo"}, 118 SP: []*string{aws.String("bar")}, 119 B: []bool{true}, 120 BP: []*bool{aws.Bool(true)}, 121 I: []int{42}, 122 IP: []*int{aws.Int(42)}, 123 F: []float64{3.14}, 124 FP: []*float64{aws.Float64(22)}, 125 } 126 127 t.Run("Convert strings", func(t *testing.T) { 128 var actual Model 129 130 err := encoding.Unstringify(map[string]interface{}{ 131 "S": []interface{}{"foo"}, 132 "SP": []interface{}{"bar"}, 133 "B": []interface{}{"true"}, 134 "BP": []interface{}{"true"}, 135 "I": []interface{}{"42"}, 136 "IP": []interface{}{"42"}, 137 "F": []interface{}{"3.14"}, 138 "FP": []interface{}{"22"}, 139 }, &actual) 140 141 if err != nil { 142 t.Fatal(err) 143 } 144 145 if d := cmp.Diff(actual, expected); d != "" { 146 t.Error(d) 147 } 148 }) 149 150 t.Run("Original types", func(t *testing.T) { 151 var actual Model 152 153 err := encoding.Unstringify(map[string]interface{}{ 154 "S": []interface{}{"foo"}, 155 "SP": []interface{}{"bar"}, 156 "B": []interface{}{true}, 157 "BP": []interface{}{true}, 158 "I": []interface{}{42}, 159 "IP": []interface{}{42}, 160 "F": []interface{}{3.14}, 161 "FP": []interface{}{22.0}, 162 }, &actual) 163 164 if err != nil { 165 t.Fatal(err) 166 } 167 168 if d := cmp.Diff(actual, expected); d != "" { 169 t.Error(d) 170 } 171 }) 172 173 t.Run("Compatible types", func(t *testing.T) { 174 var actual Model 175 176 err := encoding.Unstringify(map[string]interface{}{ 177 "S": []interface{}{"foo"}, 178 "SP": []interface{}{"bar"}, 179 "B": []interface{}{true}, 180 "BP": []interface{}{true}, 181 "I": []interface{}{float64(42)}, 182 "IP": []interface{}{float64(42)}, 183 "F": []interface{}{3.14}, 184 "FP": []interface{}{int(22)}, 185 }, &actual) 186 187 if err != nil { 188 t.Fatal(err) 189 } 190 191 if d := cmp.Diff(actual, expected); d != "" { 192 t.Error(d) 193 } 194 }) 195 } 196 197 func TestUnstringifyMaps(t *testing.T) { 198 type Model struct { 199 S map[string]string 200 SP map[string]*string 201 B map[string]bool 202 BP map[string]*bool 203 I map[string]int 204 IP map[string]*int 205 F map[string]float64 206 FP map[string]*float64 207 } 208 209 expected := Model{ 210 S: map[string]string{"Val": "foo"}, 211 SP: map[string]*string{"Val": aws.String("bar")}, 212 B: map[string]bool{"Val": true}, 213 BP: map[string]*bool{"Val": aws.Bool(true)}, 214 I: map[string]int{"Val": 42}, 215 IP: map[string]*int{"Val": aws.Int(42)}, 216 F: map[string]float64{"Val": 3.14}, 217 FP: map[string]*float64{"Val": aws.Float64(22)}, 218 } 219 220 t.Run("Convert strings", func(t *testing.T) { 221 var actual Model 222 223 err := encoding.Unstringify(map[string]interface{}{ 224 "S": map[string]interface{}{"Val": "foo"}, 225 "SP": map[string]interface{}{"Val": "bar"}, 226 "B": map[string]interface{}{"Val": "true"}, 227 "BP": map[string]interface{}{"Val": "true"}, 228 "I": map[string]interface{}{"Val": "42"}, 229 "IP": map[string]interface{}{"Val": "42"}, 230 "F": map[string]interface{}{"Val": "3.14"}, 231 "FP": map[string]interface{}{"Val": "22"}, 232 }, &actual) 233 234 if err != nil { 235 t.Fatal(err) 236 } 237 238 if d := cmp.Diff(actual, expected); d != "" { 239 t.Error(d) 240 } 241 }) 242 243 t.Run("Original types", func(t *testing.T) { 244 var actual Model 245 246 err := encoding.Unstringify(map[string]interface{}{ 247 "S": map[string]interface{}{"Val": "foo"}, 248 "SP": map[string]interface{}{"Val": "bar"}, 249 "B": map[string]interface{}{"Val": true}, 250 "BP": map[string]interface{}{"Val": true}, 251 "I": map[string]interface{}{"Val": 42}, 252 "IP": map[string]interface{}{"Val": 42}, 253 "F": map[string]interface{}{"Val": 3.14}, 254 "FP": map[string]interface{}{"Val": 22.0}, 255 }, &actual) 256 257 if err != nil { 258 t.Fatal(err) 259 } 260 261 if d := cmp.Diff(actual, expected); d != "" { 262 t.Error(d) 263 } 264 }) 265 266 t.Run("Compatible types", func(t *testing.T) { 267 var actual Model 268 269 err := encoding.Unstringify(map[string]interface{}{ 270 "S": map[string]interface{}{"Val": "foo"}, 271 "SP": map[string]interface{}{"Val": "bar"}, 272 "B": map[string]interface{}{"Val": true}, 273 "BP": map[string]interface{}{"Val": true}, 274 "I": map[string]interface{}{"Val": float64(42)}, 275 "IP": map[string]interface{}{"Val": float64(42)}, 276 "F": map[string]interface{}{"Val": 3.14}, 277 "FP": map[string]interface{}{"Val": int(22)}, 278 }, &actual) 279 280 if err != nil { 281 t.Fatal(err) 282 } 283 284 if d := cmp.Diff(actual, expected); d != "" { 285 t.Error(d) 286 } 287 }) 288 } 289 290 func TestUnstringifyPointers(t *testing.T) { 291 type Model struct { 292 SSP *[]string 293 SMP *map[string]string 294 } 295 296 expected := Model{ 297 SSP: &[]string{"foo"}, 298 SMP: &map[string]string{"bar": "baz"}, 299 } 300 301 var actual Model 302 303 err := encoding.Unstringify(map[string]interface{}{ 304 "SSP": []interface{}{"foo"}, 305 "SMP": map[string]interface{}{"bar": "baz"}, 306 }, &actual) 307 308 if err != nil { 309 t.Fatal(err) 310 } 311 312 if d := cmp.Diff(actual, expected); d != "" { 313 t.Error(d) 314 } 315 }