-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
gen_method.go
341 lines (297 loc) 路 9.2 KB
/
gen_method.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
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
335
336
337
338
339
340
341
// Copyright 2016-2021, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Pulling out some of the repeated strings tokens into constants would harm readability, so we just ignore the
// goconst linter's warning.
//
// nolint: lll, goconst
package docs
import (
"bytes"
"fmt"
"sort"
"strings"
"github.com/pulumi/pulumi/pkg/v3/codegen/python"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)
type methodDocArgs struct {
Title string
ResourceName string
DeprecationMessage string
Comment string
ExamplesSection []exampleSection
// MethodName is a map of the language and the method name in that language.
MethodName map[string]string
// MethodArgs is map per language view of the parameters
// in the method.
MethodArgs map[string]string
// MethodResult is a map per language property types
// that is returned as a result of calling a method.
MethodResult map[string]propertyType
// InputProperties is a map per language and the corresponding slice
// of input properties accepted by the method.
InputProperties map[string][]property
// OutputProperties is a map per language and the corresponding slice
// of output properties, which are properties of the MethodResult type.
OutputProperties map[string][]property
}
func (mod *modContext) genMethods(r *schema.Resource) []methodDocArgs {
methods := make([]methodDocArgs, 0, len(r.Methods))
for _, m := range r.Methods {
methods = append(methods, mod.genMethod(r, m))
}
return methods
}
func (mod *modContext) genMethod(r *schema.Resource, m *schema.Method) methodDocArgs {
dctx := mod.docGenContext
f := m.Function
inputProps, outputProps := make(map[string][]property), make(map[string][]property)
for _, lang := range dctx.supportedLanguages {
if f.Inputs != nil {
exclude := func(name string) bool {
return name == "__self__"
}
props := mod.getPropertiesWithIDPrefixAndExclude(f.Inputs.Properties, lang, true, false, false,
fmt.Sprintf("%s_arg_", m.Name), exclude)
if len(props) > 0 {
inputProps[lang] = props
}
}
if f.ReturnType != nil {
if objectType, ok := f.ReturnType.(*schema.ObjectType); ok && objectType != nil {
outputProps[lang] = mod.getPropertiesWithIDPrefixAndExclude(objectType.Properties, lang, false, false, false,
fmt.Sprintf("%s_result_", m.Name), nil)
}
}
}
// Generate the per-language map for the method name.
methodNameMap := map[string]string{}
for _, lang := range dctx.supportedLanguages {
docHelper := dctx.getLanguageDocHelper(lang)
methodNameMap[lang] = docHelper.GetMethodName(m)
}
docInfo := dctx.decomposeDocstring(f.Comment)
args := methodDocArgs{
Title: title(m.Name, ""),
ResourceName: resourceName(r),
MethodName: methodNameMap,
MethodArgs: mod.genMethodArgs(r, m, methodNameMap),
MethodResult: mod.getMethodResult(r, m),
Comment: docInfo.description,
DeprecationMessage: f.DeprecationMessage,
ExamplesSection: docInfo.examples,
InputProperties: inputProps,
OutputProperties: outputProps,
}
return args
}
func (mod *modContext) genMethodTS(f *schema.Function, resourceName, methodName string,
optionalArgs bool) []formalParam {
argsType := fmt.Sprintf("%s.%sArgs", resourceName, title(methodName, "nodejs"))
var optionalFlag string
if optionalArgs {
optionalFlag = "?"
}
var params []formalParam
if f.Inputs != nil {
params = append(params, formalParam{
Name: "args",
OptionalFlag: optionalFlag,
Type: propertyType{
Name: argsType,
},
})
}
return params
}
func (mod *modContext) genMethodGo(f *schema.Function, resourceName, methodName string,
optionalArgs bool) []formalParam {
argsType := fmt.Sprintf("%s%sArgs", resourceName, title(methodName, "go"))
params := []formalParam{
{
Name: "ctx",
OptionalFlag: "*",
Type: propertyType{
Name: "Context",
Link: "https://pkg.go.dev/github.com/pulumi/pulumi/sdk/v3/go/pulumi?tab=doc#Context",
},
},
}
var optionalFlag string
if optionalArgs {
optionalFlag = "*"
}
if f.Inputs != nil {
params = append(params, formalParam{
Name: "args",
OptionalFlag: optionalFlag,
Type: propertyType{
Name: argsType,
},
})
}
return params
}
func (mod *modContext) genMethodCS(f *schema.Function, resourceName, methodName string, optionalArgs bool) []formalParam {
argsType := fmt.Sprintf("%s.%sArgs", resourceName, title(methodName, "csharp"))
var params []formalParam
if f.Inputs != nil {
var optionalFlag string
if optionalArgs {
optionalFlag = "?"
}
params = append(params, formalParam{
Name: "args",
OptionalFlag: optionalFlag,
DefaultValue: "",
Type: propertyType{
Name: argsType,
},
})
}
return params
}
func (mod *modContext) genMethodPython(f *schema.Function) []formalParam {
dctx := mod.docGenContext
docLanguageHelper := dctx.getLanguageDocHelper("python")
var params []formalParam
params = append(params, formalParam{
Name: "self",
})
if f.Inputs != nil {
// Filter out the __self__ argument from the inputs.
args := make([]*schema.Property, 0, len(f.Inputs.InputShape.Properties)-1)
for _, arg := range f.Inputs.InputShape.Properties {
if arg.Name == "__self__" {
continue
}
args = append(args, arg)
}
// Sort required args first.
sort.Slice(args, func(i, j int) bool {
pi, pj := args[i], args[j]
switch {
case pi.IsRequired() != pj.IsRequired():
return pi.IsRequired() && !pj.IsRequired()
default:
return pi.Name < pj.Name
}
})
for _, arg := range args {
def, err := mod.pkg.Definition()
contract.AssertNoError(err)
typ := docLanguageHelper.GetLanguageTypeString(def, mod.mod, arg.Type, true /*input*/)
var defaultValue string
if !arg.IsRequired() {
defaultValue = " = None"
}
params = append(params, formalParam{
Name: python.PyName(arg.Name),
DefaultValue: defaultValue,
Type: propertyType{
Name: typ,
},
})
}
}
return params
}
// genMethodArgs generates the arguments string for a given method that can be
// rendered directly into a template. An empty string indicates no args.
func (mod *modContext) genMethodArgs(r *schema.Resource, m *schema.Method,
methodNameMap map[string]string) map[string]string {
dctx := mod.docGenContext
f := m.Function
functionParams := make(map[string]string)
for _, lang := range dctx.supportedLanguages {
var (
paramTemplate string
params []formalParam
)
b := &bytes.Buffer{}
paramSeparatorTemplate := "param_separator"
ps := paramSeparator{}
var hasArgs bool
optionalArgs := true
if f.Inputs != nil {
for _, arg := range f.Inputs.InputShape.Properties {
if arg.Name == "__self__" {
continue
}
hasArgs = true
if arg.IsRequired() {
optionalArgs = false
}
}
}
if !hasArgs {
functionParams[lang] = ""
continue
}
switch lang {
case "nodejs":
params = mod.genMethodTS(f, resourceName(r), methodNameMap["nodejs"], optionalArgs)
paramTemplate = "ts_formal_param"
case "go":
params = mod.genMethodGo(f, resourceName(r), methodNameMap["go"], optionalArgs)
paramTemplate = "go_formal_param"
case "csharp":
params = mod.genMethodCS(f, resourceName(r), methodNameMap["csharp"], optionalArgs)
paramTemplate = "csharp_formal_param"
case "python":
params = mod.genMethodPython(f)
paramTemplate = "py_formal_param"
paramSeparatorTemplate = "py_param_separator"
docHelper := dctx.getLanguageDocHelper(lang)
methodName := docHelper.GetMethodName(m)
ps = paramSeparator{Indent: strings.Repeat(" ", len("def (")+len(methodName))}
}
n := len(params)
if n == 0 {
functionParams[lang] = ""
continue
}
for i, p := range params {
if err := dctx.templates.ExecuteTemplate(b, paramTemplate, p); err != nil {
panic(err)
}
if i != n-1 {
if err := dctx.templates.ExecuteTemplate(b, paramSeparatorTemplate, ps); err != nil {
panic(err)
}
}
}
functionParams[lang] = b.String()
}
return functionParams
}
// getMethodResult returns a map of per-language information about the method result.
// An empty propertyType.Name indicates no result.
func (mod *modContext) getMethodResult(r *schema.Resource, m *schema.Method) map[string]propertyType {
resourceMap := make(map[string]propertyType)
dctx := mod.docGenContext
var resultTypeName string
for _, lang := range dctx.supportedLanguages {
if m.Function.ReturnType != nil {
def, err := mod.pkg.Definition()
contract.AssertNoError(err)
resultTypeName = dctx.getLanguageDocHelper(lang).GetMethodResultName(def, mod.mod, r, m)
}
resourceMap[lang] = propertyType{
Name: resultTypeName,
Link: fmt.Sprintf("#method_%s_result", title(m.Name, "")),
}
}
return resourceMap
}