From 314700baaa1450d9d450dfe8c74381d16540d3a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vitor?= Date: Fri, 16 Aug 2024 15:35:06 -0300 Subject: [PATCH] Properties.Merge: check for nil (#66) --- properties.go | 4 ++++ properties_test.go | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) 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) } }