Skip to content

Commit

Permalink
style: improve function sign
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou committed Dec 5, 2022
1 parent 06beaf9 commit 8ccd16e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion _example/cookie/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion _example/redis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions tester/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 8ccd16e

Please sign in to comment.