Skip to content

Commit aabfaf5

Browse files
bfladpaddycarver
authored andcommitted
helper/schema: Introduce ResourceData type HasChangeExcept and HasChangesExcept receiver methods
Reference: #457 Reference: #458 (original v1 PR) Allows provider developers to simplify conditional logic on disproportionate attribute set sizes, essentially "any but this one" or "any but these few".
1 parent 68adfd1 commit aabfaf5

File tree

2 files changed

+686
-0
lines changed

2 files changed

+686
-0
lines changed

helper/schema/resource_data.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,29 @@ func (d *ResourceData) HasChanges(keys ...string) bool {
128128
return false
129129
}
130130

131+
// HasChangesExcept returns whether any keys outside the given keys have been changed.
132+
//
133+
// This function only works with root attribute keys.
134+
func (d *ResourceData) HasChangesExcept(keys ...string) bool {
135+
for attr := range d.diff.Attributes {
136+
rootAttr := strings.Split(attr, ".")[0]
137+
var skipAttr bool
138+
139+
for _, key := range keys {
140+
if rootAttr == key {
141+
skipAttr = true
142+
break
143+
}
144+
}
145+
146+
if !skipAttr && d.HasChange(rootAttr) {
147+
return true
148+
}
149+
}
150+
151+
return false
152+
}
153+
131154
// HasChange returns whether or not the given key has been changed.
132155
func (d *ResourceData) HasChange(key string) bool {
133156
o, n := d.GetChange(key)
@@ -142,6 +165,25 @@ func (d *ResourceData) HasChange(key string) bool {
142165
return !reflect.DeepEqual(o, n)
143166
}
144167

168+
// HasChangeExcept returns whether any keys outside the given key have been changed.
169+
//
170+
// This function only works with root attribute keys.
171+
func (d *ResourceData) HasChangeExcept(key string) bool {
172+
for attr := range d.diff.Attributes {
173+
rootAttr := strings.Split(attr, ".")[0]
174+
175+
if rootAttr == key {
176+
continue
177+
}
178+
179+
if d.HasChange(rootAttr) {
180+
return true
181+
}
182+
}
183+
184+
return false
185+
}
186+
145187
// Partial is a legacy function that was used for capturing state of specific
146188
// attributes if an update only partially worked. Enabling this flag without
147189
// setting any specific keys with the now removed SetPartial has a useful side

0 commit comments

Comments
 (0)