diff --git a/helper/array.go b/helper/array.go index 0712b69f..21a0debd 100644 --- a/helper/array.go +++ b/helper/array.go @@ -14,6 +14,27 @@ type ArrayPluckValue struct { Items []map[string]interface{} `json:"items"` } +// ArraySplit 将多条数记录集合,分解为一个 columns:[]string 和 values: [][]interface{} +func ArraySplit(records []map[string]interface{}) ([]string, [][]interface{}) { + columns := []string{} + values := [][]interface{}{} + if len(records) == 0 { + return columns, values + } + for column := range records[0] { + columns = append(columns, column) + } + + for _, record := range records { + value := []interface{}{} + for _, key := range columns { + value = append(value, record[key]) + } + values = append(values, value) + } + return columns, values +} + // ArrayPluck 将多个数据记录集合,合并为一个数据记录集合 // columns: ["城市", "行业", "计费"] // pluck: { diff --git a/helper/array_test.go b/helper/array_test.go index 586101c1..0a425ddc 100644 --- a/helper/array_test.go +++ b/helper/array_test.go @@ -21,3 +21,16 @@ func TestArrayPluck(t *testing.T) { maps.Of(item).Has("计费") } } + +func TestArraySplit(t *testing.T) { + records := []map[string]interface{}{ + {"name": "阿里云计算有限公司", "short_name": "阿里云"}, + {"name": "世纪互联蓝云", "short_name": "上海蓝云"}, + } + columns, values := ArraySplit(records) + assert.Equal(t, 2, len(columns)) + assert.Equal(t, 2, len(values)) + for _, value := range values { + assert.Equal(t, 2, len(value)) + } +} diff --git a/helper/process.go b/helper/process.go index 98a9216a..52668acf 100644 --- a/helper/process.go +++ b/helper/process.go @@ -8,6 +8,7 @@ import ( func init() { // 注册处理器 gou.RegisterProcessHandler("xiang.helper.ArrayPluck", ProcessArrayPluck) + gou.RegisterProcessHandler("xiang.helper.ArraySplit", ProcessArraySplit) } @@ -34,3 +35,33 @@ func ProcessArrayPluck(process *gou.Process) interface{} { pluck := process.ArgsMap(1) return ArrayPluck(columns, pluck) } + +// ProcessArraySplit xiang.helper.ArraySplit 将多条数记录集合,分解为一个 columns:[]string 和 values: [][]interface{} +func ProcessArraySplit(process *gou.Process) interface{} { + process.ValidateArgNums(1) + args := process.Args[0] + records := []map[string]interface{}{} + + switch args.(type) { + case []interface{}: + for _, v := range args.([]interface{}) { + value, ok := v.(map[string]interface{}) + if ok { + records = append(records, value) + continue + } + exception.New("参数错误: 第1个参数不是字符串数组", 400).Ctx(process.Args[0]).Throw() + } + case []map[string]interface{}: + records = args.([]map[string]interface{}) + + default: + exception.New("参数错误: 第1个参数不是字符串数组", 400).Ctx(process.Args[0]).Throw() + break + } + columns, values := ArraySplit(records) + return map[string]interface{}{ + "columns": columns, + "values": values, + } +} diff --git a/helper/process_test.go b/helper/process_test.go index c6af9f33..a49f16e2 100644 --- a/helper/process_test.go +++ b/helper/process_test.go @@ -29,3 +29,28 @@ func TestProcessArrayPluck(t *testing.T) { maps.Of(item).Has("计费") } } + +func TestProcessArraySplit(t *testing.T) { + args := []interface{}{ + []map[string]interface{}{ + {"name": "阿里云计算有限公司", "short_name": "阿里云"}, + {"name": "世纪互联蓝云", "short_name": "上海蓝云"}, + }, + } + process := gou.NewProcess("xiang.helper.ArraySplit", args...) + response := process.Run() + assert.NotNil(t, response) + res, ok := response.(map[string]interface{}) + assert.True(t, ok) + + columns, ok := res["columns"].([]string) + assert.True(t, ok) + + values, ok := res["values"].([][]interface{}) + assert.True(t, ok) + assert.Equal(t, 2, len(columns)) + assert.Equal(t, 2, len(values)) + for _, value := range values { + assert.Equal(t, 2, len(value)) + } +} diff --git a/share/const.go b/share/const.go index 880ecacf..16ff0ba5 100644 --- a/share/const.go +++ b/share/const.go @@ -5,7 +5,7 @@ import ( ) // VERSION 版本号 -const VERSION = "0.8.16" +const VERSION = "0.8.17" // DOMAIN 许可域 const DOMAIN = "*.iqka.com"