Skip to content

Commit b908c7e

Browse files
author
Paddy Carver
committed
Add the ability to do a deep comparison in Is.
When comparing tftypes.Types using the Is method, we didn't used to check if aggregate types were truly equal, with all their element or attribute types matching. This was because we wanted to be able to detect when something was an object, without caring about which attributes or types of attributes it had, for e.g. casting purposes. This enhancement checks if the type a type is being compared to has any element or attribute types set. If so, the type will be deeply compared against it, with all attribute and alement types needing to match. If not, the old behavior will continue to work. This lets us do the following: ```go typ1 := tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "foo": tftypes.String, "bar": tftypes.Number, "quux": tftypes.List{ ElementTypes: tftypes.String, }, }, } typ1.Is(tftypes.Object{}) // true typ1.Is(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "foo": tftypes.String, "bar": tftypes.Number, "quux": tftypes.List{ ElementTypes: tftypes.String, }, }, }) // true typ1.Is(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "foo": tftypes.String, "bar": tftypes.Number, "quux": tftypes.List{}, }, }) // true typ1.Is(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "foo": tftypes.String, "bar": tftypes.Number, }, }) // false typ1.Is(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "foo": tftypes.String, "bar": tftypes.Number, "quux": tftypes.List{ ElementTypes: tftypes.Number, }, }, }) // false typ1.Is(tftypes.Object{ AttributeTypes: map[string]tftypes.Type{ "foo": tftypes.String, "bar": tftypes.String, "quux": tftypes.List{ ElementTypes: tftypes.String, }, }, }) // false ``` and so on.
1 parent 01eb778 commit b908c7e

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

tfprotov5/tftypes/list.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ type List struct {
77
}
88

99
func (l List) Is(t Type) bool {
10-
_, ok := t.(List)
10+
v, ok := t.(List)
11+
if !ok {
12+
return false
13+
}
14+
if v.ElementType != nil {
15+
return l.ElementType.Is(v.ElementType)
16+
}
1117
return ok
1218
}
1319

tfprotov5/tftypes/map.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ type Map struct {
77
}
88

99
func (m Map) Is(t Type) bool {
10-
_, ok := t.(Map)
10+
v, ok := t.(Map)
11+
if !ok {
12+
return false
13+
}
14+
if m.AttributeType != nil {
15+
return m.AttributeType.Is(v.AttributeType)
16+
}
1117
return ok
1218
}
1319

tfprotov5/tftypes/object.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,23 @@ type Object struct {
77
}
88

99
func (o Object) Is(t Type) bool {
10-
_, ok := t.(Object)
10+
v, ok := t.(Object)
11+
if !ok {
12+
return false
13+
}
14+
if v.AttributeTypes != nil {
15+
if len(o.AttributeTypes) != len(v.AttributeTypes) {
16+
return false
17+
}
18+
for k, typ := range o.AttributeTypes {
19+
if _, ok := v.AttributeTypes[k]; !ok {
20+
return false
21+
}
22+
if !typ.Is(v.AttributeTypes[k]) {
23+
return false
24+
}
25+
}
26+
}
1127
return ok
1228
}
1329

tfprotov5/tftypes/set.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ type Set struct {
77
}
88

99
func (s Set) Is(t Type) bool {
10-
_, ok := t.(Set)
10+
v, ok := t.(Set)
11+
if !ok {
12+
return false
13+
}
14+
if v.ElementType != nil {
15+
return s.ElementType.Is(v.ElementType)
16+
}
1117
return ok
1218
}
1319

tfprotov5/tftypes/tuple.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,20 @@ type Tuple struct {
77
}
88

99
func (tu Tuple) Is(t Type) bool {
10-
_, ok := t.(Tuple)
10+
v, ok := t.(Tuple)
11+
if !ok {
12+
return false
13+
}
14+
if v.ElementTypes != nil {
15+
if len(v.ElementTypes) != len(tu.ElementTypes) {
16+
return false
17+
}
18+
for pos, typ := range tu.ElementTypes {
19+
if !typ.Is(v.ElementTypes[pos]) {
20+
return false
21+
}
22+
}
23+
}
1124
return ok
1225
}
1326

0 commit comments

Comments
 (0)