aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/zclconf/go-cty/cty/set_helper.go
blob: a88ddaffb764c1de5e5b7910d79a66dad657adf6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package cty

import (
	"fmt"

	"github.com/zclconf/go-cty/cty/set"
)

// ValueSet is to cty.Set what []cty.Value is to cty.List and
// map[string]cty.Value is to cty.Map. It's provided to allow callers a
// convenient interface for manipulating sets before wrapping them in cty.Set
// values using cty.SetValFromValueSet.
//
// Unlike value slices and value maps, ValueSet instances have a single
// homogenous element type because that is a requirement of the underlying
// set implementation, which uses the element type to select a suitable
// hashing function.
//
// Set mutations are not concurrency-safe.
type ValueSet struct {
	// ValueSet is just a thin wrapper around a set.Set with our value-oriented
	// "rules" applied. We do this so that the caller can work in terms of
	// cty.Value objects even though the set internals use the raw values.
	s set.Set
}

// NewValueSet creates and returns a new ValueSet with the given element type.
func NewValueSet(ety Type) ValueSet {
	return newValueSet(set.NewSet(setRules{Type: ety}))
}

func newValueSet(s set.Set) ValueSet {
	return ValueSet{
		s: s,
	}
}

// ElementType returns the element type for the receiving ValueSet.
func (s ValueSet) ElementType() Type {
	return s.s.Rules().(setRules).Type
}

// Add inserts the given value into the receiving set.
func (s ValueSet) Add(v Value) {
	s.requireElementType(v)
	s.s.Add(v.v)
}

// Remove deletes the given value from the receiving set, if indeed it was
// there in the first place. If the value is not present, this is a no-op.
func (s ValueSet) Remove(v Value) {
	s.requireElementType(v)
	s.s.Remove(v.v)
}

// Has returns true if the given value is in the receiving set, or false if
// it is not.
func (s ValueSet) Has(v Value) bool {
	s.requireElementType(v)
	return s.s.Has(v.v)
}

// Copy performs a shallow copy of the receiving set, returning a new set
// with the same rules and elements.
func (s ValueSet) Copy() ValueSet {
	return newValueSet(s.s.Copy())
}

// Length returns the number of values in the set.
func (s ValueSet) Length() int {
	return s.s.Length()
}

// Values returns a slice of all of the values in the set in no particular
// order.
func (s ValueSet) Values() []Value {
	l := s.s.Length()
	if l == 0 {
		return nil
	}
	ret := make([]Value, 0, l)
	ety := s.ElementType()
	for it := s.s.Iterator(); it.Next(); {
		ret = append(ret, Value{
			ty: ety,
			v:  it.Value(),
		})
	}
	return ret
}

// Union returns a new set that contains all of the members of both the
// receiving set and the given set. Both sets must have the same element type,
// or else this function will panic.
func (s ValueSet) Union(other ValueSet) ValueSet {
	return newValueSet(s.s.Union(other.s))
}

// Intersection returns a new set that contains the values that both the
// receiver and given sets have in common. Both sets must have the same element
// type, or else this function will panic.
func (s ValueSet) Intersection(other ValueSet) ValueSet {
	return newValueSet(s.s.Intersection(other.s))
}

// Subtract returns a new set that contains all of the values from the receiver
// that are not also in the given set. Both sets must have the same element
// type, or else this function will panic.
func (s ValueSet) Subtract(other ValueSet) ValueSet {
	return newValueSet(s.s.Subtract(other.s))
}

// SymmetricDifference returns a new set that contains all of the values from
// both the receiver and given sets, except those that both sets have in
// common. Both sets must have the same element type, or else this function
// will panic.
func (s ValueSet) SymmetricDifference(other ValueSet) ValueSet {
	return newValueSet(s.s.SymmetricDifference(other.s))
}

// requireElementType panics if the given value is not of the set's element type.
func (s ValueSet) requireElementType(v Value) {
	if !v.Type().Equals(s.ElementType()) {
		panic(fmt.Errorf("attempt to use %#v value with set of %#v", v.Type(), s.ElementType()))
	}
}