diff --git a/README.MD b/README.MD index 6f130eb22..8a8e66365 100644 --- a/README.MD +++ b/README.MD @@ -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). diff --git a/README_ZH.MD b/README_ZH.MD index 19a98b0ba..9a1c03d4a 100644 --- a/README_ZH.MD +++ b/README_ZH.MD @@ -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) 留言。 diff --git a/database/gdb/gdb_z_mysql_model_test.go b/database/gdb/gdb_z_mysql_model_test.go index 52e758c89..5427292e2 100644 --- a/database/gdb/gdb_z_mysql_model_test.go +++ b/database/gdb/gdb_z_mysql_model_test.go @@ -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) diff --git a/util/gutil/gutil.go b/util/gutil/gutil.go index 5ea65c127..ee27a23b7 100644 --- a/util/gutil/gutil.go +++ b/util/gutil/gutil.go @@ -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