customization_passes.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // +build codegen
  2. package api
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "path/filepath"
  7. "strings"
  8. )
  9. type service struct {
  10. srcName string
  11. dstName string
  12. serviceVersion string
  13. }
  14. var mergeServices = map[string]service{
  15. "dynamodbstreams": service{
  16. dstName: "dynamodb",
  17. srcName: "streams.dynamodb",
  18. },
  19. "wafregional": service{
  20. dstName: "waf",
  21. srcName: "waf-regional",
  22. serviceVersion: "2015-08-24",
  23. },
  24. }
  25. // customizationPasses Executes customization logic for the API by package name.
  26. func (a *API) customizationPasses() {
  27. var svcCustomizations = map[string]func(*API){
  28. "s3": s3Customizations,
  29. "cloudfront": cloudfrontCustomizations,
  30. "rds": rdsCustomizations,
  31. }
  32. for k, _ := range mergeServices {
  33. svcCustomizations[k] = mergeServicesCustomizations
  34. }
  35. if fn := svcCustomizations[a.PackageName()]; fn != nil {
  36. fn(a)
  37. }
  38. blobDocStringCustomizations(a)
  39. }
  40. const base64MarshalDocStr = "// %s is automatically base64 encoded/decoded by the SDK.\n"
  41. func blobDocStringCustomizations(a *API) {
  42. for _, s := range a.Shapes {
  43. payloadMemberName := s.Payload
  44. for refName, ref := range s.MemberRefs {
  45. if refName == payloadMemberName {
  46. // Payload members have their own encoding and may
  47. // be raw bytes or io.Reader
  48. continue
  49. }
  50. if ref.Shape.Type == "blob" {
  51. docStr := fmt.Sprintf(base64MarshalDocStr, refName)
  52. if len(strings.TrimSpace(ref.Shape.Documentation)) != 0 {
  53. ref.Shape.Documentation += "//\n" + docStr
  54. } else if len(strings.TrimSpace(ref.Documentation)) != 0 {
  55. ref.Documentation += "//\n" + docStr
  56. } else {
  57. ref.Documentation = docStr
  58. }
  59. }
  60. }
  61. }
  62. }
  63. // s3Customizations customizes the API generation to replace values specific to S3.
  64. func s3Customizations(a *API) {
  65. var strExpires *Shape
  66. for name, s := range a.Shapes {
  67. // Remove ContentMD5 members
  68. if _, ok := s.MemberRefs["ContentMD5"]; ok {
  69. delete(s.MemberRefs, "ContentMD5")
  70. }
  71. // Expires should be a string not time.Time since the format is not
  72. // enforced by S3, and any value can be set to this field outside of the SDK.
  73. if strings.HasSuffix(name, "Output") {
  74. if ref, ok := s.MemberRefs["Expires"]; ok {
  75. if strExpires == nil {
  76. newShape := *ref.Shape
  77. strExpires = &newShape
  78. strExpires.Type = "string"
  79. strExpires.refs = []*ShapeRef{}
  80. }
  81. ref.Shape.removeRef(ref)
  82. ref.Shape = strExpires
  83. ref.Shape.refs = append(ref.Shape.refs, &s.MemberRef)
  84. }
  85. }
  86. }
  87. }
  88. // cloudfrontCustomizations customized the API generation to replace values
  89. // specific to CloudFront.
  90. func cloudfrontCustomizations(a *API) {
  91. // MaxItems members should always be integers
  92. for _, s := range a.Shapes {
  93. if ref, ok := s.MemberRefs["MaxItems"]; ok {
  94. ref.ShapeName = "Integer"
  95. ref.Shape = a.Shapes["Integer"]
  96. }
  97. }
  98. }
  99. // mergeServicesCustomizations references any duplicate shapes from DynamoDB
  100. func mergeServicesCustomizations(a *API) {
  101. info := mergeServices[a.PackageName()]
  102. p := strings.Replace(a.path, info.srcName, info.dstName, -1)
  103. if info.serviceVersion != "" {
  104. index := strings.LastIndex(p, "/")
  105. files, _ := ioutil.ReadDir(p[:index])
  106. if len(files) > 1 {
  107. panic("New version was introduced")
  108. }
  109. p = p[:index] + "/" + info.serviceVersion
  110. }
  111. file := filepath.Join(p, "api-2.json")
  112. serviceAPI := API{}
  113. serviceAPI.Attach(file)
  114. serviceAPI.Setup()
  115. for n := range a.Shapes {
  116. if _, ok := serviceAPI.Shapes[n]; ok {
  117. a.Shapes[n].resolvePkg = "github.com/aws/aws-sdk-go/service/" + info.dstName
  118. }
  119. }
  120. }
  121. // rdsCustomizations are customization for the service/rds. This adds non-modeled fields used for presigning.
  122. func rdsCustomizations(a *API) {
  123. inputs := []string{
  124. "CopyDBSnapshotInput",
  125. }
  126. for _, input := range inputs {
  127. if ref, ok := a.Shapes[input]; ok {
  128. ref.MemberRefs["SourceRegion"] = &ShapeRef{
  129. Documentation: docstring(`SourceRegion is the source region where the resource exists. This is not sent over the wire and is only used for presigning. This value should always have the same region as the source ARN.`),
  130. ShapeName: "String",
  131. Shape: a.Shapes["String"],
  132. Ignore: true,
  133. }
  134. ref.MemberRefs["DestinationRegion"] = &ShapeRef{
  135. Documentation: docstring(`DestinationRegion is used for presigning the request to a given region.`),
  136. ShapeName: "String",
  137. Shape: a.Shapes["String"],
  138. }
  139. }
  140. }
  141. }