+ Heper: MapMultiDel

This commit is contained in:
Max 2022-01-08 09:24:11 +08:00
parent 747b395e3d
commit 93071b583f
4 changed files with 38 additions and 3 deletions

View file

@ -37,3 +37,11 @@ func MapDel(record map[string]interface{}, key string) map[string]interface{} {
delete(record, key) delete(record, key)
return record 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
}

View file

@ -1,6 +1,10 @@
package helper package helper
import "github.com/yaoapp/gou" import (
"fmt"
"github.com/yaoapp/gou"
)
// ProcessMapValues xiang.helper.MapValues 返回映射表的数值 // ProcessMapValues xiang.helper.MapValues 返回映射表的数值
func ProcessMapValues(process *gou.Process) interface{} { func ProcessMapValues(process *gou.Process) interface{} {
@ -24,7 +28,7 @@ func ProcessMapGet(process *gou.Process) interface{} {
return MapGet(record, key) return MapGet(record, key)
} }
// ProcessMapSet xiang.helper.MapSet 返回映射表给定键的值 // ProcessMapSet xiang.helper.MapSet 设定键值,返回映射表给定键的值
func ProcessMapSet(process *gou.Process) interface{} { func ProcessMapSet(process *gou.Process) interface{} {
process.ValidateArgNums(3) process.ValidateArgNums(3)
record := process.ArgsMap(0) record := process.ArgsMap(0)
@ -33,10 +37,21 @@ func ProcessMapSet(process *gou.Process) interface{} {
return MapSet(record, key, value) return MapSet(record, key, value)
} }
// ProcessMapDel xiang.helper.MapDel 返回映射表给定键的值 // ProcessMapDel xiang.helper.MapDel 删除给定键, 返回映射表
func ProcessMapDel(process *gou.Process) interface{} { func ProcessMapDel(process *gou.Process) interface{} {
process.ValidateArgNums(2) process.ValidateArgNums(2)
record := process.ArgsMap(0) record := process.ArgsMap(0)
key := process.ArgsString(1) key := process.ArgsString(1)
return MapDel(record, key) 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...)
}

View file

@ -49,3 +49,14 @@ func TestProcessMapValues(t *testing.T) {
assert.Contains(t, values, "Value1") assert.Contains(t, values, "Value1")
assert.Contains(t, values, "Value2") 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"])
}

View file

@ -20,6 +20,7 @@ func init() {
gou.RegisterProcessHandler("xiang.helper.MapGet", ProcessMapGet) gou.RegisterProcessHandler("xiang.helper.MapGet", ProcessMapGet)
gou.RegisterProcessHandler("xiang.helper.MapSet", ProcessMapSet) gou.RegisterProcessHandler("xiang.helper.MapSet", ProcessMapSet)
gou.RegisterProcessHandler("xiang.helper.MapDel", ProcessMapDel) gou.RegisterProcessHandler("xiang.helper.MapDel", ProcessMapDel)
gou.RegisterProcessHandler("xiang.helper.MapMultiDel", ProcessMapMultiDel)
gou.RegisterProcessHandler("xiang.helper.StrConcat", ProcessStrConcat) gou.RegisterProcessHandler("xiang.helper.StrConcat", ProcessStrConcat)