This is an automated email from the ASF dual-hosted git repository.
klesh pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git
The following commit(s) were added to refs/heads/main by this push:
new f324b4ed fix: fix walk fields not filter struct (#2661)
f324b4ed is described below
commit f324b4ede14b2845aea9ab32163af23e337bdfe8
Author: mappjzc <[email protected]>
AuthorDate: Wed Aug 3 11:13:51 2022 +0800
fix: fix walk fields not filter struct (#2661)
Add TestWalkFields
Fix WalkFileds lost to filter the struct.
Nddtfjiang <[email protected]>
---
utils/structfield.go | 23 ++++++++++++-----
utils/{structfield.go => structfield_test.go} | 37 ++++++++++++++-------------
2 files changed, 35 insertions(+), 25 deletions(-)
diff --git a/utils/structfield.go b/utils/structfield.go
index 9fa8d7cf..4ab4d5b6 100644
--- a/utils/structfield.go
+++ b/utils/structfield.go
@@ -26,17 +26,26 @@ func WalkFields(t reflect.Type, filter func(field
*reflect.StructField) bool) (f
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
- for i := 0; i < t.NumField(); i++ {
- field := t.Field(i)
- if field.Type.Kind() == reflect.Struct {
- f = append(f, WalkFields(field.Type, filter)...)
- } else {
- if filter == nil {
+
+ if filter == nil {
+ for i := 0; i < t.NumField(); i++ {
+ field := t.Field(i)
+ if field.Type.Kind() == reflect.Struct {
+ f = append(f, WalkFields(field.Type, filter)...)
+ } else {
f = append(f, field)
- } else if filter(&field) {
+ }
+ }
+ } else {
+ for i := 0; i < t.NumField(); i++ {
+ field := t.Field(i)
+ if filter(&field) {
f = append(f, field)
+ } else if field.Type.Kind() == reflect.Struct {
+ f = append(f, WalkFields(field.Type, filter)...)
}
}
}
+
return f
}
diff --git a/utils/structfield.go b/utils/structfield_test.go
similarity index 60%
copy from utils/structfield.go
copy to utils/structfield_test.go
index 9fa8d7cf..181ba6d7 100644
--- a/utils/structfield.go
+++ b/utils/structfield_test.go
@@ -19,24 +19,25 @@ package utils
import (
"reflect"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/assert"
)
-// WalkFields get the field data by tag
-func WalkFields(t reflect.Type, filter func(field *reflect.StructField) bool)
(f []reflect.StructField) {
- if t.Kind() == reflect.Ptr {
- t = t.Elem()
- }
- for i := 0; i < t.NumField(); i++ {
- field := t.Field(i)
- if field.Type.Kind() == reflect.Struct {
- f = append(f, WalkFields(field.Type, filter)...)
- } else {
- if filter == nil {
- f = append(f, field)
- } else if filter(&field) {
- f = append(f, field)
- }
- }
- }
- return f
+type TestStructForWalkFields struct {
+ ID string `gorm:"primaryKey"`
+ Time time.Time `gorm:"primaryKey"`
+ Data string
+}
+
+// TestWalkFields test the WalkFields
+func TestWalkFields(t *testing.T) {
+ fs := WalkFields(reflect.TypeOf(TestStructForWalkFields{}), func(field
*reflect.StructField) bool {
+ return strings.Contains(strings.ToLower(field.Tag.Get("gorm")),
"primarykey")
+ })
+
+ assert.Equal(t, fs[0].Name, "ID")
+ assert.Equal(t, fs[1].Name, "Time")
}