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

A null dynamic value is a known value when comparing equality #55

Merged
merged 6 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cty/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (t Type) HasDynamicTypes() bool {
case t.IsPrimitiveType():
return false
case t.IsCollectionType():
return false
return t.ElementType().HasDynamicTypes()
case t.IsObjectType():
attrTypes := t.AttributeTypes()
for _, at := range attrTypes {
Expand Down
56 changes: 56 additions & 0 deletions cty/type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cty

import (
"fmt"
"testing"
)

func TestHasDynamicTypes(t *testing.T) {
tests := []struct {
ty Type
expected bool
}{
{
DynamicPseudoType,
true,
},
{
List(DynamicPseudoType),
true,
},
{
Tuple([]Type{String, DynamicPseudoType}),
true,
},
{
Object(map[string]Type{
"a": String,
"unknown": DynamicPseudoType,
}),
true,
},
{
List(Object(map[string]Type{
"a": String,
"unknown": DynamicPseudoType,
})),
true,
},
{
Tuple([]Type{Object(map[string]Type{
"a": String,
"unknown": DynamicPseudoType,
})}),
true,
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("%#v.HasDynamicTypes()", test.ty), func(t *testing.T) {
got := test.ty.HasDynamicTypes()
if got != test.expected {
t.Errorf("Equals returned %#v; want %#v", got, test.expected)
}
})
}
}
12 changes: 9 additions & 3 deletions cty/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ func (val Value) IsWhollyKnown() bool {
}
}

// HasDynamicValues checks if the value is dynamic, or contains any nested
// HasWhollyKnownType checks if the value is dynamic, or contains any nested
// DynamicVal. This implies that both the value is not known, and the final
// type may change.
func (val Value) HasDynamicValues() bool {
func (val Value) HasWhollyKnownType() bool {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like you inverted the direction of the name here but not the implementation along with it... if I'm reading correctly, the logic below seems to produce an answer to "Doesn't have a wholly known type".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😆 oh yes, I just did a find and replace 😞 Will fix ASAP

// a null dynamic type is known
if val.IsNull() {
return false
Expand All @@ -124,9 +124,15 @@ func (val Value) HasDynamicValues() bool {
}

if val.CanIterateElements() {
// if the value is not known, then we can look directly at the internal
// types
if !val.IsKnown() {
return val.ty.HasDynamicTypes()
}

for it := val.ElementIterator(); it.Next(); {
_, ev := it.Element()
if ev.HasDynamicValues() {
if ev.HasWhollyKnownType() {
return true
}
}
Expand Down
2 changes: 1 addition & 1 deletion cty/value_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (val Value) Equals(other Value) Value {

// Check if there are any nested dynamic values making this comparison
// unknown.
if val.HasDynamicValues() || other.HasDynamicValues() {
if val.HasWhollyKnownType() || other.HasWhollyKnownType() {
// Even if we have dynamic values, we can still determine inequality if
// there is no way the types could later conform.
if val.ty.TestConformance(other.ty) != nil && other.ty.TestConformance(val.ty) != nil {
Expand Down
24 changes: 22 additions & 2 deletions cty/value_ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,26 @@ func TestValueEquals(t *testing.T) {
}),
UnknownVal(Bool),
},
{
ObjectVal(map[string]Value{
"a": NullVal(List(String)),
}),
// While the unknown val does contain dynamic types, the overall
// container types can't conform.
ObjectVal(map[string]Value{
"a": UnknownVal(List(List(DynamicPseudoType))),
}),
BoolVal(false),
},
{
ObjectVal(map[string]Value{
"a": NullVal(List(List(String))),
}),
ObjectVal(map[string]Value{
"a": UnknownVal(List(List(DynamicPseudoType))),
}),
UnknownVal(Bool),
},

// Marks
{
Expand Down Expand Up @@ -2490,7 +2510,7 @@ func TestValueGoString(t *testing.T) {
}
}

func TestHasDynamicValues(t *testing.T) {
func TestHasWhollyKnownType(t *testing.T) {
tests := []struct {
Value Value
Want bool
Expand Down Expand Up @@ -2556,7 +2576,7 @@ func TestHasDynamicValues(t *testing.T) {
}
for _, test := range tests {
t.Run(test.Value.GoString(), func(t *testing.T) {
got := test.Value.HasDynamicValues()
got := test.Value.HasWhollyKnownType()
want := test.Want
if got != want {
t.Errorf("wrong result\ngot: %v\nwant: %v", got, want)
Expand Down