-
Notifications
You must be signed in to change notification settings - Fork 0
/
equal.go
61 lines (57 loc) · 1 KB
/
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
package vector
import "bytes"
// EqualWith compares vector with exp.
func (vec *Vector) EqualWith(exp *Vector) bool {
return equal(vec.Root(), exp.Root())
}
// EqualWith compares node with exp.
func (n *Node) EqualWith(exp *Node) bool {
return equal(n, exp)
}
func equal(a, b *Node) bool {
switch {
case a == nil && b == nil:
return true
case a == nil:
return false
case b == nil:
return false
}
if a.Type() != b.Type() {
return false
}
if a.Limit() != b.Limit() {
return false
}
ok := true
switch a.Type() {
case TypeObject:
a.Each(func(_ int, ac *Node) {
bc := b.Get(ac.KeyString())
if bc.Type() == TypeNull {
ok = false
return
}
if !equal(ac, bc) {
ok = false
return
}
})
case TypeArray:
a.Each(func(idx int, ac *Node) {
bc := b.At(idx)
if !equal(ac, bc) {
ok = false
return
}
})
case TypeUnknown:
case TypeNull:
return true
default:
if !bytes.Equal(a.Value().RawBytes(), b.Value().RawBytes()) {
return false
}
}
return ok
}