diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index 236882035..b74826b5a 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -23,6 +23,9 @@ - [codegen/go] Register input types for schema object types. [#7959](https://github.com/pulumi/pulumi/pull/7959) +- [codegen/go] Add schema flag to disable registering input types. + [#8198](https://github.com/pulumi/pulumi/pull/8198) + ### Bug Fixes - [codegen/go] - Use `importBasePath` before `name` if specified for name diff --git a/pkg/codegen/go/gen.go b/pkg/codegen/go/gen.go index 07c1538de..18c3d4e0e 100644 --- a/pkg/codegen/go/gen.go +++ b/pkg/codegen/go/gen.go @@ -113,6 +113,9 @@ type pkgContext struct { // Determines whether to make single-return-value methods return an output struct or the value liftSingleValueMethodReturns bool + + // Determines if we should emit type registration code + disableInputTypeRegistrations bool } func (pkg *pkgContext) detailsForType(t schema.Type) *typeDetails { @@ -2144,24 +2147,26 @@ func (pkg *pkgContext) genTypeRegistrations(w io.Writer, objTypes []*schema.Obje fmt.Fprintf(w, "func init() {\n") // Input types. - for _, obj := range objTypes { - name, details := pkg.tokenToType(obj.Token), pkg.detailsForType(obj) - fmt.Fprintf(w, "\tpulumi.RegisterInputType(reflect.TypeOf((*%[1]sInput)(nil)).Elem(), %[1]sArgs{})\n", name) - if details.ptrElement { - fmt.Fprintf(w, - "\tpulumi.RegisterInputType(reflect.TypeOf((*%[1]sPtrInput)(nil)).Elem(), %[1]sArgs{})\n", name) + if !pkg.disableInputTypeRegistrations { + for _, obj := range objTypes { + name, details := pkg.tokenToType(obj.Token), pkg.detailsForType(obj) + fmt.Fprintf(w, "\tpulumi.RegisterInputType(reflect.TypeOf((*%[1]sInput)(nil)).Elem(), %[1]sArgs{})\n", name) + if details.ptrElement { + fmt.Fprintf(w, + "\tpulumi.RegisterInputType(reflect.TypeOf((*%[1]sPtrInput)(nil)).Elem(), %[1]sArgs{})\n", name) + } + if details.arrayElement { + fmt.Fprintf(w, + "\tpulumi.RegisterInputType(reflect.TypeOf((*%[1]sArrayInput)(nil)).Elem(), %[1]sArray{})\n", name) + } + if details.mapElement { + fmt.Fprintf(w, + "\tpulumi.RegisterInputType(reflect.TypeOf((*%[1]sMapInput)(nil)).Elem(), %[1]sMap{})\n", name) + } } - if details.arrayElement { - fmt.Fprintf(w, - "\tpulumi.RegisterInputType(reflect.TypeOf((*%[1]sArrayInput)(nil)).Elem(), %[1]sArray{})\n", name) + for _, t := range types { + fmt.Fprintf(w, "\tpulumi.RegisterInputType(reflect.TypeOf((*%[1]sInput)(nil)).Elem(), %[1]s{})\n", t) } - if details.mapElement { - fmt.Fprintf(w, - "\tpulumi.RegisterInputType(reflect.TypeOf((*%[1]sMapInput)(nil)).Elem(), %[1]sMap{})\n", name) - } - } - for _, t := range types { - fmt.Fprintf(w, "\tpulumi.RegisterInputType(reflect.TypeOf((*%[1]sInput)(nil)).Elem(), %[1]s{})\n", t) } // Output types. @@ -2552,21 +2557,22 @@ func generatePackageContextMap(tool string, pkg *schema.Package, goInfo GoPackag pack, ok := packages[mod] if !ok { pack = &pkgContext{ - pkg: pkg, - mod: mod, - importBasePath: goInfo.ImportBasePath, - rootPackageName: goInfo.RootPackageName, - typeDetails: map[schema.Type]*typeDetails{}, - names: codegen.NewStringSet(), - schemaNames: codegen.NewStringSet(), - renamed: map[string]string{}, - duplicateTokens: map[string]bool{}, - functionNames: map[*schema.Function]string{}, - tool: tool, - modToPkg: goInfo.ModuleToPackage, - pkgImportAliases: goInfo.PackageImportAliases, - packages: packages, - liftSingleValueMethodReturns: goInfo.LiftSingleValueMethodReturns, + pkg: pkg, + mod: mod, + importBasePath: goInfo.ImportBasePath, + rootPackageName: goInfo.RootPackageName, + typeDetails: map[schema.Type]*typeDetails{}, + names: codegen.NewStringSet(), + schemaNames: codegen.NewStringSet(), + renamed: map[string]string{}, + duplicateTokens: map[string]bool{}, + functionNames: map[*schema.Function]string{}, + tool: tool, + modToPkg: goInfo.ModuleToPackage, + pkgImportAliases: goInfo.PackageImportAliases, + packages: packages, + liftSingleValueMethodReturns: goInfo.LiftSingleValueMethodReturns, + disableInputTypeRegistrations: goInfo.DisableInputTypeRegistrations, } packages[mod] = pack } diff --git a/pkg/codegen/go/importer.go b/pkg/codegen/go/importer.go index e05d6ea57..45a9ae817 100644 --- a/pkg/codegen/go/importer.go +++ b/pkg/codegen/go/importer.go @@ -1,4 +1,4 @@ -// Copyright 2016-2020, Pulumi Corporation. +// Copyright 2016-2021, Pulumi Corporation. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -56,6 +56,10 @@ type GoPackageInfo struct { // Determines whether to make single-return-value methods return an output struct or the value. LiftSingleValueMethodReturns bool `json:"liftSingleValueMethodReturns,omitempty"` + + // Feature flag to disable generating input type registration. This is a + // space saving measure. + DisableInputTypeRegistrations bool `json:"disableInputTypeRegistrations,omitempty"` } // Importer implements schema.Language for Go. diff --git a/pkg/codegen/internal/test/testdata/replace-on-change/go/example/pulumiTypes.go b/pkg/codegen/internal/test/testdata/replace-on-change/go/example/pulumiTypes.go index db6d0c6bb..0dc64b509 100644 --- a/pkg/codegen/internal/test/testdata/replace-on-change/go/example/pulumiTypes.go +++ b/pkg/codegen/internal/test/testdata/replace-on-change/go/example/pulumiTypes.go @@ -702,16 +702,6 @@ func (o ToyMapOutput) MapIndex(k pulumi.StringInput) ToyOutput { } func init() { - pulumi.RegisterInputType(reflect.TypeOf((*ChewInput)(nil)).Elem(), ChewArgs{}) - pulumi.RegisterInputType(reflect.TypeOf((*ChewPtrInput)(nil)).Elem(), ChewArgs{}) - pulumi.RegisterInputType(reflect.TypeOf((*LaserInput)(nil)).Elem(), LaserArgs{}) - pulumi.RegisterInputType(reflect.TypeOf((*LaserPtrInput)(nil)).Elem(), LaserArgs{}) - pulumi.RegisterInputType(reflect.TypeOf((*RecInput)(nil)).Elem(), RecArgs{}) - pulumi.RegisterInputType(reflect.TypeOf((*RecPtrInput)(nil)).Elem(), RecArgs{}) - pulumi.RegisterInputType(reflect.TypeOf((*ToyInput)(nil)).Elem(), ToyArgs{}) - pulumi.RegisterInputType(reflect.TypeOf((*ToyPtrInput)(nil)).Elem(), ToyArgs{}) - pulumi.RegisterInputType(reflect.TypeOf((*ToyArrayInput)(nil)).Elem(), ToyArray{}) - pulumi.RegisterInputType(reflect.TypeOf((*ToyMapInput)(nil)).Elem(), ToyMap{}) pulumi.RegisterOutputType(ChewOutput{}) pulumi.RegisterOutputType(ChewPtrOutput{}) pulumi.RegisterOutputType(LaserOutput{}) diff --git a/pkg/codegen/internal/test/testdata/replace-on-change/schema.json b/pkg/codegen/internal/test/testdata/replace-on-change/schema.json index 42da7625a..427ce4b41 100644 --- a/pkg/codegen/internal/test/testdata/replace-on-change/schema.json +++ b/pkg/codegen/internal/test/testdata/replace-on-change/schema.json @@ -139,7 +139,8 @@ } }, "go": { - "generateResourceContainerTypes": true + "generateResourceContainerTypes": true, + "disableInputTypeRegistrations": true }, "nodejs": { "dependencies": {