/
value_equal.go
213 lines (161 loc) · 5.26 KB
/
value_equal.go
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tftypes
import (
"errors"
"fmt"
"math/big"
)
// deepEqual walks both Value to ensure any underlying Value are equal. This
// logic is essentially a duplicate of Diff, however it is intended to return
// early on any inequality and avoids memory allocations where possible.
//
// There might be ways to better share the internal logic of this method with
// Diff, however that effort is reserved for a time when the effort is justified
// over resolving the inherent compute and memory performance issues with Diff
// when only checking for inequality.
func (val1 Value) deepEqual(val2 Value) (bool, error) {
if val1.Type() == nil && val2.Type() == nil && val1.value == nil && val2.value == nil {
return false, nil
}
if (val1.Type() == nil && val2.Type() != nil) || (val1.Type() != nil && val2.Type() == nil) {
return false, errors.New("cannot diff value missing type")
}
if !val1.Type().Is(val2.Type()) {
return false, errors.New("Can't diff values of different types")
}
// Capture walk differences for returning early
var hasDiff bool
// make sure everything in val2 is also in val1
err := Walk(val2, func(path *AttributePath, _ Value) (bool, error) {
_, _, err := val1.walkAttributePath(path)
if err != nil && err != ErrInvalidStep {
return false, fmt.Errorf("Error walking %q: %w", path, err)
} else if err == ErrInvalidStep {
hasDiff = true
return false, stopWalkError
}
return true, nil
})
if err != nil {
return false, err
}
if hasDiff {
return false, nil
}
// make sure everything in val1 is also in val2 and also that it all matches
err = Walk(val1, func(path *AttributePath, value1 Value) (bool, error) {
// pull out the Value at the same path in val2
value2, _, err := val2.walkAttributePath(path)
if err != nil && err != ErrInvalidStep {
return false, fmt.Errorf("Error walking %q: %w", path, err)
} else if err == ErrInvalidStep {
hasDiff = true
return false, stopWalkError
}
// if they're both unknown, no need to continue
if !value1.IsKnown() && !value2.IsKnown() {
return false, nil
}
if value1.IsKnown() != value2.IsKnown() {
hasDiff = true
return false, stopWalkError
}
// if they're both null, no need to continue
if value1.IsNull() && value2.IsNull() {
return false, nil
}
if value1.IsNull() != value2.IsNull() {
hasDiff = true
return false, stopWalkError
}
// We know there are known, non-null values, time to compare them.
// Since this logic is very hot path, it is optimized to use type and
// value implementation details rather than Equal() and As()
// respectively, since both result in memory allocations.
switch typ := value1.Type().(type) {
case primitive:
switch typ.name {
case String.name:
s1, ok := value1.value.(string)
if !ok {
return false, fmt.Errorf("cannot convert %T into string", value1.value)
}
s2, ok := value2.value.(string)
if !ok {
return false, fmt.Errorf("cannot convert %T into string", value2.value)
}
if s1 != s2 {
hasDiff = true
return false, stopWalkError
}
case Number.name:
n1, ok := value1.value.(*big.Float)
if !ok {
return false, fmt.Errorf("cannot convert %T into *big.Float", value1.value)
}
n2, ok := value2.value.(*big.Float)
if !ok {
return false, fmt.Errorf("cannot convert %T into *big.Float", value2.value)
}
if n1.Cmp(n2) != 0 {
hasDiff = true
return false, stopWalkError
}
case Bool.name:
b1, ok := value1.value.(bool)
if !ok {
return false, fmt.Errorf("cannot convert %T into bool", value1.value)
}
b2, ok := value2.value.(bool)
if !ok {
return false, fmt.Errorf("cannot convert %T into bool", value2.value)
}
if b1 != b2 {
hasDiff = true
return false, stopWalkError
}
case DynamicPseudoType.name:
// Let recursion from the walk check the sub-values match
return true, nil
}
return false, nil
case List, Set, Tuple:
s1, ok := value1.value.([]Value)
if !ok {
return false, fmt.Errorf("cannot convert %T into []tftypes.Value", value1.value)
}
s2, ok := value2.value.([]Value)
if !ok {
return false, fmt.Errorf("cannot convert %T into []tftypes.Value", value2.value)
}
// we only care about if the lengths match for lists,
// sets, and tuples. If any of the elements differ,
// the recursion of the walk will find them for us.
if len(s1) != len(s2) {
hasDiff = true
return false, stopWalkError
}
return true, nil
case Map, Object:
m1, ok := value1.value.(map[string]Value)
if !ok {
return false, fmt.Errorf("cannot convert %T into map[string]tftypes.Value", value1.value)
}
m2, ok := value2.value.(map[string]Value)
if !ok {
return false, fmt.Errorf("cannot convert %T into map[string]tftypes.Value", value2.value)
}
// we only care about if the number of keys match for maps and
// objects. If any of the elements differ, the recursion of the walk
// will find them for us.
if len(m1) != len(m2) {
hasDiff = true
return false, stopWalkError
}
return true, nil
}
return false, fmt.Errorf("unexpected type %v in Diff at %s", value1.Type(), path)
})
return !hasDiff, err
}