diff --git a/filter/filter_impl/generic_filter.go b/filter/filter_impl/generic_filter.go index d385054ed98518177aea5e574e034cb35c72e398..f5a03ba37941bd0f27c9e72c59fa62e26174f6f4 100644 --- a/filter/filter_impl/generic_filter.go +++ b/filter/filter_impl/generic_filter.go @@ -93,11 +93,19 @@ func struct2MapAll(obj interface{}) interface{} { if t.Kind() == reflect.Struct { result := make(map[string]interface{}, t.NumField()) for i := 0; i < t.NumField(); i++ { - if v.Field(i).Kind() == reflect.Struct || v.Field(i).Kind() == reflect.Slice || v.Field(i).Kind() == reflect.Map { + switch v.Field(i).Kind() { + case reflect.Struct: + if v.Field(i).Type().String() == "time.Time" { + setInMap(result, t.Field(i), v.Field(i).Interface()) + break + } + fallthrough + case reflect.Slice | reflect.Map: if v.Field(i).CanInterface() { setInMap(result, t.Field(i), struct2MapAll(v.Field(i).Interface())) } - } else { + break + default: if v.Field(i).CanInterface() { setInMap(result, t.Field(i), v.Field(i).Interface()) } diff --git a/filter/filter_impl/generic_filter_test.go b/filter/filter_impl/generic_filter_test.go index e40733209b2e1db972ab576dea54f206a1e888c0..c8d6f2ec38452fb97f71cde529940372067d1541 100644 --- a/filter/filter_impl/generic_filter_test.go +++ b/filter/filter_impl/generic_filter_test.go @@ -20,6 +20,7 @@ package filter_impl import ( "reflect" "testing" + "time" ) import ( @@ -38,6 +39,7 @@ func TestStruct2MapAll(t *testing.T) { Xx string `m:"xx"` } `m:"xxYy"` } `m:"caCa"` + DaDa time.Time } testData.AaAa = "1" testData.BaBa = "1" @@ -45,6 +47,7 @@ func TestStruct2MapAll(t *testing.T) { testData.CaCa.AaAa = "2" testData.CaCa.XxYy.xxXx = "3" testData.CaCa.XxYy.Xx = "3" + testData.DaDa = time.Date(2020, 10, 29, 2, 34, 0, 0, time.Local) m := struct2MapAll(testData).(map[string]interface{}) assert.Equal(t, "1", m["aaAa"].(string)) assert.Equal(t, "1", m["baBa"].(string)) @@ -53,6 +56,7 @@ func TestStruct2MapAll(t *testing.T) { assert.Equal(t, reflect.Map, reflect.TypeOf(m["caCa"]).Kind()) assert.Equal(t, reflect.Map, reflect.TypeOf(m["caCa"].(map[string]interface{})["xxYy"]).Kind()) + assert.Equal(t, "2020-10-29 02:34:00", m["daDa"].(time.Time).Format("2006-01-02 15:04:05")) } type testStruct struct {