diff --git a/helper/map.go b/helper/map.go index 121c9ee3..4548e872 100644 --- a/helper/map.go +++ b/helper/map.go @@ -37,3 +37,11 @@ func MapDel(record map[string]interface{}, key string) map[string]interface{} { delete(record, key) return record } + +// MapMultiDel xiang.helper.MapMultiDel 删除数值并返回新映射表 +func MapMultiDel(record map[string]interface{}, keys ...string) map[string]interface{} { + for _, key := range keys { + delete(record, key) + } + return record +} diff --git a/helper/map.process.go b/helper/map.process.go index fde5acb1..f7129f0d 100644 --- a/helper/map.process.go +++ b/helper/map.process.go @@ -1,6 +1,10 @@ package helper -import "github.com/yaoapp/gou" +import ( + "fmt" + + "github.com/yaoapp/gou" +) // ProcessMapValues xiang.helper.MapValues 返回映射表的数值 func ProcessMapValues(process *gou.Process) interface{} { @@ -24,7 +28,7 @@ func ProcessMapGet(process *gou.Process) interface{} { return MapGet(record, key) } -// ProcessMapSet xiang.helper.MapSet 返回映射表给定键的值 +// ProcessMapSet xiang.helper.MapSet 设定键值,返回映射表给定键的值 func ProcessMapSet(process *gou.Process) interface{} { process.ValidateArgNums(3) record := process.ArgsMap(0) @@ -33,10 +37,21 @@ func ProcessMapSet(process *gou.Process) interface{} { return MapSet(record, key, value) } -// ProcessMapDel xiang.helper.MapDel 返回映射表给定键的值 +// ProcessMapDel xiang.helper.MapDel 删除给定键, 返回映射表 func ProcessMapDel(process *gou.Process) interface{} { process.ValidateArgNums(2) record := process.ArgsMap(0) key := process.ArgsString(1) return MapDel(record, key) } + +// ProcessMapMultiDel xiang.helper.MapMultiDel 删除一组给定键, 返回映射表 +func ProcessMapMultiDel(process *gou.Process) interface{} { + process.ValidateArgNums(2) + record := process.ArgsMap(0) + keys := []string{} + for _, key := range process.Args { + keys = append(keys, fmt.Sprintf("%v", key)) + } + return MapMultiDel(record, keys...) +} diff --git a/helper/map_test.go b/helper/map_test.go index 908c6926..0d5d2cfb 100644 --- a/helper/map_test.go +++ b/helper/map_test.go @@ -49,3 +49,14 @@ func TestProcessMapValues(t *testing.T) { assert.Contains(t, values, "Value1") assert.Contains(t, values, "Value2") } + +func TestProcessMapMultiDel(t *testing.T) { + args := []interface{}{ + map[string]interface{}{"foo": "Value1", "bar": "Value2"}, + "foo", + "bar", + } + new := gou.NewProcess("xiang.helper.MapMultiDel", args...).Run().(map[string]interface{}) + assert.Nil(t, new["foo"]) + assert.Nil(t, new["bar"]) +} diff --git a/helper/process.go b/helper/process.go index 5068a9a2..666dd7c3 100644 --- a/helper/process.go +++ b/helper/process.go @@ -20,6 +20,7 @@ func init() { gou.RegisterProcessHandler("xiang.helper.MapGet", ProcessMapGet) gou.RegisterProcessHandler("xiang.helper.MapSet", ProcessMapSet) gou.RegisterProcessHandler("xiang.helper.MapDel", ProcessMapDel) + gou.RegisterProcessHandler("xiang.helper.MapMultiDel", ProcessMapMultiDel) gou.RegisterProcessHandler("xiang.helper.StrConcat", ProcessStrConcat)