From 3c91f960113863d18ee22a428491d21000e4b90e Mon Sep 17 00:00:00 2001 From: mazkev Date: Wed, 25 Nov 2026 14:08:25 +0700 Subject: [PATCH 1/3] fix(mutations): guard against nil map in MergeHere and nil transformer in Transform --- mutations.go | 6 ++++++ mutations_test.go | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/mutations.go b/mutations.go index 97b401f..012690b 100644 --- a/mutations.go +++ b/mutations.go @@ -38,6 +38,9 @@ func (m Map) Merge(merge Map) Map { // will be modified. This method requires that // the wrapped object be a map[string]any func (m Map) MergeHere(merge Map) Map { + if m == nil { + return m + } maps.Copy(m, merge) return m } @@ -46,6 +49,9 @@ func (m Map) MergeHere(merge Map) Map { // to change the keys and values as it goes. This method requires that // the wrapped object be a map[string]any func (m Map) Transform(transformer func(key string, value any) (string, any)) Map { + if transformer == nil { + return m.Copy() + } newMap := Map{} for k, v := range m { modifiedKey, modifiedVal := transformer(k, v) diff --git a/mutations_test.go b/mutations_test.go index 236316a..2b13759 100644 --- a/mutations_test.go +++ b/mutations_test.go @@ -98,6 +98,18 @@ func TestTransformKeys(t *testing.T) { }, r) } +func TestMergeHere_NilMap(t *testing.T) { + var m objx.Map + res := m.MergeHere(objx.Map{"a": 1}) + assert.Nil(t, res) +} + +func TestTransform_NilFunc(t *testing.T) { + m := objx.Map{"a": 1, "b": 2} + res := m.Transform(nil) + assert.Equal(t, m, res) +} + func keyToUpper(s string, v any) (string, any) { return strings.ToUpper(s), v } From c04c6c0cdf07d5e678e12331ee4a998ba983bff3 Mon Sep 17 00:00:00 2001 From: mazkev Date: Thu, 26 Nov 2026 06:57:09 +0700 Subject: [PATCH 2/3] fix(mutations): return shallow copy of merge on nil MergeHere and drop Transform(nil) guard --- mutations.go | 10 ++++------ mutations_test.go | 16 +++++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/mutations.go b/mutations.go index 012690b..b5c1fbc 100644 --- a/mutations.go +++ b/mutations.go @@ -35,11 +35,12 @@ func (m Map) Merge(merge Map) Map { // MergeHere blends the specified map with this map and returns the current map. // // Keys that appear in both will be selected from the specified map. The original map -// will be modified. This method requires that -// the wrapped object be a map[string]any +// will be modified. If the receiver is nil, a shallow copy of merge is returned; +// callers that may pass a nil Map must use the return value (m = m.MergeHere(x)). +// This method requires that the wrapped object be a map[string]any func (m Map) MergeHere(merge Map) Map { if m == nil { - return m + return merge.Copy() } maps.Copy(m, merge) return m @@ -49,9 +50,6 @@ func (m Map) MergeHere(merge Map) Map { // to change the keys and values as it goes. This method requires that // the wrapped object be a map[string]any func (m Map) Transform(transformer func(key string, value any) (string, any)) Map { - if transformer == nil { - return m.Copy() - } newMap := Map{} for k, v := range m { modifiedKey, modifiedVal := transformer(k, v) diff --git a/mutations_test.go b/mutations_test.go index 2b13759..01af3be 100644 --- a/mutations_test.go +++ b/mutations_test.go @@ -100,14 +100,16 @@ func TestTransformKeys(t *testing.T) { func TestMergeHere_NilMap(t *testing.T) { var m objx.Map - res := m.MergeHere(objx.Map{"a": 1}) - assert.Nil(t, res) -} + merge := objx.Map{"a": 1} + res := m.MergeHere(merge) + assert.Equal(t, objx.Map{"a": 1}, res) + + // Verify res is a shallow copy and mutating it does not affect merge + res["b"] = 2 + assert.False(t, merge.Has("b")) -func TestTransform_NilFunc(t *testing.T) { - m := objx.Map{"a": 1, "b": 2} - res := m.Transform(nil) - assert.Equal(t, m, res) + // Nil receiver with nil merge returns an empty non-nil Map + assert.Equal(t, objx.Map{}, m.MergeHere(nil)) } func keyToUpper(s string, v any) (string, any) { From 3e4f4e009ed3a56cfd9dd2dd0cf8281b8ec3485a Mon Sep 17 00:00:00 2001 From: mazkev Date: Fri, 27 Nov 2026 08:16:45 +0700 Subject: [PATCH 3/3] fix(mutations): panic with clear message and document non-nil receiver in MergeHere --- mutations.go | 7 +++---- mutations_test.go | 18 ++++++++---------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/mutations.go b/mutations.go index b5c1fbc..e9d50b9 100644 --- a/mutations.go +++ b/mutations.go @@ -35,12 +35,11 @@ func (m Map) Merge(merge Map) Map { // MergeHere blends the specified map with this map and returns the current map. // // Keys that appear in both will be selected from the specified map. The original map -// will be modified. If the receiver is nil, a shallow copy of merge is returned; -// callers that may pass a nil Map must use the return value (m = m.MergeHere(x)). -// This method requires that the wrapped object be a map[string]any +// will be modified in place. The receiver must be non-nil; calling MergeHere on a +// nil Map panics. This method requires that the wrapped object be a map[string]any func (m Map) MergeHere(merge Map) Map { if m == nil { - return merge.Copy() + panic("objx: MergeHere called on nil Map") } maps.Copy(m, merge) return m diff --git a/mutations_test.go b/mutations_test.go index 01af3be..c56d525 100644 --- a/mutations_test.go +++ b/mutations_test.go @@ -100,16 +100,14 @@ func TestTransformKeys(t *testing.T) { func TestMergeHere_NilMap(t *testing.T) { var m objx.Map - merge := objx.Map{"a": 1} - res := m.MergeHere(merge) - assert.Equal(t, objx.Map{"a": 1}, res) - - // Verify res is a shallow copy and mutating it does not affect merge - res["b"] = 2 - assert.False(t, merge.Has("b")) - - // Nil receiver with nil merge returns an empty non-nil Map - assert.Equal(t, objx.Map{}, m.MergeHere(nil)) + for _, merge := range []objx.Map{{"a": 1}, nil} { + func() { + defer func() { + assert.Equal(t, "objx: MergeHere called on nil Map", recover()) + }() + m.MergeHere(merge) + }() + } } func keyToUpper(s string, v any) (string, any) {