improve field filtering for list type parameters of function Data for ForDao models

This commit is contained in:
John Guo 2021-11-15 17:27:35 +08:00
parent f99b037c60
commit 98e5fb1542
3 changed files with 16 additions and 7 deletions

View File

@ -80,6 +80,7 @@ func (m *Model) Data(data ...interface{}) *Model {
// which will filter all nil parameters in `data`.
if isForDaoModel(reflectInfo.OriginValue.Index(0).Type()) {
model = model.OmitNilData()
model.option |= optionOmitNilDataInternal
}
}
list := make(List, reflectInfo.OriginValue.Len())

View File

@ -7,12 +7,14 @@
package gdb
const (
optionOmitNil = optionOmitNilWhere | optionOmitNilData
optionOmitEmpty = optionOmitEmptyWhere | optionOmitEmptyData
optionOmitEmptyWhere = 1 << iota // 8
optionOmitEmptyData // 16
optionOmitNilWhere // 32
optionOmitNilData // 64
optionOmitNil = optionOmitNilWhere | optionOmitNilData
optionOmitEmpty = optionOmitEmptyWhere | optionOmitEmptyData
optionOmitNilDataInternal = optionOmitNilData | optionOmitNilDataList // this option is used internally only for ForDao feature.
optionOmitEmptyWhere = 1 << iota // 8
optionOmitEmptyData // 16
optionOmitNilWhere // 32
optionOmitNilData // 64
optionOmitNilDataList // 128
)
// OmitEmpty sets optionOmitEmpty option for the model, which automatically filers

View File

@ -87,8 +87,14 @@ func (m *Model) filterDataForInsertOrUpdate(data interface{}) (interface{}, erro
var err error
switch value := data.(type) {
case List:
var (
omitEmpty bool
)
if m.option&optionOmitNilDataList > 0 {
omitEmpty = true
}
for k, item := range value {
value[k], err = m.doMappingAndFilterForInsertOrUpdateDataMap(item, false)
value[k], err = m.doMappingAndFilterForInsertOrUpdateDataMap(item, omitEmpty)
if err != nil {
return nil, err
}