Skip to content

Switch dyn.Mapping implementation to use map underneath - #2737

Closed
denik wants to merge 10 commits into
mainfrom
denik/map-exper2
Closed

Switch dyn.Mapping implementation to use map underneath#2737
denik wants to merge 10 commits into
mainfrom
denik/map-exper2

Conversation

@denik

@denikdenik commented Apr 17, 2025

Copy link
Copy Markdown
Contributor

Changes

  • Switch dyn.Mapping implementation to use map underneath.
  • Note, Keys()/Values()/Pairs() sort the result, which changes time complexity from O(n) to O(nlogn). However, this makes output & walks stable (currently it is randomized after every mutator execution), which I think is a worthy property and would make downstream sorting unnecessary in many places.

Follow up to #1301

Why

  • Mapping type and dyn.Value in general becomes comparable with reflect.DeepEqual() and testify/assert.Equal().
  • We can deprecate dynassert in the follow up.
  • We have an option to implement O(1) delete later.

Tests

Existing tests.

@denik
deniktemporarily deployed to test-trigger-is April 17, 2025 11:21 — with GitHub Actions Inactive
@denik
deniktemporarily deployed to test-trigger-is April 17, 2025 11:30 — with GitHub Actions Inactive
@denik
deniktemporarily deployed to test-trigger-is April 17, 2025 11:49 — with GitHub Actions Inactive

@pieternpietern left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks reasonable.

While this removes the ability to iterate in definition order, that can always be reconstructed using location information if it turns out we need it.

Can you link to #1301 from the PR summary as well?

What is the significance of []dyn.Location{} vs nil? Does this trip up equality checks?

Comment threadlibs/dyn/value.go Outdated
Comment threadlibs/dyn/value.go
}
}

func (v Value) DropKeyLocations() Value {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include a comment to explain why this function exists.

Comment threadlibs/dyn/walk_test.go Outdated
func TestYAMLSpecExample_2_1(t *testing.T) {
file := "testdata/spec_example_2.1.yml"
self := loadExample(t, file)
self = self.DropKeyLocations()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably fold this call into the loadExample function.

func TestYAMLSpecExample_2_28(t *testing.T) {
file := "testdata/spec_example_2.28.yml"
self := loadExample(t, file)
self = self.DropKeyLocations()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you include one or two tests that confirm that map keys actually have a location?

Requires a bit more work on constructing the expected value, but worth it to include it. Can be a follow up.

Comment threadlibs/dyn/value.go Outdated
Comment threadlibs/dyn/mapping_test.go Outdated
Comment threadlibs/dyn/mapping_test.go Outdated
Comment threadlibs/dyn/mapping_test.go Outdated
Comment threadlibs/dyn/mapping.go
Value: m.data[k],
})
}
return pairs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious: the iteration is identical for Pairs/Keys/Values. Might be a nice use case for https://go.dev/blog/range-functions#range-over-some-function-types.

@denik
deniktemporarily deployed to test-trigger-is April 17, 2025 15:02 — with GitHub Actions Inactive
@denik
deniktemporarily deployed to test-trigger-is April 17, 2025 15:04 — with GitHub Actions Inactive
@denik
deniktemporarily deployed to test-trigger-is April 17, 2025 16:09 — with GitHub Actions Inactive
github-merge-queueBot pushed a commit that referenced this pull request Apr 22, 2025
## Why
- This starts to matter once we replace dynassert with regular assert
#2737
- We agreed before to use nils consistently instead of empty slices.
@github-actions

Copy link
Copy Markdown
Contributor

This PR has not received an update in a while. If you want to keep this PR open, please leave a comment below or push a new commit and auto-close will be canceled.

denik added a commit that referenced this pull request May 20, 2026
## Why
- This starts to matter once we replace dynassert with regular assert
#2737
- We agreed before to use nils consistently instead of empty slices.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@denik@pietern