1// +build go1.10,codegen 2 3package api 4 5import ( 6 "encoding/json" 7 "testing" 8) 9 10func buildAPI() *API { 11 a := &API{} 12 13 stringShape := &Shape{ 14 API: a, 15 ShapeName: "string", 16 Type: "string", 17 } 18 stringShapeRef := &ShapeRef{ 19 API: a, 20 ShapeName: "string", 21 Shape: stringShape, 22 } 23 24 intShape := &Shape{ 25 API: a, 26 ShapeName: "int", 27 Type: "int", 28 } 29 intShapeRef := &ShapeRef{ 30 API: a, 31 ShapeName: "int", 32 Shape: intShape, 33 } 34 35 nestedComplexShape := &Shape{ 36 API: a, 37 ShapeName: "NestedComplexShape", 38 MemberRefs: map[string]*ShapeRef{ 39 "NestedField": stringShapeRef, 40 }, 41 Type: "structure", 42 } 43 44 nestedComplexShapeRef := &ShapeRef{ 45 API: a, 46 ShapeName: "NestedComplexShape", 47 Shape: nestedComplexShape, 48 } 49 50 nestedListShape := &Shape{ 51 API: a, 52 ShapeName: "NestedListShape", 53 MemberRef: *nestedComplexShapeRef, 54 Type: "list", 55 } 56 57 nestedListShapeRef := &ShapeRef{ 58 API: a, 59 ShapeName: "NestedListShape", 60 Shape: nestedListShape, 61 } 62 63 complexShape := &Shape{ 64 API: a, 65 ShapeName: "ComplexShape", 66 MemberRefs: map[string]*ShapeRef{ 67 "Field": stringShapeRef, 68 "List": nestedListShapeRef, 69 }, 70 Type: "structure", 71 } 72 73 complexShapeRef := &ShapeRef{ 74 API: a, 75 ShapeName: "ComplexShape", 76 Shape: complexShape, 77 } 78 79 listShape := &Shape{ 80 API: a, 81 ShapeName: "ListShape", 82 MemberRef: *complexShapeRef, 83 Type: "list", 84 } 85 86 listShapeRef := &ShapeRef{ 87 API: a, 88 ShapeName: "ListShape", 89 Shape: listShape, 90 } 91 92 listsShape := &Shape{ 93 API: a, 94 ShapeName: "ListsShape", 95 MemberRef: *listShapeRef, 96 Type: "list", 97 } 98 99 listsShapeRef := &ShapeRef{ 100 API: a, 101 ShapeName: "ListsShape", 102 Shape: listsShape, 103 } 104 105 input := &Shape{ 106 API: a, 107 ShapeName: "FooInput", 108 MemberRefs: map[string]*ShapeRef{ 109 "BarShape": stringShapeRef, 110 "ComplexField": complexShapeRef, 111 "ListField": listShapeRef, 112 "ListsField": listsShapeRef, 113 }, 114 Type: "structure", 115 } 116 output := &Shape{ 117 API: a, 118 ShapeName: "FooOutput", 119 MemberRefs: map[string]*ShapeRef{ 120 "BazShape": intShapeRef, 121 "ComplexField": complexShapeRef, 122 "ListField": listShapeRef, 123 "ListsField": listsShapeRef, 124 }, 125 Type: "structure", 126 } 127 128 inputRef := ShapeRef{ 129 API: a, 130 ShapeName: "FooInput", 131 Shape: input, 132 } 133 outputRef := ShapeRef{ 134 API: a, 135 ShapeName: "FooOutput", 136 Shape: output, 137 } 138 139 operations := map[string]*Operation{ 140 "Foo": { 141 API: a, 142 Name: "Foo", 143 ExportedName: "Foo", 144 InputRef: inputRef, 145 OutputRef: outputRef, 146 }, 147 } 148 149 a.Operations = operations 150 a.Shapes = map[string]*Shape{ 151 "FooInput": input, 152 "FooOutput": output, 153 "string": stringShape, 154 "int": intShape, 155 "NestedComplexShape": nestedComplexShape, 156 "NestedListShape": nestedListShape, 157 "ComplexShape": complexShape, 158 "ListShape": listShape, 159 "ListsShape": listsShape, 160 } 161 a.Metadata = Metadata{ 162 ServiceAbbreviation: "FooService", 163 } 164 165 a.BaseImportPath = "github.com/aws/aws-sdk-go/service/" 166 167 a.Setup() 168 return a 169} 170 171func TestExampleGeneration(t *testing.T) { 172 example := ` 173{ 174 "version": "1.0", 175 "examples": { 176 "Foo": [ 177 { 178 "input": { 179 "BarShape": "Hello world", 180 "ComplexField": { 181 "Field": "bar", 182 "List": [ 183 { 184 "NestedField": "qux" 185 } 186 ] 187 }, 188 "ListField": [ 189 { 190 "Field": "baz" 191 } 192 ], 193 "ListsField": [ 194 [ 195 { 196 "Field": "baz" 197 } 198 ] 199 ] 200 }, 201 "output": { 202 "BazShape": 1 203 }, 204 "comments": { 205 "input": { 206 }, 207 "output": { 208 } 209 }, 210 "description": "Foo bar baz qux", 211 "title": "I pity the foo" 212 } 213 ] 214 } 215} 216 ` 217 a := buildAPI() 218 def := &ExamplesDefinition{} 219 err := json.Unmarshal([]byte(example), def) 220 if err != nil { 221 t.Error(err) 222 } 223 def.API = a 224 225 def.setup() 226 expected := ` 227import ( 228 "fmt" 229 "strings" 230 "time" 231 232 "` + SDKImportRoot + `/aws" 233 "` + SDKImportRoot + `/aws/awserr" 234 "` + SDKImportRoot + `/aws/session" 235 "` + SDKImportRoot + `/service/fooservice" 236 237) 238 239var _ time.Duration 240var _ strings.Reader 241var _ aws.Config 242 243func parseTime(layout, value string) *time.Time { 244 t, err := time.Parse(layout, value) 245 if err != nil { 246 panic(err) 247 } 248 return &t 249} 250 251// I pity the foo 252// 253// Foo bar baz qux 254func ExampleFooService_Foo_shared00() { 255 svc := fooservice.New(session.New()) 256 input := &fooservice.FooInput{ 257 BarShape: aws.String("Hello world"), 258 ComplexField: &fooservice.ComplexShape{ 259 Field: aws.String("bar"), 260 List: []*fooservice.NestedComplexShape{ 261 { 262 NestedField: aws.String("qux"), 263 }, 264 }, 265 }, 266 ListField: []*fooservice.ComplexShape{ 267 { 268 Field: aws.String("baz"), 269 }, 270 }, 271 ListsField: [][]*fooservice.ComplexShape{ 272 { 273 { 274 Field: aws.String("baz"), 275 }, 276 }, 277 }, 278 } 279 280 result, err := svc.Foo(input) 281 if err != nil { 282 if aerr, ok := err.(awserr.Error); ok { 283 switch aerr.Code() { 284 default: 285 fmt.Println(aerr.Error()) 286 } 287 } else { 288 // Print the error, cast err to awserr.Error to get the Code and 289 // Message from an error. 290 fmt.Println(err.Error()) 291 } 292 return 293 } 294 295 fmt.Println(result) 296} 297` 298 if expected != a.ExamplesGoCode() { 299 t.Errorf("Expected:\n%s\nReceived:\n%s\n", expected, a.ExamplesGoCode()) 300 } 301} 302 303func TestBuildShape(t *testing.T) { 304 a := buildAPI() 305 cases := []struct { 306 defs map[string]interface{} 307 expected string 308 }{ 309 { 310 defs: map[string]interface{}{ 311 "barShape": "Hello World", 312 }, 313 expected: "BarShape: aws.String(\"Hello World\"),\n", 314 }, 315 { 316 defs: map[string]interface{}{ 317 "BarShape": "Hello World", 318 }, 319 expected: "BarShape: aws.String(\"Hello World\"),\n", 320 }, 321 } 322 323 for _, c := range cases { 324 ref := a.Operations["Foo"].InputRef 325 shapeStr := defaultExamplesBuilder{}.BuildShape(&ref, c.defs, false) 326 if c.expected != shapeStr { 327 t.Errorf("Expected:\n%s\nReceived:\n%s", c.expected, shapeStr) 328 } 329 } 330} 331