1 // Copyright 2019, The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE.md file.
9 // valueNode represents a single node within a report, which is a
10 // structured representation of the value tree, containing information
11 // regarding which nodes are equal or not.
12 type valueNode struct {
19 // NumSame is the number of leaf nodes that are equal.
20 // All descendants are equal only if NumDiff is 0.
22 // NumDiff is the number of leaf nodes that are not equal.
24 // NumIgnored is the number of leaf nodes that are ignored.
26 // NumCompared is the number of leaf nodes that were compared
27 // using an Equal method or Comparer function.
29 // NumTransformed is the number of non-leaf nodes that were transformed.
31 // NumChildren is the number of transitive descendants of this node.
32 // This counts from zero; thus, leaf nodes have no descendants.
34 // MaxDepth is the maximum depth of the tree. This counts from zero;
35 // thus, leaf nodes have a depth of zero.
38 // Records is a list of struct fields, slice elements, or map entries.
39 Records []reportRecord // If populated, implies Value is not populated
41 // Value is the result of a transformation, pointer indirect, of
43 Value *valueNode // If populated, implies Records is not populated
45 // TransformerName is the name of the transformer.
46 TransformerName string // If non-empty, implies Value is populated
48 type reportRecord struct {
49 Key reflect.Value // Invalid for slice element
53 func (parent *valueNode) PushStep(ps PathStep) (child *valueNode) {
55 child = &valueNode{parent: parent, Type: ps.Type(), ValueX: vx, ValueY: vy}
56 switch s := ps.(type) {
58 assert(parent.Value == nil)
59 parent.Records = append(parent.Records, reportRecord{Key: reflect.ValueOf(s.Name()), Value: child})
61 assert(parent.Value == nil)
62 parent.Records = append(parent.Records, reportRecord{Value: child})
64 assert(parent.Value == nil)
65 parent.Records = append(parent.Records, reportRecord{Key: s.Key(), Value: child})
67 assert(parent.Value == nil && parent.Records == nil)
70 assert(parent.Value == nil && parent.Records == nil)
73 assert(parent.Value == nil && parent.Records == nil)
75 parent.TransformerName = s.Name()
76 parent.NumTransformed++
78 assert(parent == nil) // Must be the root step
83 func (r *valueNode) Report(rs Result) {
84 assert(r.MaxDepth == 0) // May only be called on leaf nodes
95 assert(r.NumSame+r.NumDiff+r.NumIgnored == 1)
103 assert(r.NumCompared <= 1)
106 func (child *valueNode) PopStep() (parent *valueNode) {
107 if child.parent == nil {
110 parent = child.parent
111 parent.NumSame += child.NumSame
112 parent.NumDiff += child.NumDiff
113 parent.NumIgnored += child.NumIgnored
114 parent.NumCompared += child.NumCompared
115 parent.NumTransformed += child.NumTransformed
116 parent.NumChildren += child.NumChildren + 1
117 if parent.MaxDepth < child.MaxDepth+1 {
118 parent.MaxDepth = child.MaxDepth + 1