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

View File

@ -2,6 +2,7 @@ package schema
import ( import (
"fmt" "fmt"
"reflect"
"sort" "sort"
"sync" "sync"
) )
@ -101,6 +102,15 @@ func (s *Set) Union(other *Set) *Set {
return result 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 { func (s *Set) GoString() string {
return fmt.Sprintf("*Set(%#v)", s.m) return fmt.Sprintf("*Set(%#v)", s.m)
} }