Skip to content

Commit

Permalink
helper/schema: Introduce ResourceData type HasChangeExcept and HasCha…
Browse files Browse the repository at this point in the history
…ngesExcept 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".
  • Loading branch information
bflad authored and paddycarver committed Nov 17, 2020
1 parent 68adfd1 commit aabfaf5
Show file tree
Hide file tree
Showing 2 changed files with 686 additions and 0 deletions.
42 changes: 42 additions & 0 deletions helper/schema/resource_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ func (d *ResourceData) HasChanges(keys ...string) bool {
return false
}

// HasChangesExcept returns whether any keys outside the given keys have been changed.
//
// This function only works with root attribute keys.
func (d *ResourceData) HasChangesExcept(keys ...string) bool {
for attr := range d.diff.Attributes {
rootAttr := strings.Split(attr, ".")[0]
var skipAttr bool

for _, key := range keys {
if rootAttr == key {
skipAttr = true
break
}
}

if !skipAttr && d.HasChange(rootAttr) {
return true
}
}

return false
}

// HasChange returns whether or not the given key has been changed.
func (d *ResourceData) HasChange(key string) bool {
o, n := d.GetChange(key)
Expand All @@ -142,6 +165,25 @@ func (d *ResourceData) HasChange(key string) bool {
return !reflect.DeepEqual(o, n)
}

// HasChangeExcept returns whether any keys outside the given key have been changed.
//
// This function only works with root attribute keys.
func (d *ResourceData) HasChangeExcept(key string) bool {
for attr := range d.diff.Attributes {
rootAttr := strings.Split(attr, ".")[0]

if rootAttr == key {
continue
}

if d.HasChange(rootAttr) {
return true
}
}

return false
}

// Partial is a legacy function that was used for capturing state of specific
// attributes if an update only partially worked. Enabling this flag without
// setting any specific keys with the now removed SetPartial has a useful side
Expand Down
Loading

0 comments on commit aabfaf5

Please sign in to comment.