Skip to content

Add IgnoreMapKeys to cmpopts - #415

Open
ava-cassiopeia wants to merge 1 commit into
google:masterfrom
ava-cassiopeia:master
Open

ava-cassiopeia wants to merge 1 commit into
google:masterfrom
ava-cassiopeia:master

Conversation

@ava-cassiopeia

Copy link
Copy Markdown

This is a convenience on top of IgnoreMapEntries() that allows for uses like:

mapA := map[string]string{ /* ... */ }
mapB := map[string]string{ /* ... */ }

diff := cmp.Diff(mapA, mapB, cmpopts.IgnoreMapKeys("tobeignored", "another"))

I've wanted this now a few times while writing tests for work, and I figured I'd see what y'all think about adding this to the actual cmpopts package so I can avoid adding basically this same implementation to a common lib for my team's projects.

Some misc comments:

  • I added a copyright comment to the top of the new test file, which I guessed should have the current year.
  • I can add more tests for more types of comparables, if desired.
  • This uses generics, which are somewhat new, but I see other uses of newer go features here so hopefully that's ok? It could probably be rewritten without generics if needed for backwards compatibility, though.

@ava-cassiopeia

Copy link
Copy Markdown
Author

Hmm, the test failure here seems unrelated to this change. Maybe something with the version of go that the macos check is pulling in..?

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.

1 participant