diff --git a/properties.go b/properties.go index 9a5efb9..ed63d12 100644 --- a/properties.go +++ b/properties.go @@ -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 } diff --git a/properties_test.go b/properties_test.go index 4fb6a8e..91b3802 100644 --- a/properties_test.go +++ b/properties_test.go @@ -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) } }