example_test.go 5.7 KB

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