Skip to content

Commit

Permalink
Strip optional metadata from empty default collections (#568)
Browse files Browse the repository at this point in the history
* add test cases that verify the downstream go-cty fix has worked

* update go-cty
  • Loading branch information
liamcervante authored Nov 9, 2022
1 parent fa90633 commit bcff50c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
61 changes: 61 additions & 0 deletions ext/typeexpr/get_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) })`,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit bcff50c

Please sign in to comment.