Skip to content

Switch dyn.Mapping implementation to use map underneath #2737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

denik
Copy link
Contributor

@denik denik commented Apr 17, 2025

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 denik temporarily deployed to test-trigger-is April 17, 2025 11:21 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is April 17, 2025 11:30 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is April 17, 2025 11:49 — with GitHub Actions Inactive
Copy link
Contributor

@pietern pietern left a comment

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?

@@ -214,3 +232,26 @@ func (v Value) eq(w Value) bool {
return v.v == w.v
}
}

func (v Value) DropKeyLocations() Value {
Copy link
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.

@@ -25,6 +25,7 @@ func loadExample(t *testing.T, file string) dyn.Value {
func TestYAMLSpecExample_2_1(t *testing.T) {
file := "testdata/spec_example_2.1.yml"
self := loadExample(t, file)
self = self.DropKeyLocations()
Copy link
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.

@@ -801,6 +822,7 @@ func TestYAMLSpecExample_2_27(t *testing.T) {
func TestYAMLSpecExample_2_28(t *testing.T) {
file := "testdata/spec_example_2.28.yml"
self := loadExample(t, file)
self = self.DropKeyLocations()
Copy link
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.

Value: m.data[k],
})
}
return pairs
Copy link
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 denik temporarily deployed to test-trigger-is April 17, 2025 15:02 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is April 17, 2025 15:04 — with GitHub Actions Inactive
@denik denik temporarily deployed to test-trigger-is April 17, 2025 16:09 — with GitHub Actions Inactive
github-merge-queue bot 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.
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.

2 participants