go-gallery/modules/utils/utils.go

30 lines
536 B
Go
Raw Normal View History

2018-04-17 10:04:36 +00:00
package utils
2018-04-18 06:16:29 +00:00
import (
"reflect"
"regexp"
)
2018-04-17 10:04:36 +00:00
// ToMap struct to map[string]interface{}
func ToMap(ss interface{}) map[string]interface{} {
t := reflect.ValueOf(ss).Elem()
smap := make(map[string]interface{})
2018-04-18 06:16:29 +00:00
mtag := regexp.MustCompile(`cc:\"(.+)\"`)
2018-04-17 10:04:36 +00:00
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
2018-04-18 06:16:29 +00:00
tag := string(t.Type().Field(i).Tag)
str := mtag.FindStringSubmatch(tag)
name := t.Type().Field(i).Name
if len(str) > 1 {
name = str[1]
}
if name != "-" {
smap[name] = f.Interface()
}
2018-04-17 10:04:36 +00:00
}
return smap
}