mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
improve field filtering for list type parameters of function Data for ForDao models
This commit is contained in:
@ -72,6 +72,11 @@ var (
|
||||
structTagPriority = append([]string{OrmTagForStruct}, gconv.StructTagPriority...)
|
||||
)
|
||||
|
||||
// isForDaoModel checks and returns whether given type is for dao model.
|
||||
func isForDaoModel(t reflect.Type) bool {
|
||||
return gstr.HasSuffix(t.String(), modelForDaoSuffix)
|
||||
}
|
||||
|
||||
// getTableNameFromOrmTag retrieves and returns the table name from struct object.
|
||||
func getTableNameFromOrmTag(object interface{}) string {
|
||||
var tableName string
|
||||
@ -416,7 +421,7 @@ func formatWhere(db DB, in formatWhereInput) (newWhere string, newArgs []interfa
|
||||
case reflect.Struct:
|
||||
// If the `where` parameter is defined like `xxxForDao`, it then adds `OmitNil` option for this condition,
|
||||
// which will filter all nil parameters in `where`.
|
||||
if gstr.HasSuffix(reflect.TypeOf(in.Where).String(), modelForDaoSuffix) {
|
||||
if isForDaoModel(reflect.TypeOf(in.Where)) {
|
||||
in.OmitNil = true
|
||||
}
|
||||
// If `where` struct implements iIterator interface,
|
||||
|
||||
@ -74,20 +74,25 @@ func (m *Model) Data(data ...interface{}) *Model {
|
||||
)
|
||||
switch reflectInfo.OriginKind {
|
||||
case reflect.Slice, reflect.Array:
|
||||
if reflectInfo.OriginValue.Len() > 0 {
|
||||
// If the `data` parameter is defined like `xxxForDao`,
|
||||
// it then adds `OmitNilData` option for this condition,
|
||||
// which will filter all nil parameters in `data`.
|
||||
if isForDaoModel(reflectInfo.OriginValue.Index(0).Type()) {
|
||||
model = model.OmitNilData()
|
||||
}
|
||||
}
|
||||
list := make(List, reflectInfo.OriginValue.Len())
|
||||
for i := 0; i < reflectInfo.OriginValue.Len(); i++ {
|
||||
list[i] = ConvertDataForTableRecord(reflectInfo.OriginValue.Index(i).Interface())
|
||||
}
|
||||
model.data = list
|
||||
|
||||
case reflect.Map:
|
||||
model.data = ConvertDataForTableRecord(data[0])
|
||||
|
||||
case reflect.Struct:
|
||||
// If the `data` parameter is defined like `xxxForDao`,
|
||||
// it then adds `OmitNilData` option for this condition,
|
||||
// which will filter all nil parameters in `data`.
|
||||
if gstr.HasSuffix(reflect.TypeOf(value).String(), modelForDaoSuffix) {
|
||||
if isForDaoModel(reflect.TypeOf(value)) {
|
||||
model = model.OmitNilData()
|
||||
}
|
||||
if v, ok := data[0].(iInterfaces); ok {
|
||||
@ -103,6 +108,9 @@ func (m *Model) Data(data ...interface{}) *Model {
|
||||
model.data = ConvertDataForTableRecord(data[0])
|
||||
}
|
||||
|
||||
case reflect.Map:
|
||||
model.data = ConvertDataForTableRecord(data[0])
|
||||
|
||||
default:
|
||||
model.data = data[0]
|
||||
}
|
||||
|
||||
@ -43,6 +43,53 @@ func Test_Model_Insert_Data_ForDao(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Insert_Data_LIst_ForDao(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type UserForDao struct {
|
||||
Id interface{}
|
||||
Passport interface{}
|
||||
Password interface{}
|
||||
Nickname interface{}
|
||||
CreateTime interface{}
|
||||
}
|
||||
data := []UserForDao{
|
||||
{
|
||||
Id: 1,
|
||||
Passport: "user_1",
|
||||
Password: "pass_1",
|
||||
},
|
||||
{
|
||||
Id: 2,
|
||||
Passport: "user_2",
|
||||
Password: "pass_2",
|
||||
},
|
||||
}
|
||||
result, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.LastInsertId()
|
||||
t.Assert(n, 2)
|
||||
|
||||
one, err := db.Model(table).WherePri(1).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], `1`)
|
||||
t.Assert(one[`passport`], `user_1`)
|
||||
t.Assert(one[`password`], `pass_1`)
|
||||
t.Assert(one[`nickname`], ``)
|
||||
t.Assert(one[`create_time`], ``)
|
||||
|
||||
one, err = db.Model(table).WherePri(2).One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(one[`id`], `2`)
|
||||
t.Assert(one[`passport`], `user_2`)
|
||||
t.Assert(one[`password`], `pass_2`)
|
||||
t.Assert(one[`nickname`], ``)
|
||||
t.Assert(one[`create_time`], ``)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Update_Data_ForDao(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
Reference in New Issue
Block a user