Repository navigation
fix(mutations): panic with clear message and document non-nil receiver in MergeHere - #194
Conversation
There was a problem hiding this comment.
🟡 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.Transformwhen 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
| func TestTransform_NilFunc(t *testing.T) { | ||
| m := objx.Map{"a": 1, "b": 2} | ||
| res := m.Transform(nil) | ||
| assert.Equal(t, m, res) |
hanzei
left a comment
There was a problem hiding this comment.
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_NilFuncwould also pass ifmitself were returned. Mutate a top-level key inresand assertmis unchanged, asTestCopydoes. TestMergeHere_NilMapassertsnil, 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
…p Transform(nil) guard
|
Thanks for the thoughtful review, @hanzei! You're completely right on both points. I've updated the PR accordingly:
|
hanzei
left a comment
There was a problem hiding this comment.
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.
|
That makes total sense, @hanzei — keeping the fail-loud behaviour consistent with I've updated
|

Summary
This pull request updates
Map.MergeHereinmutations.goto fail loudly with a clear panic message when called on anilMap, and documents that the receiver must be non-nil.Key Changes
Map.MergeHere(mutations.go):if m == nil { panic("objx: MergeHere called on nil Map") }so callingMergeHereon anilreceiver fails consistently with a clear error message, even whenmergeis empty ornil.nil.Tests (
mutations_test.go):TestMergeHere_NilMapto assert thatm.MergeHere(...)panics with"objx: MergeHere called on nil Map"for both non-empty andnilmergearguments.go test ./...onamd64and386andgo vet ./....