11 // setRules provides a Rules implementation for the ./set package that
12 // respects the equality rules for cty values of the given type.
14 // This implementation expects that values added to the set will be
15 // valid internal values for the given Type, which is to say that wrapping
16 // the given value in a Value struct along with the ruleset's type should
17 // produce a valid, working Value.
18 type setRules struct {
22 func (r setRules) Hash(v interface{}) int {
23 hashBytes := makeSetHashBytes(Value{
27 return int(crc32.ChecksumIEEE(hashBytes))
30 func (r setRules) Equivalent(v1 interface{}, v2 interface{}) bool {
40 eqv := v1v.Equals(v2v)
42 // By comparing the result to true we ensure that an Unknown result,
43 // which will result if either value is unknown, will be considered
44 // as non-equivalent. Two unknown values are not equivalent for the
45 // sake of set membership.
49 func makeSetHashBytes(val Value) []byte {
51 appendSetHashBytes(val, &buf)
55 func appendSetHashBytes(val Value, buf *bytes.Buffer) {
56 // Exactly what bytes we generate here don't matter as long as the following
58 // - Unknown and null values all generate distinct strings from
59 // each other and from any normal value of the given type.
60 // - The delimiter used to separate items in a compound structure can
61 // never appear literally in any of its elements.
62 // Since we don't support hetrogenous lists we don't need to worry about
63 // collisions between values of different types, apart from
65 // If in practice we *do* get a collision then it's not a big deal because
66 // the Equivalent function will still distinguish values, but set
67 // performance will be best if we are able to produce a distinct string
68 // for each distinct value, unknown values notwithstanding.
80 buf.WriteString(val.v.(*big.Float).String())
90 buf.WriteString(fmt.Sprintf("%q", val.v.(string)))
94 if val.ty.IsMapType() {
96 val.ForEachElement(func(keyVal, elementVal Value) bool {
97 appendSetHashBytes(keyVal, buf)
99 appendSetHashBytes(elementVal, buf)
107 if val.ty.IsListType() || val.ty.IsSetType() {
109 val.ForEachElement(func(keyVal, elementVal Value) bool {
110 appendSetHashBytes(elementVal, buf)
118 if val.ty.IsObjectType() {
120 attrNames := make([]string, 0, len(val.ty.AttributeTypes()))
121 for attrName := range val.ty.AttributeTypes() {
122 attrNames = append(attrNames, attrName)
124 sort.Strings(attrNames)
125 for _, attrName := range attrNames {
126 appendSetHashBytes(val.GetAttr(attrName), buf)
133 if val.ty.IsTupleType() {
135 val.ForEachElement(func(keyVal, elementVal Value) bool {
136 appendSetHashBytes(elementVal, buf)
144 // should never get down here
145 panic("unsupported type in set hash")