github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/python/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, 16 // so we just ignore the goconst linter's warning. 17 // 18 // nolint: lll, goconst 19 package python 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 Python-specific implementation of the DocLanguageHelper. 30 type DocLanguageHelper struct{} 31 32 var _ codegen.DocLanguageHelper = DocLanguageHelper{} 33 34 // GetDocLinkForPulumiType is not implemented at this time for Python. 35 func (d DocLanguageHelper) GetDocLinkForPulumiType(pkg *schema.Package, typeName string) string { 36 return "" 37 } 38 39 // GetDocLinkForResourceType returns the Python API doc for a type belonging to a resource provider. 40 func (d DocLanguageHelper) GetDocLinkForResourceType(pkg *schema.Package, modName, typeName string) string { 41 // The k8s module names contain the domain names. For now we are stripping them off manually so they link correctly. 42 if modName != "" { 43 modName = strings.ReplaceAll(modName, ".k8s.io", "") 44 modName = strings.ReplaceAll(modName, ".apiserver", "") 45 modName = strings.ReplaceAll(modName, ".authorization", "") 46 } 47 48 var path string 49 var fqdnTypeName string 50 switch { 51 case pkg.Name != "" && modName != "": 52 path = fmt.Sprintf("pulumi_%s/%s", pkg.Name, modName) 53 fqdnTypeName = fmt.Sprintf("pulumi_%s.%s.%s", pkg.Name, modName, typeName) 54 case pkg.Name == "" && modName != "": 55 path = modName 56 fqdnTypeName = fmt.Sprintf("%s.%s", modName, typeName) 57 case pkg.Name != "" && modName == "": 58 path = fmt.Sprintf("pulumi_%s", pkg.Name) 59 fqdnTypeName = fmt.Sprintf("pulumi_%s.%s", pkg.Name, typeName) 60 } 61 62 return fmt.Sprintf("/docs/reference/pkg/python/%s/#%s", path, fqdnTypeName) 63 } 64 65 // GetDocLinkForResourceInputOrOutputType is not implemented at this time for Python. 66 func (d DocLanguageHelper) GetDocLinkForResourceInputOrOutputType(pkg *schema.Package, modName, typeName string, input bool) string { 67 return "" 68 } 69 70 // GetDocLinkForFunctionInputOrOutputType is not implemented at this time for Python. 71 func (d DocLanguageHelper) GetDocLinkForFunctionInputOrOutputType(pkg *schema.Package, modName, typeName string, input bool) string { 72 return "" 73 } 74 75 // GetLanguageTypeString returns the Python-specific type given a Pulumi schema type. 76 func (d DocLanguageHelper) GetLanguageTypeString(pkg *schema.Package, moduleName string, t schema.Type, input bool) string { 77 typeDetails := map[*schema.ObjectType]*typeDetails{} 78 mod := &modContext{ 79 pkg: pkg, 80 mod: moduleName, 81 typeDetails: typeDetails, 82 } 83 typeName := mod.typeString(t, input, false /*acceptMapping*/) 84 85 // Remove any package qualifiers from the type name. 86 if !input { 87 typeName = strings.ReplaceAll(typeName, "outputs.", "") 88 } 89 90 // Remove single quote from type names. 91 typeName = strings.ReplaceAll(typeName, "'", "") 92 93 return typeName 94 } 95 96 func (d DocLanguageHelper) GetFunctionName(modName string, f *schema.Function) string { 97 return PyName(tokenToName(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 return title(tokenToName(f.Token)) + "Result" 104 } 105 106 func (d DocLanguageHelper) GetMethodName(m *schema.Method) string { 107 return PyName(m.Name) 108 } 109 110 func (d DocLanguageHelper) GetMethodResultName(pkg *schema.Package, modName string, r *schema.Resource, 111 m *schema.Method) string { 112 113 if info, ok := pkg.Language["python"].(PackageInfo); ok { 114 if info.LiftSingleValueMethodReturns && m.Function.Outputs != nil && len(m.Function.Outputs.Properties) == 1 { 115 typeDetails := map[*schema.ObjectType]*typeDetails{} 116 mod := &modContext{ 117 pkg: pkg, 118 mod: modName, 119 typeDetails: typeDetails, 120 } 121 return mod.typeString(m.Function.Outputs.Properties[0].Type, false, false) 122 } 123 } 124 return fmt.Sprintf("%s.%sResult", resourceName(r), title(d.GetMethodName(m))) 125 } 126 127 // GetPropertyName returns the property name specific to Python. 128 func (d DocLanguageHelper) GetPropertyName(p *schema.Property) (string, error) { 129 return PyName(p.Name), nil 130 } 131 132 // GetEnumName returns the enum name specific to Python. 133 func (d DocLanguageHelper) GetEnumName(e *schema.Enum, typeName string) (string, error) { 134 name := fmt.Sprintf("%v", e.Value) 135 if e.Name != "" { 136 name = e.Name 137 } 138 return makeSafeEnumName(name, typeName) 139 } 140 141 // GetModuleDocLink returns the display name and the link for a module. 142 func (d DocLanguageHelper) GetModuleDocLink(pkg *schema.Package, modName string) (string, string) { 143 var displayName string 144 var link string 145 if modName == "" { 146 displayName = pyPack(pkg.Name) 147 } else { 148 displayName = fmt.Sprintf("%s/%s", pyPack(pkg.Name), strings.ToLower(modName)) 149 } 150 link = fmt.Sprintf("/docs/reference/pkg/python/%s", displayName) 151 return displayName, link 152 }