pulumi/pkg/codegen/docs
Zaid Ajaj 2394f0de7a
[docs/go-program-gen] Fix generating constructor syntax examples for kubernetes (#16574)
In this PR we fix generating constructor syntax examples for Kubernetes.
The problem initially was due to generating a PCL program with syntax
errors for the kubernetes schema. The syntax error was happening because
we emitted properties `$ref` and `$schema` which are not valid
identifiers in PCL. Fixing this was simple enough, we only needed to
quote these properties that start with dollar signs and we get a valid
PCL program.

Full PCL program for kubernetes:
https://gist.github.com/Zaid-Ajaj/abe899430a0b5f99a428934dcac75d52

However, the valid PCL program wouldn't convert to Go with program-gen
and it would hang without showing any errors or stack traces. I wrote a
script to split the programs for each resource and convert each
separately. This way I narrowed down the problem to this program:

```
resource "def" "kubernetes:apiextensions.k8s.io/v1:CustomResourceDefinition" {
  apiVersion = "string"
  kind = "string"
  spec = {
    versions = [{}]
  }
}
```

Debugging this in Go program-gen didn't show an obvious error and it
kept on hanging. However I noticed that we lowering expressions twice:
once for the entirety of resource inputs when generating resources and
again when generating the expressions separately. Lowering expressions
has been the source of many bugs and it seems to trip up program-gen a
lot and suspected something wrong was going on here, so I simplified it
to lower the expressions once when we generate resource inputs. This
fixed the hang issue as well as a few of our test programs (see diffs
for test programs and tests schemas)

Tested this against the full kubernetes schema and we can now generate
the full Go program:
https://gist.github.com/Zaid-Ajaj/3ac734536969a989edae92219968d51f

> The second time we lower expressions not only is redundant but also
can generate invalid code if the lowered expressions have temporary
variables because program-gen would just emit these variables inside
inline expressions like objects and lists which gives invalid Go code.

Resolves part of #16463
2024-07-05 12:42:41 +00:00
..
templates Hide expanded constructor syntax when language fails (#16479) 2024-06-25 22:41:47 +00:00
testdata Fix coming soon misrender (#15783) 2024-03-26 17:24:37 +00:00
README.md Replace Hugo shortcodes (#9491) 2022-04-29 15:04:15 -07:00
constructor_syntax_extractor.go upgrade to latest version of golangci-lint (#15977) 2024-04-19 06:20:33 +00:00
constructor_syntax_generator.go [docs/go-program-gen] Fix generating constructor syntax examples for kubernetes (#16574) 2024-07-05 12:42:41 +00:00
constructor_syntax_generator_test.go [docs] Fix generating constructor examples for resources that have numeric enums as input (#16223) 2024-05-30 22:43:12 +00:00
description.go Fix coming soon misrender (#15783) 2024-03-26 17:24:37 +00:00
description_test.go Fix coming soon misrender (#15783) 2024-03-26 17:24:37 +00:00
examples.go Add ability to process docs without dedicated Examples logic (#15475) 2024-02-23 18:06:44 +00:00
gen.go Hide expanded constructor syntax when language fails (#16479) 2024-06-25 22:41:47 +00:00
gen_function.go Enable perfsprint linter (#14813) 2023-12-12 12:19:42 +00:00
gen_kubernetes.go sdk/go: Remove 'nolint' directives from package docs 2023-01-06 09:06:47 -08:00
gen_method.go Enable perfsprint linter (#14813) 2023-12-12 12:19:42 +00:00
gen_test.go all: Reformat with gofumpt 2023-03-03 09:00:24 -08:00
package_tree.go Use slice.Prealloc instead of make([]T, 0, ...) 2023-06-29 11:27:50 +01:00
package_tree_test.go Fix docs generator parent module computation (#15035) 2024-02-20 15:44:37 +00:00
static_schema_loader.go [docs] Emit example constructor syntax for resources in typescript, python, go and csharp (#15624) 2024-03-21 13:41:07 +00:00
utils.go Display full type names in Python references in the docs (#15784) 2024-04-01 14:59:50 +00:00
utils_test.go Display full type names in Python references in the docs (#15784) 2024-04-01 14:59:50 +00:00

README.md

Docs generator

This generator generates resource-level docs by utilizing the Pulumi schema.

Crash course on templates

The templates use Go's built-in html/template package to process templates with data. The driver for this doc generator (e.g. tfbridge for TF-based providers) then persists each file from memory onto the disk as .md files.

Although we are using the html/template package, it has the same exact interface as the text/template package, except for some HTML specific things. Therefore, all of the functions available in the text/template package are also available with the html/template package.

  • Data can be injected using {{.PropertyName}}.
  • Nested properties can be accessed using the dot notation, i.e. {{.Property1.Property2}}.
  • Templates can inject other templates using the {{template "template_name"}} directive.
    • For this to work, you will need to first define the named template using {{define "template_name"}}.
  • You can pass data to nested templates by simply passing an argument after the template's name.
  • To remove whitespace from injected values, use the - in the template tags.
    • For example, {{if .SomeBool}} some text {{- else}} some other text {{- end}}. Note the use of - to eliminate whitespace from the enclosing text.
    • Read more here.
  • To render un-encoded content use the custom global function htmlSafe.
    • Note: This should only be used if you know for sure you are not injecting any user-generated content, as it by-passes the HTML encoding.
  • To render strings to Markdown, use the custom global function markdownify.
  • To print regular strings, that share the same syntax as the Go templating engine, use the built-in global function print function.

Learn more from here: https://curtisvermeeren.github.io/2017/09/14/Golang-Templates-Cheatsheet

Modifying templates and updating tests

We run tests that validate our template-rendering output. If you need to make change that produces a set of Markdown files that differs from the set that we use in our tests (see codegen/testing/test/testdata/**/*.md), your pull-request checks will fail, and to get them to pass, you'll need to modify the test data to match the output produced by your change.

For minor diffs, you can just update the test files manually and include those updates with your PR. But for large diffs, you may want to regenerate the full set. To do that, from the root of the repo, run:

cd pkg/codegen/docs && PULUMI_ACCEPT=true go test . && cd -