Skip to content

Preserve slices when setting an indexed value - #170

Merged
hanzei merged 1 commit into
stretchr:masterfrom
vitalivo:fix/indexed-slice-access
Sep 21, 2026
Merged

hanzei merged 1 commit into
stretchr:masterfrom
vitalivo:fix/indexed-slice-access

Conversation

@vitalivo

Copy link
Copy Markdown
Contributor

Summary

Set("names[0]", "Mat") replaces the entire names slice with "Mat". The existing test misses the data loss because Get("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

  • Tests pass: ran the Taskfile's go test -race ./... with coverage enabled (99.1%).
  • Code style is correct: ran the Taskfile's gofmt -d -s *.go and go vet ./... commands; both are clean.

Validated with Go 1.27.1.

@vitalivo vitalivo closed this Sep 12, 2026
@vitalivo vitalivo reopened this Sep 12, 2026
@hanzei
hanzei self-requested a review September 13, 2026 22:03

@Voyagerroc-Lab Voyagerroc-Lab 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.

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.

@hanzei
hanzei merged commit 89f7fb1 into stretchr:master Sep 21, 2026
6 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