-
-
Notifications
You must be signed in to change notification settings - Fork 560
/
client_types.go
251 lines (234 loc) Β· 7.06 KB
/
client_types.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package codegen
import (
"path/filepath"
"goa.design/goa/v3/codegen"
"goa.design/goa/v3/expr"
)
// ClientTypeFiles returns the HTTP transport client types files.
func ClientTypeFiles(genpkg string, root *expr.RootExpr) []*codegen.File {
fw := make([]*codegen.File, len(root.API.HTTP.Services))
for i, svc := range root.API.HTTP.Services {
fw[i] = clientType(genpkg, svc, make(map[string]struct{}))
}
return fw
}
// clientType return the file containing the type definitions used by the HTTP
// transport for the given service client. seen keeps track of the names of the
// types that have already been generated to prevent duplicate code generation.
//
// Below are the rules governing whether values are pointers or not. Note that
// the rules only applies to values that hold primitive types, values that hold
// slices, maps or objects always use pointers either implicitly - slices and
// maps - or explicitly - objects.
//
// * The payload struct fields (if a struct) hold pointers when not required
// and have no default value.
//
// * Request and response body fields (if the body is a struct) always hold
// pointers to allow for explicit validation.
//
// * Request header, path and query string parameter variables hold pointers
// when not required. Request header, body fields and param variables that
// have default values are never required (enforced by DSL engine).
//
// * The result struct fields (if a struct) hold pointers when not required
// or have a default value (so generated code can set when null).
//
// * Response header variables hold pointers when not required and have no
// default value.
//
func clientType(genpkg string, svc *expr.HTTPServiceExpr, seen map[string]struct{}) *codegen.File {
var (
path string
data = HTTPServices.Get(svc.Name())
svcName = data.Service.PathName
)
path = filepath.Join(codegen.Gendir, "http", svcName, "client", "types.go")
imports := []*codegen.ImportSpec{
{Path: "encoding/json"},
{Path: "unicode/utf8"},
{Path: genpkg + "/" + svcName, Name: data.Service.PkgName},
{Path: genpkg + "/" + svcName + "/" + "views", Name: data.Service.ViewsPkg},
codegen.GoaImport(""),
}
imports = append(imports, data.Service.UserTypeImports...)
header := codegen.Header(svc.Name()+" HTTP client types", "client", imports)
var (
initData []*InitData
validatedTypes []*TypeData
sections = []*codegen.SectionTemplate{header}
)
// request body types
for _, a := range svc.HTTPEndpoints {
adata := data.Endpoint(a.Name())
if data := adata.Payload.Request.ClientBody; data != nil {
if _, ok := seen[data.Name]; ok {
continue
}
seen[data.Name] = struct{}{}
if data.Def != "" {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-request-body",
Source: typeDeclT,
Data: data,
})
}
if data.Init != nil {
initData = append(initData, data.Init)
}
if data.ValidateDef != "" {
validatedTypes = append(validatedTypes, data)
}
}
if adata.ClientWebSocket != nil {
if data := adata.ClientWebSocket.Payload; data != nil {
if _, ok := seen[data.Name]; ok {
continue
}
seen[data.Name] = struct{}{}
if data.Def != "" {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-request-body",
Source: typeDeclT,
Data: data,
})
}
if data.Init != nil {
initData = append(initData, data.Init)
}
if data.ValidateDef != "" {
validatedTypes = append(validatedTypes, data)
}
}
}
}
// response body types
for _, a := range svc.HTTPEndpoints {
adata := data.Endpoint(a.Name())
for _, resp := range adata.Result.Responses {
if data := resp.ClientBody; data != nil {
if _, ok := seen[data.Name]; ok {
continue
}
seen[data.Name] = struct{}{}
if data.Def != "" {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-response-body",
Source: typeDeclT,
Data: data,
})
}
if data.ValidateDef != "" {
validatedTypes = append(validatedTypes, data)
}
}
}
}
// error body types
for _, a := range svc.HTTPEndpoints {
adata := data.Endpoint(a.Name())
for _, gerr := range adata.Errors {
for _, herr := range gerr.Errors {
if data := herr.Response.ClientBody; data != nil {
if _, ok := seen[data.Name]; ok {
continue
}
seen[data.Name] = struct{}{}
if data.Def != "" {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-error-body",
Source: typeDeclT,
Data: data,
})
}
if data.ValidateDef != "" {
validatedTypes = append(validatedTypes, data)
}
}
}
}
}
for _, data := range data.ClientBodyAttributeTypes {
if data.Def != "" {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-body-attributes",
Source: typeDeclT,
Data: data,
})
}
if data.ValidateDef != "" {
validatedTypes = append(validatedTypes, data)
}
}
// body constructors
for _, init := range initData {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-body-init",
Source: clientBodyInitT,
Data: init,
})
}
for _, adata := range data.Endpoints {
// response to method result (client)
for _, resp := range adata.Result.Responses {
if init := resp.ResultInit; init != nil {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-result-init",
Source: clientTypeInitT,
Data: init,
FuncMap: map[string]interface{}{"fieldCode": fieldCode},
})
}
}
// error response to method result (client)
for _, gerr := range adata.Errors {
for _, herr := range gerr.Errors {
if init := herr.Response.ResultInit; init != nil {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-error-result-init",
Source: clientTypeInitT,
Data: init,
FuncMap: map[string]interface{}{"fieldCode": fieldCode},
})
}
}
}
}
// body attribute types
// validate methods
for _, data := range validatedTypes {
sections = append(sections, &codegen.SectionTemplate{
Name: "client-validate",
Source: validateT,
Data: data,
})
}
return &codegen.File{Path: path, SectionTemplates: sections}
}
// input: InitData
const clientBodyInitT = `{{ comment .Description }}
func {{ .Name }}({{ range .ClientArgs }}{{ .VarName }} {{.TypeRef }}, {{ end }}) {{ .ReturnTypeRef }} {
{{ .ClientCode }}
return body
}
`
// input: InitData
const clientTypeInitT = `{{ comment .Description }}
func {{ .Name }}({{- range .ClientArgs }}{{ .VarName }} {{ .TypeRef }}, {{ end }}) {{ .ReturnTypeRef }} {
{{- if .ClientCode }}
{{ .ClientCode }}
{{- if .ReturnTypeAttribute }}
res := &{{ .ReturnTypeName }}{
{{ .ReturnTypeAttribute }}: {{ if .ReturnIsPrimitivePointer }}&{{ end }}v,
}
{{- end }}
{{- end }}
{{- if .ReturnIsStruct }}
{{- if not .ClientCode }}
{{ if .ReturnTypeAttribute }}res{{ else }}v{{ end }} := &{{ .ReturnTypeName }}{}
{{- end }}
{{- end }}
{{ fieldCode . "client" }}
return {{ if .ReturnTypeAttribute }}res{{ else }}v{{ end }}
}
`