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