-
Notifications
You must be signed in to change notification settings - Fork 419
Added support for type parameters in the ParseXXX
functions
#271
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
Conversation
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. L 8000 earn more.
// 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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map_claims_test.go
Outdated
for _, test := range tests { | ||
t.Run(test.Name, func(t *testing.T) { | ||
var opts []ParserOption | ||
var opts []ParserOption[MapClaims] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map_claims_test.go
Outdated
|
||
if test.Required { | ||
opts = append(opts, WithAudience(test.Comparison)) | ||
opts = append(opts, WithAudience[MapClaims](test.Comparison)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map_claims_test.go
Outdated
} | ||
want := false | ||
got := newValidator(WithIssuedAt()).Validate(mapClaims) | ||
got := newValidator[MapClaims](WithIssuedAt[MapClaims]()).Validate(mapClaims) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map_claims_test.go
Outdated
} | ||
want := false | ||
got := newValidator(WithTimeFunc(func() time.Time { | ||
got := newValidator[MapClaims](WithTimeFunc[MapClaims](func() time.Time { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map_claims_test.go
Outdated
} | ||
|
||
got = newValidator(WithTimeFunc(func() time.Time { | ||
got = newValidator[MapClaims](WithTimeFunc[MapClaims](func() time.Time { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map_claims_test.go
Outdated
|
||
want = true | ||
got = newValidator(WithTimeFunc(func() time.Time { | ||
got = newValidator[MapClaims](WithTimeFunc[MapClaims](func() time.Time { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ParseXXX
functions
Will continue discussion in #272 |
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 thekeyfunc
or the resultingToken
struct.