endpoint_trait.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // +build codegen
  2. package api
  3. import (
  4. "fmt"
  5. "text/template"
  6. )
  7. func setupEndpointHostPrefix(op *Operation) {
  8. op.API.AddSDKImport("private/protocol")
  9. buildHandler := fmt.Sprintf("protocol.NewHostPrefixHandler(%q, ",
  10. op.Endpoint.HostPrefix)
  11. if op.InputRef.Shape.HasHostLabelMembers() {
  12. buildHandler += "input.hostLabels"
  13. } else {
  14. buildHandler += "nil"
  15. }
  16. buildHandler += ")"
  17. op.CustomBuildHandlers = append(op.CustomBuildHandlers,
  18. buildHandler,
  19. "protocol.ValidateEndpointHostHandler",
  20. )
  21. }
  22. // HasHostLabelMembers returns true if the shape contains any members which are
  23. // decorated with the hostLabel trait.
  24. func (s *Shape) HasHostLabelMembers() bool {
  25. for _, ref := range s.MemberRefs {
  26. if ref.HostLabel {
  27. return true
  28. }
  29. }
  30. return false
  31. }
  32. var hostLabelsShapeTmpl = template.Must(
  33. template.New("hostLabelsShapeTmpl").
  34. Parse(hostLabelsShapeTmplDef),
  35. )
  36. const hostLabelsShapeTmplDef = `
  37. {{- define "hostLabelsShapeTmpl" }}
  38. func (s *{{ $.ShapeName }}) hostLabels() map[string]string {
  39. return map[string]string{
  40. {{- range $name, $ref := $.MemberRefs }}
  41. {{- if $ref.HostLabel }}
  42. "{{ $name }}": aws.StringValue(s.{{ $name }}),
  43. {{- end }}
  44. {{- end }}
  45. }
  46. }
  47. {{- end }}
  48. `