Skip to content

Commit 5568da0

Browse files
author
Paddy
authored
Merge pull request #18 from hashicorp/paddy_deep_is
Add the ability to do a deep comparison in Is.
2 parents 01eb778 + 0c3b7cf commit 5568da0

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 v.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)