Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties.Merge: check for nil #66

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading