Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional attributes fixes #88

Merged
merged 3 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions cty/convert/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ func getConversion(in cty.Type, out cty.Type, unsafe bool) conversion {
// We'll pass through nulls, albeit type converted, and let
// the caller deal with whatever handling they want to do in
// case null values are considered valid in some applications.

// Avoid constructing values with types which include optional
// attributes. Non-null object values will be passed to a
// conversion function which drops the optional attributes from the
// type. Null pass through values must do the same to ensure that
// homogeneous collections have a single element type.
if out.IsObjectType() && len(out.OptionalAttributes()) > 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing this check made me wonder if the shallow check here would be sufficient to deal with a case where we have a type constraint that only indirectly contains an object type with optional attributes. In order to try to test that I added the following test case in public_test.go, adapting one of the ones you added in this PR:

		{
			Value: cty.TupleVal([]cty.Value{
				cty.NullVal(cty.DynamicPseudoType),
				cty.ObjectVal(map[string]cty.Value{
					"foo": cty.NullVal(cty.DynamicPseudoType),
				}),
			}),
			Type: cty.List(cty.Object(
				map[string]cty.Type{
					"foo": cty.ObjectWithOptionalAttrs(
						map[string]cty.Type{
							"bar": cty.String,
						},
						[]string{"bar"},
					),
				},
			)),
			Want: cty.ListVal([]cty.Value{
				cty.NullVal(cty.Object(map[string]cty.Type{
					"foo": cty.Object(map[string]cty.Type{
						"bar": cty.String,
					}),
				})),
				cty.ObjectVal(map[string]cty.Value{
					"foo": cty.NullVal(cty.Object(map[string]cty.Type{
						"bar": cty.String,
					})),
				}),
			}),
		},

Unfortunately this seems to fall into the same panic trap as the original upstream report 😖

panic: inconsistent list element types (cty.Object(map[string]cty.Type{"foo":cty.ObjectWithOptionalAttrs(map[string]cty.Type{"bar":cty.String}, []string{"bar"})}) then cty.Object(map[string]cty.Type{"foo":cty.Object(map[string]cty.Type{"bar":cty.String})})) [recovered]
        panic: inconsistent list element types (cty.Object(map[string]cty.Type{"foo":cty.ObjectWithOptionalAttrs(map[string]cty.Type{"bar":cty.String}, []string{"bar"})}) then cty.Object(map[string]cty.Type{"foo":cty.Object(map[string]cty.Type{"bar":cty.String})}))

goroutine 156 [running]:
testing.tRunner.func1.2(0x5ad520, 0xc0001be020)
        /home/me/.goenv/versions/1.16.0/src/testing/testing.go:1144 +0x332
testing.tRunner.func1(0xc0001abb00)
        /home/me/.goenv/versions/1.16.0/src/testing/testing.go:1147 +0x4b6
panic(0x5ad520, 0xc0001be020)
        /home/me/.goenv/versions/1.16.0/src/runtime/panic.go:965 +0x1b9
github.com/zclconf/go-cty/cty.ListVal(0xc000160f80, 0x2, 0x2, 0xc0001483c0, 0xc00017ff00, 0x1, 0x1)
        /home/me/Devel/go-cty/cty/value_init.go:166 +0x57e
github.com/zclconf/go-cty/cty/convert.conversionCollectionToList.func1(0x6b97d0, 0xc000133540, 0x5a40e0, 0xc0000bd2f0, 0x0, 0x0, 0x0, 0x557e14, 0xc00009ad68, 0x40da5b, ...)
        /home/me/Devel/go-cty/cty/convert/conversion_collection.go:54 +0x793
github.com/zclconf/go-cty/cty/convert.getConversion.func1(0x6b97d0, 0xc000133540, 0x5a40e0, 0xc0000bd2f0, 0x0, 0x0, 0x0, 0xc0001a3ac0, 0xc00017fef0, 0x6b97d0, ...)
        /home/me/Devel/go-cty/cty/convert/conversion.go:56 +0x52c
github.com/zclconf/go-cty/cty/convert.retConversion.func1(0x6b97d0, 0xc000133540, 0x5a40e0, 0xc0000bd2f0, 0xc00017fef0, 0x4d33a4, 0x76c940, 0x6d17d2, 0xf, 0x6e0f68)
        /home/me/Devel/go-cty/cty/convert/conversion.go:198 +0x6b
github.com/zclconf/go-cty/cty/convert.Convert(0x6b97d0, 0xc000133540, 0x5a40e0, 0xc0000bd2f0, 0x6b97d0, 0xc000133570, 0xf, 0x6e0f68, 0x38, 0x439, ...)
        /home/me/Devel/go-cty/cty/convert/public.go:51 +0x1c5
github.com/zclconf/go-cty/cty/convert.TestConvert.func1(0xc0001abb00)
        /home/me/Devel/go-cty/cty/convert/public_test.go:986 +0x97
testing.tRunner(0xc0001abb00, 0xc00017fe80)
        /home/me/.goenv/versions/1.16.0/src/testing/testing.go:1194 +0xef
created by testing.(*T).Run
        /home/me/.goenv/versions/1.16.0/src/testing/testing.go:1239 +0x2b3

I think unfortunately getting correct behavior here is going to require a recursive walk of the type constraint for any null value (and possibly also the same for ths !in.IsKnown() case above?) to concrete-ize any of the object types with optional attributes inside. The method cty.Type.HasDynamicTypes shows a pattern for implementing a recursive walk of that sort, so I guess my first idea is to add cty.Type.WithoutOptionalAttributesDeep that will return a type with all of the optional attributes inside removed. That will unfortunately be a bit expensive for new memory allocation, but I think in a way that will have similar impact to the "normal" (non-null, known) type conversion codepaths.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apparentlymart I'm unable to reproduce this panic when both of the commits on this branch are in place. The extra unification step introduced for lists (and sets) handles the disjoint types of the null and object tuple members, and the result is that the test passes. If I revert the unification commit, the panic occurs.

With unification in place, I can't see any situation where the WithoutOptionalAttributesDeep function is needed. I'm happy to add a commit to add and use it, but I'm not sure it's necessary until I can find a test case which fails without it.

Am I missing something, or did you test the panic against the first commit only? Any suggestions for an extended test which would fail in this way?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @alisdair,

Curiously, I went back to my dev environment where I'd previously tested this and re-ran tests without any modifications and it didn't panic for me anymore either, so I have to assume I'd somehow got a stale result out of the test suite, although I have no concrete explanation as to why that would be.

Your explanation of how the unification step should get expected result makes sense to me now I re-read the changeset with that idea in mind, and I can no longer reproduce this panic, so instead of trying to explain this oddity I'm going to just assume I made a human error while I was testing and inadvertently tested something other than what I thought I was testing.

With that said, I made some adjustments to the test to take type unification out of the process and focus only on the type conversion aspect, with this new test case:

		{
			Value: cty.NullVal(cty.DynamicPseudoType),
			Type: cty.Object(
				map[string]cty.Type{
					"foo": cty.ObjectWithOptionalAttrs(
						map[string]cty.Type{
							"bar": cty.String,
						},
						[]string{"bar"},
					),
				},
			),
			Want: cty.NullVal(cty.Object(map[string]cty.Type{
				"foo": cty.Object(map[string]cty.Type{
					"bar": cty.String,
				}),
			})),
		},

This failed for me because the resulting null value had a type still containing optional attributes:

    --- FAIL: TestConvert/cty.NullVal(cty.DynamicPseudoType)_to_cty.Object(map[string]cty.Type{"foo":cty.ObjectWithOptionalAttrs(map[string]cty.Type{"bar":cty.String},_[]string{"bar"})}) (0.00s)
        public_test.go:987: wrong result
            value: cty.NullVal(cty.DynamicPseudoType)
            type:  cty.Object(map[string]cty.Type{"foo":cty.ObjectWithOptionalAttrs(map[string]cty.Type{"bar":cty.String}, []string{"bar"})})
            got:   cty.NullVal(cty.Object(map[string]cty.Type{"foo":cty.ObjectWithOptionalAttrs(map[string]cty.Type{"bar":cty.String}, []string{"bar"})}))
            want:  cty.NullVal(cty.Object(map[string]cty.Type{"foo":cty.Object(map[string]cty.Type{"bar":cty.String})}))

Of course, we could decide that the Want value I wrote here is what's wrong, rather than the actual result, but given that in other cases we've decided that the result should have all of the optional attribute annotations removed, I wanted to raise this to see what you think about the inconsistency of this result.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the extra clarification! I think the most recent commit addresses this now, for both unknown and null values.

out = cty.Object(out.AttributeTypes())
}

