10 "github.com/zclconf/go-cty/cty/set"
13 // setRules provides a Rules implementation for the ./set package that
14 // respects the equality rules for cty values of the given type.
16 // This implementation expects that values added to the set will be
17 // valid internal values for the given Type, which is to say that wrapping
18 // the given value in a Value struct along with the ruleset's type should
19 // produce a valid, working Value.
20 type setRules struct {
24 var _ set.OrderedRules = setRules{}
26 // Hash returns a hash value for the receiver that can be used for equality
27 // checks where some inaccuracy is tolerable.
29 // The hash function is value-type-specific, so it is not meaningful to compare
30 // hash results for values of different types.
32 // This function is not safe to use for security-related applications, since
33 // the hash used is not strong enough.
34 func (val Value) Hash() int {
35 hashBytes := makeSetHashBytes(val)
36 return int(crc32.ChecksumIEEE(hashBytes))
39 func (r setRules) Hash(v interface{}) int {
46 func (r setRules) Equivalent(v1 interface{}, v2 interface{}) bool {
56 eqv := v1v.Equals(v2v)
58 // By comparing the result to true we ensure that an Unknown result,
59 // which will result if either value is unknown, will be considered
60 // as non-equivalent. Two unknown values are not equivalent for the
61 // sake of set membership.
65 // Less is an implementation of set.OrderedRules so that we can iterate over
66 // set elements in a consistent order, where such an order is possible.
67 func (r setRules) Less(v1, v2 interface{}) bool {
77 if v1v.RawEquals(v2v) { // Easy case: if they are equal then v1 can't be less
81 // Null values always sort after non-null values
82 if v2v.IsNull() && !v1v.IsNull() {
84 } else if v1v.IsNull() {
87 // Unknown values always sort after known values
88 if v1v.IsKnown() && !v2v.IsKnown() {
90 } else if !v1v.IsKnown() {
96 // String values sort lexicographically
97 return v1v.AsString() < v2v.AsString()
99 // Weird to have a set of bools, but if we do then false sorts before true.
100 if v2v.True() || !v1v.True() {
105 v1f := v1v.AsBigFloat()
106 v2f := v2v.AsBigFloat()
107 return v1f.Cmp(v2f) < 0
109 // No other types have a well-defined ordering, so we just produce a
110 // default consistent-but-undefined ordering then. This situation is
111 // not considered a compatibility constraint; callers should rely only
112 // on the ordering rules for primitive values.
113 v1h := makeSetHashBytes(v1v)
114 v2h := makeSetHashBytes(v2v)
115 return bytes.Compare(v1h, v2h) < 0
119 func makeSetHashBytes(val Value) []byte {
121 appendSetHashBytes(val, &buf)
125 func appendSetHashBytes(val Value, buf *bytes.Buffer) {
126 // Exactly what bytes we generate here don't matter as long as the following
128 // - Unknown and null values all generate distinct strings from
129 // each other and from any normal value of the given type.
130 // - The delimiter used to separate items in a compound structure can
131 // never appear literally in any of its elements.
132 // Since we don't support hetrogenous lists we don't need to worry about
133 // collisions between values of different types, apart from
134 // PseudoTypeDynamic.
135 // If in practice we *do* get a collision then it's not a big deal because
136 // the Equivalent function will still distinguish values, but set
137 // performance will be best if we are able to produce a distinct string
138 // for each distinct value, unknown values notwithstanding.
150 buf.WriteString(val.v.(*big.Float).String())
160 buf.WriteString(fmt.Sprintf("%q", val.v.(string)))
164 if val.ty.IsMapType() {
166 val.ForEachElement(func(keyVal, elementVal Value) bool {
167 appendSetHashBytes(keyVal, buf)
169 appendSetHashBytes(elementVal, buf)
177 if val.ty.IsListType() || val.ty.IsSetType() {
179 val.ForEachElement(func(keyVal, elementVal Value) bool {
180 appendSetHashBytes(elementVal, buf)
188 if val.ty.IsObjectType() {
190 attrNames := make([]string, 0, len(val.ty.AttributeTypes()))
191 for attrName := range val.ty.AttributeTypes() {
192 attrNames = append(attrNames, attrName)
194 sort.Strings(attrNames)
195 for _, attrName := range attrNames {
196 appendSetHashBytes(val.GetAttr(attrName), buf)
203 if val.ty.IsTupleType() {
205 val.ForEachElement(func(keyVal, elementVal Value) bool {
206 appendSetHashBytes(elementVal, buf)
214 // should never get down here
215 panic("unsupported type in set hash")