github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/nodejs/doc.go (about) 1 // Copyright 2016-2020, Pulumi Corporation. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Pulling out some of the repeated strings tokens into constants would harm readability, so we just ignore the 16 // goconst linter's warning. 17 // 18 // nolint: lll, goconst 19 package nodejs 20 21 import ( 22 "fmt" 23 "strings" 24 25 "github.com/pulumi/pulumi/pkg/v3/codegen" 26 "github.com/pulumi/pulumi/pkg/v3/codegen/schema" 27 ) 28 29 // DocLanguageHelper is the NodeJS-specific implementation of the DocLanguageHelper. 30 type DocLanguageHelper struct{} 31 32 var _ codegen.DocLanguageHelper = DocLanguageHelper{} 33 34 // GetDocLinkForPulumiType returns the NodeJS API doc link for a Pulumi type. 35 func (d DocLanguageHelper) GetDocLinkForPulumiType(pkg *schema.Package, typeName string) string { 36 typeName = strings.ReplaceAll(typeName, "?", "") 37 return fmt.Sprintf("/docs/reference/pkg/nodejs/pulumi/pulumi/#%s", typeName) 38 } 39 40 // GetDocLinkForResourceType returns the NodeJS API doc for a type belonging to a resource provider. 41 func (d DocLanguageHelper) GetDocLinkForResourceType(pkg *schema.Package, modName, typeName string) string { 42 var path string 43 switch { 44 case pkg.Name != "" && modName != "": 45 path = fmt.Sprintf("%s/%s", pkg.Name, modName) 46 case pkg.Name == "" && modName != "": 47 path = modName 48 case pkg.Name != "" && modName == "": 49 path = pkg.Name 50 } 51 typeName = strings.ReplaceAll(typeName, "?", "") 52 return fmt.Sprintf("/docs/reference/pkg/nodejs/pulumi/%s/#%s", path, typeName) 53 } 54 55 // GetDocLinkForResourceInputOrOutputType returns the doc link for an input or output type of a Resource. 56 func (d DocLanguageHelper) GetDocLinkForResourceInputOrOutputType(pkg *schema.Package, modName, typeName string, input bool) string { 57 typeName = strings.TrimSuffix(typeName, "?") 58 parts := strings.Split(typeName, ".") 59 typeName = parts[len(parts)-1] 60 if input { 61 return fmt.Sprintf("/docs/reference/pkg/nodejs/pulumi/%s/types/input/#%s", pkg.Name, typeName) 62 } 63 return fmt.Sprintf("/docs/reference/pkg/nodejs/pulumi/%s/types/output/#%s", pkg.Name, typeName) 64 } 65 66 // GetDocLinkForFunctionInputOrOutputType returns the doc link for an input or output type of a Function. 67 func (d DocLanguageHelper) GetDocLinkForFunctionInputOrOutputType(pkg *schema.Package, modName, typeName string, input bool) string { 68 return d.GetDocLinkForResourceInputOrOutputType(pkg, modName, typeName, input) 69 } 70 71 // GetLanguageTypeString returns the language-specific type given a Pulumi schema type. 72 func (d DocLanguageHelper) GetLanguageTypeString(pkg *schema.Package, moduleName string, t schema.Type, input bool) string { 73 // Remove the union with `undefined` for optional types, 74 // since we will show that information separately anyway. 75 if optional, ok := t.(*schema.OptionalType); ok { 76 t = optional.ElementType 77 } 78 79 modCtx := &modContext{ 80 pkg: pkg, 81 mod: moduleName, 82 } 83 typeName := modCtx.typeString(t, input, nil) 84 85 // Remove any package qualifiers from the type name. 86 typeQualifierPackage := "inputs" 87 if !input { 88 typeQualifierPackage = "outputs" 89 } 90 typeName = strings.ReplaceAll(typeName, typeQualifierPackage+".", "") 91 typeName = strings.ReplaceAll(typeName, "enums.", "") 92 93 return typeName 94 } 95 96 func (d DocLanguageHelper) GetFunctionName(modName string, f *schema.Function) string { 97 return tokenToFunctionName(f.Token) 98 } 99 100 // GetResourceFunctionResultName returns the name of the result type when a function is used to lookup 101 // an existing resource. 102 func (d DocLanguageHelper) GetResourceFunctionResultName(modName string, f *schema.Function) string { 103 funcName := d.GetFunctionName(modName, f) 104 return title(funcName) + "Result" 105 } 106 107 func (d DocLanguageHelper) GetMethodName(m *schema.Method) string { 108 return camel(m.Name) 109 } 110 111 func (d DocLanguageHelper) GetMethodResultName(pkg *schema.Package, modName string, r *schema.Resource, 112 m *schema.Method) string { 113 114 if info, ok := pkg.Language["nodejs"].(NodePackageInfo); ok { 115 if info.LiftSingleValueMethodReturns && m.Function.Outputs != nil && len(m.Function.Outputs.Properties) == 1 { 116 modCtx := &modContext{ 117 pkg: pkg, 118 mod: modName, 119 } 120 return modCtx.typeString(m.Function.Outputs.Properties[0].Type, false, nil) 121 } 122 } 123 return fmt.Sprintf("%s.%sResult", resourceName(r), title(d.GetMethodName(m))) 124 } 125 126 // GetPropertyName returns the property name specific to NodeJS. 127 func (d DocLanguageHelper) GetPropertyName(p *schema.Property) (string, error) { 128 return p.Name, nil 129 } 130 131 // GetEnumName returns the enum name specific to NodeJS. 132 func (d DocLanguageHelper) GetEnumName(e *schema.Enum, typeName string) (string, error) { 133 return enumMemberName(typeName, e) 134 } 135 136 // GetModuleDocLink returns the display name and the link for a module. 137 func (d DocLanguageHelper) GetModuleDocLink(pkg *schema.Package, modName string) (string, string) { 138 var displayName string 139 var link string 140 if modName == "" { 141 displayName = fmt.Sprintf("@pulumi/%s", pkg.Name) 142 } else { 143 displayName = fmt.Sprintf("@pulumi/%s/%s", pkg.Name, strings.ToLower(modName)) 144 } 145 link = d.GetDocLinkForResourceType(pkg, modName, "") 146 return displayName, link 147 }