From 47df4ca8beca980b026d0741bb425ea6e20369a1 Mon Sep 17 00:00:00 2001 From: Pat Gavlin Date: Mon, 14 Jun 2021 14:51:50 -0700 Subject: [PATCH] [codegen/pcl] Allow missing object properties. Add an option to allow missing object properties. This will prevent us from losing examples once resource typechecking is fixed (it is currently unintentionally disabled because the resource inputs object type has an unexpected shape). --- pkg/codegen/hcl2/binder.go | 11 ++++++++--- pkg/codegen/hcl2/binder_schema.go | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pkg/codegen/hcl2/binder.go b/pkg/codegen/hcl2/binder.go index 0540d7ca63a7..12ce5716a947 100644 --- a/pkg/codegen/hcl2/binder.go +++ b/pkg/codegen/hcl2/binder.go @@ -29,9 +29,10 @@ import ( ) type bindOptions struct { - allowMissingVariables bool - loader schema.Loader - packageCache *PackageCache + allowMissingVariables bool + allowMissingProperties bool + loader schema.Loader + packageCache *PackageCache } func (opts bindOptions) modelOptions() []model.BindOption { @@ -58,6 +59,10 @@ func AllowMissingVariables(options *bindOptions) { options.allowMissingVariables = true } +func AllowMissingProperties(options *bindOptions) { + options.allowMissingProperties = true +} + func PluginHost(host plugin.Host) BindOption { return Loader(schema.NewPluginLoader(host)) } diff --git a/pkg/codegen/hcl2/binder_schema.go b/pkg/codegen/hcl2/binder_schema.go index 23a9dc61afb9..45d14f34b8a7 100644 --- a/pkg/codegen/hcl2/binder_schema.go +++ b/pkg/codegen/hcl2/binder_schema.go @@ -168,7 +168,7 @@ func (b *binder) schemaTypeToTypeImpl(src schema.Type, seen map[schema.Type]mode seen[src] = objType for _, prop := range src.Properties { t := b.schemaTypeToTypeImpl(prop.Type, seen) - if !prop.IsRequired { + if !prop.IsRequired || b.options.allowMissingProperties { t = model.NewOptionalType(t) } if prop.ConstValue != nil {