Skip to content

Commit

Permalink
Properties.Merge: check for nil
Browse files Browse the repository at this point in the history
  • Loading branch information
jvitoroc committed Aug 16, 2024
1 parent 3941ef1 commit 7a9f2bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func (p Properties) Set(name string, value interface{}) Properties {
// Merge adds the properties from the provided `props` into the receiver `p`.
// If a property in `props` already exists in `p`, its value will be overwritten.
func (p Properties) Merge(props Properties) Properties {
if props == nil {
return p
}

for k, v := range props {
p[k] = v
}
Expand Down
13 changes: 12 additions & 1 deletion properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@ func TestPropertiesMerge(t *testing.T) {

expected := Properties{"title": "A", "value": 0.5, "currency": "USD", "service": "api"}

if !reflect.DeepEqual(props, Properties{"title": "A", "value": 0.5, "currency": "USD", "service": "api"}) {
if !reflect.DeepEqual(props, expected) {
t.Errorf("invalid properties produced by merge:\n- expected %#v\n- found: %#v", expected, props)
}
}

func TestPropertiesMergeNil(t *testing.T) {
props := NewProperties().Set("title", "A")
props.Merge(nil)

expected := Properties{"title": "A"}

if !reflect.DeepEqual(props, expected) {
t.Errorf("invalid properties produced by merge:\n- expected %#v\n- found: %#v", expected, props)
}
}

0 comments on commit 7a9f2bd

Please sign in to comment.