pulumi/pkg/codegen/docs.go
Pat Gavlin 7b1d6ec1ac
Reify Input and Optional types in the schema type system. (#7059)
These changes support arbitrary combinations of input + plain types
within a schema. Handling plain types at the property level was not
sufficient to support such combinations. Reifying these types
required updating quite a bit of code. This is likely to have caused
some temporary complications, but should eventually lead to
substantial simplification in the SDK and program code generators.

With the new design, input and optional types are explicit in the schema
type system. Optionals will only appear at the outermost level of a type
(i.e. Input<Optional<>>, Array<Optional<>>, etc. will not occur). In
addition to explicit input types, each object type now has a "plain"
shape and an "input" shape. The former uses only plain types; the latter
uses input shapes wherever a plain type is not specified. Plain types
are indicated in the schema by setting the "plain" property of a type spec
to true.
2021-06-24 09:17:55 -07:00

99 lines
3.6 KiB
Go

// Copyright 2016-2020, Pulumi Corporation.
//
// 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.
package codegen
import (
"github.com/pgavlin/goldmark/ast"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
)
// DocLanguageHelper is an interface for extracting language-specific information from a Pulumi schema.
// See the implementation for this interface under each of the language code generators.
type DocLanguageHelper interface {
GetPropertyName(p *schema.Property) (string, error)
GetEnumName(e *schema.Enum, typeName string) (string, error)
GetDocLinkForResourceType(pkg *schema.Package, moduleName, typeName string) string
GetDocLinkForPulumiType(pkg *schema.Package, typeName string) string
GetDocLinkForResourceInputOrOutputType(pkg *schema.Package, moduleName, typeName string, input bool) string
GetDocLinkForFunctionInputOrOutputType(pkg *schema.Package, moduleName, typeName string, input bool) string
GetLanguageTypeString(pkg *schema.Package, moduleName string, t schema.Type, input 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(modName string, f *schema.Function) string
// GetModuleDocLink returns the display name and the link for a module (including root modules) in a given package.
GetModuleDocLink(pkg *schema.Package, modName string) (string, string)
}
func filterExamples(source []byte, node ast.Node, lang string) {
var c, next ast.Node
for c = node.FirstChild(); c != nil; c = next {
filterExamples(source, c, lang)
next = c.NextSibling()
switch c := c.(type) {
case *ast.FencedCodeBlock:
sourceLang := string(c.Language(source))
if sourceLang != lang && sourceLang != "sh" {
node.RemoveChild(node, c)
}
case *schema.Shortcode:
switch string(c.Name) {
case schema.ExampleShortcode:
hasCode := false
for gc := c.FirstChild(); gc != nil; gc = gc.NextSibling() {
if gc.Kind() == ast.KindFencedCodeBlock {
hasCode = true
break
}
}
if hasCode {
var grandchild, nextGrandchild ast.Node
for grandchild = c.FirstChild(); grandchild != nil; grandchild = nextGrandchild {
nextGrandchild = grandchild.NextSibling()
node.InsertBefore(node, c, grandchild)
}
}
node.RemoveChild(node, c)
case schema.ExamplesShortcode:
if first := c.FirstChild(); first != nil {
first.SetBlankPreviousLines(c.HasBlankPreviousLines())
}
var grandchild, nextGrandchild ast.Node
for grandchild = c.FirstChild(); grandchild != nil; grandchild = nextGrandchild {
nextGrandchild = grandchild.NextSibling()
node.InsertBefore(node, c, grandchild)
}
node.RemoveChild(node, c)
}
}
}
}
// FilterExamples filters the code snippets in a schema docstring to include only those that target the given language.
func FilterExamples(description string, lang string) string {
if description == "" {
return ""
}
source := []byte(description)
parsed := schema.ParseDocs(source)
filterExamples(source, parsed, lang)
return schema.RenderDocsToString(source, parsed)
}