package utils

import (
	"reflect"
	"regexp"
)

// ToMap struct to map[string]interface{}
func ToMap(ss interface{}) map[string]interface{} {
	t := reflect.ValueOf(ss).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
}
