customization_passes.go 2.8 KB

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