From 98e5fb15420390d50f40e22a54662ea67a7355b1 Mon Sep 17 00:00:00 2001 From: John Guo Date: Mon, 15 Nov 2021 17:27:35 +0800 Subject: [PATCH] improve field filtering for list type parameters of function Data for ForDao models --- database/gdb/gdb_model_insert.go | 1 + database/gdb/gdb_model_option.go | 14 ++++++++------ database/gdb/gdb_model_utility.go | 8 +++++++- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/database/gdb/gdb_model_insert.go b/database/gdb/gdb_model_insert.go index 7e27c7a06..34bb4df24 100644 --- a/database/gdb/gdb_model_insert.go +++ b/database/gdb/gdb_model_insert.go @@ -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()) diff --git a/database/gdb/gdb_model_option.go b/database/gdb/gdb_model_option.go index 3311f6b25..e3947c627 100644 --- a/database/gdb/gdb_model_option.go +++ b/database/gdb/gdb_model_option.go @@ -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 diff --git a/database/gdb/gdb_model_utility.go b/database/gdb/gdb_model_utility.go index 1d0914e0a..d20241d5a 100644 --- a/database/gdb/gdb_model_utility.go +++ b/database/gdb/gdb_model_utility.go @@ -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 }