2014-08-21 02:51:27 +02:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
2015-08-18 04:26:58 +02:00
|
|
|
"bytes"
|
2015-01-08 20:33:15 +01:00
|
|
|
"fmt"
|
2015-01-16 18:32:15 +01:00
|
|
|
"reflect"
|
2014-08-21 02:51:27 +02:00
|
|
|
"sort"
|
2015-11-18 11:24:04 +01:00
|
|
|
"strconv"
|
2014-08-21 02:51:27 +02:00
|
|
|
"sync"
|
2015-04-09 15:38:16 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2014-08-21 02:51:27 +02:00
|
|
|
)
|
|
|
|
|
2015-04-09 15:38:16 +02:00
|
|
|
// HashString hashes strings. If you want a Set of strings, this is the
|
|
|
|
// SchemaSetFunc you want.
|
|
|
|
func HashString(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
}
|
|
|
|
|
2015-08-18 04:26:58 +02:00
|
|
|
// HashResource hashes complex structures that are described using
|
|
|
|
// a *Resource. This is the default set implementation used when a set's
|
|
|
|
// element type is a full resource.
|
|
|
|
func HashResource(resource *Resource) SchemaSetFunc {
|
|
|
|
return func(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
SerializeResourceForHash(&buf, v, resource)
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HashSchema hashes values that are described using a *Schema. This is the
|
|
|
|
// default set implementation used when a set's element type is a single
|
|
|
|
// schema.
|
|
|
|
func HashSchema(schema *Schema) SchemaSetFunc {
|
|
|
|
return func(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
SerializeValueForHash(&buf, v, schema)
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 02:51:27 +02:00
|
|
|
// Set is a set data structure that is returned for elements of type
|
|
|
|
// TypeSet.
|
|
|
|
type Set struct {
|
2014-08-21 03:30:28 +02:00
|
|
|
F SchemaSetFunc
|
2014-08-21 02:51:27 +02:00
|
|
|
|
2015-11-18 11:24:04 +01:00
|
|
|
m map[string]interface{}
|
2014-08-21 02:51:27 +02:00
|
|
|
once sync.Once
|
|
|
|
}
|
|
|
|
|
2014-10-21 19:57:55 +02:00
|
|
|
// NewSet is a convenience method for creating a new set with the given
|
|
|
|
// items.
|
|
|
|
func NewSet(f SchemaSetFunc, items []interface{}) *Set {
|
|
|
|
s := &Set{F: f}
|
|
|
|
for _, i := range items {
|
|
|
|
s.Add(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-02-06 16:34:24 +01:00
|
|
|
// CopySet returns a copy of another set.
|
|
|
|
func CopySet(otherSet *Set) *Set {
|
|
|
|
return NewSet(otherSet.F, otherSet.List())
|
|
|
|
}
|
|
|
|
|
2014-08-21 02:51:27 +02:00
|
|
|
// Add adds an item to the set if it isn't already in the set.
|
|
|
|
func (s *Set) Add(item interface{}) {
|
2015-11-18 11:24:04 +01:00
|
|
|
s.add(item, false)
|
2014-08-21 02:51:27 +02:00
|
|
|
}
|
|
|
|
|
2015-02-06 16:34:24 +01:00
|
|
|
// Remove removes an item if it's already in the set. Idempotent.
|
|
|
|
func (s *Set) Remove(item interface{}) {
|
|
|
|
s.remove(item)
|
|
|
|
}
|
|
|
|
|
2014-08-21 06:02:42 +02:00
|
|
|
// Contains checks if the set has the given item.
|
|
|
|
func (s *Set) Contains(item interface{}) bool {
|
2015-04-23 18:23:33 +02:00
|
|
|
_, ok := s.m[s.hash(item)]
|
2014-08-21 06:02:42 +02:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the amount of items in the set.
|
|
|
|
func (s *Set) Len() int {
|
|
|
|
return len(s.m)
|
|
|
|
}
|
|
|
|
|
2014-08-21 02:51:27 +02:00
|
|
|
// List returns the elements of this set in slice format.
|
|
|
|
//
|
|
|
|
// The order of the returned elements is deterministic. Given the same
|
|
|
|
// set, the order of this will always be the same.
|
2014-08-21 03:30:28 +02:00
|
|
|
func (s *Set) List() []interface{} {
|
2014-08-21 02:51:27 +02:00
|
|
|
result := make([]interface{}, len(s.m))
|
|
|
|
for i, k := range s.listCode() {
|
|
|
|
result[i] = s.m[k]
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2015-06-04 22:03:01 +02:00
|
|
|
// Difference performs a set difference of the two sets, returning
|
2014-08-21 03:30:28 +02:00
|
|
|
// a new third set that has only the elements unique to this set.
|
|
|
|
func (s *Set) Difference(other *Set) *Set {
|
|
|
|
result := &Set{F: s.F}
|
2015-01-15 15:33:52 +01:00
|
|
|
result.once.Do(result.init)
|
2014-08-21 03:30:28 +02:00
|
|
|
|
|
|
|
for k, v := range s.m {
|
|
|
|
if _, ok := other.m[k]; !ok {
|
|
|
|
result.m[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intersection performs the set intersection of the two sets
|
|
|
|
// and returns a new third set.
|
|
|
|
func (s *Set) Intersection(other *Set) *Set {
|
|
|
|
result := &Set{F: s.F}
|
2015-01-15 15:33:52 +01:00
|
|
|
result.once.Do(result.init)
|
2014-08-21 03:30:28 +02:00
|
|
|
|
|
|
|
for k, v := range s.m {
|
|
|
|
if _, ok := other.m[k]; ok {
|
|
|
|
result.m[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Union performs the set union of the two sets and returns a new third
|
|
|
|
// set.
|
|
|
|
func (s *Set) Union(other *Set) *Set {
|
|
|
|
result := &Set{F: s.F}
|
2015-01-15 15:33:52 +01:00
|
|
|
result.once.Do(result.init)
|
2014-08-21 03:30:28 +02:00
|
|
|
|
|
|
|
for k, v := range s.m {
|
|
|
|
result.m[k] = v
|
|
|
|
}
|
|
|
|
for k, v := range other.m {
|
|
|
|
result.m[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2015-01-16 18:32:15 +01:00
|
|
|
func (s *Set) Equal(raw interface{}) bool {
|
|
|
|
other, ok := raw.(*Set)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return reflect.DeepEqual(s.m, other.m)
|
|
|
|
}
|
|
|
|
|
2015-01-08 20:33:15 +01:00
|
|
|
func (s *Set) GoString() string {
|
|
|
|
return fmt.Sprintf("*Set(%#v)", s.m)
|
|
|
|
}
|
|
|
|
|
2014-08-21 02:51:27 +02:00
|
|
|
func (s *Set) init() {
|
2015-11-18 11:24:04 +01:00
|
|
|
s.m = make(map[string]interface{})
|
2014-08-21 02:51:27 +02:00
|
|
|
}
|
|
|
|
|
2015-11-18 11:24:04 +01:00
|
|
|
func (s *Set) add(item interface{}, computed bool) string {
|
2014-08-21 02:51:27 +02:00
|
|
|
s.once.Do(s.init)
|
|
|
|
|
2015-04-23 18:23:33 +02:00
|
|
|
code := s.hash(item)
|
2015-11-18 11:24:04 +01:00
|
|
|
if computed {
|
|
|
|
code = "~" + code
|
|
|
|
}
|
|
|
|
|
2014-08-21 02:51:27 +02:00
|
|
|
if _, ok := s.m[code]; !ok {
|
|
|
|
s.m[code] = item
|
|
|
|
}
|
|
|
|
|
|
|
|
return code
|
|
|
|
}
|
|
|
|
|
2015-11-18 11:24:04 +01:00
|
|
|
func (s *Set) hash(item interface{}) string {
|
2015-04-23 18:23:33 +02:00
|
|
|
code := s.F(item)
|
|
|
|
// Always return a nonnegative hashcode.
|
|
|
|
if code < 0 {
|
2015-11-18 11:24:04 +01:00
|
|
|
code = -code
|
2015-04-23 18:23:33 +02:00
|
|
|
}
|
2015-11-18 11:24:04 +01:00
|
|
|
return strconv.Itoa(code)
|
2015-04-23 18:23:33 +02:00
|
|
|
}
|
|
|
|
|
2015-11-18 11:24:04 +01:00
|
|
|
func (s *Set) remove(item interface{}) string {
|
2015-02-06 16:34:24 +01:00
|
|
|
s.once.Do(s.init)
|
|
|
|
|
2015-11-18 11:24:04 +01:00
|
|
|
code := s.hash(item)
|
2015-02-06 16:34:24 +01:00
|
|
|
delete(s.m, code)
|
|
|
|
|
|
|
|
return code
|
|
|
|
}
|
|
|
|
|
2014-08-21 06:02:42 +02:00
|
|
|
func (s *Set) index(item interface{}) int {
|
2015-11-18 11:24:04 +01:00
|
|
|
return sort.SearchStrings(s.listCode(), s.hash(item))
|
2014-08-21 06:02:42 +02:00
|
|
|
}
|
|
|
|
|
2015-11-18 11:24:04 +01:00
|
|
|
func (s *Set) listCode() []string {
|
2014-08-21 02:51:27 +02:00
|
|
|
// Sort the hash codes so the order of the list is deterministic
|
2015-11-18 11:24:04 +01:00
|
|
|
keys := make([]string, 0, len(s.m))
|
|
|
|
for k := range s.m {
|
2014-08-21 02:51:27 +02:00
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
2015-11-18 11:24:04 +01:00
|
|
|
sort.Sort(sort.StringSlice(keys))
|
2014-08-21 02:51:27 +02:00
|
|
|
return keys
|
|
|
|
}
|