customization_passes.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package api
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. )
  7. // customizationPasses Executes customization logic for the API by package name.
  8. func (a *API) customizationPasses() {
  9. var svcCustomizations = map[string]func(*API){
  10. "s3": s3Customizations,
  11. "cloudfront": cloudfrontCustomizations,
  12. "dynamodbstreams": dynamodbstreamsCustomizations,
  13. }
  14. if fn := svcCustomizations[a.PackageName()]; fn != nil {
  15. fn(a)
  16. }
  17. blobDocStringCustomizations(a)
  18. }
  19. const base64MarshalDocStr = "// %s is automatically base64 encoded/decoded by the SDK.\n"
  20. func blobDocStringCustomizations(a *API) {
  21. for _, s := range a.Shapes {
  22. payloadMemberName := s.Payload
  23. for refName, ref := range s.MemberRefs {
  24. if refName == payloadMemberName {
  25. // Payload members have their own encoding and may
  26. // be raw bytes or io.Reader
  27. continue
  28. }
  29. if ref.Shape.Type == "blob" {
  30. docStr := fmt.Sprintf(base64MarshalDocStr, refName)
  31. if len(strings.TrimSpace(ref.Shape.Documentation)) != 0 {
  32. ref.Shape.Documentation += "//\n" + docStr
  33. } else if len(strings.TrimSpace(ref.Documentation)) != 0 {
  34. ref.Documentation += "//\n" + docStr
  35. } else {
  36. ref.Documentation = docStr
  37. }
  38. }
  39. }
  40. }
  41. }
  42. // s3Customizations customizes the API generation to replace values specific to S3.
  43. func s3Customizations(a *API) {
  44. var strExpires *Shape
  45. for name, s := range a.Shapes {
  46. // Remove ContentMD5 members
  47. if _, ok := s.MemberRefs["ContentMD5"]; ok {
  48. delete(s.MemberRefs, "ContentMD5")
  49. }
  50. // Expires should be a string not time.Time since the format is not
  51. // enforced by S3, and any value can be set to this field outside of the SDK.
  52. if strings.HasSuffix(name, "Output") {
  53. if ref, ok := s.MemberRefs["Expires"]; ok {
  54. if strExpires == nil {
  55. newShape := *ref.Shape
  56. strExpires = &newShape
  57. strExpires.Type = "string"
  58. strExpires.refs = []*ShapeRef{}
  59. }
  60. ref.Shape.removeRef(ref)
  61. ref.Shape = strExpires
  62. ref.Shape.refs = append(ref.Shape.refs, &s.MemberRef)
  63. }
  64. }
  65. }
  66. }
  67. // cloudfrontCustomizations customized the API generation to replace values
  68. // specific to CloudFront.
  69. func cloudfrontCustomizations(a *API) {
  70. // MaxItems members should always be integers
  71. for _, s := range a.Shapes {
  72. if ref, ok := s.MemberRefs["MaxItems"]; ok {
  73. ref.ShapeName = "Integer"
  74. ref.Shape = a.Shapes["Integer"]
  75. }
  76. }
  77. }
  78. // dynamodbstreamsCustomizations references any duplicate shapes from DynamoDB
  79. func dynamodbstreamsCustomizations(a *API) {
  80. p := strings.Replace(a.path, "streams.dynamodb", "dynamodb", -1)
  81. file := filepath.Join(p, "api-2.json")
  82. dbAPI := API{}
  83. dbAPI.Attach(file)
  84. dbAPI.Setup()
  85. for n := range a.Shapes {
  86. if _, ok := dbAPI.Shapes[n]; ok {
  87. a.Shapes[n].resolvePkg = "github.com/aws/aws-sdk-go/service/dynamodb"
  88. }
  89. }
  90. }