shallow-copy instead of (deep) Copy() in Merge()

Merge() must not modify self or other; shallow-copying is sufficient
to achieve that.
This commit is contained in:
Matthias Radestock
2016-07-29 09:44:05 +01:00
parent c4fa72110c
commit 17fe25d8c3
4 changed files with 25 additions and 13 deletions

View File

@@ -124,11 +124,14 @@ func (e MetadataTemplates) Copy() MetadataTemplates {
// Merge merges two sets of MetadataTemplates so far just ignores based
// on duplicate id key
func (e MetadataTemplates) Merge(other MetadataTemplates) MetadataTemplates {
result := e.Copy()
if e == nil && other == nil {
return nil
}
result := make(MetadataTemplates, len(e))
for k, v := range e {
result[k] = v
}
for k, v := range other {
if result == nil {
result = MetadataTemplates{}
}
if existing, ok := result[k]; !ok || existing.Priority < v.Priority {
result[k] = v
}

View File

@@ -67,11 +67,14 @@ func (e MetricTemplates) Copy() MetricTemplates {
// Merge merges two sets of MetricTemplates so far just ignores based
// on duplicate id key
func (e MetricTemplates) Merge(other MetricTemplates) MetricTemplates {
result := e.Copy()
if e == nil && other == nil {
return nil
}
result := make(MetricTemplates, len(e))
for k, v := range e {
result[k] = v
}
for k, v := range other {
if result == nil {
result = MetricTemplates{}
}
if existing, ok := result[k]; !ok || existing.Priority < v.Priority {
result[k] = v
}

View File

@@ -154,11 +154,14 @@ func (t TableTemplates) Copy() TableTemplates {
// Merge merges two sets of TableTemplates
func (t TableTemplates) Merge(other TableTemplates) TableTemplates {
result := t.Copy()
if t == nil && other == nil {
return nil
}
result := make(TableTemplates, len(t))
for k, v := range t {
result[k] = v
}
for k, v := range other {
if result == nil {
result = TableTemplates{}
}
if existing, ok := result[k]; ok {
v = v.Merge(existing)
}

View File

@@ -175,7 +175,10 @@ func (n Nodes) Copy() Nodes {
// Merge merges the other object into this one, and returns the result object.
// The original is not modified.
func (n Nodes) Merge(other Nodes) Nodes {
cp := n.Copy()
cp := make(Nodes, len(n))
for k, v := range n {
cp[k] = v
}
for k, v := range other {
if n, ok := cp[k]; ok { // don't overwrite
v = v.Merge(n)