15 Star 45 Fork 15

fifsky/gosql

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
reflect.go 1.24 KB
一键复制 编辑 原始数据 按行查看 历史
fifsky 提交于 2018-12-05 19:41 . support relation
package gosql
import (
"reflect"
"github.com/jmoiron/sqlx/reflectx"
)
type ReflectMapper struct {
mapper *reflectx.Mapper
}
func NewReflectMapper(tagName string) *ReflectMapper {
return &ReflectMapper{
mapper: reflectx.NewMapper(tagName),
}
}
// FieldByName returns a field by its mapped name as a reflect.Value.
// Panics if v's Kind is not Struct or v is not Indirectable to a struct Kind.
// Returns zero Value if the name is not found.
func (r *ReflectMapper) FieldByName(v reflect.Value, name string) reflect.Value {
return r.mapper.FieldByName(v, name)
}
// FieldMap returns the mapper's mapping of field names to reflect values. Panics
// if v's Kind is not Struct, or v is not Indirectable to a struct kind.
func (r *ReflectMapper) FieldMap(v reflect.Value) map[string]reflect.Value {
v = reflect.Indirect(v)
ret := map[string]reflect.Value{}
tm := r.mapper.TypeMap(v.Type())
for tagName, fi := range tm.Names {
//fmt.Println(tagName,fi.Parent.Zero.Kind(),fi.Parent.Field.Anonymous)
if (fi.Parent.Zero.Kind() == reflect.Struct || (fi.Zero.Kind() == reflect.Ptr && fi.Zero.Type().Elem().Kind() == reflect.Struct)) && !fi.Parent.Field.Anonymous {
continue
}
ret[tagName] = reflectx.FieldByIndexes(v, fi.Index)
}
return ret
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/fifsky/gosql.git
[email protected]:fifsky/gosql.git
fifsky
gosql
gosql
master

搜索帮助