Skip to content

Commit

Permalink
feat: [#338] Support c.Request().BindQuery to bind query params to a …
Browse files Browse the repository at this point in the history
…struct (#74)

* bind query to struct

* reorder methods

* add test cases

* update go mod file

* optimize test
  • Loading branch information
kkumar-gcc authored Jul 14, 2024
1 parent fe9e02c commit 25b3f7e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 15 deletions.
4 changes: 4 additions & 0 deletions context_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func (r *ContextRequest) Bind(obj any) error {
return r.instance.ShouldBind(obj)
}

func (r *ContextRequest) BindQuery(obj any) error {
return r.instance.ShouldBindQuery(obj)
}

func (r *ContextRequest) Cookie(key string, defaultValue ...string) string {
cookie, err := r.instance.Cookie(key)
if err != nil {
Expand Down
49 changes: 49 additions & 0 deletions context_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,55 @@ func (s *ContextRequestSuite) TestBind_Query() {
s.Equal(http.StatusOK, code)
}

func (s *ContextRequestSuite) TestBindQueryToStruct() {
s.route.Get("/bind/query/struct", func(ctx contractshttp.Context) contractshttp.Response {
type Test struct {
ID string `form:"id"`
}
var test Test
_ = ctx.Request().BindQuery(&test)
return ctx.Response().Success().Json(contractshttp.Json{
"id": test.ID,
})
})

req, err := http.NewRequest("GET", "/bind/query/struct?id=2", nil)
s.Require().Nil(err)

req.Header.Set("Content-Type", "application/json")
code, body, _, _ := s.request(req)

s.Equal("{\"id\":\"2\"}", body)
s.Equal(http.StatusOK, code)

// complex struct
s.route.Get("/bind/query/struct/complex", func(ctx contractshttp.Context) contractshttp.Response {
type Person struct {
Name string `form:"name" json:"name"`
Age int `form:"age" json:"age"`
}
type Test struct {
ID string `form:"id"`
Persons []Person `form:"persons"`
}
var test Test
_ = ctx.Request().BindQuery(&test)
return ctx.Response().Success().Json(contractshttp.Json{
"id": test.ID,
"persons": test.Persons,
})
})

req, err = http.NewRequest("GET", "/bind/query/struct/complex?id=2&persons={\"name\":\"John\",\"age\":30}&persons={\"name\":\"Doe\",\"age\":40}", nil)
s.Require().Nil(err)

req.Header.Set("Content-Type", "application/json")
code, body, _, _ = s.request(req)

s.Equal("{\"id\":\"2\",\"persons\":[{\"name\":\"John\",\"age\":30},{\"name\":\"Doe\",\"age\":40}]}", body)
s.Equal(http.StatusOK, code)
}

func (s *ContextRequestSuite) TestBind_ThenInput() {
s.route.Post("/bind/input", func(ctx contractshttp.Context) contractshttp.Response {
type Test struct {
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
require (
github.com/gin-gonic/gin v1.10.0
github.com/gookit/validate v1.5.2
github.com/goravel/framework v1.14.1-0.20240712021404-0ba88953a817
github.com/goravel/framework v1.14.1-0.20240713081914-3c96472aa3a4
github.com/rs/cors v1.11.0
github.com/savioxavier/termlink v1.3.0
github.com/spf13/cast v1.6.0
Expand Down Expand Up @@ -39,9 +39,9 @@ require (
github.com/charmbracelet/bubbles v0.18.0 // indirect
github.com/charmbracelet/bubbletea v0.26.4 // indirect
github.com/charmbracelet/huh v0.5.1 // indirect
github.com/charmbracelet/huh/spinner v0.0.0-20240711191530-dbf5a5ac0510 // indirect
github.com/charmbracelet/lipgloss v0.12.0 // indirect
github.com/charmbracelet/x/ansi v0.1.3 // indirect
github.com/charmbracelet/huh/spinner v0.0.0-20240712195021-ccca06d54254 // indirect
github.com/charmbracelet/lipgloss v0.12.1 // indirect
github.com/charmbracelet/x/ansi v0.1.4 // indirect
github.com/charmbracelet/x/exp/strings v0.0.0-20240617190524-788ec55faed1 // indirect
github.com/charmbracelet/x/input v0.1.2 // indirect
github.com/charmbracelet/x/term v0.1.1 // indirect
Expand Down Expand Up @@ -124,7 +124,7 @@ require (
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/pterm/pterm v0.12.79 // indirect
github.com/rabbitmq/amqp091-go v1.9.0 // indirect
github.com/redis/go-redis/v9 v9.5.3 // indirect
github.com/redis/go-redis/v9 v9.5.4 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ github.com/charmbracelet/bubbletea v0.26.4 h1:2gDkkzLZaTjMl/dQBpNVtnvcCxsh/FCkim
github.com/charmbracelet/bubbletea v0.26.4/go.mod h1:P+r+RRA5qtI1DOHNFn0otoNwB4rn+zNAzSj/EXz6xU0=
github.com/charmbracelet/huh v0.5.1 h1:t5j6g9sMjAE2a9AQuc4lNL7pf/0X4WdHiiMGkL8v/aM=
github.com/charmbracelet/huh v0.5.1/go.mod h1:gs7b2brpzXkY0PBWUqJrlzvOowTCL0vNAR6OTItc+kA=
github.com/charmbracelet/huh/spinner v0.0.0-20240711191530-dbf5a5ac0510 h1:+mSQLejKRzoK3bzK6ZXuA8fwrZMd3GthHn5/Gj9BGo4=
github.com/charmbracelet/huh/spinner v0.0.0-20240711191530-dbf5a5ac0510/go.mod h1:CrXBZnOWs3zpyppOZZS7lu2CpLq2jx6U5chL/frRG/E=
github.com/charmbracelet/lipgloss v0.12.0 h1:k2RgmrgUjrWWu9FaMERoYK1iiPiwg+SSqGYu9t8/k4Y=
github.com/charmbracelet/lipgloss v0.12.0/go.mod h1:beLlcmkF7MWA+5UrKKIRo/VJ21xGXr7YJ9miWfdMRIU=
github.com/charmbracelet/x/ansi v0.1.3 h1:RBh/eleNWML5R524mjUF0yVRePTwqN9tPtV+DPgO5Lw=
github.com/charmbracelet/x/ansi v0.1.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/huh/spinner v0.0.0-20240712195021-ccca06d54254 h1:GhjNbVAWHcYdw5DHmELKs3xhiVFvZ+/fte4jl2PVA7s=
github.com/charmbracelet/huh/spinner v0.0.0-20240712195021-ccca06d54254/go.mod h1:CrXBZnOWs3zpyppOZZS7lu2CpLq2jx6U5chL/frRG/E=
github.com/charmbracelet/lipgloss v0.12.1 h1:/gmzszl+pedQpjCOH+wFkZr/N90Snz40J/NR7A0zQcs=
github.com/charmbracelet/lipgloss v0.12.1/go.mod h1:V2CiwIuhx9S1S1ZlADfOj9HmxeMAORuz5izHb0zGbB8=
github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM=
github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/exp/strings v0.0.0-20240617190524-788ec55faed1 h1:VZIQzjwFE0EamzG2v8HfemeisB8X02Tl0BZBnJ0PeU8=
github.com/charmbracelet/x/exp/strings v0.0.0-20240617190524-788ec55faed1/go.mod h1:pBhA0ybfXv6hDjQUZ7hk1lVxBiUbupdw5R31yPUViVQ=
github.com/charmbracelet/x/exp/term v0.0.0-20240524151031-ff83003bf67a h1:k/s6UoOSVynWiw7PlclyGO2VdVs5ZLbMIHiGp4shFZE=
Expand Down Expand Up @@ -394,8 +394,8 @@ github.com/gookit/validate v1.5.2 h1:i5I2OQ7WYHFRPRATGu9QarR9snnNHydvwSuHXaRWAV0
github.com/gookit/validate v1.5.2/go.mod h1:yuPy2WwDlwGRa06fFJ5XIO8QEwhRnTC2LmxmBa5SE14=
github.com/goravel/file-rotatelogs/v2 v2.4.2 h1:g68AzbePXcm0V2CpUMc9j4qVzcDn7+7aoWSjZ51C0m4=
github.com/goravel/file-rotatelogs/v2 v2.4.2/go.mod h1:23VuSW8cBS4ax5cmbV+5AaiLpq25b8UJ96IhbAkdo8I=
github.com/goravel/framework v1.14.1-0.20240712021404-0ba88953a817 h1:EUrvRlOe8NaUfjCbqNnNGksfXzC6gfWYu6iJE2zJ9ys=
github.com/goravel/framework v1.14.1-0.20240712021404-0ba88953a817/go.mod h1:0uHydqW/ZCaHOZ+8kMx3uxRkhpam2L2odwbyszSPLJk=
github.com/goravel/framework v1.14.1-0.20240713081914-3c96472aa3a4 h1:7+HOyQEfGLSvSzHSvY+0guk4t5KRLtQB4pwzSSPW7KI=
github.com/goravel/framework v1.14.1-0.20240713081914-3c96472aa3a4/go.mod h1:w5TR5ChUoRTV63nQbwHKreY9lwqGAb9f5iZYmoHxH+U=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -553,8 +553,8 @@ github.com/rabbitmq/amqp091-go v1.9.0 h1:qrQtyzB4H8BQgEuJwhmVQqVHB9O4+MNDJCCAcpc
github.com/rabbitmq/amqp091-go v1.9.0/go.mod h1:+jPrT9iY2eLjRaMSRHUhc3z14E/l85kv/f+6luSD3pc=
github.com/redis/go-redis/v9 v9.0.2/go.mod h1:/xDTe9EF1LM61hek62Poq2nzQSGj0xSrEtEHbBQevps=
github.com/redis/go-redis/v9 v9.0.5/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
github.com/redis/go-redis/v9 v9.5.3 h1:fOAp1/uJG+ZtcITgZOfYFmTKPE7n4Vclj1wZFgRciUU=
github.com/redis/go-redis/v9 v9.5.3/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/redis/go-redis/v9 v9.5.4 h1:vOFYDKKVgrI5u++QvnMT7DksSMYg7Aw/Np4vLJLKLwY=
github.com/redis/go-redis/v9 v9.5.4/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 h1:mZHayPoR0lNmnHyvtYjDeq0zlVHn9K/ZXoy17ylucdo=
Expand Down

0 comments on commit 25b3f7e

Please sign in to comment.