mirror of
https://github.com/go-kratos/kratos.git
synced 2025-02-03 13:11:42 +02:00
b9f821c29f
* add http handler * fix request error
31 lines
683 B
Go
31 lines
683 B
Go
package binding
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// BindForm bind form parameters to 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)
|
|
}
|
|
|
|
// BindValue bind map parameters to target.
|
|
func BindValue(vars map[string]string, target interface{}) error {
|
|
values := make(map[string][]string, len(vars))
|
|
for k, v := range vars {
|
|
values[k] = []string{v}
|
|
}
|
|
if msg, ok := target.(proto.Message); ok {
|
|
return mapProto(msg, values)
|
|
}
|
|
return mapForm(target, values)
|
|
}
|