pulumi/pkg/codegen/docs/gen_method.go

357 lines
10 KiB
Go
Raw Normal View History

// 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.
//
sdk/go: Remove 'nolint' directives from package docs Go treats comments that match the following regex as directives. //[a-z0-9]+:[a-z0-9] Comments that are directives don't show in an entity's documentation. https://github.com/golang/go/commit/5a550b695117f07a4f2454039a4871250cd3ed09#diff-f56160fd9fcea272966a8a1d692ad9f49206fdd8dbcbfe384865a98cd9bc2749R165 Our code has `//nolint` directives that now show in the API Reference. This is because these directives are in one of the following forms, which don't get this special treatment. // nolint:foo //nolint: foo This change fixes all such directives found by the regex: `// nolint|//nolint: `. See bottom of commit for command used for the fix. Verification: Here's the output of `go doc` on some entities before and after this change. Before ``` % go doc github.com/pulumi/pulumi/sdk/v3/go/pulumi | head -n8 package pulumi // import "github.com/pulumi/pulumi/sdk/v3/go/pulumi" nolint: lll, interfacer nolint: lll, interfacer const EnvOrganization = "PULUMI_ORGANIZATION" ... var ErrPlugins = errors.New("pulumi: plugins requested") ``` After ``` % go doc github.com/pulumi/pulumi/sdk/v3/go/pulumi | head -n8 package pulumi // import "github.com/pulumi/pulumi/sdk/v3/go/pulumi" const EnvOrganization = "PULUMI_ORGANIZATION" ... var ErrPlugins = errors.New("pulumi: plugins requested") func BoolRef(v bool) *bool func Float64Ref(v float64) *float64 func IntRef(v int) *int func IsSecret(o Output) bool ``` Before ``` % go doc github.com/pulumi/pulumi/sdk/v3/go/pulumi URN_ package pulumi // import "github.com/pulumi/pulumi/sdk/v3/go/pulumi" func URN_(o string) ResourceOption URN_ is an optional URN of a previously-registered resource of this type to read from the engine. nolint: revive ``` After: ``` % go doc github.com/pulumi/pulumi/sdk/v3/go/pulumi URN_ package pulumi // import "github.com/pulumi/pulumi/sdk/v3/go/pulumi" func URN_(o string) ResourceOption URN_ is an optional URN of a previously-registered resource of this type to read from the engine. ``` Note that golangci-lint offers a 'nolintlint' linter that finds such miuses of nolint, but it also finds other issues so I've deferred that to a follow up PR. Resolves #11785 Related: https://github.com/golangci/golangci-lint/issues/892 [git-generate] FILES=$(mktemp) rg -l '// nolint|//nolint: ' | tee "$FILES" | xargs perl -p -i -e ' s|// nolint|//nolint|g; s|//nolint: |//nolint:|g; ' rg '.go$' < "$FILES" | xargs gofmt -w -s
2023-01-06 00:07:45 +00:00
//nolint:lll, goconst
package docs
import (
"bytes"
"fmt"
"sort"
"strings"
Add ability to constrain supported languages of resource and function overlays (#16579) The existing overlays (e.g. Chart v3 in Kubernetes, or CallbackFunction in AWS) are not available in every language Pulumi supports. This often confuses users because the generated docs include all languages Pulumi supports (e.g. see https://github.com/pulumi/pulumi-kubernetes/issues/2181). To solve that problem, this change adds a new optional parameter to the schema that allows configuring the languages an overlay (resource or function) supports. To support this in docsgen the existing Language Chooser (`LangChooserLanguages`) of resources is made configurable and extended to functions. Note: This doesn't support resource methods right now. They'll need extra handling because and overlay resource method might not support all of the languages its resource supports. I'll tackle this in a follow up PR. Here's a screenshot of how this will look like for the Helm v3 chart for example: <img width="1046" alt="Screenshot 2024-07-01 at 16 11 23" src="https://github.com/pulumi/pulumi/assets/2453580/b1a1365a-6dee-4099-829a-2859639a4c8c"> The PR contains the following commits. I'd recommend to look at the first three ones and then check the regenerated golden files in the last one: - **Add schema parameter to constrain supported languages for overlays** - **Update developer docs and changelog** - **Refactor LanguageChooser and always pass supported languages** - **Regenerate testdata** relates to #13231
2024-07-09 14:54:50 +00:00
"github.com/golang/glog"
"github.com/pulumi/pulumi/pkg/v3/codegen/python"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/slice"
2022-12-12 14:55:35 +00:00
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)
type methodDocArgs struct {
Title string
ResourceName string
DeprecationMessage string
Comment string
Add ability to constrain supported languages of resource and function overlays (#16579) The existing overlays (e.g. Chart v3 in Kubernetes, or CallbackFunction in AWS) are not available in every language Pulumi supports. This often confuses users because the generated docs include all languages Pulumi supports (e.g. see https://github.com/pulumi/pulumi-kubernetes/issues/2181). To solve that problem, this change adds a new optional parameter to the schema that allows configuring the languages an overlay (resource or function) supports. To support this in docsgen the existing Language Chooser (`LangChooserLanguages`) of resources is made configurable and extended to functions. Note: This doesn't support resource methods right now. They'll need extra handling because and overlay resource method might not support all of the languages its resource supports. I'll tackle this in a follow up PR. Here's a screenshot of how this will look like for the Helm v3 chart for example: <img width="1046" alt="Screenshot 2024-07-01 at 16 11 23" src="https://github.com/pulumi/pulumi/assets/2453580/b1a1365a-6dee-4099-829a-2859639a4c8c"> The PR contains the following commits. I'd recommend to look at the first three ones and then check the regenerated golden files in the last one: - **Add schema parameter to constrain supported languages for overlays** - **Update developer docs and changelog** - **Refactor LanguageChooser and always pass supported languages** - **Regenerate testdata** relates to #13231
2024-07-09 14:54:50 +00:00
ExamplesSection examplesSection
// 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 := slice.Prealloc[methodDocArgs](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__"
}
2021-08-13 00:20:13 +00:00
props := mod.getPropertiesWithIDPrefixAndExclude(f.Inputs.Properties, lang, true, false, false,
Enable perfsprint linter (#14813) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Prompted by a comment in another review: https://github.com/pulumi/pulumi/pull/14654#discussion_r1419995945 This lints that we don't use `fmt.Errorf` when `errors.New` will suffice, it also covers a load of other cases where `Sprintf` is sub-optimal. Most of these edits were made by running `perfsprint --fix`. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-12 12:19:42 +00:00
m.Name+"_arg_", 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,
Enable perfsprint linter (#14813) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Prompted by a comment in another review: https://github.com/pulumi/pulumi/pull/14654#discussion_r1419995945 This lints that we don't use `fmt.Errorf` when `errors.New` will suffice, it also covers a load of other cases where `Sprintf` is sub-optimal. Most of these edits were made by running `perfsprint --fix`. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-12 12:19:42 +00:00
m.Name+"_result_", 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)
}
Add ability to constrain supported languages of resource and function overlays (#16579) The existing overlays (e.g. Chart v3 in Kubernetes, or CallbackFunction in AWS) are not available in every language Pulumi supports. This often confuses users because the generated docs include all languages Pulumi supports (e.g. see https://github.com/pulumi/pulumi-kubernetes/issues/2181). To solve that problem, this change adds a new optional parameter to the schema that allows configuring the languages an overlay (resource or function) supports. To support this in docsgen the existing Language Chooser (`LangChooserLanguages`) of resources is made configurable and extended to functions. Note: This doesn't support resource methods right now. They'll need extra handling because and overlay resource method might not support all of the languages its resource supports. I'll tackle this in a follow up PR. Here's a screenshot of how this will look like for the Helm v3 chart for example: <img width="1046" alt="Screenshot 2024-07-01 at 16 11 23" src="https://github.com/pulumi/pulumi/assets/2453580/b1a1365a-6dee-4099-829a-2859639a4c8c"> The PR contains the following commits. I'd recommend to look at the first three ones and then check the regenerated golden files in the last one: - **Add schema parameter to constrain supported languages for overlays** - **Update developer docs and changelog** - **Refactor LanguageChooser and always pass supported languages** - **Regenerate testdata** relates to #13231
2024-07-09 14:54:50 +00:00
defer glog.Flush()
resourceSnippetLanguages := mod.docGenContext.getSupportedSnippetLanguages(r.IsOverlay, r.OverlaySupportedLanguages)
methodSnippetLanguages := mod.docGenContext.getSupportedSnippetLanguages(f.IsOverlay, f.OverlaySupportedLanguages)
if resourceSnippetLanguages != methodSnippetLanguages {
// All code choosers are tied together. If the method doesn't support the same languages as the resource, we
// default to the resource's supported languages for consistency.
glog.Warningf("Resource %s (%s) and method %s (%s) have different supported snippet languages. Defaulting to the resources supported snippet languages.",
r.Token, resourceSnippetLanguages, m.Name, methodSnippetLanguages)
}
docInfo := dctx.decomposeDocstring(f.Comment, resourceSnippetLanguages)
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,
Add ability to constrain supported languages of resource and function overlays (#16579) The existing overlays (e.g. Chart v3 in Kubernetes, or CallbackFunction in AWS) are not available in every language Pulumi supports. This often confuses users because the generated docs include all languages Pulumi supports (e.g. see https://github.com/pulumi/pulumi-kubernetes/issues/2181). To solve that problem, this change adds a new optional parameter to the schema that allows configuring the languages an overlay (resource or function) supports. To support this in docsgen the existing Language Chooser (`LangChooserLanguages`) of resources is made configurable and extended to functions. Note: This doesn't support resource methods right now. They'll need extra handling because and overlay resource method might not support all of the languages its resource supports. I'll tackle this in a follow up PR. Here's a screenshot of how this will look like for the Helm v3 chart for example: <img width="1046" alt="Screenshot 2024-07-01 at 16 11 23" src="https://github.com/pulumi/pulumi/assets/2453580/b1a1365a-6dee-4099-829a-2859639a4c8c"> The PR contains the following commits. I'd recommend to look at the first three ones and then check the regenerated golden files in the last one: - **Add schema parameter to constrain supported languages for overlays** - **Update developer docs and changelog** - **Refactor LanguageChooser and always pass supported languages** - **Regenerate testdata** relates to #13231
2024-07-09 14:54:50 +00:00
ExamplesSection: examplesSection{
Examples: docInfo.examples,
LangChooserLanguages: resourceSnippetLanguages,
},
InputProperties: inputProps,
OutputProperties: outputProps,
}
return args
}
func (mod *modContext) genMethodTS(f *schema.Function, resourceName, methodName string,
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
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,
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
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 := slice.Prealloc[*schema.Property](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 {
2022-12-12 14:55:35 +00:00
def, err := mod.pkg.Definition()
contract.AssertNoErrorf(err, "failed to get definition for package %q", mod.pkg.Name())
2022-12-12 14:55:35 +00:00
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,
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
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 {
2022-12-12 14:55:35 +00:00
def, err := mod.pkg.Definition()
contract.AssertNoErrorf(err, "failed to get definition for package %q", mod.pkg.Name())
2022-12-12 14:55:35 +00:00
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
}