fiber/binder/query.go
Sumit Kumar 26cc477500
🔥 feat: Add support for CBOR encoding (#3173)
* feat(cbor): allow encoding response bodies in cbor

* fix(tests::cbor): encode struct instead of a randomly ordered hashmap

* docs(whats_new): add cbor in context section

* feat(binder): introduce CBOR

* feat(client): allow cbor in fiber client

* chore(tests): add more test

* chore(packages): go mod tidy

* fix(binder): update CBOR name and test

* improve test coverage

* improve test coverage

* update1

* add docs

* doc fixes

* update

* Fix markdown lint

* Add missing entry from binder README

* add/refresh documentation

---------

Co-authored-by: Juan Calderon-Perez <835733+gaby@users.noreply.github.com>
Co-authored-by: M. Efe Çetin <efectn@protonmail.com>
Co-authored-by: RW <rene@gofiber.io>
2024-12-01 11:03:50 +01:00

52 lines
1014 B
Go

package binder
import (
"reflect"
"strings"
"github.com/gofiber/utils/v2"
"github.com/valyala/fasthttp"
)
// queryBinding is the query binder for query request body.
type queryBinding struct{}
// Name returns the binding name.
func (*queryBinding) Name() string {
return "query"
}
// Bind parses the request query and returns the result.
func (b *queryBinding) Bind(reqCtx *fasthttp.RequestCtx, out any) error {
data := make(map[string][]string)
var err error
reqCtx.QueryArgs().VisitAll(func(key, val []byte) {
if err != nil {
return
}
k := utils.UnsafeString(key)
v := utils.UnsafeString(val)
if strings.Contains(k, "[") {
k, err = parseParamSquareBrackets(k)
}
if strings.Contains(v, ",") && equalFieldType(out, reflect.Slice, k) {
values := strings.Split(v, ",")
for i := 0; i < len(values); i++ {
data[k] = append(data[k], values[i])
}
} else {
data[k] = append(data[k], v)
}
})
if err != nil {
return err
}
return parse(b.Name(), out, data)
}