14 // A CodeGenOptions are the options for code generating the endpoints into
15 // Go code from the endpoints model definition.
16 type CodeGenOptions struct {
17 // Options for how the model will be decoded.
18 DecodeModelOptions DecodeModelOptions
20 // Disables code generation of the service endpoint prefix IDs defined in
22 DisableGenerateServiceIDs bool
25 // Set combines all of the option functions together
26 func (d *CodeGenOptions) Set(optFns ...func(*CodeGenOptions)) {
27 for _, fn := range optFns {
32 // CodeGenModel given a endpoints model file will decode it and attempt to
33 // generate Go code from the model definition. Error will be returned if
34 // the code is unable to be generated, or decoded.
35 func CodeGenModel(modelFile io.Reader, outFile io.Writer, optFns ...func(*CodeGenOptions)) error {
36 var opts CodeGenOptions
39 resolver, err := DecodeModel(modelFile, func(d *DecodeModelOptions) {
40 *d = opts.DecodeModelOptions
54 tmpl := template.Must(template.New("tmpl").Funcs(funcMap).Parse(v3Tmpl))
55 if err := tmpl.ExecuteTemplate(outFile, "defaults", v); err != nil {
56 return fmt.Errorf("failed to execute template, %v", err)
62 func toSymbol(v string) string {
64 for _, c := range strings.Title(v) {
65 if !(unicode.IsNumber(c) || unicode.IsLetter(c)) {
75 func quoteString(v string) string {
76 return fmt.Sprintf("%q", v)
79 func regionConstName(p, r string) string {
80 return toSymbol(p) + toSymbol(r)
83 func partitionGetter(id string) string {
84 return fmt.Sprintf("%sPartition", toSymbol(id))
87 func partitionVarName(id string) string {
88 return fmt.Sprintf("%sPartition", strings.ToLower(toSymbol(id)))
91 func listPartitionNames(ps partitions) string {
97 return fmt.Sprintf("%s and %s", ps[0].Name, ps[1].Name)
99 for i, p := range ps {
101 names = append(names, "and "+p.Name)
103 names = append(names, p.Name)
106 return strings.Join(names, ", ")
110 func boxedBoolIfSet(msg string, v boxedBool) string {
113 return fmt.Sprintf(msg, "boxedTrue")
115 return fmt.Sprintf(msg, "boxedFalse")
121 func stringIfSet(msg, v string) string {
126 return fmt.Sprintf(msg, v)
129 func stringSliceIfSet(msg string, vs []string) string {
135 for _, v := range vs {
136 names = append(names, `"`+v+`"`)
139 return fmt.Sprintf(msg, strings.Join(names, ","))
142 func endpointIsSet(v endpoint) bool {
143 return !reflect.DeepEqual(v, endpoint{})
146 func serviceSet(ps partitions) map[string]struct{} {
147 set := map[string]struct{}{}
148 for _, p := range ps {
149 for id := range p.Services {
157 var funcMap = template.FuncMap{
158 "ToSymbol": toSymbol,
159 "QuoteString": quoteString,
160 "RegionConst": regionConstName,
161 "PartitionGetter": partitionGetter,
162 "PartitionVarName": partitionVarName,
163 "ListPartitionNames": listPartitionNames,
164 "BoxedBoolIfSet": boxedBoolIfSet,
165 "StringIfSet": stringIfSet,
166 "StringSliceIfSet": stringSliceIfSet,
167 "EndpointIsSet": endpointIsSet,
168 "ServicesSet": serviceSet,
172 {{ define "defaults" -}}
173 // Code generated by aws/endpoints/v3model_codegen.go. DO NOT EDIT.
181 {{ template "partition consts" $.Resolver }}
183 {{ range $_, $partition := $.Resolver }}
184 {{ template "partition region consts" $partition }}
187 {{ if not $.DisableGenerateServiceIDs -}}
188 {{ template "service consts" $.Resolver }}
191 {{ template "endpoint resolvers" $.Resolver }}
194 {{ define "partition consts" }}
195 // Partition identifiers
197 {{ range $_, $p := . -}}
198 {{ ToSymbol $p.ID }}PartitionID = {{ QuoteString $p.ID }} // {{ $p.Name }} partition.
203 {{ define "partition region consts" }}
204 // {{ .Name }} partition's regions.
206 {{ range $id, $region := .Regions -}}
207 {{ ToSymbol $id }}RegionID = {{ QuoteString $id }} // {{ $region.Description }}.
212 {{ define "service consts" }}
213 // Service identifiers
215 {{ $serviceSet := ServicesSet . -}}
216 {{ range $id, $_ := $serviceSet -}}
217 {{ ToSymbol $id }}ServiceID = {{ QuoteString $id }} // {{ ToSymbol $id }}.
222 {{ define "endpoint resolvers" }}
223 // DefaultResolver returns an Endpoint resolver that will be able
224 // to resolve endpoints for: {{ ListPartitionNames . }}.
226 // Use DefaultPartitions() to get the list of the default partitions.
227 func DefaultResolver() Resolver {
228 return defaultPartitions
231 // DefaultPartitions returns a list of the partitions the SDK is bundled
232 // with. The available partitions are: {{ ListPartitionNames . }}.
234 // partitions := endpoints.DefaultPartitions
235 // for _, p := range partitions {
236 // // ... inspect partitions
238 func DefaultPartitions() []Partition {
239 return defaultPartitions.Partitions()
242 var defaultPartitions = partitions{
243 {{ range $_, $partition := . -}}
244 {{ PartitionVarName $partition.ID }},
248 {{ range $_, $partition := . -}}
249 {{ $name := PartitionGetter $partition.ID -}}
250 // {{ $name }} returns the Resolver for {{ $partition.Name }}.
251 func {{ $name }}() Partition {
252 return {{ PartitionVarName $partition.ID }}.Partition()
254 var {{ PartitionVarName $partition.ID }} = {{ template "gocode Partition" $partition }}
258 {{ define "default partitions" }}
259 func DefaultPartitions() []Partition {
261 {{ range $_, $partition := . -}}
262 // {{ ToSymbol $partition.ID}}Partition(),
268 {{ define "gocode Partition" -}}
270 {{ StringIfSet "ID: %q,\n" .ID -}}
271 {{ StringIfSet "Name: %q,\n" .Name -}}
272 {{ StringIfSet "DNSSuffix: %q,\n" .DNSSuffix -}}
273 RegionRegex: {{ template "gocode RegionRegex" .RegionRegex }},
274 {{ if EndpointIsSet .Defaults -}}
275 Defaults: {{ template "gocode Endpoint" .Defaults }},
277 Regions: {{ template "gocode Regions" .Regions }},
278 Services: {{ template "gocode Services" .Services }},
282 {{ define "gocode RegionRegex" -}}
284 Regexp: func() *regexp.Regexp{
285 reg, _ := regexp.Compile({{ QuoteString .Regexp.String }})
291 {{ define "gocode Regions" -}}
293 {{ range $id, $region := . -}}
294 "{{ $id }}": {{ template "gocode Region" $region }},
299 {{ define "gocode Region" -}}
301 {{ StringIfSet "Description: %q,\n" .Description -}}
305 {{ define "gocode Services" -}}
307 {{ range $id, $service := . -}}
308 "{{ $id }}": {{ template "gocode Service" $service }},
313 {{ define "gocode Service" -}}
315 {{ StringIfSet "PartitionEndpoint: %q,\n" .PartitionEndpoint -}}
316 {{ BoxedBoolIfSet "IsRegionalized: %s,\n" .IsRegionalized -}}
317 {{ if EndpointIsSet .Defaults -}}
318 Defaults: {{ template "gocode Endpoint" .Defaults -}},
321 Endpoints: {{ template "gocode Endpoints" .Endpoints }},
326 {{ define "gocode Endpoints" -}}
328 {{ range $id, $endpoint := . -}}
329 "{{ $id }}": {{ template "gocode Endpoint" $endpoint }},
334 {{ define "gocode Endpoint" -}}
336 {{ StringIfSet "Hostname: %q,\n" .Hostname -}}
337 {{ StringIfSet "SSLCommonName: %q,\n" .SSLCommonName -}}
338 {{ StringSliceIfSet "Protocols: []string{%s},\n" .Protocols -}}
339 {{ StringSliceIfSet "SignatureVersions: []string{%s},\n" .SignatureVersions -}}
340 {{ if or .CredentialScope.Region .CredentialScope.Service -}}
341 CredentialScope: credentialScope{
342 {{ StringIfSet "Region: %q,\n" .CredentialScope.Region -}}
343 {{ StringIfSet "Service: %q,\n" .CredentialScope.Service -}}
346 {{ BoxedBoolIfSet "HasDualStack: %s,\n" .HasDualStack -}}
347 {{ StringIfSet "DualStackHostname: %q,\n" .DualStackHostname -}}