From 8ccd16ef585188d64037056580bbb5d3f2ee4b24 Mon Sep 17 00:00:00 2001 From: kinggo Date: Mon, 5 Dec 2022 13:09:07 +0800 Subject: [PATCH] style: improve function sign --- README.md | 8 ++++---- README_CN.md | 8 ++++---- _example/cookie/main.go | 2 +- _example/redis/main.go | 2 +- sessions.go | 12 +++++++++++- tester/tester.go | 12 ++++++------ 6 files changed, 27 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 965c1a6..48e5947 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ import ( func main() { h := server.New(server.WithHostPorts(":8000")) store := cookie.NewStore([]byte("secret")) - h.Use(sessions.Sessions("mysession", store)) + h.Use(sessions.New("mysession", store)) h.GET("/hello", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) @@ -78,7 +78,7 @@ func main() { h := server.New(server.WithHostPorts(":8000")) store := cookie.NewStore([]byte("secret")) sessionNames := []string{"a", "b"} - h.Use(sessions.SessionsMany(sessionNames, store)) + h.Use(sessions.Many(sessionNames, store)) h.GET("/hello", func(ctx context.Context, c *app.RequestContext) { sessionA := sessions.DefaultMany(c, "a") sessionB := sessions.DefaultMany(c, "b") @@ -121,7 +121,7 @@ import ( func main() { h := server.New(server.WithHostPorts(":8000")) store := cookie.NewStore([]byte("secret")) - h.Use(sessions.Sessions("mysession", store)) + h.Use(sessions.New("mysession", store)) h.GET("/incr", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) var count int @@ -158,7 +158,7 @@ import ( func main() { h := server.Default(server.WithHostPorts(":8000")) store, _ := redis.NewStore(10, "tcp", "localhost:6379", "", []byte("secret")) - h.Use(sessions.Sessions("mysession", store)) + h.Use(sessions.New("mysession", store)) h.GET("/incr", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) diff --git a/README_CN.md b/README_CN.md index 88633b3..b8ff124 100644 --- a/README_CN.md +++ b/README_CN.md @@ -45,7 +45,7 @@ import ( func main() { h := server.New(server.WithHostPorts(":8000")) store := cookie.NewStore([]byte("secret")) - h.Use(sessions.Sessions("mysession", store)) + h.Use(sessions.New("mysession", store)) h.GET("/hello", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) @@ -78,7 +78,7 @@ func main() { h := server.New(server.WithHostPorts(":8000")) store := cookie.NewStore([]byte("secret")) sessionNames := []string{"a", "b"} - h.Use(sessions.SessionsMany(sessionNames, store)) + h.Use(sessions.Many(sessionNames, store)) h.GET("/hello", func(ctx context.Context, c *app.RequestContext) { sessionA := sessions.DefaultMany(c, "a") sessionB := sessions.DefaultMany(c, "b") @@ -121,7 +121,7 @@ import ( func main() { h := server.New(server.WithHostPorts(":8000")) store := cookie.NewStore([]byte("secret")) - h.Use(sessions.Sessions("mysession", store)) + h.Use(sessions.New("mysession", store)) h.GET("/incr", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) var count int @@ -158,7 +158,7 @@ import ( func main() { h := server.Default(server.WithHostPorts(":8000")) store, _ := redis.NewStore(10, "tcp", "localhost:6379", "", []byte("secret")) - h.Use(sessions.Sessions("mysession", store)) + h.Use(sessions.New("mysession", store)) h.GET("/incr", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) diff --git a/_example/cookie/main.go b/_example/cookie/main.go index 8f868a2..41c60ee 100644 --- a/_example/cookie/main.go +++ b/_example/cookie/main.go @@ -38,7 +38,7 @@ import ( func main() { h := server.New(server.WithHostPorts(":8000")) store := cookie.NewStore([]byte("secret")) - h.Use(sessions.Sessions("mysession", store)) + h.Use(sessions.New("mysession", store)) h.GET("/incr", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) var count int diff --git a/_example/redis/main.go b/_example/redis/main.go index 3c30b84..0835adb 100644 --- a/_example/redis/main.go +++ b/_example/redis/main.go @@ -38,7 +38,7 @@ import ( func main() { h := server.Default(server.WithHostPorts(":8000")) store, _ := redis.NewStore(10, "tcp", "localhost:6379", "", []byte("secret")) - h.Use(sessions.Sessions("mysession", store)) + h.Use(sessions.New("mysession", store)) h.GET("/incr", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) diff --git a/sessions.go b/sessions.go index c7f5aa2..8cdaa7f 100644 --- a/sessions.go +++ b/sessions.go @@ -73,7 +73,17 @@ type Session interface { Save() error } +// Deprecated: use New instead of Sessions func Sessions(name string, store Store) app.HandlerFunc { + return New(name, store) +} + +// Deprecated: use Many instead of SessionsMany +func SessionsMany(names []string, store Store) app.HandlerFunc { + return Many(names, store) +} + +func New(name string, store Store) app.HandlerFunc { return func(ctx gcontext.Context, c *app.RequestContext) { req, _ := adaptor.GetCompatRequest(&c.Request) resp := adaptor.GetCompatResponseWriter(&c.Response) @@ -85,7 +95,7 @@ func Sessions(name string, store Store) app.HandlerFunc { } } -func SessionsMany(names []string, store Store) app.HandlerFunc { +func Many(names []string, store Store) app.HandlerFunc { return func(ctx gcontext.Context, c *app.RequestContext) { s := make(map[string]Session, len(names)) req, _ := adaptor.GetCompatRequest(&c.Request) diff --git a/tester/tester.go b/tester/tester.go index 33b2e62..6b1dc29 100644 --- a/tester/tester.go +++ b/tester/tester.go @@ -49,7 +49,7 @@ const ok = "ok" func GetSet(t *testing.T, newStore storeFactory) { opt := config.NewOptions([]config.Option{}) r := route.NewEngine(opt) - r.Use(sessions.Sessions(sessionName, newStore(t))) + r.Use(sessions.New(sessionName, newStore(t))) r.GET("/set", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) session.Set("key", ok) @@ -78,7 +78,7 @@ func GetSet(t *testing.T, newStore storeFactory) { func DeleteKey(t *testing.T, newStore storeFactory) { opt := config.NewOptions([]config.Option{}) r := route.NewEngine(opt) - r.Use(sessions.Sessions(sessionName, newStore(t))) + r.Use(sessions.New(sessionName, newStore(t))) r.GET("/set", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) session.Set("key", ok) @@ -116,7 +116,7 @@ func DeleteKey(t *testing.T, newStore storeFactory) { func Flashes(t *testing.T, newStore storeFactory) { opt := config.NewOptions([]config.Option{}) r := route.NewEngine(opt) - r.Use(sessions.Sessions(sessionName, newStore(t))) + r.Use(sessions.New(sessionName, newStore(t))) r.GET("/set", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) @@ -167,7 +167,7 @@ func Clear(t *testing.T, newStore storeFactory) { } opt := config.NewOptions([]config.Option{}) r := route.NewEngine(opt) - r.Use(sessions.Sessions(sessionName, newStore(t))) + r.Use(sessions.New(sessionName, newStore(t))) r.GET("/set", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) @@ -205,7 +205,7 @@ func Options(t *testing.T, newStore storeFactory) { store.Options(sessions.Options{ Domain: "localhost", }) - r.Use(sessions.Sessions(sessionName, store)) + r.Use(sessions.New(sessionName, store)) r.GET("/domain", func(ctx context.Context, c *app.RequestContext) { session := sessions.Default(c) session.Set("key", ok) @@ -271,7 +271,7 @@ func Many(t *testing.T, newStore storeFactory) { opt := config.NewOptions([]config.Option{}) r := route.NewEngine(opt) sessionNames := []string{"a", "b"} - r.Use(sessions.SessionsMany(sessionNames, newStore(t))) + r.Use(sessions.Many(sessionNames, newStore(t))) r.GET("/set", func(ctx context.Context, c *app.RequestContext) { sessionA := sessions.DefaultMany(c, "a") sessionA.Set("hello", "world")