Skip to content
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

How to ignore date fields in JSON #156

Closed
tiagomelo opened this issue Aug 21, 2019 · 4 comments
Closed

How to ignore date fields in JSON #156

tiagomelo opened this issue Aug 21, 2019 · 4 comments
Labels

Comments

@tiagomelo
Copy link

tiagomelo commented Aug 21, 2019

Hi,

I'm working in a project that compares two JSONs in unit tests this way:

if diff := cmp.Diff(string(expected), string(got)); diff != "" {
fmt.Println(string(got))
t.Errorf("%s: (-want, +got)\n%s", fixturePath, diff)
}

These JSONs have datetime fields and I want to ignore those fields. I really don't want to compare them. How can I accomplish this?

the Diff() method accepts some options, right?

Best regards,

Tiago

@dsnet dsnet added the question label Aug 21, 2019
@dsnet
Copy link
Collaborator

dsnet commented Aug 21, 2019

Here's one possible way of doing it: https://play.golang.org/p/PKGzR3fXs-P

General approach:

  • Use a transformer to convert a JSON string into a structured representation that cmp can understand.
  • Use an ignore option (filtered to only apply on strings that look like some date format) to ignore all values of such form. If you only want to ignore a specific time field in the JSON, you'll need to craft a more complex filter function to select that value.

Hope this helps.

@dsnet dsnet closed this as completed Aug 21, 2019
@tiagomelo
Copy link
Author

Hi! Thanks very much for your help.

Your snippet surely works, but when I applied it to the project, I've got:

"github.com/google/go-cmp/cmp".Option does not implement "github.com/nytm/personalization-reading-list/vendor/github.com/google/go-cmp/cmp".Option (missing "github.com/nytm/personalization-reading-list/vendor/github.com/google/go-cmp/cmp".filter method)

I'm wondering if it's a version issue?

@dsnet
Copy link
Collaborator

dsnet commented Aug 21, 2019

This seems to be an issue that is beyond this project. If I could guess, you might be running in module mode, where the vendor is not being respected.

I recommend fully embracing modules and avoid use of vendor. In my experience, the vendor approach has been nothing but trouble.

@tiagomelo
Copy link
Author

Thanks again! Will work into that.

best regards!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants