mirror of
https://github.com/weaveworks/scope.git
synced 2026-05-06 09:18:27 +00:00
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:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user