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{} {
|
2018-04-24 09:49:58 +00:00
|
|
|
t := reflect.ValueOf(ss)
|
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
t = t.Elem()
|
|
|
|
}
|
2018-04-17 10:04:36 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|