v0.8.17 + xiang.helper.ArraySplit

This commit is contained in:
Max 2021-10-23 11:17:03 +08:00
parent 9076dfe7f8
commit e1f5c33f00
5 changed files with 91 additions and 1 deletions

View file

@ -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: {

View file

@ -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))
}
}

View file

@ -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,
}
}

View file

@ -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))
}
}

View file

@ -5,7 +5,7 @@ import (
)
// VERSION 版本号
const VERSION = "0.8.16"
const VERSION = "0.8.17"
// DOMAIN 许可域
const DOMAIN = "*.iqka.com"