Skip to content

Commit

Permalink
Unify type representations in gen. (#905)
Browse files Browse the repository at this point in the history
Rather than importing all types three times--once each for the
inputs, outputs, and provider APIs--simply import the types once, and
calculate the input, output, and provider API type for each property.

These changes also track whether or not a kind is nested s.t. the
unified set of types can be used to generate the input, output, and
provider APIs.

This is in preparation for Go codegen, which needs all of this
information at once.
  • Loading branch information
pgavlin authored Dec 6, 2019
1 parent 01cb280 commit 0162c7f
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 171 deletions.
2 changes: 1 addition & 1 deletion pkg/gen/dotnet-templates/kind.cs.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Pulumi.Kubernetes.{{Group}}.{{Version}}
{{#Properties}}
{{{Comment}}}
[Output("{{Name}}")]
public {{{PropType}}} {{LanguageName}} { get; private set; } = null!;
public {{{ProviderType}}} {{LanguageName}} { get; private set; } = null!;

{{/Properties}}

Expand Down
16 changes: 8 additions & 8 deletions pkg/gen/dotnet-templates/typesInput.cs.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,38 @@ namespace Pulumi.Kubernetes.Types.Inputs.{{Group}}
{{#RequiredInputProperties}}
{{#DotnetIsListOrMap}}
[Input("{{Name}}", required: true)]
private {{{PropType}}}? _{{Name}};
private {{{InputsAPIType}}}? _{{Name}};

{{{Comment}}}
public {{{PropType}}} {{LanguageName}}
public {{{InputsAPIType}}} {{LanguageName}}
{
get => _{{Name}} ?? (_{{Name}} = new {{{PropType}}}());
get => _{{Name}} ?? (_{{Name}} = new {{{InputsAPIType}}}());
set => _{{Name}} = value;
}
{{/DotnetIsListOrMap}}
{{^DotnetIsListOrMap}}
{{{Comment}}}
[Input("{{Name}}", required: true)]
public {{{PropType}}} {{LanguageName}} { get; set; } = null!;
public {{{InputsAPIType}}} {{LanguageName}} { get; set; } = null!;
{{/DotnetIsListOrMap}}

{{/RequiredInputProperties}}
{{#OptionalInputProperties}}
{{#DotnetIsListOrMap}}
[Input("{{Name}}")]
private {{{PropType}}}? _{{Name}};
private {{{InputsAPIType}}}? _{{Name}};

{{{Comment}}}
public {{{PropType}}} {{LanguageName}}
public {{{InputsAPIType}}} {{LanguageName}}
{
get => _{{Name}} ?? (_{{Name}} = new {{{PropType}}}());
get => _{{Name}} ?? (_{{Name}} = new {{{InputsAPIType}}}());
set => _{{Name}} = value;
}
{{/DotnetIsListOrMap}}
{{^DotnetIsListOrMap}}
{{{Comment}}}
[Input("{{Name}}")]
public {{{PropType}}}? {{LanguageName}} { get; set; }
public {{{InputsAPIType}}}? {{LanguageName}} { get; set; }
{{/DotnetIsListOrMap}}

{{/OptionalInputProperties}}
Expand Down
4 changes: 2 additions & 2 deletions pkg/gen/dotnet-templates/typesOutput.cs.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ namespace Pulumi.Kubernetes.Types.Outputs.{{Group}}
{
{{#Properties}}
{{{Comment}}}
public readonly {{{PropType}}} {{LanguageName}};
public readonly {{{OutputsAPIType}}} {{LanguageName}};

{{/Properties}}
[OutputConstructor]
private {{Kind}}(
{{#Properties}}
{{{PropType}}} {{DotnetVarName}}{{^IsLast}},{{/IsLast}}{{#IsLast}}){{/IsLast}}
{{{OutputsAPIType}}} {{DotnetVarName}}{{^IsLast}},{{/IsLast}}{{#IsLast}}){{/IsLast}}
{{/Properties}}
{
{{#Properties}}
Expand Down
14 changes: 5 additions & 9 deletions pkg/gen/dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,28 @@ func DotnetClient(
) (inputsts, outputsts string, groups map[string]string, err error) {
definitions := swagger["definitions"].(map[string]interface{})

inputGroupsSlice := createGroups(definitions, dotnetInputs())
groupsSlice := createGroups(definitions, dotnetOpts())

inputsts, err = mustache.RenderFile(fmt.Sprintf("%s/typesInput.cs.mustache", templateDir),
map[string]interface{}{
"Groups": inputGroupsSlice,
"Groups": groupsSlice,
})
if err != nil {
return
}

outputGroupsSlice := createGroups(definitions, dotnetOutputs())
outputsts, err = mustache.RenderFile(fmt.Sprintf("%s/typesOutput.cs.mustache", templateDir),
map[string]interface{}{
"Groups": outputGroupsSlice,
"Groups": groupsSlice,
})
if err != nil {
return
}

groupsSlice := createGroups(definitions, dotnetProvider())
fmt.Printf("%v\n", groupsSlice)

groups = make(map[string]string)

for _, group := range groupsSlice {
for _, version := range group.Versions() {
for _, kind := range version.Kinds() {
for _, kind := range version.TopLevelKinds() {
inputMap := map[string]interface{}{
"RawAPIVersion": kind.RawAPIVersion(),
"Comment": kind.Comment(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/gen/nodejs-templates/kind.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getVersion } from "../../version";
export class {{Kind}} extends pulumi.CustomResource {
{{#Properties}}
{{{Comment}}}
public readonly {{Name}}: {{{PropType}}};
public readonly {{Name}}: {{{ProviderType}}};

{{/Properties}}
/**
Expand Down
4 changes: 3 additions & 1 deletion pkg/gen/nodejs-templates/providerIndex.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ export { helm, yaml };

// Import groups
{{#Groups}}
{{#HasTopLevelKinds}}
import * as {{Group}} from "./{{Group}}/index";
{{/HasTopLevelKinds}}
{{/Groups}}

// Export sub-modules
export { {{#Groups}}{{Group}}, {{/Groups}} };
export { {{#Groups}}{{#HasTopLevelKinds}}{{Group}}, {{/HasTopLevelKinds}}{{/Groups}} };

// Import and export sub-modules for all Kubernetes types.
import * as types from "./types";
Expand Down
4 changes: 2 additions & 2 deletions pkg/gen/nodejs-templates/typesInput.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export namespace {{Group}} {
export interface {{Kind}} {
{{#RequiredInputProperties}}
{{{Comment}}}
{{Name}}: {{{PropType}}}
{{Name}}: {{{InputsAPIType}}}

{{/RequiredInputProperties}}
{{#OptionalInputProperties}}
{{{Comment}}}
{{Name}}?: {{{PropType}}}
{{Name}}?: {{{InputsAPIType}}}

{{/OptionalInputProperties}}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gen/nodejs-templates/typesOutput.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export namespace {{Group}} {
export interface {{Kind}} {
{{#Properties}}
{{{Comment}}}
readonly {{Name}}: {{{PropType}}}
readonly {{Name}}: {{{OutputsAPIType}}}

{{/Properties}}
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/gen/nodejs-templates/yaml.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ import * as outputs from "../types/output";
*/
{{#Groups}}
{{#Versions}}
{{#KindsAndAliases}}
{{#TopLevelKindsAndAliases}}
public getResource(groupVersionKind: "{{RawAPIVersion}}/{{Kind}}", name: string): pulumi.Output<k8s.{{Group}}.{{Version}}.{{Kind}}>;
public getResource(groupVersionKind: "{{RawAPIVersion}}/{{Kind}}", namespace: string, name: string): pulumi.Output<k8s.{{Group}}.{{Version}}.{{Kind}}>;
{{/KindsAndAliases}}
{{/TopLevelKindsAndAliases}}
{{/Versions}}
{{/Groups}}
public getResource(groupVersionKind: string, namespaceOrName: string, name?: string): pulumi.Output<pulumi.CustomResource> {
Expand All @@ -230,12 +230,12 @@ import * as outputs from "../types/output";
*/
{{#Groups}}
{{#Versions}}
{{#KindsAndAliases}}
{{#TopLevelKindsAndAliases}}
{{#Properties}}
public getResourceProperty(groupVersionKind: "{{RawAPIVersion}}/{{Kind}}", name: string, property: "{{LanguageName}}"): {{{PropType}}};
public getResourceProperty(groupVersionKind: "{{RawAPIVersion}}/{{Kind}}", namespace: string, name: string, property: "{{LanguageName}}"): {{{PropType}}};
public getResourceProperty(groupVersionKind: "{{RawAPIVersion}}/{{Kind}}", name: string, property: "{{LanguageName}}"): {{{ProviderType}}};
public getResourceProperty(groupVersionKind: "{{RawAPIVersion}}/{{Kind}}", namespace: string, name: string, property: "{{LanguageName}}"): {{{ProviderType}}};
{{/Properties}}
{{/KindsAndAliases}}
{{/TopLevelKindsAndAliases}}
{{/Versions}}
{{/Groups}}
public getResourceProperty(groupVersionKind: string, namespaceOrName: string, nameOrProperty: string, property?: string): pulumi.Output<any> {
Expand Down Expand Up @@ -400,9 +400,9 @@ import * as outputs from "../types/output";
(apiVersion == "v1" && kind == "List")
{{#Groups}}
{{#Versions}}
{{#ListKindsAndAliases}}
{{#ListTopLevelKindsAndAliases}}
|| (apiVersion == "{{RawAPIVersion}}" && kind == "{{Kind}}")
{{/ListKindsAndAliases}}
{{/ListTopLevelKindsAndAliases}}
{{/Versions}}
{{/Groups}}
) {
Expand Down Expand Up @@ -430,13 +430,13 @@ import * as outputs from "../types/output";
switch (`${apiVersion}/${kind}`) {
{{#Groups}}
{{#Versions}}
{{#KindsAndAliases}}
{{#TopLevelKindsAndAliases}}
case "{{RawAPIVersion}}/{{Kind}}":
return [id.apply(id => ({
name: `{{RawAPIVersion}}/{{Kind}}::${id}`,
resource: new k8s.{{Group}}.{{Version}}.{{Kind}}(id, obj, opts),
}))];
{{/KindsAndAliases}}
{{/TopLevelKindsAndAliases}}
{{/Versions}}
{{/Groups}}
default:
Expand Down
17 changes: 12 additions & 5 deletions pkg/gen/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func NodeJSClient(swagger map[string]interface{}, templateDir string,
) (inputsts, outputsts, indexts, yamlts, packagejson string, groupsts map[string]*GroupTS, err error) {
definitions := swagger["definitions"].(map[string]interface{})

groupsSlice := createGroups(definitions, nodeJSInputs())
groupsSlice := createGroups(definitions, nodeJSOpts())

inputsts, err = mustache.RenderFile(fmt.Sprintf("%s/typesInput.ts.mustache", templateDir),
map[string]interface{}{
"Groups": groupsSlice,
Expand All @@ -51,7 +52,6 @@ func NodeJSClient(swagger map[string]interface{}, templateDir string,
return
}

groupsSlice = createGroups(definitions, nodeJSOutputs())
outputsts, err = mustache.RenderFile(fmt.Sprintf("%s/typesOutput.ts.mustache", templateDir),
map[string]interface{}{
"Groups": groupsSlice,
Expand All @@ -60,16 +60,23 @@ func NodeJSClient(swagger map[string]interface{}, templateDir string,
return
}

groupsSlice = createGroups(definitions, nodeJSProvider())
groupsts = make(map[string]*GroupTS)
for _, group := range groupsSlice {
if !group.HasTopLevelKinds() {
continue
}

groupTS := &GroupTS{}
for _, version := range group.Versions() {
if !version.HasTopLevelKinds() {
continue
}

if groupTS.Versions == nil {
groupTS.Versions = make(map[string]*VersionTS)
}
versionTS := &VersionTS{}
for _, kind := range version.Kinds() {
for _, kind := range version.TopLevelKinds() {
if versionTS.Kinds == nil {
versionTS.Kinds = make(map[string]string)
}
Expand Down Expand Up @@ -103,7 +110,7 @@ func NodeJSClient(swagger map[string]interface{}, templateDir string,

kindIndexTS, err := mustache.RenderFile(fmt.Sprintf("%s/kindIndex.ts.mustache", templateDir),
map[string]interface{}{
"Kinds": version.Kinds(),
"Kinds": version.TopLevelKinds(),
})
if err != nil {
return "", "", "", "", "", nil, err
Expand Down
6 changes: 3 additions & 3 deletions pkg/gen/python-templates/kind.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class {{Kind}}(pulumi.CustomResource):
"""

{{#Properties}}
{{LanguageName}}: {{{PropType}}}
{{LanguageName}}: {{{ProviderType}}}
{{{Comment}}}

{{/Properties}}
Expand All @@ -42,10 +42,10 @@ class {{Kind}}(pulumi.CustomResource):
:param str resource_name: The _unique_ name of the resource.
:param pulumi.ResourceOptions opts: A bag of options that control this resource's behavior.
{{#RequiredInputProperties}}
:param {{{PythonConstructorPropType}}} {{LanguageName}}: {{{PythonConstructorComment}}}
:param {{{InputsAPIType}}} {{LanguageName}}: {{{PythonConstructorComment}}}
{{/RequiredInputProperties}}
{{#OptionalInputProperties}}
:param {{{PythonConstructorPropType}}} {{LanguageName}}: {{{PythonConstructorComment}}}
:param {{{InputsAPIType}}} {{LanguageName}}: {{{PythonConstructorComment}}}
{{/OptionalInputProperties}}
"""
if __name__ is not None:
Expand Down
2 changes: 2 additions & 0 deletions pkg/gen/python-templates/root__init__.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
# Make subpackages available:
__all__ = [
{{#Groups}}
{{#HasTopLevelKinds}}
"{{Group}}",
{{/HasTopLevelKinds}}
{{/Groups}}
"helm",
"provider",
Expand Down
4 changes: 2 additions & 2 deletions pkg/gen/python-templates/version__init__.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
# *** Do not edit by hand unless you're certain you know what you are doing! ***

# Export this package's modules as members:
{{#Kinds}}
{{#TopLevelKinds}}
from .{{Kind}} import ({{Kind}})
{{/Kinds}}
{{/TopLevelKinds}}
4 changes: 2 additions & 2 deletions pkg/gen/python-templates/yaml.py.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@ def _parse_yaml_object(
gvk = f"{api_version}/{kind}"
{{#Groups}}
{{#Versions}}
{{#Kinds}}
{{#TopLevelKinds}}
if gvk == "{{RawAPIVersion}}/{{Kind}}":
# Import locally to avoid name collisions.
from pulumi_kubernetes.{{Group}}.{{Version}} import {{Kind}}
return [identifier.apply(
lambda x: (f"{{RawAPIVersion}}/{{Kind}}:{x}",
{{Kind}}(f"{x}", opts, **obj)))]
{{/Kinds}}
{{/TopLevelKinds}}
{{/Versions}}
{{/Groups}}
return [identifier.apply(
Expand Down
13 changes: 10 additions & 3 deletions pkg/gen/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func PythonClient(

// Generate casing tables from property names.
// { properties: [ {name: fooBar, casedName: foo_bar}, ]}
properties := allCamelCasePropertyNames(definitions, pythonProvider())
properties := allCamelCasePropertyNames(definitions, pythonOpts())
cases := map[string][]map[string]string{"properties": make([]map[string]string, 0)}
for _, name := range properties {
cases["properties"] = append(cases["properties"],
Expand All @@ -61,7 +61,7 @@ func PythonClient(
return err
}

groupsSlice := createGroups(definitions, pythonProvider())
groupsSlice := createGroups(definitions, pythonOpts())

yamlPy, err := mustache.RenderFile(
fmt.Sprintf("%s/yaml.py.mustache", templateDir),
Expand Down Expand Up @@ -90,6 +90,9 @@ func PythonClient(
}

for _, group := range groupsSlice {
if !group.HasTopLevelKinds() {
continue
}

groupInitPy, err := mustache.RenderFile(
fmt.Sprintf("%s/group__init__.py.mustache", templateDir), group)
Expand Down Expand Up @@ -118,6 +121,10 @@ from .CustomResource import (CustomResource)
}

for _, version := range group.Versions() {
if !version.HasTopLevelKinds() {
continue
}

versionInitPy, err := mustache.RenderFile(
fmt.Sprintf("%s/version__init__.py.mustache", templateDir), version)
if err != nil {
Expand All @@ -129,7 +136,7 @@ from .CustomResource import (CustomResource)
return err
}

for _, kind := range version.Kinds() {
for _, kind := range version.TopLevelKinds() {
inputMap := map[string]interface{}{
"RawAPIVersion": kind.RawAPIVersion(),
"Comment": kind.Comment(),
Expand Down
Loading

0 comments on commit 0162c7f

Please sign in to comment.