Skip to content

Commit

Permalink
chore: replace vendored gorilla/schema package
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Sep 30, 2024
1 parent 44cd700 commit 54778a2
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 1,364 deletions.
36 changes: 36 additions & 0 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func Test_Bind_Query(t *testing.T) {
No []int64
ID int
Bool bool
Default string `query:"default,default:hello"`
Defaults []string `query:"defaults,default:hello|world"`
}

c.Request().URI().SetQueryString("id=1&name=tom&hobby=basketball,football&favouriteDrinks=milo,coke,pepsi&alloc=&no=1")
Expand All @@ -76,6 +78,8 @@ func Test_Bind_Query(t *testing.T) {
require.Equal(t, nilSlice, q2.Empty)
require.Equal(t, []string{""}, q2.Alloc)
require.Equal(t, []int64{1}, q2.No)
require.Equal(t, "hello", q2.Default)
require.Equal(t, []string{"hello", "world"}, q2.Defaults)

type RequiredQuery struct {
Name string `query:"name,required"`
Expand Down Expand Up @@ -648,7 +652,39 @@ func Benchmark_Bind_Query(b *testing.B) {
for n := 0; n < b.N; n++ {
err = c.Bind().Query(q)
}

require.NoError(b, err)
require.Equal(b, "tom", q.Name)
require.Equal(b, 1, q.ID)
require.Len(b, q.Hobby, 2)
}

// go test -v -run=^$ -bench=Benchmark_Bind_Query_Default -benchmem -count=4
func Benchmark_Bind_Query_Default(b *testing.B) {
var err error

app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Query struct {
Name string `query:"name,default:tom"`
Hobby []string `query:"hobby,default:football|basketball"`
ID int `query:"id,default:1"`
}
c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
c.Request().URI().SetQueryString("")
q := new(Query)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
err = c.Bind().Query(q)
}

require.NoError(b, err)
require.Equal(b, "tom", q.Name)
require.Equal(b, 1, q.ID)
require.Len(b, q.Hobby, 2)
}

// go test -v -run=^$ -bench=Benchmark_Bind_Query_Map -benchmem -count=4
Expand Down
2 changes: 1 addition & 1 deletion binder/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/gofiber/utils/v2"
"github.com/valyala/bytebufferpool"

"github.com/gofiber/fiber/v3/internal/schema"
"github.com/gofiber/schema"
)

// ParserConfig form decoder config for SetParserDecoder
Expand Down
37 changes: 37 additions & 0 deletions docs/api/bind.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,40 @@ app.Post("/", func(c fiber.Ctx) error {
}
})
```

## Default Fields

You can set default values for fields in the struct by using the `default` struct tag. Supported types:
- bool

Check failure on line 580 in docs/api/bind.md

View workflow job for this annotation

GitHub Actions / markdownlint

Lists should be surrounded by blank lines

docs/api/bind.md:580 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "- bool"] https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md032.md
- float variants (float32, float64)
- int variants (int, int8, int16, int32, int64)
- uint variants (uint, uint8, uint16, uint32, uint64)
- string
- a slice of the above types. As shown in the example above, **| should be used to separate between slice items**.
- a pointer to one of the above types **(pointer to slice and slice of pointers are not supported)**.


Check failure on line 588 in docs/api/bind.md

View workflow job for this annotation

GitHub Actions / markdownlint

Multiple consecutive blank lines

docs/api/bind.md:588 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2] https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md012.md
```go title="Example"
type Person struct {
Name string `query:"name,default:john"`
Pass string `query:"pass"`
Products []string `query:"products,default:shoe|hat"`
}

app.Get("/", func(c fiber.Ctx) error {
p := new(Person)

if err := c.Bind().Query(p); err != nil {
return err
}

log.Println(p.Name) // john
log.Println(p.Pass) // doe
log.Println(p.Products) // ["shoe,hat"]

// ...
})
// Run tests with the following curl command

// curl "http://localhost:3000/?pass=doe"
```

Check failure on line 612 in docs/api/bind.md

View workflow job for this annotation

GitHub Actions / markdownlint

Files should end with a single newline character

docs/api/bind.md:612:3 MD047/single-trailing-newline Files should end with a single newline character https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md047.md
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"errors"

"github.com/gofiber/fiber/v3/internal/schema"
"github.com/gofiber/schema"
)

// Wrap and return this for unreachable code if panicking is undesirable (i.e., in a handler).
Expand Down
2 changes: 1 addition & 1 deletion error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"testing"

"github.com/gofiber/fiber/v3/internal/schema"
"github.com/gofiber/schema"
"github.com/stretchr/testify/require"
)

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gofiber/schema v0.0.0-20240930192558-a38168951d4b // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/philhofer/fwd v1.1.3-0.20240612014219-fbbf4953d986 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofiber/schema v0.0.0-20240930192558-a38168951d4b h1:V18jx4lrq1QwBHKfQpiEAezWxlMLwBA5WRuwx7A1Mjo=
github.com/gofiber/schema v0.0.0-20240930192558-a38168951d4b/go.mod h1:YYwj01w3hVfaNjhtJzaqetymL56VW642YS3qZPhuE6c=
github.com/gofiber/utils/v2 v2.0.0-beta.6 h1:ED62bOmpRXdgviPlfTmf0Q+AXzhaTUAFtdWjgx+XkYI=
github.com/gofiber/utils/v2 v2.0.0-beta.6/go.mod h1:3Kz8Px3jInKFvqxDzDeoSygwEOO+3uyubTmUa6PqY+0=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand Down
27 changes: 0 additions & 27 deletions internal/schema/LICENSE

This file was deleted.

Loading

0 comments on commit 54778a2

Please sign in to comment.