44 lines
977 B
Go
44 lines
977 B
Go
package router
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
)
|
|
|
|
type genericValues map[string][]string
|
|
|
|
func (gv genericValues) Parse(data any, tag string) (err error) {
|
|
if gv == nil {
|
|
gv = make(genericValues)
|
|
}
|
|
d := reflect.ValueOf(data)
|
|
if d.Kind() != reflect.Struct {
|
|
err = fmt.Errorf("expected struct input for data")
|
|
return
|
|
}
|
|
for i := 0; i < d.NumField(); i++ {
|
|
key, ok := d.Type().Field(i).Tag.Lookup(tag)
|
|
if !ok || key == "" {
|
|
continue
|
|
}
|
|
v := make([]string, 0)
|
|
val := d.Field(i)
|
|
if val.Kind() == reflect.Pointer || val.Kind() == reflect.Interface {
|
|
val = val.Elem()
|
|
}
|
|
if val.Kind() == reflect.Slice || val.Kind() == reflect.Array {
|
|
for j := 0; j < val.Len(); j++ {
|
|
item := val.Index(j)
|
|
if item.Kind() == reflect.Pointer || item.Kind() == reflect.Interface {
|
|
item = item.Elem()
|
|
}
|
|
v = append(v, fmt.Sprintf("%s", item.Interface()))
|
|
}
|
|
} else {
|
|
v = append(v, fmt.Sprintf("%s", val.Interface()))
|
|
}
|
|
gv[key] = v
|
|
}
|
|
return
|
|
}
|