1 Star 2 Fork 3

kristas/booting-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
value_mapper.go 3.65 KB
一键复制 编辑 原始数据 按行查看 历史
package iris_plugin
import (
"encoding/json"
"gitee.com/kristas/booting-go/framework/web"
"github.com/kataras/iris/v12/context"
"io/ioutil"
"mime/multipart"
"net/http"
"reflect"
)
type IrisMapperAdapter struct {
c context.Context
}
func (r *IrisMapperAdapter) GetStringFromPath(param web.Param) string {
return r.c.Params().GetString(param.Name)
}
func (r *IrisMapperAdapter) GetIntFromPath(param web.Param) int {
return r.c.Params().GetIntDefault(param.Name, 0)
}
func (r *IrisMapperAdapter) GetBoolFromPath(param web.Param) bool {
return r.c.Params().GetBoolDefault(param.Name, false)
}
func (r *IrisMapperAdapter) GetFloatFromPath(param web.Param) float64 {
return r.c.Params().GetFloat64Default(param.Name, 0.00)
}
func (r *IrisMapperAdapter) GetStringFromQuery(param web.Param) string {
return r.c.URLParam(param.Name)
}
func (r *IrisMapperAdapter) GetIntFromQuery(param web.Param) int {
return r.c.URLParamIntDefault(param.Name, 0)
}
func (r *IrisMapperAdapter) GetBoolFromQuery(param web.Param) bool {
paramBool, _ := r.c.URLParamBool(param.Name)
return paramBool
}
func (r *IrisMapperAdapter) GetFloatFromQuery(param web.Param) float64 {
return r.c.URLParamFloat64Default(param.Name, 0.00)
}
func (r *IrisMapperAdapter) GetStructFromQuery(param web.Param) interface{} {
v := reflect.New(param.Type).Interface()
params := r.c.URLParams()
bytes, _ := json.Marshal(params)
json.Unmarshal(bytes, v)
return reflect.ValueOf(v).Elem().Interface()
}
func (r *IrisMapperAdapter) GetStructFromBody(param web.Param) interface{} {
return buildBody(param.Type, r.c)
}
func (r *IrisMapperAdapter) GetMapFromBody(param web.Param) interface{} {
return buildBody(param.Type, r.c)
}
func (r *IrisMapperAdapter) GetStringFromHeader(param web.Param) string {
return r.c.GetHeader(param.Name)
}
func (r *IrisMapperAdapter) GetStringFromForm(param web.Param) string {
return r.c.PostValue(param.Name)
}
func (r *IrisMapperAdapter) GetIntFromForm(param web.Param) int {
return r.c.PostValueIntDefault(param.Name, 0)
}
func (r *IrisMapperAdapter) GetBoolFromForm(param web.Param) bool {
valueBool, _ := r.c.PostValueBool(param.Name)
return valueBool
}
func (r *IrisMapperAdapter) GetFloatFromForm(param web.Param) float64 {
return r.c.PostValueFloat64Default(param.Name, 0.00)
}
func (r *IrisMapperAdapter) GetMapFromForm() map[string][]string {
return r.c.FormValues()
}
func (r *IrisMapperAdapter) GetStringFromMultipart(param web.Param) string {
file, header, err := r.c.FormFile(param.Name)
if header == nil {
return err.Error()
}
var temp = make([]byte, header.Size)
if file != nil {
file.Read(temp)
return string(temp)
}
return ""
}
func (r *IrisMapperAdapter) GetMultipartHeader(param web.Param) *multipart.FileHeader {
_, header, _ := r.c.FormFile(param.Name)
return header
}
func (r *IrisMapperAdapter) GetMultipartFile(param web.Param) multipart.File {
file, _, _ := r.c.FormFile(param.Name)
return file
}
func (r *IrisMapperAdapter) GetStream() []byte {
bodyReader := r.GetRequest().Body
buf, _ := ioutil.ReadAll(bodyReader)
return buf
}
func (r *IrisMapperAdapter) GetRequest() *http.Request {
return r.c.Request()
}
func (r *IrisMapperAdapter) GetResponseWriter() http.ResponseWriter {
return r.c.ResponseWriter()
}
func (r *IrisMapperAdapter) Get(param web.Param) interface{} {
return map[string]interface{}{
"context": r.c,
"app": irisApp,
"iris": irisApp,
}[param.Source]
}
func NewIrisMapperAdapter(c context.Context) *IrisMapperAdapter {
return &IrisMapperAdapter{c: c}
}
func buildBody(in reflect.Type, c context.Context) interface{} {
v := reflect.New(in).Interface()
c.ReadJSON(v)
return reflect.ValueOf(v).Elem().Interface()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kristas/booting-go.git
git@gitee.com:kristas/booting-go.git
kristas
booting-go
booting-go
v1.3.3

搜索帮助