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 (
|
2020-03-23 22:17:14 +01:00
|
|
|
"errors"
|
2020-03-09 18:35:20 +01:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-04-14 10:30:25 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/v2/codegen"
|
|
|
|
"github.com/pulumi/pulumi/pkg/v2/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
|
|
|
var path string
|
|
|
|
// 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", "")
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case pkg.Name != "" && modName != "":
|
|
|
|
path = fmt.Sprintf("pulumi_%s/%s", pkg.Name, modName)
|
|
|
|
case pkg.Name == "" && modName != "":
|
|
|
|
path = modName
|
|
|
|
case pkg.Name != "" && modName == "":
|
|
|
|
path = pkg.Name
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("/docs/reference/pkg/python/%s/#%s", path, typeName)
|
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 ""
|
|
|
|
}
|
|
|
|
|
2020-04-15 21:56:28 +02:00
|
|
|
// GetDocLinkForBuiltInType returns the Python URL for a built-in type.
|
|
|
|
// Currently not using the typeName parameter because the returned link takes to a general
|
|
|
|
// top-level page containing info for all built in types.
|
|
|
|
func (d DocLanguageHelper) GetDocLinkForBuiltInType(typeName string) string {
|
|
|
|
return "https://docs.python.org/3/library/stdtypes.html"
|
|
|
|
}
|
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
// GetLanguageTypeString returns the Python-specific type given a Pulumi schema type.
|
|
|
|
func (d DocLanguageHelper) GetLanguageTypeString(pkg *schema.Package, moduleName string, t schema.Type, input, optional bool) string {
|
|
|
|
name := pyType(t)
|
|
|
|
|
2020-03-17 21:37:40 +01:00
|
|
|
// The Python SDK generator will simply return "list" or "dict" for enumerables.
|
|
|
|
// So we examine the underlying types to provide some more information on
|
|
|
|
// the elements inside the enumerable.
|
2020-03-09 18:35:20 +01:00
|
|
|
switch name {
|
|
|
|
case "list":
|
|
|
|
arrTy := t.(*schema.ArrayType)
|
|
|
|
elType := arrTy.ElementType.String()
|
|
|
|
return getListWithTypeName(elementTypeToName(elType))
|
|
|
|
case "dict":
|
2020-04-10 23:07:52 +02:00
|
|
|
switch dTy := t.(type) {
|
|
|
|
case *schema.UnionType:
|
|
|
|
types := make([]string, 0, len(dTy.ElementTypes))
|
|
|
|
for _, e := range dTy.ElementTypes {
|
|
|
|
if schema.IsPrimitiveType(e) {
|
|
|
|
types = append(types, e.String())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t := d.GetLanguageTypeString(pkg, moduleName, e, input, optional)
|
|
|
|
types = append(types, t)
|
|
|
|
}
|
|
|
|
return strings.Join(types, " | ")
|
2020-03-09 18:35:20 +01:00
|
|
|
case *schema.MapType:
|
2020-04-10 23:07:52 +02:00
|
|
|
if uTy, ok := dTy.ElementType.(*schema.UnionType); ok {
|
|
|
|
return d.GetLanguageTypeString(pkg, moduleName, uTy, input, optional)
|
|
|
|
}
|
|
|
|
|
|
|
|
elType := dTy.ElementType.String()
|
2020-03-17 21:37:40 +01:00
|
|
|
return getMapWithTypeName(elementTypeToName(elType))
|
2020-03-09 18:35:20 +01:00
|
|
|
case *schema.ObjectType:
|
2020-04-10 23:07:52 +02:00
|
|
|
return getDictWithTypeName(tokenToName(dTy.Token))
|
2020-03-17 21:37:40 +01:00
|
|
|
default:
|
|
|
|
return "Dict[str, Any]"
|
2020-03-09 18:35:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2020-04-28 02:47:01 +02:00
|
|
|
func (d DocLanguageHelper) GetFunctionName(modName string, f *schema.Function) string {
|
|
|
|
return PyName(tokenToName(f.Token))
|
|
|
|
}
|
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
// GetResourceFunctionResultName is not implemented for Python and returns an empty string.
|
2020-04-28 02:47:01 +02:00
|
|
|
func (d DocLanguageHelper) GetResourceFunctionResultName(modName string, f *schema.Function) string {
|
2020-03-12 01:58:12 +01:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-03-19 20:18:18 +01:00
|
|
|
// GenPropertyCaseMap generates the case maps for a property.
|
2020-06-26 20:47:13 +02:00
|
|
|
func (d DocLanguageHelper) GenPropertyCaseMap(pkg *schema.Package, modName, tool string, prop *schema.Property, snakeCaseToCamelCase, camelCaseToSnakeCase map[string]string, seenTypes codegen.Set) {
|
2020-04-21 01:36:05 +02:00
|
|
|
if err := pkg.ImportLanguages(map[string]schema.Language{"python": Importer}); err != nil {
|
|
|
|
fmt.Printf("error building case map for %q in module %q", prop.Name, modName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-26 20:47:13 +02:00
|
|
|
recordProperty(prop, snakeCaseToCamelCase, camelCaseToSnakeCase, seenTypes)
|
2020-03-19 20:18:18 +01:00
|
|
|
}
|
|
|
|
|
2020-03-23 22:17:14 +01:00
|
|
|
// GetPropertyName is not implemented for Python because property names in Python must use
|
|
|
|
// property case maps, which need to be generated at each provider's package-level.
|
|
|
|
func (d DocLanguageHelper) GetPropertyName(p *schema.Property) (string, error) {
|
|
|
|
return "", errors.New("this method is not supported for the python language")
|
|
|
|
}
|
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
// elementTypeToName returns the type name from an element type of the form
|
|
|
|
// package:module:_type, with its leading "_" stripped.
|
|
|
|
func elementTypeToName(el string) string {
|
|
|
|
parts := strings.Split(el, ":")
|
|
|
|
if len(parts) == 3 {
|
|
|
|
el = parts[2]
|
|
|
|
}
|
|
|
|
el = strings.TrimPrefix(el, "_")
|
|
|
|
|
|
|
|
return el
|
|
|
|
}
|
|
|
|
|
|
|
|
// getListWithTypeName returns a Python representation of a list containing
|
|
|
|
// items of `t`.
|
|
|
|
func getListWithTypeName(t string) string {
|
2020-03-17 21:37:40 +01:00
|
|
|
if t == "string" {
|
|
|
|
return "List[str]"
|
|
|
|
}
|
|
|
|
|
2020-03-19 20:18:18 +01:00
|
|
|
return fmt.Sprintf("List[%s]", strings.Title(t))
|
2020-03-09 18:35:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// getDictWithTypeName returns the Python representation of a dictionary
|
|
|
|
// where each item is of type `t`.
|
|
|
|
func getDictWithTypeName(t string) string {
|
2020-03-19 20:18:18 +01:00
|
|
|
return fmt.Sprintf("Dict[%s]", strings.Title(t))
|
2020-03-17 21:37:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// getMapWithTypeName returns the Python representation of a dictionary
|
|
|
|
// with a string keu and a value of type `t`.
|
|
|
|
func getMapWithTypeName(t string) string {
|
|
|
|
switch t {
|
|
|
|
case "string":
|
|
|
|
return "Dict[str, str]"
|
|
|
|
case "any":
|
|
|
|
return "Dict[str, Any]"
|
|
|
|
default:
|
2020-03-19 20:18:18 +01:00
|
|
|
return fmt.Sprintf("Dict[str, %s]", strings.Title(t))
|
2020-03-17 21:37:40 +01:00
|
|
|
}
|
2020-03-09 18:35:20 +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 == "" {
|
|
|
|
displayName = fmt.Sprintf("pulumi_%s", pkg.Name)
|
|
|
|
} else {
|
|
|
|
displayName = fmt.Sprintf("pulumi_%s/%s", pkg.Name, strings.ToLower(modName))
|
|
|
|
}
|
|
|
|
link = fmt.Sprintf("/docs/reference/pkg/python/%s", displayName)
|
|
|
|
return displayName, link
|
|
|
|
}
|