add ErrorAsType and related functions - #1861
Conversation
ccoVeille
left a comment
There was a problem hiding this comment.
This sounds reasonable. I didn't check by pulling the branch, but the first look seems OK .
I was afraid you might break old Go versions by using a recent feature, but you used gu:build tag 👍
My only concern now is to check if the new files you add are auto generated or not.
| // Skip generic functions (type parameters present) — they cannot be | ||
| // reproduced by codegen and are maintained by hand. | ||
| if sig.TypeParams() != nil && sig.TypeParams().Len() > 0 { | ||
| continue | ||
| } | ||
|
|
There was a problem hiding this comment.
I'm unsure what this could mean and what would be the problems if you are not adding this to the PR
dolmen
left a comment
There was a problem hiding this comment.
To introduce generic functions in assert and require the code generator has to be improved.
Introducing generic functions without codegen will be too heavy to maintain.
| - "1.23" | ||
| - "1.24" | ||
| - "1.25" | ||
| - "1.26" |
There was a problem hiding this comment.
This must be a separate PR.
| } | ||
|
|
||
| // Skip generic functions (type parameters present) — they cannot be | ||
| // reproduced by codegen and are maintained by hand. |
There was a problem hiding this comment.
This is not the way we must take.
To introduce generics in Testify, someone has to do the job of porting the code generator to support generics.
|
In Testify Go doesn't yet supports methods with type parameters. While this is coming (see golang/go#77273), we aren't there yet. We need changes in the codegen that will be ready to handle methods with type parameters once they are there. |
|
Thanks for the feedback. I'm away for the weekend but will revisit this soon. |
|
Yes interesting. The assertion expression is definitely lighter. |
|
Revised substantially for Go 1.27 in #1937 |
|
@mattjohnsonpint an implementation of this feature already exists at my fork github.com/go-openapi/testify/v2 - it might provide you some extra inspiration, although it is essentially in the same spirit as your implementation. The fork is planned to support the generic "forward" methods in a few weeks (waiting for go1.27 to release). Super happy that the code generator at stretchr/testify gets some uplifting! thanks. Fred |
Summary
Adds Testify functions that mirror the new
errors.AsType[E]in Go 1.26.Changes
For Go 1.26+ consumers, adds the following:
Note that
Eis returned where appropriate, to allow the caller to follow with additional requirements without further casting.Motivation
Instead of:
With this change, and if I'm using Go 1.26.0 or later, I can now write:
Other functions are similar in usage to their existing
*ErrorAs*counterparts.Related issues
Closes #1860