shape.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. // +build codegen
  2. package api
  3. import (
  4. "bytes"
  5. "fmt"
  6. "path"
  7. "regexp"
  8. "sort"
  9. "strings"
  10. "text/template"
  11. )
  12. // A ShapeRef defines the usage of a shape within the API.
  13. type ShapeRef struct {
  14. API *API `json:"-"`
  15. Shape *Shape `json:"-"`
  16. Documentation string
  17. ShapeName string `json:"shape"`
  18. Location string
  19. LocationName string
  20. QueryName string
  21. Flattened bool
  22. Streaming bool
  23. XMLAttribute bool
  24. XMLNamespace XMLInfo
  25. Payload string
  26. IdempotencyToken bool `json:"idempotencyToken"`
  27. Deprecated bool `json:"deprecated"`
  28. }
  29. // ErrorInfo represents the error block of a shape's structure
  30. type ErrorInfo struct {
  31. Code string
  32. HTTPStatusCode int
  33. }
  34. // A XMLInfo defines URL and prefix for Shapes when rendered as XML
  35. type XMLInfo struct {
  36. Prefix string
  37. URI string
  38. }
  39. // A Shape defines the definition of a shape type
  40. type Shape struct {
  41. API *API `json:"-"`
  42. ShapeName string
  43. Documentation string
  44. MemberRefs map[string]*ShapeRef `json:"members"`
  45. MemberRef ShapeRef `json:"member"`
  46. KeyRef ShapeRef `json:"key"`
  47. ValueRef ShapeRef `json:"value"`
  48. Required []string
  49. Payload string
  50. Type string
  51. Exception bool
  52. Enum []string
  53. EnumConsts []string
  54. Flattened bool
  55. Streaming bool
  56. Location string
  57. LocationName string
  58. IdempotencyToken bool `json:"idempotencyToken"`
  59. XMLNamespace XMLInfo
  60. Min float64 // optional Minimum length (string, list) or value (number)
  61. Max float64 // optional Maximum length (string, list) or value (number)
  62. refs []*ShapeRef // References to this shape
  63. resolvePkg string // use this package in the goType() if present
  64. // Defines if the shape is a placeholder and should not be used directly
  65. Placeholder bool
  66. Deprecated bool `json:"deprecated"`
  67. Validations ShapeValidations
  68. // Error information that is set if the shape is an error shape.
  69. IsError bool
  70. ErrorInfo ErrorInfo `json:"error"`
  71. }
  72. // ErrorName will return the shape's name or error code if available based
  73. // on the API's protocol.
  74. func (s *Shape) ErrorName() string {
  75. name := s.ShapeName
  76. switch s.API.Metadata.Protocol {
  77. case "query", "ec2query", "rest-xml":
  78. if len(s.ErrorInfo.Code) > 0 {
  79. name = s.ErrorInfo.Code
  80. }
  81. }
  82. return name
  83. }
  84. // GoTags returns the struct tags for a shape.
  85. func (s *Shape) GoTags(root, required bool) string {
  86. ref := &ShapeRef{ShapeName: s.ShapeName, API: s.API, Shape: s}
  87. return ref.GoTags(root, required)
  88. }
  89. // Rename changes the name of the Shape to newName. Also updates
  90. // the associated API's reference to use newName.
  91. func (s *Shape) Rename(newName string) {
  92. for _, r := range s.refs {
  93. r.ShapeName = newName
  94. }
  95. delete(s.API.Shapes, s.ShapeName)
  96. s.API.Shapes[newName] = s
  97. s.ShapeName = newName
  98. }
  99. // MemberNames returns a slice of struct member names.
  100. func (s *Shape) MemberNames() []string {
  101. i, names := 0, make([]string, len(s.MemberRefs))
  102. for n := range s.MemberRefs {
  103. names[i] = n
  104. i++
  105. }
  106. sort.Strings(names)
  107. return names
  108. }
  109. // GoTypeWithPkgName returns a shape's type as a string with the package name in
  110. // <packageName>.<type> format. Package naming only applies to structures.
  111. func (s *Shape) GoTypeWithPkgName() string {
  112. return goType(s, true)
  113. }
  114. // GoStructType returns the type of a struct field based on the API
  115. // model definition.
  116. func (s *Shape) GoStructType(name string, ref *ShapeRef) string {
  117. if (ref.Streaming || ref.Shape.Streaming) && s.Payload == name {
  118. rtype := "io.ReadSeeker"
  119. if len(s.refs) > 1 {
  120. rtype = "aws.ReaderSeekCloser"
  121. } else if strings.HasSuffix(s.ShapeName, "Output") {
  122. rtype = "io.ReadCloser"
  123. }
  124. s.API.imports["io"] = true
  125. return rtype
  126. }
  127. for _, v := range s.Validations {
  128. // TODO move this to shape validation resolution
  129. if (v.Ref.Shape.Type == "map" || v.Ref.Shape.Type == "list") && v.Type == ShapeValidationNested {
  130. s.API.imports["fmt"] = true
  131. }
  132. }
  133. return ref.GoType()
  134. }
  135. // GoType returns a shape's Go type
  136. func (s *Shape) GoType() string {
  137. return goType(s, false)
  138. }
  139. // GoType returns a shape ref's Go type.
  140. func (ref *ShapeRef) GoType() string {
  141. if ref.Shape == nil {
  142. panic(fmt.Errorf("missing shape definition on reference for %#v", ref))
  143. }
  144. return ref.Shape.GoType()
  145. }
  146. // GoTypeWithPkgName returns a shape's type as a string with the package name in
  147. // <packageName>.<type> format. Package naming only applies to structures.
  148. func (ref *ShapeRef) GoTypeWithPkgName() string {
  149. if ref.Shape == nil {
  150. panic(fmt.Errorf("missing shape definition on reference for %#v", ref))
  151. }
  152. return ref.Shape.GoTypeWithPkgName()
  153. }
  154. // Returns a string version of the Shape's type.
  155. // If withPkgName is true, the package name will be added as a prefix
  156. func goType(s *Shape, withPkgName bool) string {
  157. switch s.Type {
  158. case "structure":
  159. if withPkgName || s.resolvePkg != "" {
  160. pkg := s.resolvePkg
  161. if pkg != "" {
  162. s.API.imports[pkg] = true
  163. pkg = path.Base(pkg)
  164. } else {
  165. pkg = s.API.PackageName()
  166. }
  167. return fmt.Sprintf("*%s.%s", pkg, s.ShapeName)
  168. }
  169. return "*" + s.ShapeName
  170. case "map":
  171. return "map[string]" + s.ValueRef.GoType()
  172. case "list":
  173. return "[]" + s.MemberRef.GoType()
  174. case "boolean":
  175. return "*bool"
  176. case "string", "character":
  177. return "*string"
  178. case "blob":
  179. return "[]byte"
  180. case "integer", "long":
  181. return "*int64"
  182. case "float", "double":
  183. return "*float64"
  184. case "timestamp":
  185. s.API.imports["time"] = true
  186. return "*time.Time"
  187. default:
  188. panic("Unsupported shape type: " + s.Type)
  189. }
  190. }
  191. // GoTypeElem returns the Go type for the Shape. If the shape type is a pointer just
  192. // the type will be returned minus the pointer *.
  193. func (s *Shape) GoTypeElem() string {
  194. t := s.GoType()
  195. if strings.HasPrefix(t, "*") {
  196. return t[1:]
  197. }
  198. return t
  199. }
  200. // GoTypeElem returns the Go type for the Shape. If the shape type is a pointer just
  201. // the type will be returned minus the pointer *.
  202. func (ref *ShapeRef) GoTypeElem() string {
  203. if ref.Shape == nil {
  204. panic(fmt.Errorf("missing shape definition on reference for %#v", ref))
  205. }
  206. return ref.Shape.GoTypeElem()
  207. }
  208. // ShapeTag is a struct tag that will be applied to a shape's generated code
  209. type ShapeTag struct {
  210. Key, Val string
  211. }
  212. // String returns the string representation of the shape tag
  213. func (s ShapeTag) String() string {
  214. return fmt.Sprintf(`%s:"%s"`, s.Key, s.Val)
  215. }
  216. // ShapeTags is a collection of shape tags and provides serialization of the
  217. // tags in an ordered list.
  218. type ShapeTags []ShapeTag
  219. // Join returns an ordered serialization of the shape tags with the provided
  220. // separator.
  221. func (s ShapeTags) Join(sep string) string {
  222. o := &bytes.Buffer{}
  223. for i, t := range s {
  224. o.WriteString(t.String())
  225. if i < len(s)-1 {
  226. o.WriteString(sep)
  227. }
  228. }
  229. return o.String()
  230. }
  231. // String is an alias for Join with the empty space separator.
  232. func (s ShapeTags) String() string {
  233. return s.Join(" ")
  234. }
  235. // GoTags returns the rendered tags string for the ShapeRef
  236. func (ref *ShapeRef) GoTags(toplevel bool, isRequired bool) string {
  237. tags := ShapeTags{}
  238. if ref.Location != "" {
  239. tags = append(tags, ShapeTag{"location", ref.Location})
  240. } else if ref.Shape.Location != "" {
  241. tags = append(tags, ShapeTag{"location", ref.Shape.Location})
  242. }
  243. if ref.LocationName != "" {
  244. tags = append(tags, ShapeTag{"locationName", ref.LocationName})
  245. } else if ref.Shape.LocationName != "" {
  246. tags = append(tags, ShapeTag{"locationName", ref.Shape.LocationName})
  247. }
  248. if ref.QueryName != "" {
  249. tags = append(tags, ShapeTag{"queryName", ref.QueryName})
  250. }
  251. if ref.Shape.MemberRef.LocationName != "" {
  252. tags = append(tags, ShapeTag{"locationNameList", ref.Shape.MemberRef.LocationName})
  253. }
  254. if ref.Shape.KeyRef.LocationName != "" {
  255. tags = append(tags, ShapeTag{"locationNameKey", ref.Shape.KeyRef.LocationName})
  256. }
  257. if ref.Shape.ValueRef.LocationName != "" {
  258. tags = append(tags, ShapeTag{"locationNameValue", ref.Shape.ValueRef.LocationName})
  259. }
  260. if ref.Shape.Min > 0 {
  261. tags = append(tags, ShapeTag{"min", fmt.Sprintf("%v", ref.Shape.Min)})
  262. }
  263. if ref.Deprecated || ref.Shape.Deprecated {
  264. tags = append(tags, ShapeTag{"deprecated", "true"})
  265. }
  266. // All shapes have a type
  267. tags = append(tags, ShapeTag{"type", ref.Shape.Type})
  268. // embed the timestamp type for easier lookups
  269. if ref.Shape.Type == "timestamp" {
  270. t := ShapeTag{Key: "timestampFormat"}
  271. if ref.Location == "header" {
  272. t.Val = "rfc822"
  273. } else {
  274. switch ref.API.Metadata.Protocol {
  275. case "json", "rest-json":
  276. t.Val = "unix"
  277. case "rest-xml", "ec2", "query":
  278. t.Val = "iso8601"
  279. }
  280. }
  281. tags = append(tags, t)
  282. }
  283. if ref.Shape.Flattened || ref.Flattened {
  284. tags = append(tags, ShapeTag{"flattened", "true"})
  285. }
  286. if ref.XMLAttribute {
  287. tags = append(tags, ShapeTag{"xmlAttribute", "true"})
  288. }
  289. if isRequired {
  290. tags = append(tags, ShapeTag{"required", "true"})
  291. }
  292. if ref.Shape.IsEnum() {
  293. tags = append(tags, ShapeTag{"enum", ref.ShapeName})
  294. }
  295. if toplevel {
  296. if ref.Shape.Payload != "" {
  297. tags = append(tags, ShapeTag{"payload", ref.Shape.Payload})
  298. }
  299. if ref.XMLNamespace.Prefix != "" {
  300. tags = append(tags, ShapeTag{"xmlPrefix", ref.XMLNamespace.Prefix})
  301. } else if ref.Shape.XMLNamespace.Prefix != "" {
  302. tags = append(tags, ShapeTag{"xmlPrefix", ref.Shape.XMLNamespace.Prefix})
  303. }
  304. if ref.XMLNamespace.URI != "" {
  305. tags = append(tags, ShapeTag{"xmlURI", ref.XMLNamespace.URI})
  306. } else if ref.Shape.XMLNamespace.URI != "" {
  307. tags = append(tags, ShapeTag{"xmlURI", ref.Shape.XMLNamespace.URI})
  308. }
  309. }
  310. if ref.IdempotencyToken || ref.Shape.IdempotencyToken {
  311. tags = append(tags, ShapeTag{"idempotencyToken", "true"})
  312. }
  313. return fmt.Sprintf("`%s`", tags)
  314. }
  315. // Docstring returns the godocs formated documentation
  316. func (ref *ShapeRef) Docstring() string {
  317. if ref.Documentation != "" {
  318. return strings.Trim(ref.Documentation, "\n ")
  319. }
  320. return ref.Shape.Docstring()
  321. }
  322. // Docstring returns the godocs formated documentation
  323. func (s *Shape) Docstring() string {
  324. return strings.Trim(s.Documentation, "\n ")
  325. }
  326. // IndentedDocstring is the indented form of the doc string.
  327. func (ref *ShapeRef) IndentedDocstring() string {
  328. doc := ref.Docstring()
  329. return strings.Replace(doc, "// ", "// ", -1)
  330. }
  331. var goCodeStringerTmpl = template.Must(template.New("goCodeStringerTmpl").Parse(`
  332. // String returns the string representation
  333. func (s {{ .ShapeName }}) String() string {
  334. return awsutil.Prettify(s)
  335. }
  336. // GoString returns the string representation
  337. func (s {{ .ShapeName }}) GoString() string {
  338. return s.String()
  339. }
  340. `))
  341. // GoCodeStringers renders the Stringers for API input/output shapes
  342. func (s *Shape) GoCodeStringers() string {
  343. w := bytes.Buffer{}
  344. if err := goCodeStringerTmpl.Execute(&w, s); err != nil {
  345. panic(fmt.Sprintln("Unexpected error executing GoCodeStringers template", err))
  346. }
  347. return w.String()
  348. }
  349. var enumStrip = regexp.MustCompile(`[^a-zA-Z0-9_:\./-]`)
  350. var enumDelims = regexp.MustCompile(`[-_:\./]+`)
  351. var enumCamelCase = regexp.MustCompile(`([a-z])([A-Z])`)
  352. // EnumName returns the Nth enum in the shapes Enum list
  353. func (s *Shape) EnumName(n int) string {
  354. enum := s.Enum[n]
  355. enum = enumStrip.ReplaceAllLiteralString(enum, "")
  356. enum = enumCamelCase.ReplaceAllString(enum, "$1-$2")
  357. parts := enumDelims.Split(enum, -1)
  358. for i, v := range parts {
  359. v = strings.ToLower(v)
  360. parts[i] = ""
  361. if len(v) > 0 {
  362. parts[i] = strings.ToUpper(v[0:1])
  363. }
  364. if len(v) > 1 {
  365. parts[i] += v[1:]
  366. }
  367. }
  368. enum = strings.Join(parts, "")
  369. enum = strings.ToUpper(enum[0:1]) + enum[1:]
  370. return enum
  371. }
  372. // NestedShape returns the shape pointer value for the shape which is nested
  373. // under the current shape. If the shape is not nested nil will be returned.
  374. //
  375. // strucutures, the current shape is returned
  376. // map: the value shape of the map is returned
  377. // list: the element shape of the list is returned
  378. func (s *Shape) NestedShape() *Shape {
  379. var nestedShape *Shape
  380. switch s.Type {
  381. case "structure":
  382. nestedShape = s
  383. case "map":
  384. nestedShape = s.ValueRef.Shape
  385. case "list":
  386. nestedShape = s.MemberRef.Shape
  387. }
  388. return nestedShape
  389. }
  390. var structShapeTmpl = template.Must(template.New("StructShape").Parse(`
  391. {{ .Docstring }}
  392. type {{ .ShapeName }} struct {
  393. _ struct{} {{ .GoTags true false }}
  394. {{ $context := . -}}
  395. {{ range $_, $name := $context.MemberNames -}}
  396. {{ $elem := index $context.MemberRefs $name -}}
  397. {{ $isRequired := $context.IsRequired $name -}}
  398. {{ $doc := $elem.Docstring -}}
  399. {{ $doc }}
  400. {{ if $isRequired -}}
  401. {{ if $doc -}}
  402. //
  403. {{ end -}}
  404. // {{ $name }} is a required field
  405. {{ end -}}
  406. {{ $name }} {{ $context.GoStructType $name $elem }} {{ $elem.GoTags false $isRequired }}
  407. {{ end }}
  408. }
  409. {{ if not .API.NoStringerMethods }}
  410. {{ .GoCodeStringers }}
  411. {{ end }}
  412. {{ if not .API.NoValidataShapeMethods }}
  413. {{ if .Validations -}}
  414. {{ .Validations.GoCode . }}
  415. {{ end }}
  416. {{ end }}
  417. `))
  418. var enumShapeTmpl = template.Must(template.New("EnumShape").Parse(`
  419. {{ .Docstring }}
  420. const (
  421. {{ $context := . -}}
  422. {{ range $index, $elem := .Enum -}}
  423. {{ $name := index $context.EnumConsts $index -}}
  424. // {{ $name }} is a {{ $context.ShapeName }} enum value
  425. {{ $name }} = "{{ $elem }}"
  426. {{ end }}
  427. )
  428. `))
  429. // GoCode returns the rendered Go code for the Shape.
  430. func (s *Shape) GoCode() string {
  431. b := &bytes.Buffer{}
  432. switch {
  433. case s.Type == "structure":
  434. if err := structShapeTmpl.Execute(b, s); err != nil {
  435. panic(fmt.Sprintf("Failed to generate struct shape %s, %v\n", s.ShapeName, err))
  436. }
  437. case s.IsEnum():
  438. if err := enumShapeTmpl.Execute(b, s); err != nil {
  439. panic(fmt.Sprintf("Failed to generate enum shape %s, %v\n", s.ShapeName, err))
  440. }
  441. default:
  442. panic(fmt.Sprintln("Cannot generate toplevel shape for", s.Type))
  443. }
  444. return b.String()
  445. }
  446. // IsEnum returns whether this shape is an enum list
  447. func (s *Shape) IsEnum() bool {
  448. return s.Type == "string" && len(s.Enum) > 0
  449. }
  450. // IsRequired returns if member is a required field.
  451. func (s *Shape) IsRequired(member string) bool {
  452. for _, n := range s.Required {
  453. if n == member {
  454. return true
  455. }
  456. }
  457. return false
  458. }
  459. // IsInternal returns whether the shape was defined in this package
  460. func (s *Shape) IsInternal() bool {
  461. return s.resolvePkg == ""
  462. }
  463. // removeRef removes a shape reference from the list of references this
  464. // shape is used in.
  465. func (s *Shape) removeRef(ref *ShapeRef) {
  466. r := s.refs
  467. for i := 0; i < len(r); i++ {
  468. if r[i] == ref {
  469. j := i + 1
  470. copy(r[i:], r[j:])
  471. for k, n := len(r)-j+i, len(r); k < n; k++ {
  472. r[k] = nil // free up the end of the list
  473. } // for k
  474. s.refs = r[:len(r)-j+i]
  475. break
  476. }
  477. }
  478. }