aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/aws/aws-sdk-go/aws/endpoints/v3model_codegen.go
blob: 1e7369dbfd1c31c5fd40b0000809141d81e8d91c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// +build codegen

package endpoints

import (
	"fmt"
	"io"
	"reflect"
	"strings"
	"text/template"
	"unicode"
)

// A CodeGenOptions are the options for code generating the endpoints into
// Go code from the endpoints model definition.
type CodeGenOptions struct {
	// Options for how the model will be decoded.
	DecodeModelOptions DecodeModelOptions
}

// Set combines all of the option functions together
func (d *CodeGenOptions) Set(optFns ...func(*CodeGenOptions)) {
	for _, fn := range optFns {
		fn(d)
	}
}

// CodeGenModel given a endpoints model file will decode it and attempt to
// generate Go code from the model definition. Error will be returned if
// the code is unable to be generated, or decoded.
func CodeGenModel(modelFile io.Reader, outFile io.Writer, optFns ...func(*CodeGenOptions)) error {
	var opts CodeGenOptions
	opts.Set(optFns...)

	resolver, err := DecodeModel(modelFile, func(d *DecodeModelOptions) {
		*d = opts.DecodeModelOptions
	})
	if err != nil {
		return err
	}

	tmpl := template.Must(template.New("tmpl").Funcs(funcMap).Parse(v3Tmpl))
	if err := tmpl.ExecuteTemplate(outFile, "defaults", resolver); err != nil {
		return fmt.Errorf("failed to execute template, %v", err)
	}

	return nil
}

func toSymbol(v string) string {
	out := []rune{}
	for _, c := range strings.Title(v) {
		if !(unicode.IsNumber(c) || unicode.IsLetter(c)) {
			continue
		}

		out = append(out, c)
	}

	return string(out)
}

func quoteString(v string) string {
	return fmt.Sprintf("%q", v)
}

func regionConstName(p, r string) string {
	return toSymbol(p) + toSymbol(r)
}

func partitionGetter(id string) string {
	return fmt.Sprintf("%sPartition", toSymbol(id))
}

func partitionVarName(id string) string {
	return fmt.Sprintf("%sPartition", strings.ToLower(toSymbol(id)))
}

func listPartitionNames(ps partitions) string {
	names := []string{}
	switch len(ps) {
	case 1:
		return ps[0].Name
	case 2:
		return fmt.Sprintf("%s and %s", ps[0].Name, ps[1].Name)
	default:
		for i, p := range ps {
			if i == len(ps)-1 {
				names = append(names, "and "+p.Name)
			} else {
				names = append(names, p.Name)
			}
		}
		return strings.Join(names, ", ")
	}
}

func boxedBoolIfSet(msg string, v boxedBool) string {
	switch v {
	case boxedTrue:
		return fmt.Sprintf(msg, "boxedTrue")
	case boxedFalse:
		return fmt.Sprintf(msg, "boxedFalse")
	default:
		return ""
	}
}

func stringIfSet(msg, v string) string {
	if len(v) == 0 {
		return ""
	}

	return fmt.Sprintf(msg, v)
}

func stringSliceIfSet(msg string, vs []string) string {
	if len(vs) == 0 {
		return ""
	}

	names := []string{}
	for _, v := range vs {
		names = append(names, `"`+v+`"`)
	}

	return fmt.Sprintf(msg, strings.Join(names, ","))
}

func endpointIsSet(v endpoint) bool {
	return !reflect.DeepEqual(v, endpoint{})
}

func serviceSet(ps partitions) map[string]struct{} {
	set := map[string]struct{}{}
	for _, p := range ps {
		for id := range p.Services {
			set[id] = struct{}{}
		}
	}

	return set
}

var funcMap = template.FuncMap{
	"ToSymbol":           toSymbol,
	"QuoteString":        quoteString,
	"RegionConst":        regionConstName,
	"PartitionGetter":    partitionGetter,
	"PartitionVarName":   partitionVarName,
	"ListPartitionNames": listPartitionNames,
	"BoxedBoolIfSet":     boxedBoolIfSet,
	"StringIfSet":        stringIfSet,
	"StringSliceIfSet":   stringSliceIfSet,
	"EndpointIsSet":      endpointIsSet,
	"ServicesSet":        serviceSet,
}

