+ MapToArray Helper

This commit is contained in:
Max 2022-01-11 19:51:17 +08:00
parent 43849e8456
commit ee50523980
3 changed files with 28 additions and 0 deletions

View file

@ -55,3 +55,17 @@ func ProcessMapMultiDel(process *gou.Process) interface{} {
}
return MapMultiDel(record, keys...)
}
// ProcessMapToArray xiang.helper.MapToArray 映射转换为 KeyValue 数组
func ProcessMapToArray(process *gou.Process) interface{} {
process.ValidateArgNums(1)
m := process.ArgsMap(0)
res := []map[string]interface{}{}
for key, value := range m {
res = append(res, map[string]interface{}{
"key": key,
"value": value,
})
}
return res
}

View file

@ -60,3 +60,16 @@ func TestProcessMapMultiDel(t *testing.T) {
assert.Nil(t, new["foo"])
assert.Nil(t, new["bar"])
}
func TestProcessMapToArray(t *testing.T) {
arr := gou.NewProcess("xiang.helper.MapToArray", map[string]interface{}{
"foo": "Value1",
"bar": "Value2",
}).Run().([]map[string]interface{})
assert.Equal(t, arr[0]["key"], "foo")
assert.Equal(t, arr[0]["value"], "Value1")
assert.Equal(t, arr[1]["key"], "bar")
assert.Equal(t, arr[1]["value"], "Value2")
}

View file

@ -19,6 +19,7 @@ func init() {
gou.RegisterProcessHandler("xiang.helper.MapKeys", ProcessMapKeys)
gou.RegisterProcessHandler("xiang.helper.MapValues", ProcessMapValues)
gou.RegisterProcessHandler("xiang.helper.MapToArray", ProcessMapToArray)
gou.RegisterProcessHandler("xiang.helper.MapGet", ProcessMapGet)
gou.RegisterProcessHandler("xiang.helper.MapSet", ProcessMapSet)
gou.RegisterProcessHandler("xiang.helper.MapDel", ProcessMapDel)