diff --git a/pkg/codegen/docs.go b/pkg/codegen/docs.go index bd4b676eb..69345e9c7 100644 --- a/pkg/codegen/docs.go +++ b/pkg/codegen/docs.go @@ -50,9 +50,11 @@ type DocLanguageHelper interface { GetDocLinkForFunctionInputOrOutputType(pkg *schema.Package, moduleName, typeName string, input bool) string GetDocLinkForBuiltInType(typeName string) string GetLanguageTypeString(pkg *schema.Package, moduleName string, t schema.Type, input, optional bool) string + + GetFunctionName(modName string, f *schema.Function) string // GetResourceFunctionResultName returns the name of the result type when a static resource function is used to lookup // an existing resource. - GetResourceFunctionResultName(resourceName string) string + GetResourceFunctionResultName(modName string, f *schema.Function) string } type exampleParts struct { diff --git a/pkg/codegen/docs/gen.go b/pkg/codegen/docs/gen.go index b5435762f..746c11666 100644 --- a/pkg/codegen/docs/gen.go +++ b/pkg/codegen/docs/gen.go @@ -80,9 +80,11 @@ var ( "f5bigip": "f5 BIG-IP", "fastly": "Fastly", "gcp": "GCP", + "github": "GitHub", "gitlab": "GitLab", "kafka": "Kafka", "keycloak": "Keycloak", + "kong": "Kong", "kubernetes": "Kubernetes", "linode": "Linode", "mailgun": "Mailgun", @@ -298,7 +300,7 @@ func resourceName(r *schema.Resource) string { if r.IsProvider { return "Provider" } - return tokenToName(r.Token) + return strings.Title(tokenToName(r.Token)) } func getLanguageDocHelper(lang string) codegen.DocLanguageHelper { @@ -741,7 +743,7 @@ func (mod *modContext) genNestedTypes(member interface{}, resourceType bool) []d props[lang] = mod.getProperties(obj.Properties, lang, true, true) } - name := tokenToName(obj.Token) + name := strings.Title(tokenToName(obj.Token)) objs = append(objs, docNestedType{ Name: wbr(name), AnchorID: strings.ToLower(name), @@ -1533,7 +1535,7 @@ func (mod *modContext) genIndex() indexData { name := tokenToName(f.Token) functions = append(functions, indexEntry{ Link: strings.ToLower(name), - DisplayName: name, + DisplayName: strings.Title(name), }) } sortIndexEntries(functions) diff --git a/pkg/codegen/docs/gen_function.go b/pkg/codegen/docs/gen_function.go index 095a25995..2368430ba 100644 --- a/pkg/codegen/docs/gen_function.go +++ b/pkg/codegen/docs/gen_function.go @@ -34,10 +34,11 @@ type functionDocArgs struct { Tool string - ResourceName string DeprecationMessage string Comment string + // FunctionName is a map of the language and the function name in that language. + FunctionName map[string]string // FunctionArgs is map per language view of the parameters // in the Function. FunctionArgs map[string]string @@ -59,7 +60,7 @@ type functionDocArgs struct { // getFunctionResourceInfo returns a map of per-language information about // the resource being looked-up using a static "getter" function. -func (mod *modContext) getFunctionResourceInfo(resourceTypeName string) map[string]propertyType { +func (mod *modContext) getFunctionResourceInfo(f *schema.Function) map[string]propertyType { resourceMap := make(map[string]propertyType) var resultTypeName string @@ -67,11 +68,11 @@ func (mod *modContext) getFunctionResourceInfo(resourceTypeName string) map[stri docLangHelper := getLanguageDocHelper(lang) switch lang { case "nodejs": - resultTypeName = docLangHelper.GetResourceFunctionResultName(resourceTypeName) + resultTypeName = docLangHelper.GetResourceFunctionResultName(mod.mod, f) case "go": - resultTypeName = docLangHelper.GetResourceFunctionResultName(resourceTypeName) + resultTypeName = docLangHelper.GetResourceFunctionResultName(mod.mod, f) case "csharp": - resultTypeName = docLangHelper.GetResourceFunctionResultName(resourceTypeName) + resultTypeName = docLangHelper.GetResourceFunctionResultName(mod.mod, f) if mod.mod == "" { resultTypeName = fmt.Sprintf("Pulumi.%s.%s", strings.Title(mod.pkg.Name), resultTypeName) } else { @@ -97,8 +98,8 @@ func (mod *modContext) getFunctionResourceInfo(resourceTypeName string) map[stri return resourceMap } -func (mod *modContext) genFunctionTS(f *schema.Function, resourceName string) []formalParam { - argsType := "Get" + resourceName + "Args" +func (mod *modContext) genFunctionTS(f *schema.Function, funcName string) []formalParam { + argsType := title(funcName+"Args", "nodejs") docLangHelper := getLanguageDocHelper("nodejs") var params []formalParam @@ -125,13 +126,8 @@ func (mod *modContext) genFunctionTS(f *schema.Function, resourceName string) [] return params } -func (mod *modContext) genFunctionGo(f *schema.Function, resourceName string) []formalParam { - argsType := resourceName + "Args" - if mod.mod == "" { - argsType = "Get" + argsType - } else { - argsType = "Lookup" + argsType - } +func (mod *modContext) genFunctionGo(f *schema.Function, funcName string) []formalParam { + argsType := funcName + "Args" docLangHelper := getLanguageDocHelper("go") params := []formalParam{ @@ -139,7 +135,7 @@ func (mod *modContext) genFunctionGo(f *schema.Function, resourceName string) [] Name: "ctx", OptionalFlag: "*", Type: propertyType{ - Name: "pulumi.Context", + Name: "Context", Link: "https://pkg.go.dev/github.com/pulumi/pulumi/sdk/v2/go/pulumi?tab=doc#Context", }, }, @@ -160,15 +156,15 @@ func (mod *modContext) genFunctionGo(f *schema.Function, resourceName string) [] Name: "opts", OptionalFlag: "...", Type: propertyType{ - Name: "pulumi.InvokeOption", + Name: "InvokeOption", Link: "https://pkg.go.dev/github.com/pulumi/pulumi/sdk/v2/go/pulumi?tab=doc#InvokeOption", }, }) return params } -func (mod *modContext) genFunctionCS(f *schema.Function, resourceName string) []formalParam { - argsType := "Get" + resourceName + "Args" +func (mod *modContext) genFunctionCS(f *schema.Function, funcName string) []formalParam { + argsType := funcName + "Args" argsSchemaType := &schema.ObjectType{ Token: f.Token, } @@ -236,7 +232,7 @@ func (mod *modContext) genFunctionPython(f *schema.Function, resourceName string // genFunctionArgs generates the arguments string for a given Function that can be // rendered directly into a template. -func (mod *modContext) genFunctionArgs(f *schema.Function, resourceName string) map[string]string { +func (mod *modContext) genFunctionArgs(f *schema.Function, funcNameMap map[string]string) map[string]string { functionParams := make(map[string]string) for _, lang := range supportedLanguages { @@ -248,16 +244,16 @@ func (mod *modContext) genFunctionArgs(f *schema.Function, resourceName string) switch lang { case "nodejs": - params = mod.genFunctionTS(f, resourceName) + params = mod.genFunctionTS(f, funcNameMap["nodejs"]) paramTemplate = "ts_formal_param" case "go": - params = mod.genFunctionGo(f, resourceName) + params = mod.genFunctionGo(f, funcNameMap["go"]) paramTemplate = "go_formal_param" case "csharp": - params = mod.genFunctionCS(f, resourceName) + params = mod.genFunctionCS(f, funcNameMap["csharp"]) paramTemplate = "csharp_formal_param" case "python": - params = mod.genFunctionPython(f, resourceName) + params = mod.genFunctionPython(f, funcNameMap["python"]) paramTemplate = "py_formal_param" } @@ -282,7 +278,7 @@ func (mod *modContext) genFunctionArgs(f *schema.Function, resourceName string) } func (mod *modContext) genFunctionHeader(f *schema.Function) header { - funcName := tokenToName(f.Token) + funcName := strings.Title(tokenToName(f.Token)) packageName := formatTitleText(mod.pkg.Name) var baseDescription string var titleTag string @@ -308,9 +304,6 @@ func (mod *modContext) genFunctionHeader(f *schema.Function) header { // genFunction is the main entrypoint for generating docs for a Function. // Returns args type that can be used to execute the `function.tmpl` doc template. func (mod *modContext) genFunction(f *schema.Function) functionDocArgs { - name := tokenToName(f.Token) - resourceName := strings.ReplaceAll(name, "Get", "") - inputProps := make(map[string][]property) outputProps := make(map[string][]property) for _, lang := range supportedLanguages { @@ -324,14 +317,21 @@ func (mod *modContext) genFunction(f *schema.Function) functionDocArgs { nestedTypes := mod.genNestedTypes(f, false /*resourceType*/) + // Generate the per-language map for the function name. + funcNameMap := map[string]string{} + for _, lang := range supportedLanguages { + docHelper := getLanguageDocHelper(lang) + funcNameMap[lang] = docHelper.GetFunctionName(mod.mod, f) + } + args := functionDocArgs{ Header: mod.genFunctionHeader(f), Tool: mod.tool, - ResourceName: resourceName, - FunctionArgs: mod.genFunctionArgs(f, resourceName), - FunctionResult: mod.getFunctionResourceInfo(resourceName), + FunctionName: funcNameMap, + FunctionArgs: mod.genFunctionArgs(f, funcNameMap), + FunctionResult: mod.getFunctionResourceInfo(f), Comment: f.Comment, DeprecationMessage: f.DeprecationMessage, diff --git a/pkg/codegen/docs/templates/function.tmpl b/pkg/codegen/docs/templates/function.tmpl index b8a38bee3..4dff99158 100644 --- a/pkg/codegen/docs/templates/function.tmpl +++ b/pkg/codegen/docs/templates/function.tmpl @@ -15,23 +15,23 @@ {{ htmlSafe "{{< chooser language \"javascript,typescript,python,go,csharp\" / >}}" }} -{{ print "{{% choosable language typescript %}}" }} -
function get{{ .ResourceName }}({{ htmlSafe .FunctionArgs.nodejs }}): Promise<{{ template "linkify_param" .FunctionResult.nodejs }}>
+{{ print "{{% choosable language nodejs %}}" }} +
function {{ .FunctionName.nodejs }}({{ htmlSafe .FunctionArgs.nodejs }}): Promise<{{ template "linkify_param" .FunctionResult.nodejs }}>
{{ print "{{% /choosable %}}" }} {{ print "{{% choosable language python %}}" }} -
function  get_{{ pyName .ResourceName }}({{ htmlSafe .FunctionArgs.python }})
+
function  {{ .FunctionName.python }}({{ htmlSafe .FunctionArgs.python }})
{{ print "{{% /choosable %}}" }} {{ print "{{% choosable language go %}}" }} -
func Lookup{{ .ResourceName }}({{ htmlSafe .FunctionArgs.go }}) (*{{ template "linkify_param" .FunctionResult.go }}, error)
+
func {{ .FunctionName.go }}({{ htmlSafe .FunctionArgs.go }}) (*{{ template "linkify_param" .FunctionResult.go }}, error)
{{ print "{{% /choosable %}}" }} {{ print "{{% choosable language csharp %}}" }} -
public static class Get{{ .ResourceName }} {
+
public static class {{ .FunctionName.csharp }} {
     public static Task<{{ template "linkify_param" .FunctionResult.csharp }}> InvokeAsync({{ htmlSafe .FunctionArgs.csharp }})
 }
{{ print "{{% /choosable %}}" }} diff --git a/pkg/codegen/docs/utils.go b/pkg/codegen/docs/utils.go index 4e0ad80af..08c76c00f 100644 --- a/pkg/codegen/docs/utils.go +++ b/pkg/codegen/docs/utils.go @@ -59,7 +59,7 @@ func wbr(s string) string { func tokenToName(tok string) string { components := strings.Split(tok, ":") contract.Assertf(len(components) == 3, "malformed token %v", tok) - return strings.Title(components[2]) + return components[2] } func title(s, lang string) string { diff --git a/pkg/codegen/dotnet/doc.go b/pkg/codegen/dotnet/doc.go index c6a1a1f2e..78ebb86ad 100644 --- a/pkg/codegen/dotnet/doc.go +++ b/pkg/codegen/dotnet/doc.go @@ -84,10 +84,15 @@ func (d DocLanguageHelper) GetLanguageTypeString(pkg *schema.Package, moduleName return mod.typeString(t, qualifier, input, false /*state*/, false /*wrapInput*/, true /*requireInitializers*/, optional) } +func (d DocLanguageHelper) GetFunctionName(modName string, f *schema.Function) string { + return tokenToFunctionName(f.Token) +} + // GetResourceFunctionResultName returns the name of the result type when a function is used to lookup // an existing resource. -func (d DocLanguageHelper) GetResourceFunctionResultName(resourceName string) string { - return "Get" + resourceName + "Result" +func (d DocLanguageHelper) GetResourceFunctionResultName(modName string, f *schema.Function) string { + funcName := d.GetFunctionName(modName, f) + return funcName + "Result" } // GetPropertyName uses the property's csharp-specific language info, if available, to generate diff --git a/pkg/codegen/dotnet/gen.go b/pkg/codegen/dotnet/gen.go index d162799f5..6ef7043b1 100644 --- a/pkg/codegen/dotnet/gen.go +++ b/pkg/codegen/dotnet/gen.go @@ -160,6 +160,10 @@ func resourceName(r *schema.Resource) string { return tokenToName(r.Token) } +func tokenToFunctionName(tok string) string { + return tokenToName(tok) +} + func (mod *modContext) tokenToNamespace(tok string) string { components := strings.Split(tok, ":") contract.Assertf(len(components) == 3, "malformed token %v", tok) @@ -744,7 +748,7 @@ func (mod *modContext) genResource(w io.Writer, r *schema.Resource) error { } func (mod *modContext) genFunction(w io.Writer, fun *schema.Function) error { - className := tokenToName(fun.Token) + className := tokenToFunctionName(fun.Token) fmt.Fprintf(w, "namespace %s\n", mod.tokenToNamespace(fun.Token)) fmt.Fprintf(w, "{\n") diff --git a/pkg/codegen/go/doc.go b/pkg/codegen/go/doc.go index 7df6b46cf..ee0879f17 100644 --- a/pkg/codegen/go/doc.go +++ b/pkg/codegen/go/doc.go @@ -109,8 +109,22 @@ func (d DocLanguageHelper) GetPropertyName(p *schema.Property) (string, error) { return strings.Title(p.Name), nil } +func (d DocLanguageHelper) GetFunctionName(modName string, f *schema.Function) string { + funcName := tokenToName(f.Token) + pkg, ok := d.packages[modName] + if !ok { + return funcName + } + + if override, ok := pkg.functionNames[f]; ok { + funcName = override + } + return funcName +} + // GetResourceFunctionResultName returns the name of the result type when a function is used to lookup // an existing resource. -func (d DocLanguageHelper) GetResourceFunctionResultName(resourceName string) string { - return "Lookup" + resourceName + "Result" +func (d DocLanguageHelper) GetResourceFunctionResultName(modName string, f *schema.Function) string { + funcName := d.GetFunctionName(modName, f) + return funcName + "Result" } diff --git a/pkg/codegen/nodejs/doc.go b/pkg/codegen/nodejs/doc.go index dd1b30ddb..854f38b92 100644 --- a/pkg/codegen/nodejs/doc.go +++ b/pkg/codegen/nodejs/doc.go @@ -96,10 +96,15 @@ func (d DocLanguageHelper) GetLanguageTypeString(pkg *schema.Package, moduleName return typeName } +func (d DocLanguageHelper) GetFunctionName(modName string, f *schema.Function) string { + return tokenToFunctionName(f.Token) +} + // GetResourceFunctionResultName returns the name of the result type when a function is used to lookup // an existing resource. -func (d DocLanguageHelper) GetResourceFunctionResultName(resourceName string) string { - return "Get" + resourceName + "Result" +func (d DocLanguageHelper) GetResourceFunctionResultName(modName string, f *schema.Function) string { + funcName := d.GetFunctionName(modName, f) + return title(funcName) + "Result" } // GetPropertyName returns the property name specific to NodeJS. diff --git a/pkg/codegen/nodejs/gen.go b/pkg/codegen/nodejs/gen.go index fa67d9122..a41cc9ca6 100644 --- a/pkg/codegen/nodejs/gen.go +++ b/pkg/codegen/nodejs/gen.go @@ -135,6 +135,10 @@ func resourceName(r *schema.Resource) string { return tokenToName(r.Token) } +func tokenToFunctionName(tok string) string { + return camel(tokenToName(tok)) +} + func (mod *modContext) typeString(t schema.Type, input, wrapInput, optional bool) string { var typ string switch t := t.(type) { @@ -541,7 +545,7 @@ func (mod *modContext) genResource(w io.Writer, r *schema.Resource) error { } func (mod *modContext) genFunction(w io.Writer, fun *schema.Function) { - name := camel(tokenToName(fun.Token)) + name := tokenToFunctionName(fun.Token) // Write the TypeDoc/JSDoc for the data source function. printComment(w, codegen.StripNonRelevantExamples(fun.Comment, "typescript"), "", "") diff --git a/pkg/codegen/python/doc.go b/pkg/codegen/python/doc.go index bc57a2d62..a5d48bfb9 100644 --- a/pkg/codegen/python/doc.go +++ b/pkg/codegen/python/doc.go @@ -100,8 +100,12 @@ func (d DocLanguageHelper) GetLanguageTypeString(pkg *schema.Package, moduleName return name } +func (d DocLanguageHelper) GetFunctionName(modName string, f *schema.Function) string { + return PyName(tokenToName(f.Token)) +} + // GetResourceFunctionResultName is not implemented for Python and returns an empty string. -func (d DocLanguageHelper) GetResourceFunctionResultName(resourceName string) string { +func (d DocLanguageHelper) GetResourceFunctionResultName(modName string, f *schema.Function) string { return "" }