8000 Added support for type parameters in the `ParseXXX` functions by oxisto · Pull Request #271 · golang-jwt/jwt · GitHub
[go: up one dir, main page]

Skip to content

Conversation

oxisto
Copy link
Collaborator
@oxisto oxisto commented Feb 17, 2023

Following the discussion in #269, this PR adds support for type parameters (or generics) in the ParseXXX functions, basically allowing one to directly access a specific claims type without any uncomfortable type assertions in either the keyfunc or the resulting Token struct.

Following the discussion in #269, this PR serves as a playground for a jwt version with generics aka type parameters.

This basically breaks all the tests for now since they are non-typed but the examples in `example_test.go` work.
// Deprecated: use ParseFromRequest and the WithClaims option
func ParseFromRequestWithClaims(req *http.Request, extractor Extractor, claims jwt.Claims, keyFunc jwt.Keyfunc) (token *jwt.Token, err error) {
func ParseFromRequestWithClaims[T jwt.Claims](req *http.Request, extractor Extractor, claims T, keyFunc jwt.Keyfunc[T]) (token *jwt.Token[T], err error) {
return ParseFromRequest(req, extractor, keyFunc, WithClaims(claims))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. L 8000 earn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
cannot use ParseFromRequest(req, extractor, keyFunc, WithClaims(claims)) (value of type *jwt.Token[jwt.MapClaims]) as *jwt.Token[T] value in return statement

// Deprecated: use ParseFromRequest and the WithClaims option
func ParseFromRequestWithClaims(req *http.Request, extractor Extractor, claims jwt.Claims, keyFunc jwt.Keyfunc) (token *jwt.Token, err error) {
func ParseFromRequestWithClaims[T jwt.Claims](req *http.Request, extractor Extractor, claims T, keyFunc jwt.Keyfunc[T]) (token *jwt.Token[T], err error) {
return ParseFromRequest(req, extractor, keyFunc, WithClaims(claims))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
cannot use keyFunc (variable of type jwt.Keyfunc[T]) as jwt.Keyfunc[jwt.MapClaims] value in argument to ParseFromRequest

// Deprecated: use ParseFromRequest and the WithClaims option
func ParseFromRequestWithClaims(req *http.Request, extractor Extractor, claims jwt.Claims, keyFunc jwt.Keyfunc) (token *jwt.Token, err error) {
func ParseFromRequestWithClaims[T jwt.Claims](req *http.Request, extractor Extractor, claims T, keyFunc jwt.Keyfunc[T]) (token *jwt.Token[T], err error) {
return ParseFromRequest(req, extractor, keyFunc, WithClaims(claims))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
cannot use WithClaims(claims) (value of type ParseFromRequestOption[T]) as ParseFromRequestOption[jwt.MapClaims] value in argument to ParseFromRequest

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
var opts []ParserOption
var opts []ParserOption[MapClaims]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
invalid operation: ParserOption[MapClaims] (ParserOption is not a generic type)


if test.Required {
opts = append(opts, WithAudience(test.Comparison))
opts = append(opts, WithAudience[MapClaims](test.Comparison))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
invalid operation: cannot index WithAudience (value of type func(aud string) ParserOption)

}
want := false
got := newValidator(WithIssuedAt()).Validate(mapClaims)
got := newValidator[MapClaims](WithIssuedAt[MapClaims]()).Validate(mapClaims)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
invalid operation: cannot index WithIssuedAt (value of type func() ParserOption)

}
want := false
got := newValidator(WithTimeFunc(func() time.Time {
got := newValidator[MapClaims](WithTimeFunc[MapClaims](func() time.Time {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
invalid operation: cannot index WithTimeFunc (value of type func(f func() time.Time) ParserOption)

}

got = newValidator(WithTimeFunc(func() time.Time {
got = newValidator[MapClaims](WithTimeFunc[MapClaims](func() time.Time {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
invalid operation: cannot index WithTimeFunc (value of type func(f func() time.Time) ParserOption)


want = true
got = newValidator(WithTimeFunc(func() time.Time {
got = newValidator[MapClaims](WithTimeFunc[MapClaims](func() time.Time {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
invalid operation: cannot index WithTimeFunc (value of type func(f func() time.Time) ParserOption)

@oxisto oxisto changed the title generics Added support for type parameters in the ParseXXX functions Feb 17, 2023
@oxisto
Copy link
Collaborator Author
oxisto commented Feb 20, 2023

Will continue discussion in #272

@oxisto oxisto closed this Feb 20, 2023
@oxisto oxisto deleted the generics branch March 27, 2023 17:28
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

0