helper/schema: use interface for equality check

/cc @svanharmelen
This commit is contained in:
Mitchell Hashimoto 2015-01-16 09:32:15 -08:00
parent 8cba4a40f5
commit 87948b68fc
3 changed files with 21 additions and 5 deletions

6
helper/schema/equal.go Normal file
View File

@ -0,0 +1,6 @@
package schema
// Equal is an interface that checks for deep equality between two objects.
type Equal interface {
Equal(interface{}) bool
}

View File

@ -106,11 +106,11 @@ func (d *ResourceData) getRaw(key string, level getSource) getResult {
func (d *ResourceData) HasChange(key string) bool {
o, n := d.GetChange(key)
// There is a special case needed for *schema.Set's as they contain
// a function and reflect.DeepEqual will only say two functions are
// equal when they are both nil (which in this case they are not).
if reflect.TypeOf(o).String() == "*schema.Set" {
return !reflect.DeepEqual(o.(*Set).m, n.(*Set).m)
// If the type implements the Equal interface, then call that
// instead of just doing a reflect.DeepEqual. An example where this is
// needed is *Set
if eq, ok := o.(Equal); ok {
return !eq.Equal(n)
}
return !reflect.DeepEqual(o, n)

View File

@ -2,6 +2,7 @@ package schema
import (
"fmt"
"reflect"
"sort"
"sync"
)
@ -101,6 +102,15 @@ func (s *Set) Union(other *Set) *Set {
return result
}
func (s *Set) Equal(raw interface{}) bool {
other, ok := raw.(*Set)
if !ok {
return false
}
return reflect.DeepEqual(s.m, other.m)
}
func (s *Set) GoString() string {
return fmt.Sprintf("*Set(%#v)", s.m)
}