Skip to content

Differentiate nil values from missing keys in Map.Has - #172

Open
AdamMagued wants to merge 1 commit into
stretchr:masterfrom
AdamMagued:fix-has-nil-values
Open

AdamMagued wants to merge 1 commit into
stretchr:masterfrom
AdamMagued:fix-has-nil-values

Conversation

@AdamMagued

Copy link
Copy Markdown

Summary

Fixes #131

Previously, Map.Has(selector) was implemented as !m.Get(selector).IsNil(). Because Get returns a Value containing nil whenever a key does not exist or exists with a nil value, Has returned false for keys explicitly set to nil or slice indices containing nil. This prevented callers from differentiating between missing keys and keys mapped to nil/null.

This change adds a path traversal helper has in accessors.go that verifies key presence in maps (_, ok := cur[thisSel]) and index boundaries in slices (0 <= index < len(array)). Map.Has now delegates to has, correctly returning true for existing keys and indices with nil values.

Checklist

  • Tests are passing: task test
  • Code style is correct: task lint

Previously, Map.Has was implemented as !m.Get(selector).IsNil().
Because Get returns a Value wrapping nil when a key either does not exist
or exists with a nil value, Has returned false for keys that were explicitly
set to nil or slice indices containing nil elements. This prevented callers
from differentiating between non-existent keys and keys mapped to nil/null.

Introduce a path traversal helper has that checks for map key presence
and array index bounds rather than checking whether the retrieved value
is non-nil. Update Map.Has to use has so that existing keys with nil
values evaluate to true.
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.

Differentiate nil from not found

1 participant