fixup: standardise rhs of Merge dominates

This commit is contained in:
Bryan Boreham
2019-10-19 17:38:49 +00:00
parent 8be6ca5e1e
commit bcd1fe95a0
5 changed files with 25 additions and 15 deletions

View File

@@ -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:

View File

@@ -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)

View File

@@ -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 == "" {

View File

@@ -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)

View File

@@ -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
}