api.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // Package api represents API abstractions for rendering service generated files.
  2. package api
  3. import (
  4. "bytes"
  5. "fmt"
  6. "path"
  7. "regexp"
  8. "sort"
  9. "strings"
  10. "text/template"
  11. )
  12. // An API defines a service API's definition. and logic to serialize the definition.
  13. type API struct {
  14. Metadata Metadata
  15. Operations map[string]*Operation
  16. Shapes map[string]*Shape
  17. Waiters []Waiter
  18. Documentation string
  19. // Set to true to avoid removing unused shapes
  20. NoRemoveUnusedShapes bool
  21. // Set to true to avoid renaming to 'Input/Output' postfixed shapes
  22. NoRenameToplevelShapes bool
  23. // Set to true to ignore service/request init methods (for testing)
  24. NoInitMethods bool
  25. // Set to true to ignore String() and GoString methods (for generated tests)
  26. NoStringerMethods bool
  27. // Set to true to not generate API service name constants
  28. NoConstServiceNames bool
  29. // Set to true to not generate validation shapes
  30. NoValidataShapeMethods bool
  31. SvcClientImportPath string
  32. initialized bool
  33. imports map[string]bool
  34. name string
  35. path string
  36. }
  37. // A Metadata is the metadata about an API's definition.
  38. type Metadata struct {
  39. APIVersion string
  40. EndpointPrefix string
  41. SigningName string
  42. ServiceAbbreviation string
  43. ServiceFullName string
  44. SignatureVersion string
  45. JSONVersion string
  46. TargetPrefix string
  47. Protocol string
  48. }
  49. // PackageName name of the API package
  50. func (a *API) PackageName() string {
  51. return strings.ToLower(a.StructName())
  52. }
  53. // InterfacePackageName returns the package name for the interface.
  54. func (a *API) InterfacePackageName() string {
  55. return a.PackageName() + "iface"
  56. }
  57. var nameRegex = regexp.MustCompile(`^Amazon|AWS\s*|\(.*|\s+|\W+`)
  58. // StructName returns the struct name for a given API.
  59. func (a *API) StructName() string {
  60. if a.name == "" {
  61. name := a.Metadata.ServiceAbbreviation
  62. if name == "" {
  63. name = a.Metadata.ServiceFullName
  64. }
  65. name = nameRegex.ReplaceAllString(name, "")
  66. switch strings.ToLower(name) {
  67. case "elasticloadbalancing":
  68. a.name = "ELB"
  69. case "elasticloadbalancingv2":
  70. a.name = "ELBV2"
  71. case "config":
  72. a.name = "ConfigService"
  73. default:
  74. a.name = name
  75. }
  76. }
  77. return a.name
  78. }
  79. // UseInitMethods returns if the service's init method should be rendered.
  80. func (a *API) UseInitMethods() bool {
  81. return !a.NoInitMethods
  82. }
  83. // NiceName returns the human friendly API name.
  84. func (a *API) NiceName() string {
  85. if a.Metadata.ServiceAbbreviation != "" {
  86. return a.Metadata.ServiceAbbreviation
  87. }
  88. return a.Metadata.ServiceFullName
  89. }
  90. // ProtocolPackage returns the package name of the protocol this API uses.
  91. func (a *API) ProtocolPackage() string {
  92. switch a.Metadata.Protocol {
  93. case "json":
  94. return "jsonrpc"
  95. case "ec2":
  96. return "ec2query"
  97. default:
  98. return strings.Replace(a.Metadata.Protocol, "-", "", -1)
  99. }
  100. }
  101. // OperationNames returns a slice of API operations supported.
  102. func (a *API) OperationNames() []string {
  103. i, names := 0, make([]string, len(a.Operations))
  104. for n := range a.Operations {
  105. names[i] = n
  106. i++
  107. }
  108. sort.Strings(names)
  109. return names
  110. }
  111. // OperationList returns a slice of API operation pointers
  112. func (a *API) OperationList() []*Operation {
  113. list := make([]*Operation, len(a.Operations))
  114. for i, n := range a.OperationNames() {
  115. list[i] = a.Operations[n]
  116. }
  117. return list
  118. }
  119. // OperationHasOutputPlaceholder returns if any of the API operation input
  120. // or output shapes are place holders.
  121. func (a *API) OperationHasOutputPlaceholder() bool {
  122. for _, op := range a.Operations {
  123. if op.OutputRef.Shape.Placeholder {
  124. return true
  125. }
  126. }
  127. return false
  128. }
  129. // ShapeNames returns a slice of names for each shape used by the API.
  130. func (a *API) ShapeNames() []string {
  131. i, names := 0, make([]string, len(a.Shapes))
  132. for n := range a.Shapes {
  133. names[i] = n
  134. i++
  135. }
  136. sort.Strings(names)
  137. return names
  138. }
  139. // ShapeList returns a slice of shape pointers used by the API.
  140. func (a *API) ShapeList() []*Shape {
  141. list := make([]*Shape, len(a.Shapes))
  142. for i, n := range a.ShapeNames() {
  143. list[i] = a.Shapes[n]
  144. }
  145. return list
  146. }
  147. // resetImports resets the import map to default values.
  148. func (a *API) resetImports() {
  149. a.imports = map[string]bool{
  150. "github.com/aws/aws-sdk-go/aws": true,
  151. }
  152. }
  153. // importsGoCode returns the generated Go import code.
  154. func (a *API) importsGoCode() string {
  155. if len(a.imports) == 0 {
  156. return ""
  157. }
  158. corePkgs, extPkgs := []string{}, []string{}
  159. for i := range a.imports {
  160. if strings.Contains(i, ".") {
  161. extPkgs = append(extPkgs, i)
  162. } else {
  163. corePkgs = append(corePkgs, i)
  164. }
  165. }
  166. sort.Strings(corePkgs)
  167. sort.Strings(extPkgs)
  168. code := "import (\n"
  169. for _, i := range corePkgs {
  170. code += fmt.Sprintf("\t%q\n", i)
  171. }
  172. if len(corePkgs) > 0 {
  173. code += "\n"
  174. }
  175. for _, i := range extPkgs {
  176. code += fmt.Sprintf("\t%q\n", i)
  177. }
  178. code += ")\n\n"
  179. return code
  180. }
  181. // A tplAPI is the top level template for the API
  182. var tplAPI = template.Must(template.New("api").Parse(`
  183. {{ range $_, $o := .OperationList }}
  184. {{ $o.GoCode }}
  185. {{ end }}
  186. {{ range $_, $s := .ShapeList }}
  187. {{ if and $s.IsInternal (eq $s.Type "structure") }}{{ $s.GoCode }}{{ end }}
  188. {{ end }}
  189. {{ range $_, $s := .ShapeList }}
  190. {{ if $s.IsEnum }}{{ $s.GoCode }}{{ end }}
  191. {{ end }}
  192. `))
  193. // APIGoCode renders the API in Go code. Returning it as a string
  194. func (a *API) APIGoCode() string {
  195. a.resetImports()
  196. delete(a.imports, "github.com/aws/aws-sdk-go/aws")
  197. a.imports["github.com/aws/aws-sdk-go/aws/awsutil"] = true
  198. a.imports["github.com/aws/aws-sdk-go/aws/request"] = true
  199. if a.OperationHasOutputPlaceholder() {
  200. a.imports["github.com/aws/aws-sdk-go/private/protocol/"+a.ProtocolPackage()] = true
  201. a.imports["github.com/aws/aws-sdk-go/private/protocol"] = true
  202. }
  203. for _, op := range a.Operations {
  204. if op.AuthType == "none" {
  205. a.imports["github.com/aws/aws-sdk-go/aws/credentials"] = true
  206. break
  207. }
  208. }
  209. var buf bytes.Buffer
  210. err := tplAPI.Execute(&buf, a)
  211. if err != nil {
  212. panic(err)
  213. }
  214. code := a.importsGoCode() + strings.TrimSpace(buf.String())
  215. return code
  216. }
  217. // A tplService defines the template for the service generated code.
  218. var tplService = template.Must(template.New("service").Parse(`
  219. {{ .Documentation }}//The service client's operations are safe to be used concurrently.
  220. // It is not safe to mutate any of the client's properties though.
  221. type {{ .StructName }} struct {
  222. *client.Client
  223. }
  224. {{ if .UseInitMethods }}// Used for custom client initialization logic
  225. var initClient func(*client.Client)
  226. // Used for custom request initialization logic
  227. var initRequest func(*request.Request)
  228. {{ end }}
  229. {{ if not .NoConstServiceNames }}
  230. // A ServiceName is the name of the service the client will make API calls to.
  231. const ServiceName = "{{ .Metadata.EndpointPrefix }}"
  232. {{ end }}
  233. // New creates a new instance of the {{ .StructName }} client with a session.
  234. // If additional configuration is needed for the client instance use the optional
  235. // aws.Config parameter to add your extra config.
  236. //
  237. // Example:
  238. // // Create a {{ .StructName }} client from just a session.
  239. // svc := {{ .PackageName }}.New(mySession)
  240. //
  241. // // Create a {{ .StructName }} client with additional configuration
  242. // svc := {{ .PackageName }}.New(mySession, aws.NewConfig().WithRegion("us-west-2"))
  243. func New(p client.ConfigProvider, cfgs ...*aws.Config) *{{ .StructName }} {
  244. c := p.ClientConfig({{ if .NoConstServiceNames }}"{{ .Metadata.EndpointPrefix }}"{{ else }}ServiceName{{ end }}, cfgs...)
  245. return newClient(*c.Config, c.Handlers, c.Endpoint, c.SigningRegion)
  246. }
  247. // newClient creates, initializes and returns a new service client instance.
  248. func newClient(cfg aws.Config, handlers request.Handlers, endpoint, signingRegion string) *{{ .StructName }} {
  249. svc := &{{ .StructName }}{
  250. Client: client.New(
  251. cfg,
  252. metadata.ClientInfo{
  253. ServiceName: {{ if .NoConstServiceNames }}"{{ .Metadata.EndpointPrefix }}"{{ else }}ServiceName{{ end }}, {{ if ne .Metadata.SigningName "" }}
  254. SigningName: "{{ .Metadata.SigningName }}",{{ end }}
  255. SigningRegion: signingRegion,
  256. Endpoint: endpoint,
  257. APIVersion: "{{ .Metadata.APIVersion }}",
  258. {{ if eq .Metadata.Protocol "json" }}JSONVersion: "{{ .Metadata.JSONVersion }}",
  259. TargetPrefix: "{{ .Metadata.TargetPrefix }}",
  260. {{ end }}
  261. },
  262. handlers,
  263. ),
  264. }
  265. // Handlers
  266. svc.Handlers.Sign.PushBackNamed({{if eq .Metadata.SignatureVersion "v2"}}v2{{else}}v4{{end}}.SignRequestHandler)
  267. {{if eq .Metadata.SignatureVersion "v2"}}svc.Handlers.Sign.PushBackNamed(corehandlers.BuildContentLengthHandler)
  268. {{end}}svc.Handlers.Build.PushBackNamed({{ .ProtocolPackage }}.BuildHandler)
  269. svc.Handlers.Unmarshal.PushBackNamed({{ .ProtocolPackage }}.UnmarshalHandler)
  270. svc.Handlers.UnmarshalMeta.PushBackNamed({{ .ProtocolPackage }}.UnmarshalMetaHandler)
  271. svc.Handlers.UnmarshalError.PushBackNamed({{ .ProtocolPackage }}.UnmarshalErrorHandler)
  272. {{ if .UseInitMethods }}// Run custom client initialization if present
  273. if initClient != nil {
  274. initClient(svc.Client)
  275. }
  276. {{ end }}
  277. return svc
  278. }
  279. // newRequest creates a new request for a {{ .StructName }} operation and runs any
  280. // custom request initialization.
  281. func (c *{{ .StructName }}) newRequest(op *request.Operation, params, data interface{}) *request.Request {
  282. req := c.NewRequest(op, params, data)
  283. {{ if .UseInitMethods }}// Run custom request initialization if present
  284. if initRequest != nil {
  285. initRequest(req)
  286. }
  287. {{ end }}
  288. return req
  289. }
  290. `))
  291. // ServiceGoCode renders service go code. Returning it as a string.
  292. func (a *API) ServiceGoCode() string {
  293. a.resetImports()
  294. a.imports["github.com/aws/aws-sdk-go/aws/client"] = true
  295. a.imports["github.com/aws/aws-sdk-go/aws/client/metadata"] = true
  296. a.imports["github.com/aws/aws-sdk-go/aws/request"] = true
  297. if a.Metadata.SignatureVersion == "v2" {
  298. a.imports["github.com/aws/aws-sdk-go/private/signer/v2"] = true
  299. a.imports["github.com/aws/aws-sdk-go/aws/corehandlers"] = true
  300. } else {
  301. a.imports["github.com/aws/aws-sdk-go/aws/signer/v4"] = true
  302. }
  303. a.imports["github.com/aws/aws-sdk-go/private/protocol/"+a.ProtocolPackage()] = true
  304. var buf bytes.Buffer
  305. err := tplService.Execute(&buf, a)
  306. if err != nil {
  307. panic(err)
  308. }
  309. code := a.importsGoCode() + buf.String()
  310. return code
  311. }
  312. // ExampleGoCode renders service example code. Returning it as a string.
  313. func (a *API) ExampleGoCode() string {
  314. exs := []string{}
  315. for _, o := range a.OperationList() {
  316. exs = append(exs, o.Example())
  317. }
  318. code := fmt.Sprintf("import (\n%q\n%q\n%q\n\n%q\n%q\n%q\n)\n\n"+
  319. "var _ time.Duration\nvar _ bytes.Buffer\n\n%s",
  320. "bytes",
  321. "fmt",
  322. "time",
  323. "github.com/aws/aws-sdk-go/aws",
  324. "github.com/aws/aws-sdk-go/aws/session",
  325. path.Join(a.SvcClientImportPath, a.PackageName()),
  326. strings.Join(exs, "\n\n"),
  327. )
  328. return code
  329. }
  330. // A tplInterface defines the template for the service interface type.
  331. var tplInterface = template.Must(template.New("interface").Parse(`
  332. // {{ .StructName }}API is the interface type for {{ .PackageName }}.{{ .StructName }}.
  333. type {{ .StructName }}API interface {
  334. {{ range $_, $o := .OperationList }}
  335. {{ $o.InterfaceSignature }}
  336. {{ end }}
  337. }
  338. var _ {{ .StructName }}API = (*{{ .PackageName }}.{{ .StructName }})(nil)
  339. `))
  340. // InterfaceGoCode returns the go code for the service's API operations as an
  341. // interface{}. Assumes that the interface is being created in a different
  342. // package than the service API's package.
  343. func (a *API) InterfaceGoCode() string {
  344. a.resetImports()
  345. a.imports = map[string]bool{
  346. "github.com/aws/aws-sdk-go/aws/request": true,
  347. path.Join(a.SvcClientImportPath, a.PackageName()): true,
  348. }
  349. var buf bytes.Buffer
  350. err := tplInterface.Execute(&buf, a)
  351. if err != nil {
  352. panic(err)
  353. }
  354. code := a.importsGoCode() + strings.TrimSpace(buf.String())
  355. return code
  356. }
  357. // NewAPIGoCodeWithPkgName returns a string of instantiating the API prefixed
  358. // with its package name. Takes a string depicting the Config.
  359. func (a *API) NewAPIGoCodeWithPkgName(cfg string) string {
  360. return fmt.Sprintf("%s.New(%s)", a.PackageName(), cfg)
  361. }
  362. // computes the validation chain for all input shapes
  363. func (a *API) addShapeValidations() {
  364. for _, o := range a.Operations {
  365. resolveShapeValidations(o.InputRef.Shape)
  366. }
  367. }
  368. // Updates the source shape and all nested shapes with the validations that
  369. // could possibly be needed.
  370. func resolveShapeValidations(s *Shape, ancestry ...*Shape) {
  371. for _, a := range ancestry {
  372. if a == s {
  373. return
  374. }
  375. }
  376. children := []string{}
  377. for _, name := range s.MemberNames() {
  378. ref := s.MemberRefs[name]
  379. if s.IsRequired(name) && !s.Validations.Has(ref, ShapeValidationRequired) {
  380. s.Validations = append(s.Validations, ShapeValidation{
  381. Name: name, Ref: ref, Type: ShapeValidationRequired,
  382. })
  383. }
  384. if ref.Shape.Min != 0 && !s.Validations.Has(ref, ShapeValidationMinVal) {
  385. s.Validations = append(s.Validations, ShapeValidation{
  386. Name: name, Ref: ref, Type: ShapeValidationMinVal,
  387. })
  388. }
  389. switch ref.Shape.Type {
  390. case "map", "list", "structure":
  391. children = append(children, name)
  392. }
  393. }
  394. ancestry = append(ancestry, s)
  395. for _, name := range children {
  396. ref := s.MemberRefs[name]
  397. nestedShape := ref.Shape.NestedShape()
  398. var v *ShapeValidation
  399. if len(nestedShape.Validations) > 0 {
  400. v = &ShapeValidation{
  401. Name: name, Ref: ref, Type: ShapeValidationNested,
  402. }
  403. } else {
  404. resolveShapeValidations(nestedShape, ancestry...)
  405. if len(nestedShape.Validations) > 0 {
  406. v = &ShapeValidation{
  407. Name: name, Ref: ref, Type: ShapeValidationNested,
  408. }
  409. }
  410. }
  411. if v != nil && !s.Validations.Has(v.Ref, v.Type) {
  412. s.Validations = append(s.Validations, *v)
  413. }
  414. }
  415. ancestry = ancestry[:len(ancestry)-1]
  416. }