diff --git a/ext/typeexpr/get_type_test.go b/ext/typeexpr/get_type_test.go index 0249dffb..d349d72a 100644 --- a/ext/typeexpr/get_type_test.go +++ b/ext/typeexpr/get_type_test.go @@ -615,6 +615,67 @@ func TestGetTypeDefaults(t *testing.T) { "", }, + // Lists should remove optional metadata from the concrete default + // values. + { + `object({ list = optional(list(object({ required = string, optional = optional(string) })), [])})`, + &Defaults{ + Type: cty.ObjectWithOptionalAttrs(map[string]cty.Type{ + "list": cty.List(cty.ObjectWithOptionalAttrs(map[string]cty.Type{ + "required": cty.String, + "optional": cty.String, + }, []string{"optional"})), + }, []string{"list"}), + DefaultValues: map[string]cty.Value{ + "list": cty.ListValEmpty(cty.Object(map[string]cty.Type{ + "required": cty.String, + "optional": cty.String, + })), + }, + }, + ``, + }, + + // Lists should remove optional metadata from the concrete default + // values but should still apply recursive defaults. + { + `object({ list = optional(list(object({ required = string, optional = optional(string, "optional") })), [{ required = "required" }])})`, + &Defaults{ + Type: cty.ObjectWithOptionalAttrs(map[string]cty.Type{ + "list": cty.List(cty.ObjectWithOptionalAttrs(map[string]cty.Type{ + "required": cty.String, + "optional": cty.String, + }, []string{"optional"})), + }, []string{"list"}), + DefaultValues: map[string]cty.Value{ + "list": cty.ListVal([]cty.Value{cty.ObjectVal(map[string]cty.Value{ + "required": cty.StringVal("required"), + "optional": cty.NullVal(cty.String), + })}), + }, + Children: map[string]*Defaults{ + "list": { + Type: cty.List(cty.ObjectWithOptionalAttrs(map[string]cty.Type{ + "required": cty.String, + "optional": cty.String, + }, []string{"optional"})), + Children: map[string]*Defaults{ + "": { + Type: cty.ObjectWithOptionalAttrs(map[string]cty.Type{ + "required": cty.String, + "optional": cty.String, + }, []string{"optional"}), + DefaultValues: map[string]cty.Value{ + "optional": cty.StringVal("optional"), + }, + }, + }, + }, + }, + }, + ``, + }, + // incompatible default value causes an error { `object({ a = optional(string, "hello"), b = optional(number, true) })`, diff --git a/go.mod b/go.mod index 0d22ec3d..065cac36 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 github.com/sergi/go-diff v1.0.0 github.com/spf13/pflag v1.0.2 - github.com/zclconf/go-cty v1.12.0 + github.com/zclconf/go-cty v1.12.1 github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167 ) diff --git a/go.sum b/go.sum index ac1a7146..a064ebde 100644 --- a/go.sum +++ b/go.sum @@ -33,6 +33,8 @@ github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6Ac github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8= github.com/zclconf/go-cty v1.12.0 h1:F5E/vbilcrCtat9sYcEjlwwg1mDqbRTjyXR57nnx5sc= github.com/zclconf/go-cty v1.12.0/go.mod h1:s9IfD1LK5ccNMSWCVFCE2rJfHiZgi7JijgeWIMfhLvA= +github.com/zclconf/go-cty v1.12.1 h1:PcupnljUm9EIvbgSHQnHhUr3fO6oFmkOrvs2BAFNXXY= +github.com/zclconf/go-cty v1.12.1/go.mod h1:s9IfD1LK5ccNMSWCVFCE2rJfHiZgi7JijgeWIMfhLvA= github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b h1:FosyBZYxY34Wul7O/MSKey3txpPYyCqVO5ZyceuQJEI= github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8= golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167 h1:O8uGbHCqlTp2P6QJSLmCojM4mN6UemYv8K+dCnmHmu0=