2020-04-21 01:36:05 +02:00
|
|
|
// Copyright 2016-2020, Pulumi Corporation.
|
2020-03-09 18:35:20 +01:00
|
|
|
//
|
|
|
|
// 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 python
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-03-17 14:20:05 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen"
|
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
|
2020-03-09 18:35:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// DocLanguageHelper is the Python-specific implementation of the DocLanguageHelper.
|
|
|
|
type DocLanguageHelper struct{}
|
|
|
|
|
|
|
|
var _ codegen.DocLanguageHelper = DocLanguageHelper{}
|
|
|
|
|
2020-04-15 20:58:08 +02:00
|
|
|
// GetDocLinkForPulumiType is not implemented at this time for Python.
|
|
|
|
func (d DocLanguageHelper) GetDocLinkForPulumiType(pkg *schema.Package, typeName string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-05-15 18:51:24 +02:00
|
|
|
// GetDocLinkForResourceType returns the Python API doc for a type belonging to a resource provider.
|
2020-04-14 22:59:23 +02:00
|
|
|
func (d DocLanguageHelper) GetDocLinkForResourceType(pkg *schema.Package, modName, typeName string) string {
|
2020-05-15 18:51:24 +02:00
|
|
|
// The k8s module names contain the domain names. For now we are stripping them off manually so they link correctly.
|
|
|
|
if modName != "" {
|
|
|
|
modName = strings.ReplaceAll(modName, ".k8s.io", "")
|
|
|
|
modName = strings.ReplaceAll(modName, ".apiserver", "")
|
|
|
|
modName = strings.ReplaceAll(modName, ".authorization", "")
|
|
|
|
}
|
2020-07-08 02:24:07 +02:00
|
|
|
|
|
|
|
var path string
|
|
|
|
var fqdnTypeName string
|
2020-05-15 18:51:24 +02:00
|
|
|
switch {
|
|
|
|
case pkg.Name != "" && modName != "":
|
|
|
|
path = fmt.Sprintf("pulumi_%s/%s", pkg.Name, modName)
|
2020-07-08 02:24:07 +02:00
|
|
|
fqdnTypeName = fmt.Sprintf("pulumi_%s.%s.%s", pkg.Name, modName, typeName)
|
2020-05-15 18:51:24 +02:00
|
|
|
case pkg.Name == "" && modName != "":
|
|
|
|
path = modName
|
2020-07-08 02:24:07 +02:00
|
|
|
fqdnTypeName = fmt.Sprintf("%s.%s", modName, typeName)
|
2020-05-15 18:51:24 +02:00
|
|
|
case pkg.Name != "" && modName == "":
|
2020-07-08 02:24:07 +02:00
|
|
|
path = fmt.Sprintf("pulumi_%s", pkg.Name)
|
|
|
|
fqdnTypeName = fmt.Sprintf("pulumi_%s.%s", pkg.Name, typeName)
|
2020-05-15 18:51:24 +02:00
|
|
|
}
|
2020-07-08 02:24:07 +02:00
|
|
|
|
|
|
|
return fmt.Sprintf("/docs/reference/pkg/python/%s/#%s", path, fqdnTypeName)
|
2020-03-09 18:35:20 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
// GetDocLinkForResourceInputOrOutputType is not implemented at this time for Python.
|
2020-04-14 22:59:23 +02:00
|
|
|
func (d DocLanguageHelper) GetDocLinkForResourceInputOrOutputType(pkg *schema.Package, modName, typeName string, input bool) string {
|
2020-03-12 01:58:12 +01:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-03-17 21:37:40 +01:00
|
|
|
// GetDocLinkForFunctionInputOrOutputType is not implemented at this time for Python.
|
2020-04-14 22:59:23 +02:00
|
|
|
func (d DocLanguageHelper) GetDocLinkForFunctionInputOrOutputType(pkg *schema.Package, modName, typeName string, input bool) string {
|
2020-03-09 18:35:20 +01:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLanguageTypeString returns the Python-specific type given a Pulumi schema type.
|
2021-06-24 18:17:55 +02:00
|
|
|
func (d DocLanguageHelper) GetLanguageTypeString(pkg *schema.Package, moduleName string, t schema.Type, input bool) string {
|
2020-08-19 17:34:06 +02:00
|
|
|
typeDetails := map[*schema.ObjectType]*typeDetails{}
|
|
|
|
mod := &modContext{
|
|
|
|
pkg: pkg,
|
|
|
|
mod: moduleName,
|
|
|
|
typeDetails: typeDetails,
|
|
|
|
}
|
2021-06-24 18:17:55 +02:00
|
|
|
typeName := mod.typeString(t, input, false /*acceptMapping*/)
|
2020-08-19 17:34:06 +02:00
|
|
|
|
|
|
|
// Remove any package qualifiers from the type name.
|
|
|
|
if !input {
|
|
|
|
typeName = strings.ReplaceAll(typeName, "outputs.", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove single quote from type names.
|
|
|
|
typeName = strings.ReplaceAll(typeName, "'", "")
|
|
|
|
|
|
|
|
return typeName
|
|
|
|
}
|
|
|
|
|
2020-04-28 02:47:01 +02:00
|
|
|
func (d DocLanguageHelper) GetFunctionName(modName string, f *schema.Function) string {
|
|
|
|
return PyName(tokenToName(f.Token))
|
|
|
|
}
|
|
|
|
|
2020-08-19 17:34:06 +02:00
|
|
|
// GetResourceFunctionResultName returns the name of the result type when a function is used to lookup
|
|
|
|
// an existing resource.
|
2020-04-28 02:47:01 +02:00
|
|
|
func (d DocLanguageHelper) GetResourceFunctionResultName(modName string, f *schema.Function) string {
|
2020-08-19 17:34:06 +02:00
|
|
|
return title(tokenToName(f.Token)) + "Result"
|
2020-03-12 01:58:12 +01:00
|
|
|
}
|
|
|
|
|
2020-08-19 17:34:06 +02:00
|
|
|
// GetPropertyName returns the property name specific to Python.
|
2020-03-23 22:17:14 +01:00
|
|
|
func (d DocLanguageHelper) GetPropertyName(p *schema.Property) (string, error) {
|
2020-08-19 17:34:06 +02:00
|
|
|
return PyName(p.Name), nil
|
2020-03-23 22:17:14 +01:00
|
|
|
}
|
|
|
|
|
2020-12-14 23:40:14 +01:00
|
|
|
// GetEnumName returns the enum name specific to Python.
|
2020-12-16 18:22:44 +01:00
|
|
|
func (d DocLanguageHelper) GetEnumName(e *schema.Enum, typeName string) (string, error) {
|
2020-12-14 23:40:14 +01:00
|
|
|
name := fmt.Sprintf("%v", e.Value)
|
|
|
|
if e.Name != "" {
|
|
|
|
name = e.Name
|
|
|
|
}
|
2020-12-16 18:22:44 +01:00
|
|
|
return makeSafeEnumName(name, typeName)
|
2020-12-14 23:40:14 +01:00
|
|
|
}
|
|
|
|
|
2020-05-13 00:37:37 +02:00
|
|
|
// GetModuleDocLink returns the display name and the link for a module.
|
|
|
|
func (d DocLanguageHelper) GetModuleDocLink(pkg *schema.Package, modName string) (string, string) {
|
|
|
|
var displayName string
|
|
|
|
var link string
|
|
|
|
if modName == "" {
|
2020-09-16 20:11:56 +02:00
|
|
|
displayName = pyPack(pkg.Name)
|
2020-05-13 00:37:37 +02:00
|
|
|
} else {
|
2020-09-16 20:11:56 +02:00
|
|
|
displayName = fmt.Sprintf("%s/%s", pyPack(pkg.Name), strings.ToLower(modName))
|
2020-05-13 00:37:37 +02:00
|
|
|
}
|
|
|
|
link = fmt.Sprintf("/docs/reference/pkg/python/%s", displayName)
|
|
|
|
return displayName, link
|
|
|
|
}
|