From 4447dbfe14bb7e404be84a785394b40ad801fcc1 Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Wed, 23 Aug 2023 10:44:18 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=20StructToMap=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E6=9B=B4=E5=A5=BD=E7=9A=84=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E5=9C=A8=20nmapper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nreflect/convert.go | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/nreflect/convert.go b/nreflect/convert.go index a5e4514..2100c1f 100644 --- a/nreflect/convert.go +++ b/nreflect/convert.go @@ -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