1 Star 0 Fork 0

hh/iris

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
model.go 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
package model
import (
"reflect"
"github.com/kataras/iris/mvc/activator/field"
"github.com/kataras/iris/context"
)
// Controller is responsible
// to load and handle the `Model(s)` inside a controller struct
// via the `iris:"model"` tag field.
// It stores the optional models from
// the struct's fields values that
// are being setted by the method function
// and set them as ViewData.
type Controller struct {
fields []field.Field
}
// Load tries to lookup and set for any valid model field.
// Returns nil if no models are being used.
func Load(typ reflect.Type) *Controller {
matcher := func(f reflect.StructField) bool {
if tag, ok := f.Tag.Lookup("iris"); ok {
if tag == "model" {
return true
}
}
return false
}
fields := field.LookupFields(typ.Elem(), matcher, nil)
if len(fields) == 0 {
return nil
}
mc := &Controller{
fields: fields,
}
return mc
}
// Handle transfer the models to the view.
func (mc *Controller) Handle(ctx context.Context, c reflect.Value) {
elem := c.Elem() // controller should always be a pointer at this state
for _, f := range mc.fields {
index := f.GetIndex()
typ := f.GetType()
name := f.GetTagName()
elemField := elem.FieldByIndex(index)
// check if current controller's element field
// is valid, is not nil and it's type is the same (should be but make that check to be sure).
if !elemField.IsValid() ||
(elemField.Kind() == reflect.Ptr && elemField.IsNil()) ||
elemField.Type() != typ {
continue
}
fieldValue := elemField.Interface()
ctx.ViewData(name, fieldValue)
// /*maybe some time in the future*/ if resetable {
// // clean up
// elemField.Set(reflect.Zero(typ))
// }
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/w1229748769/iris.git
git@gitee.com:w1229748769/iris.git
w1229748769
iris
iris
v8.4.4

搜索帮助