EqualExportedValues: Support nested structs and time.Time fields#1373
EqualExportedValues: Support nested structs and time.Time fields#1373feidtmb wants to merge 7 commits intostretchr:masterfrom feidtmb:master
Conversation
|
Of course right after I undraft this I realize recursive structs need to be handled... Looking into that and will undraft again when ready. |
|
Hi @feidtmb, I was also looking into the issues of the recursive structs (my entry point was the issues with pointers and slices). Here is a fix I proposed, if you want to take a look: #1379 If you think it makes sense, I we could move the |
|
I think |
|
@HaraldNordgren I left a review on your PR. It looks good to me, thanks for tackling the recursive structures problem! I've removed the expectation for this PR to handle recursive data structures, and will leave it as a draft for now with the assumption that your PR will be merged instead. Once that's done I'll either close this or adjust it to handle only |
|
@HaraldNordgren Looking over your PR again, while I do think your approach is more complete than this one since it handles maps and arrays, I don't think it's handling recursive data structures. (See this test case.) Just wanted to call that out since I was mistaken in my comment above. (I left a similar comment on your PR.) Still happy to proceed with your solution though. |
|
Hi @feidtmb! In a real scenario in don’t see a case where there are objects referencing themselves. I don’t think I have ever seen them in production code. This I why I wouldn't want to add a comment explaining the lack of this. But maybe I’m wrong? Regarding a possible solution to it: One level of recursion should be simple to solve. But if we have multiple levels of data before the structure refers back to top level, then it becomes difficult to solve, where we have to keep track of all previous levels. Probably not impossible, but much more complex to write. If we want to do it, maybe all of this is better suited for a follow-up PR. What do you think? 🤗 |
Summary
Updates the new
EqualExportedValuesfunction to support comparing nested struct fields andtime.Timevalues. The function explicitly no longer supports recursive data structures (see this since removed test case for example).Changes
#1309 introduced
EqualExportedValues. I was looking to add support for comparing structs containingtime.Timefields, and this seemed like an ideal function to adjust for that since:time.Time.Equalis likely more in line with the desired behavior.EqualExportedValuesis not yet in a tagged release, and so this behavior change should not affect many real users.assert.Equal, but that task was found to be more complicated (for example, see Fix compairsion of struct containing time.Time. #985). Adding this support to onlyEqualExportedValuesis more easily achieved.Motivation
There is currently no good method for comparing structs that contain
time.Timefields that don't have matching timezones or monotonic clock readings.Example Usage:
Related issues
Potentially closes #984, assuming we're okay with requiring use of
EqualExportedValuesto compare structs withtime.Timefields and don't expectassert.Equalto handle that.