Skip to content

fix(mutations): panic with clear message and document non-nil receiver in MergeHere - #194

Merged
hanzei merged 3 commits into
stretchr:masterfrom
mazkev:fix/nil-guards-set-merge-transform
Nov 27, 2026
Merged

hanzei merged 3 commits into
stretchr:masterfrom
mazkev:fix/nil-guards-set-merge-transform

Conversation

@mazkev

@mazkev mazkev commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This pull request updates Map.MergeHere in mutations.go to fail loudly with a clear panic message when called on a nil Map, and documents that the receiver must be non-nil.

Key Changes

  1. Map.MergeHere (mutations.go):

    • Added if m == nil { panic("objx: MergeHere called on nil Map") } so calling MergeHere on a nil receiver fails consistently with a clear error message, even when merge is empty or nil.
    • Updated the doc comment to document that the receiver must be non-nil and panics if nil.
  2. Tests (mutations_test.go):

    • Updated TestMergeHere_NilMap to assert that m.MergeHere(...) panics with "objx: MergeHere called on nil Map" for both non-empty and nil merge arguments.
    • Verified with go test ./... on amd64 and 386 and go vet ./....

@hanzei
hanzei requested review from hanzei and a balanced review from Copilot November 25, 2026 07:53

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The Transform(nil) test does not verify that the result is a shallow copy, allowing an aliasing regression to go undetected.

1 open issue
What changed in this PR

This PR makes the map mutation helpers safer by handling nil inputs without panicking.

Changes:

  • Return the nil receiver unchanged from Map.MergeHere.
  • Return a shallow copy from Map.Transform when the transformer is nil.
  • Add regression tests for both cases.
File Description
mutations.go Adds nil guards to MergeHere and Transform.
mutations_test.go Tests nil-map merging and nil-transformer behavior.

Review effort: Balanced

Comment thread mutations_test.go Outdated
func TestTransform_NilFunc(t *testing.T) {
m := objx.Map{"a": 1, "b": 2}
res := m.Transform(nil)
assert.Equal(t, m, res)

@hanzei hanzei left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Tests and go vet pass on this branch. However, I think both guards turn a loud panic on a programming error into a silent wrong result, which is worse. Details below.

1. MergeHere on a nil Map silently loses the merge data

objx.Nil.MergeHere(objx.Map{"a": 1}) now returns nil, so the caller's data is dropped without any signal. Before, the bug surfaced as a panic.

It's also inconsistent with Set: on this branch, objx.Nil.Set("a", 1) still panics with assignment to entry in nil map.

MergeHere modifies the receiver in place, which can't be done for a nil map. Either of these would be honest:

  • keep the panic (standard Go behaviour for writing to a nil map, consistent with Set); or
  • return a new map, e.g. if m == nil { return merge.Copy() }, and document that callers must use the return value (m = m.MergeHere(x)).

Returning nil is neither. (Delete documents nil as a no-op, but a delete with no target loses nothing; a merge with no target loses the caller's input.) Merge never had this problem, since it goes through Copy(), which never returns nil.

2. Transform(nil) hides a caller bug

A nil transformer is always a programming error, and calling a nil func panicking is normal Go behaviour (cf. sort.Slice with a nil less). The only internal caller, TransformKeys, always passes a real function. I'd drop this guard. If it stays, the "nil means identity copy" behaviour should go in the doc comment, since it becomes part of the API.

3. Tests

  • As noted in the existing comment, TestTransform_NilFunc would also pass if m itself were returned. Mutate a top-level key in res and assert m is unchanged, as TestCopy does.
  • TestMergeHere_NilMap asserts nil, which locks in the data loss. It will need to change with point 1.

4. Minor

  • Neither doc comment mentions the new nil behaviour.
  • The PR description is mangled: backticks became \ and there are stray control characters (^Gssignment, ^Gmd64).

Suggested outcome

Drop the Transform guard. For MergeHere, either keep the panic or return merge.Copy() on a nil receiver, and document whichever is chosen. If a lenient API around objx.Nil is wanted, it's probably best decided once for Set, MergeHere and the other mutating methods together.


Generated by Claude Code

@mazkev mazkev changed the title fix(mutations): guard against nil map in MergeHere and nil transformer in Transform fix(mutations): return shallow copy of merge in MergeHere on nil Map Nov 26, 2026
@mazkev

mazkev commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the thoughtful review, @hanzei! You're completely right on both points.

I've updated the PR accordingly:

  1. Map.MergeHere: Now returns merge.Copy() when the receiver m is nil instead of dropping the data, and updated the doc comment to note that callers passing a potentially nil map must use the return value (m = m.MergeHere(x)).
  2. Map.Transform: Dropped the nil transformer guard so passing a nil function fails loudly as expected in Go.
  3. Tests: Updated TestMergeHere_NilMap to verify the returned values and shallow-copy isolation (mutating a top-level key in the result does not affect merge), and removed TestTransform_NilFunc.
  4. PR Description: Cleaned up the formatting and stray characters.

@hanzei hanzei left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking at this! I'm not sure we should make MergeHere accept a nil receiver, though.

MergeHere is documented to change the receiver in place, and most callers ignore what it returns. Today a nil Map fails right away with assignment to entry in nil map. With this change, m.MergeHere(x) on a nil m does nothing at all unless the caller writes m = m.MergeHere(x). A crash becomes missing data, which is harder to track down. It's also inconsistent with the other methods: Set on a nil Map still panics, and nil.MergeHere(nil) now returns an empty non-nil Map where it used to return nil.

I'd rather keep the panic, maybe with a clearer message (e.g. panic("objx: MergeHere called on nil Map")), and document that the receiver must be non-nil. If we do want nil-safe mutators, let's decide that for the whole API instead of just this one method.

@mazkev mazkev changed the title fix(mutations): return shallow copy of merge in MergeHere on nil Map fix(mutations): panic with clear message and document non-nil receiver in MergeHere Nov 27, 2026
@mazkev

mazkev commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

That makes total sense, @hanzei — keeping the fail-loud behaviour consistent with Set is much cleaner.

I've updated Map.MergeHere to:

  • Panic explicitly with "objx: MergeHere called on nil Map" when m == nil (for both non-empty and empty/nil merge inputs).
  • Document in the doc comment that the receiver must be non-nil and panics otherwise.
  • Update TestMergeHere_NilMap to assert the exact panic message.

@hanzei
hanzei merged commit 6065360 into stretchr:master Nov 27, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants