Skip to content

Commit 3b7f88b

Browse files
committed
refactor: standardize codebase to use any instead of interface{}
- Replace occurrences of interface{} with any throughout the codebase and documentation for type arguments and return values. - Update example functions, middleware initializations, and store interfaces to use any instead of interface{} for improved Go type standardization. - Revise test cases to use map[string]any in place of map[string]interface{}. - Update documentation and comments to reflect the use of any as the preferred type for generic data. - Remove several stray empty lines from main files for minor formatting cleanup. Signed-off-by: appleboy <[email protected]>
1 parent 501d370 commit 3b7f88b

File tree

15 files changed

+123
-127
lines changed

15 files changed

+123
-127
lines changed

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ func initParams() *jwt.GinJWTMiddleware {
192192
}
193193
}
194194

195-
func payloadFunc() func(data interface{}) jwt.MapClaims {
196-
return func(data interface{}) jwt.MapClaims {
195+
func payloadFunc() func(data any) jwt.MapClaims {
196+
return func(data any) jwt.MapClaims {
197197
if v, ok := data.(*User); ok {
198198
return jwt.MapClaims{
199199
identityKey: v.UserName,
@@ -203,17 +203,17 @@ func payloadFunc() func(data interface{}) jwt.MapClaims {
203203
}
204204
}
205205

206-
func identityHandler() func(c *gin.Context) interface{} {
207-
return func(c *gin.Context) interface{} {
206+
func identityHandler() func(c *gin.Context) any {
207+
return func(c *gin.Context) any {
208208
claims := jwt.ExtractClaims(c)
209209
return &User{
210210
UserName: claims[identityKey].(string),
211211
}
212212
}
213213
}
214214

215-
func authenticator() func(c *gin.Context) (interface{}, error) {
216-
return func(c *gin.Context) (interface{}, error) {
215+
func authenticator() func(c *gin.Context) (any, error) {
216+
return func(c *gin.Context) (any, error) {
217217
var loginVals login
218218
if err := c.ShouldBind(&loginVals); err != nil {
219219
return "", jwt.ErrMissingLoginValues
@@ -232,8 +232,8 @@ func authenticator() func(c *gin.Context) (interface{}, error) {
232232
}
233233
}
234234

235-
func authorizator() func(data interface{}, c *gin.Context) bool {
236-
return func(data interface{}, c *gin.Context) bool {
235+
func authorizator() func(data any, c *gin.Context) bool {
236+
return func(data any, c *gin.Context) bool {
237237
if v, ok := data.(*User); ok && v.UserName == "admin" {
238238
return true
239239
}
@@ -295,7 +295,7 @@ func main() {
295295
Key: []byte("secret key"),
296296
Timeout: time.Hour,
297297
MaxRefresh: time.Hour * 24,
298-
PayloadFunc: func(data interface{}) gojwt.MapClaims {
298+
PayloadFunc: func(data any) gojwt.MapClaims {
299299
return gojwt.MapClaims{
300300
"user_id": data,
301301
}
@@ -469,16 +469,16 @@ func main() {
469469
MaxRefresh: time.Hour * 24,
470470
IdentityKey: "id",
471471

472-
PayloadFunc: func(data interface{}) jwt.MapClaims {
473-
if v, ok := data.(map[string]interface{}); ok {
472+
PayloadFunc: func(data any) jwt.MapClaims {
473+
if v, ok := data.(map[string]any); ok {
474474
return jwt.MapClaims{
475475
"id": v["username"],
476476
}
477477
}
478478
return jwt.MapClaims{}
479479
},
480480

481-
Authenticator: func(c *gin.Context) (interface{}, error) {
481+
Authenticator: func(c *gin.Context) (any, error) {
482482
var loginVals struct {
483483
Username string `json:"username"`
484484
Password string `json:"password"`
@@ -489,7 +489,7 @@ func main() {
489489
}
490490

491491
if loginVals.Username == "admin" && loginVals.Password == "admin" {
492-
return map[string]interface{}{
492+
return map[string]any{
493493
"username": loginVals.Username,
494494
}, nil
495495
}
@@ -650,7 +650,7 @@ This function should verify the user credentials given the gin context (i.e. pas
650650

651651
OPTIONAL: `PayloadFunc`
652652

653-
This function is called after having successfully authenticated (logged in). It should take whatever was returned from `Authenticator` and convert it into `MapClaims` (i.e. map[string]interface{}). A typical use case of this function is for when `Authenticator` returns a struct which holds the user identifiers, and that struct needs to be converted into a map. `MapClaims` should include one element that is [`IdentityKey` (default is "identity"): some_user_identity]. The elements of `MapClaims` returned in `PayloadFunc` will be embedded within the jwt token (as token claims). When users pass in their token on subsequent requests, you can get these claims back by using `ExtractClaims`.
653+
This function is called after having successfully authenticated (logged in). It should take whatever was returned from `Authenticator` and convert it into `MapClaims` (i.e. map[string]any). A typical use case of this function is for when `Authenticator` returns a struct which holds the user identifiers, and that struct needs to be converted into a map. `MapClaims` should include one element that is [`IdentityKey` (default is "identity"): some_user_identity]. The elements of `MapClaims` returned in `PayloadFunc` will be embedded within the jwt token (as token claims). When users pass in their token on subsequent requests, you can get these claims back by using `ExtractClaims`.
654654

655655
OPTIONAL: `LoginResponse`
656656

README.zh-CN.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func main() {
130130
Key: []byte("secret key"),
131131
Timeout: time.Hour,
132132
MaxRefresh: time.Hour * 24,
133-
PayloadFunc: func(data interface{}) gojwt.MapClaims {
133+
PayloadFunc: func(data any) gojwt.MapClaims {
134134
return gojwt.MapClaims{
135135
"user_id": data,
136136
}
@@ -304,16 +304,16 @@ func main() {
304304
MaxRefresh: time.Hour * 24,
305305
IdentityKey: "id",
306306

307-
PayloadFunc: func(data interface{}) jwt.MapClaims {
308-
if v, ok := data.(map[string]interface{}); ok {
307+
PayloadFunc: func(data any) jwt.MapClaims {
308+
if v, ok := data.(map[string]any); ok {
309309
return jwt.MapClaims{
310310
"id": v["username"],
311311
}
312312
}
313313
return jwt.MapClaims{}
314314
},
315315

316-
Authenticator: func(c *gin.Context) (interface{}, error) {
316+
Authenticator: func(c *gin.Context) (any, error) {
317317
var loginVals struct {
318318
Username string `json:"username"`
319319
Password string `json:"password"`
@@ -324,7 +324,7 @@ func main() {
324324
}
325325

326326
if loginVals.Username == "admin" && loginVals.Password == "admin" {
327-
return map[string]interface{}{
327+
return map[string]any{
328328
"username": loginVals.Username,
329329
}, nil
330330
}
@@ -484,7 +484,7 @@ CookieSameSite: http.SameSiteDefaultMode, // SameSiteDefaultMode, SameSiteLaxM
484484
验证 Gin context 内的用户凭证。验证成功后返回要嵌入 JWT Token 的用户数据(如账号、角色等)。失败则调用 `Unauthorized`
485485

486486
- **可选:** `PayloadFunc`
487-
将认证通过的用户数据转为 `MapClaims`(map[string]interface{}),必须包含 `IdentityKey`(默认 `"identity"`)。
487+
将认证通过的用户数据转为 `MapClaims`(map[string]any),必须包含 `IdentityKey`(默认 `"identity"`)。
488488

489489
- **可选:** `LoginResponse`
490490
处理登录后逻辑,例如返回 Token JSON。

README.zh-TW.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func main() {
130130
Key: []byte("secret key"),
131131
Timeout: time.Hour,
132132
MaxRefresh: time.Hour * 24,
133-
PayloadFunc: func(data interface{}) gojwt.MapClaims {
133+
PayloadFunc: func(data any) gojwt.MapClaims {
134134
return gojwt.MapClaims{
135135
"user_id": data,
136136
}
@@ -304,16 +304,16 @@ func main() {
304304
MaxRefresh: time.Hour * 24,
305305
IdentityKey: "id",
306306

307-
PayloadFunc: func(data interface{}) jwt.MapClaims {
308-
if v, ok := data.(map[string]interface{}); ok {
307+
PayloadFunc: func(data any) jwt.MapClaims {
308+
if v, ok := data.(map[string]any); ok {
309309
return jwt.MapClaims{
310310
"id": v["username"],
311311
}
312312
}
313313
return jwt.MapClaims{}
314314
},
315315

316-
Authenticator: func(c *gin.Context) (interface{}, error) {
316+
Authenticator: func(c *gin.Context) (any, error) {
317317
var loginVals struct {
318318
Username string `json:"username"`
319319
Password string `json:"password"`
@@ -324,7 +324,7 @@ func main() {
324324
}
325325

326326
if loginVals.Username == "admin" && loginVals.Password == "admin" {
327-
return map[string]interface{}{
327+
return map[string]any{
328328
"username": loginVals.Username,
329329
}, nil
330330
}
@@ -484,7 +484,7 @@ CookieSameSite: http.SameSiteDefaultMode, // SameSiteDefaultMode, SameSiteLaxM
484484
驗證 Gin context 內的使用者憑證。驗證成功後回傳要嵌入 JWT Token 的使用者資料(如帳號、角色等)。失敗則呼叫 `Unauthorized`
485485

486486
- **可選:** `PayloadFunc`
487-
將驗證通過的使用者資料轉為 `MapClaims`(map[string]interface{}),必須包含 `IdentityKey`(預設為 `"identity"`)。
487+
將驗證通過的使用者資料轉為 `MapClaims`(map[string]any),必須包含 `IdentityKey`(預設為 `"identity"`)。
488488

489489
- **可選:** `LoginResponse`
490490
處理登入後邏輯,例如回傳 Token JSON。

_example/basic/server.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ func initParams() *jwt.GinJWTMiddleware {
9393
}
9494
}
9595

96-
func payloadFunc() func(data interface{}) gojwt.MapClaims {
97-
return func(data interface{}) gojwt.MapClaims {
96+
func payloadFunc() func(data any) gojwt.MapClaims {
97+
return func(data any) gojwt.MapClaims {
9898
if v, ok := data.(*User); ok {
9999
return gojwt.MapClaims{
100100
identityKey: v.UserName,
@@ -104,17 +104,17 @@ func payloadFunc() func(data interface{}) gojwt.MapClaims {
104104
}
105105
}
106106

107-
func identityHandler() func(c *gin.Context) interface{} {
108-
return func(c *gin.Context) interface{} {
107+
func identityHandler() func(c *gin.Context) any {
108+
return func(c *gin.Context) any {
109109
claims := jwt.ExtractClaims(c)
110110
return &User{
111111
UserName: claims[identityKey].(string),
112112
}
113113
}
114114
}
115115

116-
func authenticator() func(c *gin.Context) (interface{}, error) {
117-
return func(c *gin.Context) (interface{}, error) {
116+
func authenticator() func(c *gin.Context) (any, error) {
117+
return func(c *gin.Context) (any, error) {
118118
var loginVals login
119119
if err := c.ShouldBind(&loginVals); err != nil {
120120
return "", jwt.ErrMissingLoginValues
@@ -133,8 +133,8 @@ func authenticator() func(c *gin.Context) (interface{}, error) {
133133
}
134134
}
135135

136-
func authorizator() func(data interface{}, c *gin.Context) bool {
137-
return func(data interface{}, c *gin.Context) bool {
136+
func authorizator() func(data any, c *gin.Context) bool {
137+
return func(data any, c *gin.Context) bool {
138138
if v, ok := data.(*User); ok && v.UserName == "admin" {
139139
return true
140140
}

_example/redis_simple/main.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func main() {
3030
MaxRefresh: time.Hour,
3131
IdentityKey: identityKey,
3232

33-
PayloadFunc: func(data interface{}) gojwt.MapClaims {
33+
PayloadFunc: func(data any) gojwt.MapClaims {
3434
if v, ok := data.(*User); ok {
3535
return gojwt.MapClaims{
3636
identityKey: v.UserName,
@@ -39,14 +39,14 @@ func main() {
3939
return gojwt.MapClaims{}
4040
},
4141

42-
IdentityHandler: func(c *gin.Context) interface{} {
42+
IdentityHandler: func(c *gin.Context) any {
4343
claims := jwt.ExtractClaims(c)
4444
return &User{
4545
UserName: claims[identityKey].(string),
4646
}
4747
},
4848

49-
Authenticator: func(c *gin.Context) (interface{}, error) {
49+
Authenticator: func(c *gin.Context) (any, error) {
5050
var loginVals User
5151
if err := c.ShouldBind(&loginVals); err != nil {
5252
return "", jwt.ErrMissingLoginValues
@@ -65,7 +65,7 @@ func main() {
6565
return nil, jwt.ErrFailedAuthentication
6666
},
6767

68-
Authorizator: func(data interface{}, c *gin.Context) bool {
68+
Authorizator: func(data any, c *gin.Context) bool {
6969
if v, ok := data.(*User); ok && v.UserName == "admin" {
7070
return true
7171
}
@@ -91,7 +91,6 @@ func main() {
9191

9292
// Create the JWT middleware
9393
authMiddleware, err := jwt.New(middleware)
94-
9594
// Alternative initialization methods using functional options:
9695
//
9796
// Method 1: Simple enable with defaults
@@ -115,7 +114,6 @@ func main() {
115114
// jwt.WithRedisPool(20, time.Hour, 2*time.Hour),
116115
// jwt.WithRedisKeyPrefix("myapp:jwt:"),
117116
// ))
118-
119117
if err != nil {
120118
log.Fatal("JWT Error:" + err.Error())
121119
}
@@ -158,4 +156,4 @@ func helloHandler(c *gin.Context) {
158156
"userName": user.(*User).UserName,
159157
"text": "Hello World.",
160158
})
161-
}
159+
}

_example/redis_store/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ func main() {
2929
Timeout: time.Hour,
3030
MaxRefresh: time.Hour,
3131
IdentityKey: identityKey,
32-
PayloadFunc: func(data interface{}) gojwt.MapClaims {
32+
PayloadFunc: func(data any) gojwt.MapClaims {
3333
if v, ok := data.(*User); ok {
3434
return gojwt.MapClaims{
3535
identityKey: v.UserName,
3636
}
3737
}
3838
return gojwt.MapClaims{}
3939
},
40-
IdentityHandler: func(c *gin.Context) interface{} {
40+
IdentityHandler: func(c *gin.Context) any {
4141
claims := jwt.ExtractClaims(c)
4242
return &User{
4343
UserName: claims[identityKey].(string),
4444
}
4545
},
46-
Authenticator: func(c *gin.Context) (interface{}, error) {
46+
Authenticator: func(c *gin.Context) (any, error) {
4747
var loginVals User
4848
if err := c.ShouldBind(&loginVals); err != nil {
4949
return "", jwt.ErrMissingLoginValues
@@ -61,7 +61,7 @@ func main() {
6161

6262
return nil, jwt.ErrFailedAuthentication
6363
},
64-
Authorizator: func(data interface{}, c *gin.Context) bool {
64+
Authorizator: func(data any, c *gin.Context) bool {
6565
if v, ok := data.(*User); ok && v.UserName == "admin" {
6666
return true
6767
}

_example/token_generator/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ go run main.go
2020

2121
## Key Methods
2222

23-
### `GenerateTokenPair(userData interface{}) (*core.Token, error)`
23+
### `GenerateTokenPair(userData any) (*core.Token, error)`
2424

2525
Generates a complete token pair containing:
2626

@@ -39,7 +39,7 @@ fmt.Printf("Refresh Token: %s\n", tokenPair.RefreshToken)
3939
fmt.Printf("Expires In: %d seconds\n", tokenPair.ExpiresIn())
4040
```
4141

42-
### `GenerateTokenPairWithRevocation(userData interface{}, oldRefreshToken string) (*core.Token, error)`
42+
### `GenerateTokenPairWithRevocation(userData any, oldRefreshToken string) (*core.Token, error)`
4343

4444
Generates a new token pair and automatically revokes the old refresh token:
4545

_example/token_generator/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func main() {
1717
Key: []byte("secret key"),
1818
Timeout: time.Hour,
1919
MaxRefresh: time.Hour * 24,
20-
PayloadFunc: func(data interface{}) gojwt.MapClaims {
20+
PayloadFunc: func(data any) gojwt.MapClaims {
2121
return gojwt.MapClaims{
2222
"user_id": data,
2323
}
@@ -64,4 +64,4 @@ func main() {
6464

6565
fmt.Println("\n=== Token Generation Complete! ===")
6666
fmt.Println("You can now use these tokens without needing middleware handlers!")
67-
}
67+
}

0 commit comments

Comments
 (0)