1 Star 0 Fork 0

ichub / goconfig

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
reflect_tags.go 1.53 KB
一键复制 编辑 原始数据 按行查看 历史
leijmdas 提交于 2024-04-01 20:54 . add
package reflectutils
import (
"github.com/emirpasic/gods/maps/linkedhashmap"
"github.com/sirupsen/logrus"
"reflect"
)
const TAG_ENV = "env"
const TAG_JSON = "json"
const TAG_GROM = "gorm"
type ReflectTags struct {
tagMap *linkedhashmap.Map `json:"tag_map"`
}
func NewReflectTags() *ReflectTags {
return &ReflectTags{
tagMap: linkedhashmap.New(),
}
}
func (this *ReflectTags) TagMap() *linkedhashmap.Map {
return this.tagMap
}
func (this *ReflectTags) SetTagMap(tagMap *linkedhashmap.Map) {
this.tagMap = tagMap
}
func (this *ReflectTags) ParseOne(v any, FieldName string, tagName string) {
elem := reflect.TypeOf(v).Elem()
structField, _ := elem.FieldByName(FieldName)
this.tagMap.Put(FieldName, structField.Tag.Get(tagName))
}
func (this *ReflectTags) ToString() string {
v, _ := this.tagMap.ToJSON()
return string(v)
}
func (this *ReflectTags) Log() {
this.tagMap.Each(func(key interface{}, value interface{}) {
logrus.Info(key, value)
})
this.tagMap.Map(func(key1 interface{}, value1 interface{}) (interface{}, interface{}) {
return key1, value1
})
}
func (this *ReflectTags) ParseGorm(v any) {
this.Parse(TAG_GROM, v)
}
func (this *ReflectTags) ParseJson(v any) {
this.Parse(TAG_JSON, v)
}
func (this *ReflectTags) ParseEnv(v any) {
this.Parse(TAG_ENV, v)
}
func (this *ReflectTags) Parse(key string, v any) {
elem := reflect.TypeOf(v).Elem()
for i := 0; i < elem.NumField(); i++ {
structField := elem.Field(i)
var tag = structField.Tag.Get(key)
this.tagMap.Put(structField.Name, tag)
logrus.Info(this.tagMap)
}
}
1
https://gitee.com/ichub/goconfig.git
git@gitee.com:ichub/goconfig.git
ichub
goconfig
goconfig
v1.0.407

搜索帮助