Repository navigation
fix add to handle a nil map as the first value - #325
Conversation
Signed-off-by: Yusuf Efe <120668197+yuefdev@users.noreply.github.com>
| case nil: | ||
| v = maps.Clone(x) | ||
| m := make(map[string]any, len(x)) | ||
| maps.Copy(m, x) |
There was a problem hiding this comment.
This is slower than maps.Clone because this rehashes and inserts. I would suggest checking for nil and assign map[string]any{} after maps.Clone.
There was a problem hiding this comment.
I've restored maps.Clone(x) and only allocate map[string]any{} when the clone is nil (31af8e3). That keeps the fast cloning path for populated maps while retaining the nil-map fix. The existing regression passes for both add and add(.[]).
|
We'll also need to avoid nil slice because I want to guarantee that the emit does not emit nil slice (which may cause confusing result with json.Marshal). Replace slices.Clone(x) with append([]any{}, x...). |
Signed-off-by: Yusuf Efe <120668197+yuefdev@users.noreply.github.com>
|
I've also switched the slice case to
|
Running
addon[]any{map[string]any(nil), map[string]any{"x": 1}}panics: cloning the first map leaves a nil destination for the nextmaps.Copy.Keep
maps.Clonefor the first map and initialise an empty map only when the clone is nil. Clone the first slice withappend([]any{}, x...)so a typed nil slice produces an empty array rather thannullwithjson.Marshal.Regressions cover both nil maps and nil slices through the public query API, using
addandadd(.[]). The new slice cases fail before the follow-up and pass afterwards.Validation:
go test ./...,go vet ./..., changed-file gofmt andgit diff --checkpass. The race suite could not run for this follow-up because cgo is disabled in the current local environment.