Ai
1 Star 1 Fork 0

titan-kit/titan

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
bind.go 1.13 KB
一键复制 编辑 原始数据 按行查看 历史
蝶衣人生 提交于 2021-06-16 12:00 +08:00 . 去除mux适应新架构
package binding
import (
"net/http"
"net/url"
"strings"
"google.golang.org/protobuf/proto"
)
// BindForm 将表单参数绑定到target.
func BindForm(req *http.Request, target interface{}) error {
if err := req.ParseForm(); err != nil {
return err
}
if msg, ok := target.(proto.Message); ok {
return mapProto(msg, req.Form)
}
return mapForm(target, req.Form)
}
// GetQuery return the key value, of the current *http.Request query
func GetQuery(req *http.Request, key string) []string {
if ok, value := extractQueries(req); ok {
return value[key]
}
return nil
}
// GetAllQueries return all queries of the current *http.Request
func GetAllQueries(req *http.Request) map[string][]string {
if ok, values := extractQueries(req); ok {
return values
}
return nil
}
func extractQueries(req *http.Request) (bool, map[string][]string) {
if q, err := url.ParseQuery(req.URL.RawQuery); err == nil {
var queries = make(map[string][]string)
for k, v := range q {
for _, item := range v {
values := strings.Split(item, ",")
queries[k] = append(queries[k], values...)
}
}
return true, queries
}
return false, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/titan-kit/titan.git
git@gitee.com:titan-kit/titan.git
titan-kit
titan
titan
v0.0.4

搜索帮助