improve gutil.Keys/Values for embedded struct

This commit is contained in:
John 2020-11-20 00:53:12 +08:00
parent df86ffb61e
commit e20183e7a1
4 changed files with 81 additions and 7 deletions

View File

@ -125,6 +125,7 @@ The concurrency starts from `100` to `10000`.
- [LeYouJia](https://www.leyoujia.com/)
- [IGG](https://igg.com)
- [XiMaLaYa](https://www.ximalaya.com)
- [ZYBang](https://www.zybang.com/)
> We list part of the users here, if your company or products are using `GoFrame`, please let us know [here](https://github.com/gogf/gf/issues/168).

View File

@ -145,6 +145,7 @@ ab -t 10 -c 100 http://127.0.0.1:3000/json
- [乐有家](https://www.leyoujia.com/)
- [IGG](https://igg.com)
- [喜马拉雅](https://www.ximalaya.com)
- [作业帮](https://www.zybang.com/)
> 在这里只列举了部分知名的用户,如果您的企业或者产品正在使用`GoFrame`,欢迎到 [这里](https://github.com/gogf/gf/issues/168) 留言。

View File

@ -2717,6 +2717,49 @@ func Test_Model_FieldsEx_AutoMapping(t *testing.T) {
})
}
func Test_Model_Fields_Struct(t *testing.T) {
table := createInitTable()
defer dropTable(table)
type A struct {
Passport string
Password string
}
type B struct {
A
NickName string
}
gtest.C(t, func(t *gtest.T) {
one, err := db.Table(table).Fields(A{}).Where("id", 2).One()
t.Assert(err, nil)
t.Assert(len(one), 2)
t.Assert(one["passport"], "user_2")
t.Assert(one["password"], "pass_2")
})
gtest.C(t, func(t *gtest.T) {
one, err := db.Table(table).Fields(&A{}).Where("id", 2).One()
t.Assert(err, nil)
t.Assert(len(one), 2)
t.Assert(one["passport"], "user_2")
t.Assert(one["password"], "pass_2")
})
gtest.C(t, func(t *gtest.T) {
one, err := db.Table(table).Fields(B{}).Where("id", 2).One()
t.Assert(err, nil)
t.Assert(len(one), 3)
t.Assert(one["passport"], "user_2")
t.Assert(one["password"], "pass_2")
t.Assert(one["nickname"], "name_2")
})
gtest.C(t, func(t *gtest.T) {
one, err := db.Table(table).Fields(&B{}).Where("id", 2).One()
t.Assert(err, nil)
t.Assert(len(one), 3)
t.Assert(one["passport"], "user_2")
t.Assert(one["password"], "pass_2")
t.Assert(one["nickname"], "name_2")
})
}
func Test_Model_NullField(t *testing.T) {
table := createTable()
defer dropTable(table)

View File

@ -59,9 +59,15 @@ func Keys(mapOrStruct interface{}) (keysOrAttrs []string) {
return
}
var (
reflectValue = reflect.ValueOf(mapOrStruct)
reflectKind = reflectValue.Kind()
reflectValue reflect.Value
reflectKind reflect.Kind
)
if v, ok := mapOrStruct.(reflect.Value); ok {
reflectValue = v
} else {
reflectValue = reflect.ValueOf(mapOrStruct)
}
reflectKind = reflectValue.Kind()
if reflectKind == reflect.Ptr {
if !reflectValue.IsValid() || reflectValue.IsNil() {
reflectValue = reflect.New(reflectValue.Type().Elem()).Elem()
@ -78,9 +84,17 @@ func Keys(mapOrStruct interface{}) (keysOrAttrs []string) {
keysOrAttrs = append(keysOrAttrs, gconv.String(k.Interface()))
}
case reflect.Struct:
reflectType := reflectValue.Type()
var (
fieldType reflect.StructField
reflectType = reflectValue.Type()
)
for i := 0; i < reflectValue.NumField(); i++ {
keysOrAttrs = append(keysOrAttrs, reflectType.Field(i).Name)
fieldType = reflectType.Field(i)
if fieldType.Anonymous {
keysOrAttrs = append(keysOrAttrs, Keys(reflectValue.Field(i))...)
} else {
keysOrAttrs = append(keysOrAttrs, fieldType.Name)
}
}
}
return
@ -96,9 +110,15 @@ func Values(mapOrStruct interface{}) (values []interface{}) {
return
}
var (
reflectValue = reflect.ValueOf(mapOrStruct)
reflectKind = reflectValue.Kind()
reflectValue reflect.Value
reflectKind reflect.Kind
)
if v, ok := mapOrStruct.(reflect.Value); ok {
reflectValue = v
} else {
reflectValue = reflect.ValueOf(mapOrStruct)
}
reflectKind = reflectValue.Kind()
for reflectKind == reflect.Ptr {
reflectValue = reflectValue.Elem()
reflectKind = reflectValue.Kind()
@ -109,8 +129,17 @@ func Values(mapOrStruct interface{}) (values []interface{}) {
values = append(values, reflectValue.MapIndex(k).Interface())
}
case reflect.Struct:
var (
fieldType reflect.StructField
reflectType = reflectValue.Type()
)
for i := 0; i < reflectValue.NumField(); i++ {
values = append(values, reflectValue.Field(i).Interface())
fieldType = reflectType.Field(i)
if fieldType.Anonymous {
values = append(values, Values(reflectValue.Field(i))...)
} else {
values = append(values, reflectValue.Field(i).Interface())
}
}
}
return