From 71035dda4e096f4e0fa57969c9f16dec7ca9097b Mon Sep 17 00:00:00 2001 From: fangyincheng Date: Fri, 30 Oct 2020 01:57:18 +0800 Subject: [PATCH 1/4] fix generic struct2MapAll --- filter/filter_impl/generic_filter.go | 12 ++++++++++-- filter/filter_impl/generic_filter_test.go | 4 ++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/filter/filter_impl/generic_filter.go b/filter/filter_impl/generic_filter.go index d385054ed9..f5a03ba379 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 e40733209b..c8d6f2ec38 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 { From cd14775356336b696a8d8f5444030014db47a063 Mon Sep 17 00:00:00 2001 From: fangyincheng Date: Sat, 31 Oct 2020 03:17:55 +0800 Subject: [PATCH 2/4] Fix: fixed --- filter/filter_impl/generic_filter.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/filter/filter_impl/generic_filter.go b/filter/filter_impl/generic_filter.go index f5a03ba379..7d7a54fd1c 100644 --- a/filter/filter_impl/generic_filter.go +++ b/filter/filter_impl/generic_filter.go @@ -95,13 +95,15 @@ func struct2MapAll(obj interface{}) interface{} { for i := 0; i < t.NumField(); i++ { 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: + case reflect.Slice: + fallthrough + case reflect.Map: if v.Field(i).CanInterface() { + if v.Field(i).Type().String() == "time.Time" { + setInMap(result, t.Field(i), v.Field(i).Interface()) + break + } setInMap(result, t.Field(i), struct2MapAll(v.Field(i).Interface())) } break From 7e0df5a2a7f31c13d7b34c07c4a41dff1d005819 Mon Sep 17 00:00:00 2001 From: fangyincheng Date: Sat, 31 Oct 2020 15:16:37 +0800 Subject: [PATCH 3/4] import --- filter/filter_impl/generic_filter.go | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/filter/filter_impl/generic_filter.go b/filter/filter_impl/generic_filter.go index 7d7a54fd1c..e38a78499c 100644 --- a/filter/filter_impl/generic_filter.go +++ b/filter/filter_impl/generic_filter.go @@ -93,23 +93,20 @@ func struct2MapAll(obj interface{}) interface{} { if t.Kind() == reflect.Struct { result := make(map[string]interface{}, t.NumField()) for i := 0; i < t.NumField(); i++ { - switch v.Field(i).Kind() { - case reflect.Struct: - fallthrough - case reflect.Slice: - fallthrough - case reflect.Map: - if v.Field(i).CanInterface() { - if v.Field(i).Type().String() == "time.Time" { - setInMap(result, t.Field(i), v.Field(i).Interface()) + field := t.Field(i) + value := v.Field(i) + kind := value.Kind() + if kind == reflect.Struct || kind == reflect.Slice || kind == reflect.Map { + if value.CanInterface() { + if value.Type().String() == "time.Time" { + setInMap(result, field, value.Interface()) break } - setInMap(result, t.Field(i), struct2MapAll(v.Field(i).Interface())) + setInMap(result, field, struct2MapAll(value.Interface())) } - break - default: - if v.Field(i).CanInterface() { - setInMap(result, t.Field(i), v.Field(i).Interface()) + } else { + if value.CanInterface() { + setInMap(result, field, value.Interface()) } } } From 5ab9ccb630a0e3e6460e10b7d7a26a9e844f20e1 Mon Sep 17 00:00:00 2001 From: fangyincheng Date: Sat, 31 Oct 2020 16:21:06 +0800 Subject: [PATCH 4/4] fixed --- filter/filter_impl/generic_filter.go | 10 ++++++---- filter/filter_impl/generic_filter_test.go | 3 +++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/filter/filter_impl/generic_filter.go b/filter/filter_impl/generic_filter.go index e38a78499c..36b4b13186 100644 --- a/filter/filter_impl/generic_filter.go +++ b/filter/filter_impl/generic_filter.go @@ -21,6 +21,7 @@ import ( "context" "reflect" "strings" + "time" ) import ( @@ -98,11 +99,12 @@ func struct2MapAll(obj interface{}) interface{} { kind := value.Kind() if kind == reflect.Struct || kind == reflect.Slice || kind == reflect.Map { if value.CanInterface() { - if value.Type().String() == "time.Time" { - setInMap(result, field, value.Interface()) - break + tmp := value.Interface() + if _, ok := tmp.(time.Time); ok { + setInMap(result, field, tmp) + continue } - setInMap(result, field, struct2MapAll(value.Interface())) + setInMap(result, field, struct2MapAll(tmp)) } } else { if value.CanInterface() { diff --git a/filter/filter_impl/generic_filter_test.go b/filter/filter_impl/generic_filter_test.go index c8d6f2ec38..40cf743106 100644 --- a/filter/filter_impl/generic_filter_test.go +++ b/filter/filter_impl/generic_filter_test.go @@ -40,6 +40,7 @@ func TestStruct2MapAll(t *testing.T) { } `m:"xxYy"` } `m:"caCa"` DaDa time.Time + EeEe int } testData.AaAa = "1" testData.BaBa = "1" @@ -48,6 +49,7 @@ func TestStruct2MapAll(t *testing.T) { testData.CaCa.XxYy.xxXx = "3" testData.CaCa.XxYy.Xx = "3" testData.DaDa = time.Date(2020, 10, 29, 2, 34, 0, 0, time.Local) + testData.EeEe = 100 m := struct2MapAll(testData).(map[string]interface{}) assert.Equal(t, "1", m["aaAa"].(string)) assert.Equal(t, "1", m["baBa"].(string)) @@ -57,6 +59,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")) + assert.Equal(t, 100, m["eeEe"].(int)) } type testStruct struct {