Skip to content

Added support for type parameters in the ParseXXX functions#271

Closed
oxisto wants to merge 7 commits into
v5from
generics
Closed

Added support for type parameters in the ParseXXX functions#271
oxisto wants to merge 7 commits into
v5from
generics

Conversation

@oxisto

@oxisto oxisto commented Feb 17, 2023

Copy link
Copy Markdown
Collaborator

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.
Comment thread request/request.go
// 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
Copy Markdown
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 ParseFromRequest(req, extractor, keyFunc, WithClaims(claims)) (value of type *jwt.Token[jwt.MapClaims]) as *jwt.Token[T] value in return statement

Comment thread request/request.go
// 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
Copy Markdown
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

Comment thread request/request.go
// 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
Copy Markdown
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

Comment thread map_claims_test.go Outdated
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
var opts []ParserOption
var opts []ParserOption[MapClaims]

Copy link
Copy Markdown
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)

Comment thread map_claims_test.go Outdated

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

Copy link
Copy Markdown
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)

Comment thread map_claims_test.go Outdated
}
want := false
got := newValidator(WithIssuedAt()).Validate(mapClaims)
got := newValidator[MapClaims](WithIssuedAt[MapClaims]()).Validate(mapClaims)

Copy link
Copy Markdown
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)

Comment thread map_claims_test.go Outdated
}
want := false
got := newValidator(WithTimeFunc(func() time.Time {
got := newValidator[MapClaims](WithTimeFunc[MapClaims](func() time.Time {

Copy link
Copy Markdown
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)

Comment thread map_claims_test.go Outdated
}

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

Copy link
Copy Markdown
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)

Comment thread map_claims_test.go Outdated

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

Copy link
Copy Markdown
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

oxisto commented Feb 20, 2023

Copy link
Copy Markdown
Collaborator Author

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