Preserve slices when setting an indexed value - #170
Conversation
Voyagerroc-Lab
left a comment
There was a problem hiding this comment.
Reviewed 4c25f58 against the documented map[string]interface{} / []interface{} selector contract. Indexed writes now update the final slice element without replacing the containing map value. The regression assertions check the underlying slice directly, avoiding the old false positive where Get("names[0]") returned the replacement scalar. The nested-index, nil-assignment, out-of-range, and non-slice-read cases are covered as well.
Validation on Windows / Go 1.27.0: 257 tests passed with go test ./..., and go vet ./... passed. Local go test -race ./... could not run because CGO is disabled and no C compiler is available; the PR's six Go 1.20–1.25 CI jobs are successful and use the repository's race-enabled Taskfile test command.
Review and validation performed with OpenAI Codex at the account owner's request.
Summary
Set("names[0]", "Mat")replaces the entirenamesslice with"Mat". The existing test misses the data loss becauseGet("names[0]")also ignores an index on a non-slice and returns that scalar.For indexed writes into
[]interface{}, traverse the indices and update the final element instead of assigning the map key. Return a missing value when a read attempts to index a non-slice. Regression tests check that other elements survive, nested writes work, nil can be assigned, and out-of-range writes preserve the slice. Both new tests fail before the fix.Checklist
go test -race ./...with coverage enabled (99.1%).gofmt -d -s *.goandgo vet ./...commands; both are clean.Validated with Go 1.27.1.