go-gallery/modules/utils/utils.go

33 lines
577 B
Go

package utils
import (
"reflect"
"regexp"
)
// ToMap struct to map[string]interface{}
func ToMap(ss interface{}) map[string]interface{} {
t := reflect.ValueOf(ss)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
smap := make(map[string]interface{})
mtag := regexp.MustCompile(`cc:\"(.+)\"`)
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
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()
}
}
return smap
}