Skip to content

Commit

Permalink
Use language.Language when rendering properties
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Jan 22, 2025
1 parent 2766c0d commit d3889cc
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
36 changes: 18 additions & 18 deletions tools/resourcedocsgen/pkg/docs/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,8 @@ func (dctx *Context) setModules(modules map[string]*modContext) {
}

func NewContext(tool string, pkg *schema.Package) *Context {
supportedLanguages := []string{"csharp", "go", "nodejs", "python", "yaml", "java"}

dctx := &Context{
supportedLanguages: supportedLanguages,
supportedLanguages: []string{"csharp", "go", "nodejs", "python", "yaml", "java"},
langModuleNameLookup: map[string]string{},
docHelpers: map[language.Language]codegen.DocLanguageHelper{
language.CSharp: &dotnet.DocLanguageHelper{},
Expand Down Expand Up @@ -298,7 +296,7 @@ type docNestedType struct {
Name string
Input bool
AnchorID string
Properties map[string][]property
Properties map[language.Language][]property
EnumValues map[string][]enum
}

Expand Down Expand Up @@ -375,16 +373,16 @@ type resourceDocArgs struct {

// InputProperties is a map per language and a corresponding slice of input properties accepted as args while creating
// a new resource.
InputProperties map[string][]property
InputProperties map[language.Language][]property
// OutputProperties is a map per language and a corresponding slice of output properties returned when a new instance
// of the resource is created.
OutputProperties map[string][]property
OutputProperties map[language.Language][]property

// LookupParams is a map of the param string to be rendered per language for looking-up a resource.
LookupParams map[string]string
// StateInputs is a map per language and the corresponding slice of state input properties required while looking-up
// an existing resource.
StateInputs map[string][]property
StateInputs map[language.Language][]property
// StateParam is the type name of the state param, if any.
StateParam string

Expand Down Expand Up @@ -1124,8 +1122,9 @@ func (mod *modContext) genNestedTypes(member interface{}, resourceType, isProvid
}

// Create a map to hold the per-language properties of this object.
props := make(map[string][]property)
props := make(map[language.Language][]property)
for _, lang := range dctx.supportedLanguages {
lang := mustConvertPulumiSchemaLanguage(lang)
props[lang] = mod.getProperties(typ.Properties, lang, true, true, isProvider)
}

Expand Down Expand Up @@ -1184,13 +1183,15 @@ func (mod *modContext) genNestedTypes(member interface{}, resourceType, isProvid

// getProperties returns a slice of properties that can be rendered for docs for the provided slice of properties in the
// schema.
func (mod *modContext) getProperties(properties []*schema.Property, lang string, input, nested, isProvider bool,
func (mod *modContext) getProperties(
properties []*schema.Property, lang language.Language, input, nested, isProvider bool,
) []property {
return mod.getPropertiesWithIDPrefixAndExclude(properties, lang, input, nested, isProvider, "", nil)
}

func (mod *modContext) getPropertiesWithIDPrefixAndExclude(properties []*schema.Property, lang string, input, nested,
isProvider bool, idPrefix string, exclude func(name string) bool,
func (mod *modContext) getPropertiesWithIDPrefixAndExclude(
properties []*schema.Property, lang language.Language, input, nested, isProvider bool,
idPrefix string, exclude func(name string) bool,
) []property {
dctx := mod.context
if len(properties) == 0 {
Expand All @@ -1215,23 +1216,21 @@ func (mod *modContext) getPropertiesWithIDPrefixAndExclude(properties []*schema.

characteristics := propertyCharacteristics{input: input}

langDocHelper := dctx.getLanguageDocHelper(mustConvertPulumiSchemaLanguage(lang))
langDocHelper := dctx.getLanguageDocHelper(lang)
name, err := langDocHelper.GetPropertyName(prop)
if err != nil {
panic(err)
}
propLangName := name

propID := idPrefix + strings.ToLower(propLangName+propertyLangSeparator+lang)
propID := idPrefix + strings.ToLower(propLangName+propertyLangSeparator+lang.String())

propTypes := make([]propertyType, 0)
if typ, isUnion := codegen.UnwrapType(prop.Type).(*schema.UnionType); isUnion {
for _, elementType := range typ.ElementTypes {
lang := mustConvertPulumiSchemaLanguage(lang)
propTypes = append(propTypes, mod.typeString(elementType, lang, characteristics, true))
}
} else {
lang := mustConvertPulumiSchemaLanguage(lang)
propTypes = append(propTypes, mod.typeString(prop.Type, lang, characteristics, true))
}

Expand Down Expand Up @@ -1733,9 +1732,9 @@ func (mod *modContext) genResource(r *schema.Resource) resourceDocArgs {
// Create a resource module file into which all of this resource's types will go.
name := resourceName(r)

inputProps := make(map[string][]property)
outputProps := make(map[string][]property)
stateInputs := make(map[string][]property)
inputProps := make(map[language.Language][]property)
outputProps := make(map[language.Language][]property)
stateInputs := make(map[language.Language][]property)

var filteredOutputProps []*schema.Property
// Provider resources do not have output properties, so there won't be anything to filter.
Expand All @@ -1753,6 +1752,7 @@ func (mod *modContext) genResource(r *schema.Resource) resourceDocArgs {
}

for _, lang := range dctx.supportedLanguages {
lang := mustConvertPulumiSchemaLanguage(lang)
inputProps[lang] = mod.getProperties(r.InputProperties, lang, true, false, r.IsProvider)
outputProps[lang] = mod.getProperties(filteredOutputProps, lang, false, false, r.IsProvider)
if r.IsProvider {
Expand Down
9 changes: 5 additions & 4 deletions tools/resourcedocsgen/pkg/docs/gen_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ type functionDocArgs struct {
FunctionResult map[string]propertyType

// InputProperties is a map per language and the corresponding slice of input properties accepted by the Function.
InputProperties map[string][]property
InputProperties map[language.Language][]property
// InputProperties is a map per language and the corresponding slice of output properties, which are properties of the
// FunctionResult type.
OutputProperties map[string][]property
OutputProperties map[language.Language][]property

// NestedTypes is a slice of the nested types used in the input and output properties.
NestedTypes []docNestedType
Expand Down Expand Up @@ -433,9 +433,10 @@ func (mod *modContext) genFunctionOutputVersionMap(f *schema.Function) map[strin
// the `function.tmpl` doc template.
func (mod *modContext) genFunction(f *schema.Function) functionDocArgs {
dctx := mod.context
inputProps := make(map[string][]property)
outputProps := make(map[string][]property)
inputProps := make(map[language.Language][]property)
outputProps := make(map[language.Language][]property)
for _, lang := range dctx.supportedLanguages {
lang := mustConvertPulumiSchemaLanguage(lang)
if f.Inputs != nil {
inputProps[lang] = mod.getProperties(f.Inputs.Properties, lang, true, false, false)
}
Expand Down
7 changes: 4 additions & 3 deletions tools/resourcedocsgen/pkg/docs/gen_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ type methodDocArgs struct {
MethodResult map[string]propertyType

// InputProperties is a map per language and the corresponding slice of input properties accepted by the method.
InputProperties map[string][]property
InputProperties map[language.Language][]property
// OutputProperties is a map per language and the corresponding slice of output properties, which are properties of
// the MethodResult type.
OutputProperties map[string][]property
OutputProperties map[language.Language][]property
}

func (mod *modContext) genMethods(r *schema.Resource) []methodDocArgs {
Expand All @@ -63,8 +63,9 @@ func (mod *modContext) genMethods(r *schema.Resource) []methodDocArgs {
func (mod *modContext) genMethod(r *schema.Resource, m *schema.Method) methodDocArgs {
dctx := mod.context
f := m.Function
inputProps, outputProps := make(map[string][]property), make(map[string][]property)
inputProps, outputProps := make(map[language.Language][]property), make(map[language.Language][]property)
for _, lang := range dctx.supportedLanguages {
lang := mustConvertPulumiSchemaLanguage(lang)
if f.Inputs != nil {
exclude := func(name string) bool {
return name == "__self__"
Expand Down
2 changes: 1 addition & 1 deletion tools/resourcedocsgen/pkg/docs/templates/properties.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{{- range $lang, $props := . }}

<div>
<pulumi-choosable type="language" values="{{- if eq $lang "nodejs" -}}{{ print "javascript,typescript" }}{{- else -}}{{ print $lang }}{{- end -}}">
<pulumi-choosable type="language" values="{{- if eq $lang lNodejs -}}{{ print "javascript,typescript" }}{{- else -}}{{ print $lang }}{{- end -}}">
<dl class="resources-properties">
{{- range . -}}
<dt class="property-{{- if .IsInput -}}{{- if .IsRequired -}}required{{- else -}}optional{{- end -}}{{- end -}}{{ if .DeprecationMessage }} property-deprecated{{- end -}}{{ if .IsReplaceOnChanges }} property-replacement{{- end -}}"
Expand Down
2 changes: 1 addition & 1 deletion tools/resourcedocsgen/pkg/util/language/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
Go = Language{"go"}
Java = Language{"java"}
Python = Language{"python"}
Typescript = Language{"typescript"}
Typescript = Language{"nodejs"}
YAML = Language{"yaml"}
)

Expand Down

0 comments on commit d3889cc

Please sign in to comment.