From ee505239800d06698c1e6b0ccb1b2eca136a4c11 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 11 Jan 2022 19:51:17 +0800 Subject: [PATCH] + MapToArray Helper --- helper/map.process.go | 14 ++++++++++++++ helper/map_test.go | 13 +++++++++++++ helper/process.go | 1 + 3 files changed, 28 insertions(+) diff --git a/helper/map.process.go b/helper/map.process.go index f7129f0d..a3a7be04 100644 --- a/helper/map.process.go +++ b/helper/map.process.go @@ -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 +} diff --git a/helper/map_test.go b/helper/map_test.go index 0d5d2cfb..e2174d9f 100644 --- a/helper/map_test.go +++ b/helper/map_test.go @@ -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") +} diff --git a/helper/process.go b/helper/process.go index da794f3a..aa0e0c4d 100644 --- a/helper/process.go +++ b/helper/process.go @@ -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)