Skip to content

Commit

Permalink
Merge pull request #15 from go-labx/feature/param_parse
Browse files Browse the repository at this point in the history
feat: The newly added methods in the Context struct allow for retrieving URL parameters as different data types such as integers, floats, and strings.
  • Loading branch information
kk0829 authored Sep 22, 2023
2 parents 4cec741 + ba81a08 commit 73bf3a8
Show file tree
Hide file tree
Showing 4 changed files with 339 additions and 2 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -63,7 +75,7 @@

### Other

- chore: republish
- chore: republish

## [0.1.2] - Apr 3, 2023

Expand Down
66 changes: 66 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"encoding/xml"
"net/http"
"strconv"

"github.com/go-playground/validator/v10"
)
Expand Down Expand Up @@ -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()
Expand Down
259 changes: 259 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down

0 comments on commit 73bf3a8

Please sign in to comment.