Skip to content

Commit

Permalink
Merge 646f24a into 013fcde
Browse files Browse the repository at this point in the history
  • Loading branch information
pgavlin committed Nov 18, 2021
2 parents 013fcde + 646f24a commit 6582e74
Show file tree
Hide file tree
Showing 66 changed files with 269 additions and 1,729 deletions.
134 changes: 49 additions & 85 deletions pkg/codegen/go/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,16 +780,14 @@ type genInputImplementationArgs struct {
elementType string
ptrMethods bool
toOutputMethods bool
resourceType bool
}

func genInputImplementation(w io.Writer, name, receiverType, elementType string, ptrMethods, resourceType bool) {
func genInputImplementation(w io.Writer, name, receiverType, elementType string, ptrMethods bool) {
genInputImplementationWithArgs(w, genInputImplementationArgs{
name: name,
receiverType: receiverType,
elementType: elementType,
ptrMethods: ptrMethods,
resourceType: resourceType,
toOutputMethods: true,
})
}
Expand All @@ -798,14 +796,9 @@ func genInputImplementationWithArgs(w io.Writer, genArgs genInputImplementationA
name := genArgs.name
receiverType := genArgs.receiverType
elementType := genArgs.elementType
resourceType := genArgs.resourceType

fmt.Fprintf(w, "func (%s) ElementType() reflect.Type {\n", receiverType)
if resourceType {
fmt.Fprintf(w, "\treturn reflect.TypeOf((*%s)(nil))\n", elementType)
} else {
fmt.Fprintf(w, "\treturn reflect.TypeOf((*%s)(nil)).Elem()\n", elementType)
}
fmt.Fprintf(w, "\treturn reflect.TypeOf((*%s)(nil)).Elem()\n", elementType)
fmt.Fprintf(w, "}\n\n")

if genArgs.toOutputMethods {
Expand Down Expand Up @@ -833,15 +826,11 @@ func genInputImplementationWithArgs(w io.Writer, genArgs genInputImplementationA
}
}

func genOutputType(w io.Writer, baseName, elementType string, ptrMethods, resourceType bool) {
func genOutputType(w io.Writer, baseName, elementType string, ptrMethods bool) {
fmt.Fprintf(w, "type %sOutput struct { *pulumi.OutputState }\n\n", baseName)

fmt.Fprintf(w, "func (%sOutput) ElementType() reflect.Type {\n", baseName)
if resourceType {
fmt.Fprintf(w, "\treturn reflect.TypeOf((*%s)(nil))\n", elementType)
} else {
fmt.Fprintf(w, "\treturn reflect.TypeOf((*%s)(nil)).Elem()\n", elementType)
}
fmt.Fprintf(w, "\treturn reflect.TypeOf((*%s)(nil)).Elem()\n", elementType)
fmt.Fprintf(w, "}\n\n")

fmt.Fprintf(w, "func (o %[1]sOutput) To%[2]sOutput() %[1]sOutput {\n", baseName, Title(baseName))
Expand All @@ -865,8 +854,8 @@ func genOutputType(w io.Writer, baseName, elementType string, ptrMethods, resour
}
}

func genArrayOutput(w io.Writer, baseName, elementType string, resourceType bool) {
genOutputType(w, baseName+"Array", "[]"+elementType, false, resourceType)
func genArrayOutput(w io.Writer, baseName, elementType string) {
genOutputType(w, baseName+"Array", "[]"+elementType, false)

fmt.Fprintf(w, "func (o %[1]sArrayOutput) Index(i pulumi.IntInput) %[1]sOutput {\n", baseName)
fmt.Fprintf(w, "\treturn pulumi.All(o, i).ApplyT(func (vs []interface{}) %s {\n", elementType)
Expand All @@ -875,8 +864,8 @@ func genArrayOutput(w io.Writer, baseName, elementType string, resourceType bool
fmt.Fprintf(w, "}\n\n")
}

func genMapOutput(w io.Writer, baseName, elementType string, resourceType bool) {
genOutputType(w, baseName+"Map", "map[string]"+elementType, false, resourceType)
func genMapOutput(w io.Writer, baseName, elementType string) {
genOutputType(w, baseName+"Map", "map[string]"+elementType, false)

fmt.Fprintf(w, "func (o %[1]sMapOutput) MapIndex(k pulumi.StringInput) %[1]sOutput {\n", baseName)
fmt.Fprintf(w, "\treturn pulumi.All(o, k).ApplyT(func (vs []interface{}) %s{\n", elementType)
Expand All @@ -885,8 +874,8 @@ func genMapOutput(w io.Writer, baseName, elementType string, resourceType bool)
fmt.Fprintf(w, "}\n\n")
}

func genPtrOutput(w io.Writer, baseName, elementType string, resourceType bool) {
genOutputType(w, baseName+"Ptr", "*"+elementType, false, resourceType)
func genPtrOutput(w io.Writer, baseName, elementType string) {
genOutputType(w, baseName+"Ptr", "*"+elementType, false)

fmt.Fprintf(w, "func (o %[1]sPtrOutput) Elem() %[1]sOutput {\n", baseName)
fmt.Fprintf(w, "\treturn o.ApplyT(func(v *%[1]s) %[1]s {\n", baseName)
Expand Down Expand Up @@ -952,7 +941,7 @@ func (pkg *pkgContext) genEnum(w io.Writer, enumType *schema.EnumType) error {

fmt.Fprintf(w, "type %[1]sArray []%[1]s\n\n", name)

genInputImplementation(w, name+"Array", name+"Array", "[]"+name, false, false)
genInputImplementation(w, name+"Array", name+"Array", "[]"+name, false)
}

// Generate the map input.
Expand All @@ -961,24 +950,24 @@ func (pkg *pkgContext) genEnum(w io.Writer, enumType *schema.EnumType) error {

fmt.Fprintf(w, "type %[1]sMap map[string]%[1]s\n\n", name)

genInputImplementation(w, name+"Map", name+"Map", "map[string]"+name, false, false)
genInputImplementation(w, name+"Map", name+"Map", "map[string]"+name, false)
}

// Generate the array output
if details.arrayElement {
genArrayOutput(w, name, name, false)
genArrayOutput(w, name, name)
}

// Generate the map output.
if details.mapElement {
genMapOutput(w, name, name, false)
genMapOutput(w, name, name)
}

return nil
}

func (pkg *pkgContext) genEnumOutputTypes(w io.Writer, name, elementArgsType, elementGoType, asFuncName string) {
genOutputType(w, name, name, true, false)
genOutputType(w, name, name, true)

fmt.Fprintf(w, "func (o %[1]sOutput) To%[2]sOutput() %[3]sOutput {\n", name, asFuncName, elementArgsType)
fmt.Fprintf(w, "return o.To%sOutputWithContext(context.Background())\n", asFuncName)
Expand All @@ -1001,7 +990,7 @@ func (pkg *pkgContext) genEnumOutputTypes(w io.Writer, name, elementArgsType, el
fmt.Fprintf(w, "}).(%sPtrOutput)\n", elementArgsType)
fmt.Fprint(w, "}\n\n")

genPtrOutput(w, name, name, false)
genPtrOutput(w, name, name)

fmt.Fprintf(w, "func (o %[1]sPtrOutput) To%[2]sPtrOutput() %[3]sPtrOutput {\n", name, asFuncName, elementArgsType)
fmt.Fprintf(w, "return o.To%sPtrOutputWithContext(context.Background())\n", asFuncName)
Expand Down Expand Up @@ -1125,7 +1114,7 @@ func (pkg *pkgContext) genInputTypes(w io.Writer, t *schema.ObjectType, details

pkg.genInputArgsStruct(w, name+"Args", t)

genInputImplementation(w, name, name+"Args", name, details.ptrElement, false)
genInputImplementation(w, name, name+"Args", name, details.ptrElement)

// Generate the pointer input.
if details.ptrElement {
Expand All @@ -1139,7 +1128,7 @@ func (pkg *pkgContext) genInputTypes(w io.Writer, t *schema.ObjectType, details
fmt.Fprintf(w, "\treturn (*%s)(v)\n", ptrTypeName)
fmt.Fprintf(w, "}\n\n")

genInputImplementation(w, name+"Ptr", "*"+ptrTypeName, "*"+name, false, false)
genInputImplementation(w, name+"Ptr", "*"+ptrTypeName, "*"+name, false)
}

// Generate the array input.
Expand All @@ -1148,7 +1137,7 @@ func (pkg *pkgContext) genInputTypes(w io.Writer, t *schema.ObjectType, details

fmt.Fprintf(w, "type %[1]sArray []%[1]sInput\n\n", name)

genInputImplementation(w, name+"Array", name+"Array", "[]"+name, false, false)
genInputImplementation(w, name+"Array", name+"Array", "[]"+name, false)
}

// Generate the map input.
Expand All @@ -1157,7 +1146,7 @@ func (pkg *pkgContext) genInputTypes(w io.Writer, t *schema.ObjectType, details

fmt.Fprintf(w, "type %[1]sMap map[string]%[1]sInput\n\n", name)

genInputImplementation(w, name+"Map", name+"Map", "map[string]"+name, false, false)
genInputImplementation(w, name+"Map", name+"Map", "map[string]"+name, false)
}
}

Expand Down Expand Up @@ -1196,7 +1185,6 @@ func (pkg *pkgContext) genOutputTypes(w io.Writer, genArgs genOutputTypesArgs) {
name, /* baseName */
name, /* elementType */
details.ptrElement, /* ptrMethods */
false, /* resourceType */
)

for _, p := range t.Properties {
Expand All @@ -1215,7 +1203,7 @@ func (pkg *pkgContext) genOutputTypes(w io.Writer, genArgs genOutputTypesArgs) {
}

if details.ptrElement {
genPtrOutput(w, name, name, false)
genPtrOutput(w, name, name)

for _, p := range t.Properties {
printCommentWithDeprecationMessage(w, p.Comment, p.DeprecationMessage, false)
Expand Down Expand Up @@ -1248,11 +1236,11 @@ func (pkg *pkgContext) genOutputTypes(w io.Writer, genArgs genOutputTypesArgs) {
}

if details.arrayElement {
genArrayOutput(w, name, name, false)
genArrayOutput(w, name, name)
}

if details.mapElement {
genMapOutput(w, name, name, false)
genMapOutput(w, name, name)
}
}

Expand Down Expand Up @@ -1704,42 +1692,26 @@ func (pkg *pkgContext) genResource(w io.Writer, r *schema.Resource, generateReso
fmt.Fprintf(w, "\tTo%[1]sOutputWithContext(ctx context.Context) %[1]sOutput\n", name)
fmt.Fprintf(w, "}\n\n")

genInputImplementation(w, name, "*"+name, name, generateResourceContainerTypes, true)

if generateResourceContainerTypes {
// Emit the resource pointer input type.
fmt.Fprintf(w, "type %sPtrInput interface {\n", name)
fmt.Fprintf(w, "\tpulumi.Input\n\n")
fmt.Fprintf(w, "\tTo%[1]sPtrOutput() %[1]sPtrOutput\n", name)
fmt.Fprintf(w, "\tTo%[1]sPtrOutputWithContext(ctx context.Context) %[1]sPtrOutput\n", name)
fmt.Fprintf(w, "}\n\n")
ptrTypeName := camel(name) + "PtrType"
fmt.Fprintf(w, "type %s %sArgs\n\n", ptrTypeName, name)
genInputImplementation(w, name+"Ptr", "*"+ptrTypeName, "*"+name, false, true)
genInputImplementation(w, name, "*"+name, "*"+name, false)

if !r.IsProvider {
// Generate the resource array input.
pkg.genInputInterface(w, name+"Array")
fmt.Fprintf(w, "type %[1]sArray []%[1]sInput\n\n", name)
genInputImplementation(w, name+"Array", name+"Array", "[]*"+name, false, false)
if generateResourceContainerTypes && !r.IsProvider {
// Generate the resource array input.
pkg.genInputInterface(w, name+"Array")
fmt.Fprintf(w, "type %[1]sArray []%[1]sInput\n\n", name)
genInputImplementation(w, name+"Array", name+"Array", "[]*"+name, false)

// Generate the resource map input.
pkg.genInputInterface(w, name+"Map")
fmt.Fprintf(w, "type %[1]sMap map[string]%[1]sInput\n\n", name)
genInputImplementation(w, name+"Map", name+"Map", "map[string]*"+name, false, false)
}
// Generate the resource map input.
pkg.genInputInterface(w, name+"Map")
fmt.Fprintf(w, "type %[1]sMap map[string]%[1]sInput\n\n", name)
genInputImplementation(w, name+"Map", name+"Map", "map[string]*"+name, false)
}

// Emit the resource output type.
genOutputType(w, name, name, generateResourceContainerTypes, true)

if generateResourceContainerTypes {
genPtrOutput(w, name, name, true)
genOutputType(w, name, "*"+name, false)

if !r.IsProvider {
genArrayOutput(w, name, name, true)
genMapOutput(w, name, name, true)
}
if generateResourceContainerTypes && !r.IsProvider {
genArrayOutput(w, name, "*"+name)
genMapOutput(w, name, "*"+name)
}

pkg.genResourceRegistrations(w, r, generateResourceContainerTypes)
Expand Down Expand Up @@ -2096,16 +2068,16 @@ func (pkg *pkgContext) genNestedCollectionTypes(w io.Writer, types map[string]ma
names = append(names, name)
if strings.HasSuffix(name, "Array") {
fmt.Fprintf(w, "type %s []%sInput\n\n", name, elementTypeName)
genInputImplementation(w, name, name, elementTypeName, false, false)
genInputImplementation(w, name, name, elementTypeName, false)

genArrayOutput(w, strings.TrimSuffix(name, "Array"), elementTypeName, false)
genArrayOutput(w, strings.TrimSuffix(name, "Array"), elementTypeName)
}

if strings.HasSuffix(name, "Map") {
fmt.Fprintf(w, "type %s map[string]%sInput\n\n", name, elementTypeName)
genInputImplementation(w, name, name, elementTypeName, false, false)
genInputImplementation(w, name, name, elementTypeName, false)

genMapOutput(w, strings.TrimSuffix(name, "Map"), elementTypeName, false)
genMapOutput(w, strings.TrimSuffix(name, "Map"), elementTypeName)
}
pkg.genInputInterface(w, name)
}
Expand Down Expand Up @@ -2232,18 +2204,13 @@ func (pkg *pkgContext) genResourceRegistrations(w io.Writer, r *schema.Resource,
fmt.Fprintf(w,
"\tpulumi.RegisterInputType(reflect.TypeOf((*%[1]sInput)(nil)).Elem(), &%[1]s{})\n",
name)
if generateResourceContainerTypes {
if generateResourceContainerTypes && !r.IsProvider {
fmt.Fprintf(w,
"\tpulumi.RegisterInputType(reflect.TypeOf((*%[1]sPtrInput)(nil)).Elem(), &%[1]s{})\n",
"\tpulumi.RegisterInputType(reflect.TypeOf((*%[1]sArrayInput)(nil)).Elem(), %[1]sArray{})\n",
name)
fmt.Fprintf(w,
"\tpulumi.RegisterInputType(reflect.TypeOf((*%[1]sMapInput)(nil)).Elem(), %[1]sMap{})\n",
name)
if !r.IsProvider {
fmt.Fprintf(w,
"\tpulumi.RegisterInputType(reflect.TypeOf((*%[1]sArrayInput)(nil)).Elem(), %[1]sArray{})\n",
name)
fmt.Fprintf(w,
"\tpulumi.RegisterInputType(reflect.TypeOf((*%[1]sMapInput)(nil)).Elem(), %[1]sMap{})\n",
name)
}
}
}
// Register all output types
Expand All @@ -2258,12 +2225,9 @@ func (pkg *pkgContext) genResourceRegistrations(w io.Writer, r *schema.Resource,
}
}

if generateResourceContainerTypes {
fmt.Fprintf(w, "\tpulumi.RegisterOutputType(%sPtrOutput{})\n", name)
if !r.IsProvider {
fmt.Fprintf(w, "\tpulumi.RegisterOutputType(%sArrayOutput{})\n", name)
fmt.Fprintf(w, "\tpulumi.RegisterOutputType(%sMapOutput{})\n", name)
}
if generateResourceContainerTypes && !r.IsProvider {
fmt.Fprintf(w, "\tpulumi.RegisterOutputType(%sArrayOutput{})\n", name)
fmt.Fprintf(w, "\tpulumi.RegisterOutputType(%sMapOutput{})\n", name)
}
fmt.Fprintf(w, "}\n\n")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type ProviderInput interface {
}

func (*Provider) ElementType() reflect.Type {
return reflect.TypeOf((*Provider)(nil))
return reflect.TypeOf((**Provider)(nil)).Elem()
}

func (i *Provider) ToProviderOutput() ProviderOutput {
Expand All @@ -62,7 +62,7 @@ func (i *Provider) ToProviderOutputWithContext(ctx context.Context) ProviderOutp
type ProviderOutput struct{ *pulumi.OutputState }

func (ProviderOutput) ElementType() reflect.Type {
return reflect.TypeOf((*Provider)(nil))
return reflect.TypeOf((**Provider)(nil)).Elem()
}

func (o ProviderOutput) ToProviderOutput() ProviderOutput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type ProviderInput interface {
}

func (*Provider) ElementType() reflect.Type {
return reflect.TypeOf((*Provider)(nil))
return reflect.TypeOf((**Provider)(nil)).Elem()
}

func (i *Provider) ToProviderOutput() ProviderOutput {
Expand All @@ -62,7 +62,7 @@ func (i *Provider) ToProviderOutputWithContext(ctx context.Context) ProviderOutp
type ProviderOutput struct{ *pulumi.OutputState }

func (ProviderOutput) ElementType() reflect.Type {
return reflect.TypeOf((*Provider)(nil))
return reflect.TypeOf((**Provider)(nil)).Elem()
}

func (o ProviderOutput) ToProviderOutput() ProviderOutput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type ModuleResourceInput interface {
}

func (*ModuleResource) ElementType() reflect.Type {
return reflect.TypeOf((*ModuleResource)(nil))
return reflect.TypeOf((**ModuleResource)(nil)).Elem()
}

func (i *ModuleResource) ToModuleResourceOutput() ModuleResourceOutput {
Expand All @@ -90,7 +90,7 @@ func (i *ModuleResource) ToModuleResourceOutputWithContext(ctx context.Context)
type ModuleResourceOutput struct{ *pulumi.OutputState }

func (ModuleResourceOutput) ElementType() reflect.Type {
return reflect.TypeOf((*ModuleResource)(nil))
return reflect.TypeOf((**ModuleResource)(nil)).Elem()
}

func (o ModuleResourceOutput) ToModuleResourceOutput() ModuleResourceOutput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type ProviderInput interface {
}

func (*Provider) ElementType() reflect.Type {
return reflect.TypeOf((*Provider)(nil))
return reflect.TypeOf((**Provider)(nil)).Elem()
}

func (i *Provider) ToProviderOutput() ProviderOutput {
Expand All @@ -62,7 +62,7 @@ func (i *Provider) ToProviderOutputWithContext(ctx context.Context) ProviderOutp
type ProviderOutput struct{ *pulumi.OutputState }

func (ProviderOutput) ElementType() reflect.Type {
return reflect.TypeOf((*Provider)(nil))
return reflect.TypeOf((**Provider)(nil)).Elem()
}

func (o ProviderOutput) ToProviderOutput() ProviderOutput {
Expand Down
Loading

0 comments on commit 6582e74

Please sign in to comment.