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
21 // Set combines all of the option functions together
22 func (d *CodeGenOptions) Set(optFns ...func(*CodeGenOptions)) {
23 for _, fn := range optFns {
28 // CodeGenModel given a endpoints model file will decode it and attempt to
29 // generate Go code from the model definition. Error will be returned if
30 // the code is unable to be generated, or decoded.
31 func CodeGenModel(modelFile io.Reader, outFile io.Writer, optFns ...func(*CodeGenOptions)) error {
32 var opts CodeGenOptions
35 resolver, err := DecodeModel(modelFile, func(d *DecodeModelOptions) {
36 *d = opts.DecodeModelOptions
42 tmpl := template.Must(template.New("tmpl").Funcs(funcMap).Parse(v3Tmpl))
43 if err := tmpl.ExecuteTemplate(outFile, "defaults", resolver); err != nil {
44 return fmt.Errorf("failed to execute template, %v", err)
50 func toSymbol(v string) string {
52 for _, c := range strings.Title(v) {
53 if !(unicode.IsNumber(c) || unicode.IsLetter(c)) {
63 func quoteString(v string) string {
64 return fmt.Sprintf("%q", v)
67 func regionConstName(p, r string) string {
68 return toSymbol(p) + toSymbol(r)
71 func partitionGetter(id string) string {
72 return fmt.Sprintf("%sPartition", toSymbol(id))
75 func partitionVarName(id string) string {
76 return fmt.Sprintf("%sPartition", strings.ToLower(toSymbol(id)))
79 func listPartitionNames(ps partitions) string {
85 return fmt.Sprintf("%s and %s", ps[0].Name, ps[1].Name)
87 for i, p := range ps {
89 names = append(names, "and "+p.Name)
91 names = append(names, p.Name)
94 return strings.Join(names, ", ")
98 func boxedBoolIfSet(msg string, v boxedBool) string {
101 return fmt.Sprintf(msg, "boxedTrue")
103 return fmt.Sprintf(msg, "boxedFalse")
109 func stringIfSet(msg, v string) string {
114 return fmt.Sprintf(msg, v)
117 func stringSliceIfSet(msg string, vs []string) string {
123 for _, v := range vs {
124 names = append(names, `"`+v+`"`)
127 return fmt.Sprintf(msg, strings.Join(names, ","))
130 func endpointIsSet(v endpoint) bool {
131 return !reflect.DeepEqual(v, endpoint{})
134 func serviceSet(ps partitions) map[string]struct{} {
135 set := map[string]struct{}{}
136 for _, p := range ps {
137 for id := range p.Services {
145 var funcMap = template.FuncMap{
146 "ToSymbol": toSymbol,
147 "QuoteString": quoteString,
148 "RegionConst": regionConstName,
149 "PartitionGetter": partitionGetter,
150 "PartitionVarName": partitionVarName,
151 "ListPartitionNames": listPartitionNames,
152 "BoxedBoolIfSet": boxedBoolIfSet,
153 "StringIfSet": stringIfSet,
154 "StringSliceIfSet": stringSliceIfSet,
155 "EndpointIsSet": endpointIsSet,
156 "ServicesSet": serviceSet,
160 {{ define "defaults" -}}
161 // Code generated by aws/endpoints/v3model_codegen.go. DO NOT EDIT.
169 {{ template "partition consts" . }}
171 {{ range $_, $partition := . }}
172 {{ template "partition region consts" $partition }}
175 {{ template "service consts" . }}
177 {{ template "endpoint resolvers" . }}
180 {{ define "partition consts" }}
181 // Partition identifiers
183 {{ range $_, $p := . -}}
184 {{ ToSymbol $p.ID }}PartitionID = {{ QuoteString $p.ID }} // {{ $p.Name }} partition.
189 {{ define "partition region consts" }}
190 // {{ .Name }} partition's regions.
192 {{ range $id, $region := .Regions -}}
193 {{ ToSymbol $id }}RegionID = {{ QuoteString $id }} // {{ $region.Description }}.
198 {{ define "service consts" }}
199 // Service identifiers
201 {{ $serviceSet := ServicesSet . -}}
202 {{ range $id, $_ := $serviceSet -}}
203 {{ ToSymbol $id }}ServiceID = {{ QuoteString $id }} // {{ ToSymbol $id }}.
208 {{ define "endpoint resolvers" }}
209 // DefaultResolver returns an Endpoint resolver that will be able
210 // to resolve endpoints for: {{ ListPartitionNames . }}.
212 // Use DefaultPartitions() to get the list of the default partitions.
213 func DefaultResolver() Resolver {
214 return defaultPartitions
217 // DefaultPartitions returns a list of the partitions the SDK is bundled
218 // with. The available partitions are: {{ ListPartitionNames . }}.
220 // partitions := endpoints.DefaultPartitions
221 // for _, p := range partitions {
222 // // ... inspect partitions
224 func DefaultPartitions() []Partition {
225 return defaultPartitions.Partitions()
228 var defaultPartitions = partitions{
229 {{ range $_, $partition := . -}}
230 {{ PartitionVarName $partition.ID }},
234 {{ range $_, $partition := . -}}
235 {{ $name := PartitionGetter $partition.ID -}}
236 // {{ $name }} returns the Resolver for {{ $partition.Name }}.
237 func {{ $name }}() Partition {
238 return {{ PartitionVarName $partition.ID }}.Partition()
240 var {{ PartitionVarName $partition.ID }} = {{ template "gocode Partition" $partition }}
244 {{ define "default partitions" }}
245 func DefaultPartitions() []Partition {
247 {{ range $_, $partition := . -}}
248 // {{ ToSymbol $partition.ID}}Partition(),
254 {{ define "gocode Partition" -}}
256 {{ StringIfSet "ID: %q,\n" .ID -}}
257 {{ StringIfSet "Name: %q,\n" .Name -}}
258 {{ StringIfSet "DNSSuffix: %q,\n" .DNSSuffix -}}
259 RegionRegex: {{ template "gocode RegionRegex" .RegionRegex }},
260 {{ if EndpointIsSet .Defaults -}}
261 Defaults: {{ template "gocode Endpoint" .Defaults }},
263 Regions: {{ template "gocode Regions" .Regions }},
264 Services: {{ template "gocode Services" .Services }},
268 {{ define "gocode RegionRegex" -}}
270 Regexp: func() *regexp.Regexp{
271 reg, _ := regexp.Compile({{ QuoteString .Regexp.String }})
277 {{ define "gocode Regions" -}}
279 {{ range $id, $region := . -}}
280 "{{ $id }}": {{ template "gocode Region" $region }},
285 {{ define "gocode Region" -}}
287 {{ StringIfSet "Description: %q,\n" .Description -}}
291 {{ define "gocode Services" -}}
293 {{ range $id, $service := . -}}
294 "{{ $id }}": {{ template "gocode Service" $service }},
299 {{ define "gocode Service" -}}
301 {{ StringIfSet "PartitionEndpoint: %q,\n" .PartitionEndpoint -}}
302 {{ BoxedBoolIfSet "IsRegionalized: %s,\n" .IsRegionalized -}}
303 {{ if EndpointIsSet .Defaults -}}
304 Defaults: {{ template "gocode Endpoint" .Defaults -}},
307 Endpoints: {{ template "gocode Endpoints" .Endpoints }},
312 {{ define "gocode Endpoints" -}}
314 {{ range $id, $endpoint := . -}}
315 "{{ $id }}": {{ template "gocode Endpoint" $endpoint }},
320 {{ define "gocode Endpoint" -}}
322 {{ StringIfSet "Hostname: %q,\n" .Hostname -}}
323 {{ StringIfSet "SSLCommonName: %q,\n" .SSLCommonName -}}
324 {{ StringSliceIfSet "Protocols: []string{%s},\n" .Protocols -}}
325 {{ StringSliceIfSet "SignatureVersions: []string{%s},\n" .SignatureVersions -}}
326 {{ if or .CredentialScope.Region .CredentialScope.Service -}}
327 CredentialScope: credentialScope{
328 {{ StringIfSet "Region: %q,\n" .CredentialScope.Region -}}
329 {{ StringIfSet "Service: %q,\n" .CredentialScope.Service -}}
332 {{ BoxedBoolIfSet "HasDualStack: %s,\n" .HasDualStack -}}
333 {{ StringIfSet "DualStackHostname: %q,\n" .DualStackHostname -}}