1package api
2
3import "text/template"
4
5const endpointARNShapeTmplDef = `
6{{- define "endpointARNShapeTmpl" }}
7{{ range $_, $name := $.MemberNames -}}
8	{{ $elem := index $.MemberRefs $name -}}
9	{{ if $elem.EndpointARN -}}
10		func (s *{{ $.ShapeName }}) getEndpointARN() (arn.Resource, error) {
11			if s.{{ $name }} == nil {
12				return nil, fmt.Errorf("member {{ $name }} is nil")
13			}
14			return parseEndpointARN(*s.{{ $name }})
15		}
16
17		func (s *{{ $.ShapeName }}) hasEndpointARN() bool {
18			if s.{{ $name }} == nil {
19				return false
20			}
21			return arn.IsARN(*s.{{ $name }})
22		}
23
24		// updateArnableField updates the value of the input field that
25		// takes an ARN as an input. This method is useful to backfill
26		// the parsed resource name from ARN into the input member.
27		// It returns a pointer to a modified copy of input and an error.
28		// Note that original input is not modified.
29		func (s {{ $.ShapeName }}) updateArnableField(v string) (interface{}, error) {
30			if s.{{ $name }} == nil {
31				return nil, fmt.Errorf("member {{ $name }} is nil")
32			}
33			s.{{ $name }} = aws.String(v)
34			return &s, nil
35		}
36	{{ end -}}
37{{ end }}
38{{ end }}
39`
40
41var endpointARNShapeTmpl = template.Must(
42	template.New("endpointARNShapeTmpl").
43		Parse(endpointARNShapeTmplDef),
44)
45
46const outpostIDShapeTmplDef = `
47{{- define "outpostIDShapeTmpl" }}
48{{ range $_, $name := $.MemberNames -}}
49	{{ $elem := index $.MemberRefs $name -}}
50	{{ if $elem.OutpostIDMember -}}
51		func (s *{{ $.ShapeName }}) getOutpostID() (string, error) {
52			if s.{{ $name }} == nil {
53				return "", fmt.Errorf("member {{ $name }} is nil")
54			}
55			return *s.{{ $name }}, nil
56		}
57
58		func (s *{{ $.ShapeName }}) hasOutpostID() bool {
59			if s.{{ $name }} == nil {
60				return false
61			}
62			return true
63		}
64	{{ end -}}
65{{ end }}
66{{ end }}
67`
68
69var outpostIDShapeTmpl = template.Must(
70	template.New("outpostIDShapeTmpl").
71		Parse(outpostIDShapeTmplDef),
72)
73
74const accountIDWithARNShapeTmplDef = `
75{{- define "accountIDWithARNShapeTmpl" }}
76{{ range $_, $name := $.MemberNames -}}
77	{{ $elem := index $.MemberRefs $name -}}
78	{{ if $elem.AccountIDMemberWithARN -}}
79		// updateAccountID returns a pointer to a modified copy of input,
80		// if account id is not provided, we update the account id in modified input
81		// if account id is provided, but doesn't match with the one in ARN, we throw an error
82		// if account id is not updated, we return nil. Note that original input is not modified.
83		func (s {{ $.ShapeName }}) updateAccountID(accountId string) (interface{}, error) {
84			if s.{{ $name }} == nil {
85				s.{{ $name }} = aws.String(accountId)
86				return &s, nil
87			} else if *s.{{ $name }} != accountId  {
88				return &s, fmt.Errorf("Account ID mismatch, the Account ID cannot be specified in an ARN and in the accountId field")
89			}
90			return nil, nil
91		}
92	{{ end -}}
93{{ end }}
94{{ end }}
95`
96
97var accountIDWithARNShapeTmpl = template.Must(
98	template.New("accountIDWithARNShapeTmpl").
99		Parse(accountIDWithARNShapeTmplDef),
100)
101