const v3Tmpl = `
{{ define "defaults" -}}
// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.

package endpoints

import (
	"regexp"
)

	{{ template "partition consts" . }}

	{{ range $_, $partition := . }}
		{{ template "partition region consts" $partition }}
	{{ end }}

	{{ template "service consts" . }}
	
	{{ template "endpoint resolvers" . }}
{{- end }}

{{ define "partition consts" }}
	// Partition identifiers
	const (
		{{ range $_, $p := . -}}
			{{ ToSymbol $p.ID }}PartitionID = {{ QuoteString $p.ID }} // {{ $p.Name }} partition.
		{{ end -}}
	)
{{- end }}

{{ define "partition region consts" }}
	// {{ .Name }} partition's regions.
	const (
		{{ range $id, $region := .Regions -}}
			{{ ToSymbol $id }}RegionID = {{ QuoteString $id }} // {{ $region.Description }}.
		{{ end -}}
	)
{{- end }}

{{ define "service consts" }}
	// Service identifiers
	const (
		{{ $serviceSet := ServicesSet . -}}
		{{ range $id, $_ := $serviceSet -}}
			{{ ToSymbol $id }}ServiceID = {{ QuoteString $id }} // {{ ToSymbol $id }}.
		{{ end -}}
	)
{{- end }}

{{ define "endpoint resolvers" }}
	// DefaultResolver returns an Endpoint resolver that will be able
	// to resolve endpoints for: {{ ListPartitionNames . }}.
	//
	// Casting the return value of this func to a EnumPartitions will
	// allow you to get a list of the partitions in the order the endpoints
	// will be resolved in.
	//
	//    resolver := endpoints.DefaultResolver()
	//    partitions := resolver.(endpoints.EnumPartitions).Partitions()
	//    for _, p := range partitions {
	//        // ... inspect partitions
	//    }
	func DefaultResolver() Resolver {
		return defaultPartitions
	}

	var defaultPartitions = partitions{
		{{ range $_, $partition := . -}}
			{{ PartitionVarName $partition.ID }},
		{{ end }}
	}
	
	{{ range $_, $partition := . -}}
		{{ $name := PartitionGetter $partition.ID -}}
		// {{ $name }} returns the Resolver for {{ $partition.Name }}.
		func {{ $name }}() Partition {
			return  {{ PartitionVarName $partition.ID }}.Partition()
		}
		var {{ PartitionVarName $partition.ID }} = {{ template "gocode Partition" $partition }}
	{{ end }}
{{ end }}

{{ define "default partitions" }}
	func DefaultPartitions() []Partition {
		return []partition{
			{{ range $_, $partition := . -}}
			// {{ ToSymbol $partition.ID}}Partition(),
			{{ end }}
		}
	}
{{ end }}

{{ define "gocode Partition" -}}
partition{
	{{ StringIfSet "ID: %q,\n" .ID -}}
	{{ StringIfSet "Name: %q,\n" .Name -}}
	{{ StringIfSet "DNSSuffix: %q,\n" .DNSSuffix -}}
	RegionRegex: {{ template "gocode RegionRegex" .RegionRegex }},
	{{ if EndpointIsSet .Defaults -}}
		Defaults: {{ template "gocode Endpoint" .Defaults }},
	{{- end }}
	Regions:  {{ template "gocode Regions" .Regions }},
	Services: {{ template "gocode Services" .Services }},
}
{{- end }}

{{ define "gocode RegionRegex" -}}
regionRegex{
	Regexp: func() *regexp.Regexp{
		reg, _ := regexp.Compile({{ QuoteString .Regexp.String }})
		return reg
	}(),
}
{{- end }}

{{ define "gocode Regions" -}}
regions{
	{{ range $id, $region := . -}}
		"{{ $id }}": {{ template "gocode Region" $region }},
	{{ end -}}
}
{{- end }}

{{ define "gocode Region" -}}
region{
	{{ StringIfSet "Description: %q,\n" .Description -}}
}
{{- end }}

{{ define "gocode Services" -}}
services{
	{{ range $id, $service := . -}}
	"{{ $id }}": {{ template "gocode Service" $service }},
	{{ end }}
}
{{- end }}

{{ define "gocode Service" -}}
service{
	{{ StringIfSet "PartitionEndpoint: %q,\n" .PartitionEndpoint -}}
	{{ BoxedBoolIfSet "IsRegionalized: %s,\n" .IsRegionalized -}}
	{{ if EndpointIsSet .Defaults -}}
		Defaults: {{ template "gocode Endpoint" .Defaults -}},
	{{- end }}
	{{ if .Endpoints -}}
		Endpoints: {{ template "gocode Endpoints" .Endpoints }},
	{{- end }}
}
{{- end }}

{{ define "gocode Endpoints" -}}
endpoints{
	{{ range $id, $endpoint := . -}}
	"{{ $id }}": {{ template "gocode Endpoint" $endpoint }},
	{{ end }}
}
{{- end }}

{{ define "gocode Endpoint" -}}
endpoint{
	{{ StringIfSet "Hostname: %q,\n" .Hostname -}}
	{{ StringIfSet "SSLCommonName: %q,\n" .SSLCommonName -}}
	{{ StringSliceIfSet "Protocols: []string{%s},\n" .Protocols -}}
	{{ StringSliceIfSet "SignatureVersions: []string{%s},\n" .SignatureVersions -}}
	{{ if or .CredentialScope.Region .CredentialScope.Service -}}
	CredentialScope: credentialScope{
		{{ StringIfSet "Region: %q,\n" .CredentialScope.Region -}}
		{{ StringIfSet "Service: %q,\n" .CredentialScope.Service -}}
	},
	{{- end }}
	{{ BoxedBoolIfSet "HasDualStack: %s,\n" .HasDualStack -}}
	{{ StringIfSet "DualStackHostname: %q,\n" .DualStackHostname -}}

}
{{- end }}
`