You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by zh...@apache.org on 2022/08/07 13:02:01 UTC

[incubator-devlake] 04/06: fix: fix walk fields not filter struct (#2661)

This is an automated email from the ASF dual-hosted git repository.

zhangliang2022 pushed a commit to branch release-v0.12
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git

commit ffb43040202331a1ded1677d1bc9b291d57dbc47
Author: mappjzc <zh...@merico.dev>
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 <zh...@merico.dev>
---
 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")
 }