From b5e9cab1925474c61fa3e1524988319a7b85837d Mon Sep 17 00:00:00 2001 From: kk0829 <244098979@qq.com> Date: Fri, 22 Sep 2023 22:02:13 +0800 Subject: [PATCH 1/2] chore: update debug log --- lightning.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lightning.go b/lightning.go index bce4e27..fff7e3d 100644 --- a/lightning.go +++ b/lightning.go @@ -109,7 +109,7 @@ func (app *Application) Use(middlewares ...Middleware) { // It composes the global middlewares, route-specific middlewares, and the actual handler function // to form a single MiddlewareFunc, and then adds it to the router. func (app *Application) AddRoute(method string, pattern string, handlers []HandlerFunc) { - app.Logger.Debug("register route %s\t-> %s", method, pattern) + app.Logger.Debug(" %s\t-> %s", method, pattern) allHandlers := make([]HandlerFunc, 0) allHandlers = append(allHandlers, app.middlewares...) allHandlers = append(allHandlers, handlers...) From ba81a08b2b5e8738efcf574de851828395000f7f Mon Sep 17 00:00:00 2001 From: kk0829 <244098979@qq.com> Date: Fri, 22 Sep 2023 22:48:06 +0800 Subject: [PATCH 2/2] feat: The newly added methods in the Context struct allow for retrieving URL parameters as different data types such as integers, floats, and strings. --- CHANGELOG.md | 14 ++- context.go | 66 ++++++++++++ context_test.go | 259 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 338 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c22bc3..2ac9e51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## [0.6.0] - Sep 22, 2023 + +### Added + +- ParamInt(key string) (int, error): Returns the value of a URL parameter as an integer. +- ParamUInt(key string) (uint, error): Returns the value of a URL parameter as a uint. +- ParamInt64(key string) (int64, error): Returns the value of a URL parameter as an int64. +- ParamUInt64(key string) (uint64, error): Returns the value of a URL parameter as a uint64. +- ParamFloat32(key string) (float32, error): Returns the value of a URL parameter as a float32. +- ParamFloat64(key string) (float64, error): Returns the value of a URL parameter as a float64. +- ParamString(key string) string: Returns the value of a URL parameter as a string. + ## [0.5.0] - May 14, 2023 ### Added @@ -63,7 +75,7 @@ ### Other -- chore: republish +- chore: republish ## [0.1.2] - Apr 3, 2023 diff --git a/context.go b/context.go index 5c7114a..f7a9636 100644 --- a/context.go +++ b/context.go @@ -4,6 +4,7 @@ import ( "encoding/json" "encoding/xml" "net/http" + "strconv" "github.com/go-playground/validator/v10" ) @@ -121,6 +122,71 @@ func (c *Context) Param(key string) string { return c.req.param(key) } +// ParamInt returns the value of a URL parameter as an integer for a given key. +func (c *Context) ParamInt(key string) (int, error) { + str := c.Param(key) + value, err := strconv.Atoi(str) + if err != nil { + return 0, err + } + return value, nil +} + +// ParamUInt returns the value of a URL parameter as a uint for a given key. +func (c *Context) ParamUInt(key string) (uint, error) { + str := c.Param(key) + value, err := strconv.ParseUint(str, 10, 32) + if err != nil { + return 0, err + } + return uint(value), nil +} + +// ParamInt64 returns the value of a URL parameter as an int64 for a given key. +func (c *Context) ParamInt64(key string) (int64, error) { + str := c.Param(key) + value, err := strconv.ParseInt(str, 10, 64) + if err != nil { + return 0, err + } + return value, nil +} + +// ParamUInt64 returns the value of a URL parameter as a uint64 for a given key. +func (c *Context) ParamUInt64(key string) (uint64, error) { + str := c.Param(key) + value, err := strconv.ParseUint(str, 10, 64) + if err != nil { + return 0, err + } + return value, nil +} + +// ParamFloat32 returns the value of a URL parameter as a float32 for a given key. +func (c *Context) ParamFloat32(key string) (float32, error) { + str := c.Param(key) + value, err := strconv.ParseFloat(str, 32) + if err != nil { + return 0, err + } + return float32(value), nil +} + +// ParamFloat64 returns the value of a URL parameter as a float64 for a given key. +func (c *Context) ParamFloat64(key string) (float64, error) { + str := c.Param(key) + value, err := strconv.ParseFloat(str, 64) + if err != nil { + return 0, err + } + return value, nil +} + +// ParamString returns the value of a URL parameter as a string for a given key. +func (c *Context) ParamString(key string) string { + return c.Param(key) +} + // Params returns all URL parameters for the req. func (c *Context) Params() map[string]string { return c.req.params() diff --git a/context_test.go b/context_test.go index 89cece5..ec6f118 100644 --- a/context_test.go +++ b/context_test.go @@ -237,6 +237,265 @@ func TestContext_Param(t *testing.T) { } } +func TestContext_ParamInt(t *testing.T) { + req, err := http.NewRequest("GET", "/users/123", nil) + if err != nil { + t.Fatal(err) + } + ctx, err := NewContext(httptest.NewRecorder(), req) + if err != nil { + t.Fatal(err) + } + params := map[string]string{"id": "123"} + ctx.setParams(params) + + got, err := ctx.ParamInt("id") + if err != nil { + t.Errorf("ctx.ParamInt(\"id\") returned an error: %v", err) + } + want := 123 + if got != want { + t.Errorf("ctx.ParamInt(\"id\") = %d, want %d", got, want) + } +} + +func TestContext_ParamIntWithException(t *testing.T) { + req, err := http.NewRequest("GET", "/users/abc", nil) + if err != nil { + t.Fatal(err) + } + ctx, err := NewContext(httptest.NewRecorder(), req) + if err != nil { + t.Fatal(err) + } + params := map[string]string{"id": "abc"} + ctx.setParams(params) + + _, err = ctx.ParamInt("id") + if err == nil { + t.Error("ctx.ParamInt(\"id\") did not return an error") + } +} + +func TestContext_ParamUInt(t *testing.T) { + req, err := http.NewRequest("GET", "/users/123", nil) + if err != nil { + t.Fatal(err) + } + ctx, err := NewContext(httptest.NewRecorder(), req) + if err != nil { + t.Fatal(err) + } + params := map[string]string{"id": "123"} + ctx.setParams(params) + + got, err := ctx.ParamUInt("id") + if err != nil { + t.Errorf("ctx.ParamUInt(\"id\") returned an error: %v", err) + } + want := uint(123) + if got != want { + t.Errorf("ctx.ParamUInt(\"id\") = %d, want %d", got, want) + } +} + +func TestContext_ParamUIntWithException(t *testing.T) { + req, err := http.NewRequest("GET", "/users/abc", nil) + if err != nil { + t.Fatal(err) + } + ctx, err := NewContext(httptest.NewRecorder(), req) + if err != nil { + t.Fatal(err) + } + params := map[string]string{"id": "abc"} + ctx.setParams(params) + + _, err = ctx.ParamUInt("id") + if err == nil { + t.Error("ctx.ParamUInt(\"id\") did not return an error") + } +} + +func TestContext_ParamInt64(t *testing.T) { + req, err := http.NewRequest("GET", "/users/123", nil) + if err != nil { + t.Fatal(err) + } + ctx, err := NewContext(httptest.NewRecorder(), req) + if err != nil { + t.Fatal(err) + } + params := map[string]string{"id": "123"} + ctx.setParams(params) + + got, err := ctx.ParamInt64("id") + if err != nil { + t.Errorf("ctx.ParamInt64(\"id\") returned an error: %v", err) + } + want := int64(123) + if got != want { + t.Errorf("ctx.ParamInt64(\"id\") = %d, want %d", got, want) + } +} + +func TestContext_ParamInt64WithException(t *testing.T) { + req, err := http.NewRequest("GET", "/users/abc", nil) + if err != nil { + t.Fatal(err) + } + ctx, err := NewContext(httptest.NewRecorder(), req) + if err != nil { + t.Fatal(err) + } + params := map[string]string{"id": "abc"} + ctx.setParams(params) + + _, err = ctx.ParamInt64("id") + if err == nil { + t.Error("ctx.ParamInt64(\"id\") did not return an error") + } +} + +func TestContext_ParamUInt64(t *testing.T) { + req, err := http.NewRequest("GET", "/users/123", nil) + if err != nil { + t.Fatal(err) + } + ctx, err := NewContext(httptest.NewRecorder(), req) + if err != nil { + t.Fatal(err) + } + params := map[string]string{"id": "123"} + ctx.setParams(params) + + got, err := ctx.ParamUInt64("id") + if err != nil { + t.Errorf("ctx.ParamUInt64(\"id\") returned an error: %v", err) + } + want := uint64(123) + if got != want { + t.Errorf("ctx.ParamUInt64(\"id\") = %d, want %d", got, want) + } +} + +func TestContext_ParamUInt64WithException(t *testing.T) { + req, err := http.NewRequest("GET", "/users/abc", nil) + if err != nil { + t.Fatal(err) + } + ctx, err := NewContext(httptest.NewRecorder(), req) + if err != nil { + t.Fatal(err) + } + params := map[string]string{"id": "abc"} + ctx.setParams(params) + + _, err = ctx.ParamUInt64("id") + if err == nil { + t.Error("ctx.ParamUInt64(\"id\") did not return an error") + } +} + +func TestContext_ParamFloat32(t *testing.T) { + req, err := http.NewRequest("GET", "/users/123.456", nil) + if err != nil { + t.Fatal(err) + } + ctx, err := NewContext(httptest.NewRecorder(), req) + if err != nil { + t.Fatal(err) + } + params := map[string]string{"id": "123.456"} + ctx.setParams(params) + + got, err := ctx.ParamFloat32("id") + if err != nil { + t.Errorf("ctx.ParamFloat32(\"id\") returned an error: %v", err) + } + want := float32(123.456) + if got != want { + t.Errorf("ctx.ParamFloat32(\"id\") = %f, want %f", got, want) + } +} + +func TestContext_ParamFloat32WithException(t *testing.T) { + req, err := http.NewRequest("GET", "/users/abc", nil) + if err != nil { + t.Fatal(err) + } + ctx, err := NewContext(httptest.NewRecorder(), req) + if err != nil { + t.Fatal(err) + } + params := map[string]string{"id": "abc"} + ctx.setParams(params) + + _, err = ctx.ParamFloat32("id") + if err == nil { + t.Error("ctx.ParamFloat32(\"id\") did not return an error") + } +} + +func TestContext_ParamFloat64(t *testing.T) { + req, err := http.NewRequest("GET", "/users/123.456", nil) + if err != nil { + t.Fatal(err) + } + ctx, err := NewContext(httptest.NewRecorder(), req) + if err != nil { + t.Fatal(err) + } + params := map[string]string{"id": "123.456"} + ctx.setParams(params) + + got, err := ctx.ParamFloat64("id") + if err != nil { + t.Errorf("ctx.ParamFloat64(\"id\") returned an error: %v", err) + } + want := float64(123.456) + if got != want { + t.Errorf("ctx.ParamFloat64(\"id\") = %f, want %f", got, want) + } +} + +func TestContext_ParamFloat64WithException(t *testing.T) { + req, err := http.NewRequest("GET", "/users/abc", nil) + if err != nil { + t.Fatal(err) + } + ctx, err := NewContext(httptest.NewRecorder(), req) + if err != nil { + t.Fatal(err) + } + params := map[string]string{"id": "abc"} + ctx.setParams(params) + + _, err = ctx.ParamFloat64("id") + if err == nil { + t.Error("ctx.ParamFloat64(\"id\") did not return an error") + } +} + +func TestContext_ParamString(t *testing.T) { + req, err := http.NewRequest("GET", "/users/123", nil) + if err != nil { + t.Fatal(err) + } + ctx, err := NewContext(httptest.NewRecorder(), req) + if err != nil { + t.Fatal(err) + } + params := map[string]string{"id": "123"} + ctx.setParams(params) + + got := ctx.ParamString("id") + want := "123" + if got != want { + t.Errorf("ctx.ParamString(\"id\") = %s, want %s", got, want) + } +} + func TestContext_Query(t *testing.T) { req, err := http.NewRequest("GET", "/path?key=value", nil) if err != nil {