return cty.NullVal(out), nil
}

Expand Down
84 changes: 84 additions & 0 deletions cty/convert/public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,45 @@ func TestConvert(t *testing.T) {
),
WantError: true, // Attribute "bar" is required
},
{
Value: cty.NullVal(cty.DynamicPseudoType),
Type: cty.ObjectWithOptionalAttrs(
map[string]cty.Type{
"foo": cty.String,
"bar": cty.String,
},
[]string{"foo"},
),
Want: cty.NullVal(cty.Object(map[string]cty.Type{
"foo": cty.String,
"bar": cty.String,
})),
},
{
Value: cty.ListVal([]cty.Value{
cty.NullVal(cty.DynamicPseudoType),
cty.ObjectVal(map[string]cty.Value{
"bar": cty.StringVal("bar value"),
}),
}),
Type: cty.List(cty.ObjectWithOptionalAttrs(
map[string]cty.Type{
"foo": cty.String,
"bar": cty.String,
},
[]string{"foo"},
)),
Want: cty.ListVal([]cty.Value{
cty.NullVal(cty.Object(map[string]cty.Type{
"foo": cty.String,
"bar": cty.String,
})),
cty.ObjectVal(map[string]cty.Value{
"foo": cty.NullVal(cty.String),
"bar": cty.StringVal("bar value"),
}),
}),
},
{
Value: cty.ObjectVal(map[string]cty.Value{
"foo": cty.True,
Expand Down Expand Up @@ -748,6 +787,51 @@ func TestConvert(t *testing.T) {
"b": cty.MapValEmpty(cty.String),
}),
},
// reduction of https://github.com/hashicorp/terraform/issues/27269
{
Value: cty.TupleVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"a": cty.NullVal(cty.DynamicPseudoType),
}),
cty.ObjectVal(map[string]cty.Value{
"a": cty.ObjectVal(map[string]cty.Value{
"b": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"c": cty.StringVal("d"),
}),
}),
}),
}),
}),
Type: cty.List(cty.Object(map[string]cty.Type{
"a": cty.Object(map[string]cty.Type{
"b": cty.List(cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"c": cty.String,
"d": cty.String,
}, []string{"d"})),
}),
})),
Want: cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"a": cty.NullVal(cty.Object(map[string]cty.Type{
"b": cty.List(cty.Object(map[string]cty.Type{
"c": cty.String,
"d": cty.String,
})),
})),
}),
cty.ObjectVal(map[string]cty.Value{
"a": cty.ObjectVal(map[string]cty.Value{
"b": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"c": cty.StringVal("d"),
"d": cty.NullVal(cty.String),
}),
}),
}),
}),
}),
},
// https://github.com/hashicorp/terraform/issues/21588:
{
Value: cty.TupleVal([]cty.Value{
Expand Down
16 changes: 13 additions & 3 deletions cty/convert/unify.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ func unify(types []cty.Type, unsafe bool) (cty.Type, []Conversion) {
// unification purposes.
{
mapCt := 0
listCt := 0
setCt := 0
objectCt := 0
tupleCt := 0
dynamicCt := 0
for _, ty := range types {
switch {
case ty.IsMapType():
mapCt++
case ty.IsListType():
listCt++
case ty.IsSetType():
setCt++
case ty.IsObjectType():
objectCt++
case ty.IsTupleType():
Expand All @@ -48,7 +54,11 @@ func unify(types []cty.Type, unsafe bool) (cty.Type, []Conversion) {
}
switch {
case mapCt > 0 && (mapCt+dynamicCt) == len(types):
return unifyMapTypes(types, unsafe, dynamicCt > 0)
return unifyCollectionTypes(cty.Map, types, unsafe, dynamicCt > 0)
case listCt > 0 && (listCt+dynamicCt) == len(types):
return unifyCollectionTypes(cty.List, types, unsafe, dynamicCt > 0)
case setCt > 0 && (setCt+dynamicCt) == len(types):
return unifyCollectionTypes(cty.Set, types, unsafe, dynamicCt > 0)
case objectCt > 0 && (objectCt+dynamicCt) == len(types):
return unifyObjectTypes(types, unsafe, dynamicCt > 0)
case tupleCt > 0 && (tupleCt+dynamicCt) == len(types):
Expand Down Expand Up @@ -100,7 +110,7 @@ Preferences:
return cty.NilType, nil
}

func unifyMapTypes(types []cty.Type, unsafe bool, hasDynamic bool) (cty.Type, []Conversion) {
func unifyCollectionTypes(collectionType func(cty.Type) cty.Type, types []cty.Type, unsafe bool, hasDynamic bool) (cty.Type, []Conversion) {
// If we had any dynamic types in the input here then we can't predict
// what path we'll take through here once these become known types, so
// we'll conservatively produce DynamicVal for these.
Expand All @@ -117,7 +127,7 @@ func unifyMapTypes(types []cty.Type, unsafe bool, hasDynamic bool) (cty.Type, []
return cty.NilType, nil
}

retTy := cty.Map(retElemType)
retTy := collectionType(retElemType)

conversions := make([]Conversion, len(types))
for i, ty := range types {
Expand Down