|
|
|
@ -10,37 +10,6 @@ import (
|
|
|
|
|
"strconv"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ConvertFn func(t reflect.Type, value reflect.Value) string
|
|
|
|
|
|
|
|
|
|
// StructToStrMap convert struct to string map, json tag
|
|
|
|
|
func StructToStrMap(v any, fn ConvertFn) map[string]string {
|
|
|
|
|
// 返回 key-value
|
|
|
|
|
ret := make(map[string]string)
|
|
|
|
|
rType := reflect.TypeOf(v)
|
|
|
|
|
rValue := reflect.ValueOf(v)
|
|
|
|
|
|
|
|
|
|
rValue = Indirect(rValue)
|
|
|
|
|
if rType.Kind() == reflect.Ptr {
|
|
|
|
|
rType = rType.Elem()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < rType.NumField(); i++ {
|
|
|
|
|
rField := rType.Field(i)
|
|
|
|
|
jsonTag := rField.Tag.Get("json")
|
|
|
|
|
if jsonTag == "" || jsonTag == "-" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
//rField.Type
|
|
|
|
|
if fn == nil {
|
|
|
|
|
fn = func(t reflect.Type, value reflect.Value) string {
|
|
|
|
|
return String(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ret[jsonTag] = fn(rField.Type, rValue.Field(i))
|
|
|
|
|
}
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BaseTypeVal convert custom type or intX,uintX,floatX to generic base type.
|
|
|
|
|
//
|
|
|
|
|
// intX/unitX => int64
|
|
|
|
|