Skip to content

Commit

Permalink
Refactor Merge based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Feb 1, 2024
1 parent 14a73bb commit 47e7b94
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions sdk/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,33 +191,25 @@ func Merge(a, b *Resource) (*Resource, error) {
return a, nil
}

// Merge the schema URL.
var (
schemaURL string
err error
)
switch true {
case a.schemaURL == "":
schemaURL = b.schemaURL
case b.schemaURL == "":
schemaURL = a.schemaURL
case a.schemaURL == b.schemaURL:
schemaURL = a.schemaURL
default:
// Return the merged resource with an appropriate error. It is up to
// the user to decide if the returned resource can be used or not.
err = ErrSchemaURLConflict
}

// Note: 'b' attributes will overwrite 'a' with last-value-wins in attribute.Key()
// Meaning this is equivalent to: append(a.Attributes(), b.Attributes()...)
mi := attribute.NewMergeIterator(b.Set(), a.Set())
combine := make([]attribute.KeyValue, 0, a.Len()+b.Len())
for mi.Next() {
combine = append(combine, mi.Attribute())
}
merged := NewWithAttributes(schemaURL, combine...)
return merged, err

switch {
case a.schemaURL == "":
return NewWithAttributes(b.schemaURL, combine...), nil
case b.schemaURL == "":
return NewWithAttributes(a.schemaURL, combine...), nil
case a.schemaURL == b.schemaURL:
return NewWithAttributes(a.schemaURL, combine...), nil
}
// Return the merged resource with an appropriate error. It is up to
// the user to decide if the returned resource can be used or not.
return NewSchemaless(combine...), ErrSchemaURLConflict
}

// Empty returns an instance of Resource with no attributes. It is
Expand Down

0 comments on commit 47e7b94

Please sign in to comment.