From bcd1fe95a045c746601893dfd4c2afafa207d1da Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Sat, 19 Oct 2019 17:38:49 +0000 Subject: [PATCH] fixup: standardise rhs of Merge dominates --- report/latest_map_generated.go | 9 ++++----- report/latest_map_internal_test.go | 22 +++++++++++++++------- report/node.go | 2 +- report/report.go | 2 +- report/topology.go | 5 ++++- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/report/latest_map_generated.go b/report/latest_map_generated.go index 22bd69030..67676ffdf 100644 --- a/report/latest_map_generated.go +++ b/report/latest_map_generated.go @@ -30,8 +30,7 @@ func (m StringLatestMap) Size() int { } // Merge produces a StringLatestMap containing the keys from both inputs. -// m must be at least as new as n -// When both inputs contain the same key, the newer value is used. +// When both inputs contain the same key, the value in 'n' is used. // Tries to return one of its inputs, if that already holds the correct result. func (m StringLatestMap) Merge(n StringLatestMap) StringLatestMap { switch { @@ -42,12 +41,12 @@ func (m StringLatestMap) Merge(n StringLatestMap) StringLatestMap { } i, j := 0, 0 -loop: +loop: // go as far as we can get using elements from m for i < len(m) { switch { case j >= len(n): return m - case m[i].key == n[j].key: + case m[i] == n[j]: i++ j++ case m[i].key < n[j].key: @@ -69,7 +68,7 @@ loop: out = append(out, m[i:]...) return out case m[i].key == n[j].key: - out = append(out, m[i]) + out = append(out, n[j]) i++ j++ case m[i].key < n[j].key: diff --git a/report/latest_map_internal_test.go b/report/latest_map_internal_test.go index cbc0575db..1c40ca7ff 100644 --- a/report/latest_map_internal_test.go +++ b/report/latest_map_internal_test.go @@ -81,6 +81,14 @@ func TestLatestMapMerge(t *testing.T) { want: MakeStringLatestMap(). Set("foo", "bar"), }, + "Identical a & b": { + a: MakeStringLatestMap(). + Set("foo", "bar"), + b: MakeStringLatestMap(). + Set("foo", "bar"), + want: MakeStringLatestMap(). + Set("foo", "bar"), + }, "Disjoint a & b": { a: MakeStringLatestMap(). Set("foo", "bar"), @@ -90,20 +98,20 @@ func TestLatestMapMerge(t *testing.T) { Set("foo", "bar"). Set("baz", "bop"), }, - "Common a & b": { + "Common a & b": { // b overrides a where there are keys in common a: MakeStringLatestMap(). - Set("foo", "bar"), - b: MakeStringLatestMap(). Set("foo", "baz"), + b: MakeStringLatestMap(). + Set("foo", "bar"), want: MakeStringLatestMap(). Set("foo", "bar"), }, - "Longer": { + "Longer": { // b overrides a where there are keys in common a: MakeStringLatestMap(). - Set("PID", "23128"). + Set("PID", "0"). Set("Name", "curl"), b: MakeStringLatestMap(). - Set("PID", "0"). + Set("PID", "23128"). Set("Name", "curl"). Set("Domain", "node-a.local"), want: MakeStringLatestMap(). @@ -172,7 +180,7 @@ func TestLatestMapDecoding(t *testing.T) { { "bar": "baz", "foo": "bar", - "emptyval": + "emptyval": "" }` h := &codec.JsonHandle{} decoder := codec.NewDecoder(bytes.NewBufferString(data), h) diff --git a/report/node.go b/report/node.go index da17141c3..1c8ef9638 100644 --- a/report/node.go +++ b/report/node.go @@ -158,7 +158,7 @@ func (n Node) WithChild(child Node) Node { // Merge mergses the individual components of a node and returns a // fresh node. -// TODO: we must know at this point that n is at least as new as other +// 'n' is taken to be older, and string values in 'other' will override func (n Node) Merge(other Node) Node { id := n.ID if id == "" { diff --git a/report/report.go b/report/report.go index 85798a67d..5b10dc959 100644 --- a/report/report.go +++ b/report/report.go @@ -352,7 +352,7 @@ func (r Report) Merge(other Report) Report { } // UnsafeMerge merges another Report into the receiver. The original is modified. -// TODO: r must be at least as new as other +// 'r' is taken to be older, and Node string values in 'other' will override func (r *Report) UnsafeMerge(other Report) { r.DNS = r.DNS.Merge(other.DNS) r.Sampling = r.Sampling.Merge(other.Sampling) diff --git a/report/topology.go b/report/topology.go index 15e91ac1c..e5ca57a5e 100644 --- a/report/topology.go +++ b/report/topology.go @@ -191,6 +191,7 @@ func (t Topology) Merge(other Topology) Topology { } // UnsafeMerge merges the other object into this one, modifying the original. +// 't' is taken to be older, and Node string values in 'other' will override func (t *Topology) UnsafeMerge(other Topology) { if t.Shape == "" { t.Shape = other.Shape @@ -246,6 +247,7 @@ func (n Nodes) Copy() Nodes { } // Merge merges the other object into this one, and returns the result object. +// 'n' is taken to be older, and Node string values in 'other' will override // The original is not modified. func (n Nodes) Merge(other Nodes) Nodes { if len(other) > len(n) { @@ -260,10 +262,11 @@ func (n Nodes) Merge(other Nodes) Nodes { } // UnsafeMerge merges the other object into this one, modifying the original. +// 'n' is taken to be older, and Node string values in 'other' will override func (n *Nodes) UnsafeMerge(other Nodes) { for k, v := range other { if existing, ok := (*n)[k]; ok { // don't overwrite - (*n)[k] = v.Merge(existing) + (*n)[k] = existing.Merge(v) } else { (*n)[k] = v }