This repository has been archived by the owner on May 14, 2020. It is now read-only.
forked from aws/amazon-ecs-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
231 lines (195 loc) · 4.93 KB
/
api.go
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
package api
import (
"bytes"
"fmt"
"regexp"
"sort"
"strings"
"text/template"
"github.com/awslabs/aws-sdk-go/internal/util"
)
type API struct {
Metadata Metadata
Operations map[string]*Operation
Shapes map[string]*Shape
// Disables inflection checks. Only use this when generating tests
NoInflections bool
// Set to true to avoid removing unused shapes
NoRemoveUnusedShapes bool
initialized bool
imports map[string]bool
name string
unrecognizedNames map[string]string
}
type Metadata struct {
APIVersion string
EndpointPrefix string
ServiceAbbreviation string
ServiceFullName string
SignatureVersion string
JSONVersion string
TargetPrefix string
Protocol string
}
func (a *API) PackageName() string {
return strings.ToLower(a.StructName())
}
var nameRegex = regexp.MustCompile(`^Amazon|AWS\s*|\(.*|\s+|\W+`)
// StructName returns the service struct name for a given service
func (a *API) StructName() string {
if a.name == "" {
name := a.Metadata.ServiceAbbreviation
if name == "" {
name = a.Metadata.ServiceFullName
}
name = nameRegex.ReplaceAllString(name, "")
switch name {
case "ElasticLoadBalancing":
a.name = "ELB"
case "Config":
a.name = "ConfigService"
default:
a.name = name
}
}
return a.name
}
func (a *API) NiceName() string {
if a.Metadata.ServiceAbbreviation != "" {
return a.Metadata.ServiceAbbreviation
}
return a.Metadata.ServiceFullName
}
func (a *API) ProtocolPackage() string {
switch a.Metadata.Protocol {
case "json":
return "jsonrpc"
case "ec2":
return "ec2query"
default:
return strings.Replace(a.Metadata.Protocol, "-", "", -1)
}
}
func (a *API) OperationNames() []string {
i, names := 0, make([]string, len(a.Operations))
for n, _ := range a.Operations {
names[i] = n
i++
}
sort.Strings(names)
return names
}
func (a *API) OperationList() []*Operation {
list := make([]*Operation, len(a.Operations))
for i, n := range a.OperationNames() {
list[i] = a.Operations[n]
}
return list
}
func (a *API) ShapeNames() []string {
i, names := 0, make([]string, len(a.Shapes))
for n, _ := range a.Shapes {
names[i] = n
i++
}
sort.Strings(names)
return names
}
func (a *API) ShapeList() []*Shape {
list := make([]*Shape, len(a.Shapes))
for i, n := range a.ShapeNames() {
list[i] = a.Shapes[n]
}
return list
}
func (a *API) resetImports() {
a.imports = map[string]bool{
"github.com/awslabs/aws-sdk-go/aws": true,
}
}
func (a *API) importsGoCode() string {
if len(a.imports) == 0 {
return ""
}
corePkgs, extPkgs := []string{}, []string{}
for i, _ := range a.imports {
if strings.Contains(i, ".") {
extPkgs = append(extPkgs, i)
} else {
corePkgs = append(corePkgs, i)
}
}
sort.Strings(corePkgs)
sort.Strings(extPkgs)
code := "import (\n"
for _, i := range corePkgs {
code += fmt.Sprintf("\t%q\n", i)
}
code += "\n"
for _, i := range extPkgs {
code += fmt.Sprintf("\t%q\n", i)
}
code += ")\n\n"
return code
}
var tplAPI = template.Must(template.New("api").Parse(`
{{ range $_, $o := .OperationList }}
{{ $o.GoCode }}
{{ end }}
{{ range $_, $s := .ShapeList }}
{{ if eq $s.Type "structure" }}{{ $s.GoCode }}{{ end }}
{{ end }}
`))
func (a *API) APIGoCode() string {
a.resetImports()
var buf bytes.Buffer
err := tplAPI.Execute(&buf, a)
if err != nil {
panic(err)
}
code := a.importsGoCode() + strings.TrimSpace(buf.String())
return util.GoFmt(code)
}
var tplService = template.Must(template.New("service").Parse(`
// {{ .StructName }} is a client for {{ .NiceName }}.
type {{ .StructName }} struct {
*aws.Service
}
type {{ .StructName }}Config struct {
*aws.Config
}
// New returns a new {{ .StructName }} client.
func New(config *{{ .StructName }}Config) *{{ .StructName }} {
if config == nil {
config = &{{ .StructName }}Config{}
}
service := &aws.Service{
Config: aws.DefaultConfig.Merge(config.Config),
ServiceName: "{{ .Metadata.EndpointPrefix }}",
APIVersion: "{{ .Metadata.APIVersion }}",
{{ if eq .Metadata.Protocol "json" }}JSONVersion: "{{ .Metadata.JSONVersion }}",
TargetPrefix: "{{ .Metadata.TargetPrefix }}",
{{ end }}
}
service.Initialize()
// Handlers
service.Handlers.Sign.PushBack(v4.Sign)
service.Handlers.Build.PushBack({{ .ProtocolPackage }}.Build)
service.Handlers.Unmarshal.PushBack({{ .ProtocolPackage }}.Unmarshal)
service.Handlers.UnmarshalMeta.PushBack({{ .ProtocolPackage }}.UnmarshalMeta)
service.Handlers.UnmarshalError.PushBack({{ .ProtocolPackage }}.UnmarshalError)
return &{{ .StructName }}{service}
}
`))
func (a *API) ServiceGoCode() string {
a.resetImports()
a.imports["github.com/awslabs/aws-sdk-go/internal/signer/v4"] = true
a.imports["github.com/awslabs/aws-sdk-go/internal/protocol/"+a.ProtocolPackage()] = true
var buf bytes.Buffer
err := tplService.Execute(&buf, a)
if err != nil {
panic(err)
}
code := a.importsGoCode() + buf.String()
return util.GoFmt(code)
}