From 10bf4139620e4768fd5db8c7eb5edb954133c093 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 6 Jan 2022 20:08:55 +0800 Subject: [PATCH] + Select Hook --- docker/yao/Dockerfile | 6 ++--- flow/flow_test.go | 2 +- helper/array.go | 23 +++++++++++++++++ helper/array.process.go | 17 ++++++++++++- helper/process.go | 1 + share/const.go | 2 +- table/process.go | 9 ++++++- table/process_test.go | 31 +++++++++++++++++++++++ table/table.go | 2 -- table/table_test.go | 2 +- tests/flows/hooks/after_select.flow.json | 13 ++++++++++ tests/flows/hooks/before_select.flow.json | 7 +++++ tests/tables/hooks/select.tab.json | 30 ++++++++++++++++++++++ 13 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 tests/flows/hooks/after_select.flow.json create mode 100644 tests/flows/hooks/before_select.flow.json create mode 100644 tests/tables/hooks/select.tab.json diff --git a/docker/yao/Dockerfile b/docker/yao/Dockerfile index 69788184..55c794f0 100644 --- a/docker/yao/Dockerfile +++ b/docker/yao/Dockerfile @@ -1,6 +1,6 @@ # =========================================== -# Yao 0.9.21+ MySQL 8.0.27 -# docker build -t yaoapp/yao:0.9.21 . +# Yao 0.9.22+ MySQL 8.0.27 +# docker build -t yaoapp/yao:0.9.22 . # =========================================== FROM debian:buster-slim RUN groupadd -r yao && useradd -r -g yao yao @@ -11,7 +11,7 @@ RUN sed -i 's#http://deb.debian.org#http://mirrors.163.com#g' /etc/apt/sources.l mkdir -p /data/app/db && \ chown -R yao:yao /data && \ echo user=root >> /etc/supervisor/supervisord.conf && \ - curl -fsSL https://demo-crm.iqka.com/releases/0.9.21/install.sh | bash + curl -fsSL https://demo-crm.iqka.com/releases/0.9.22/install.sh | bash COPY conf/yao.conf /etc/supervisor/conf.d/yao.conf COPY conf/yao.env /data/app/.env diff --git a/flow/flow_test.go b/flow/flow_test.go index 71b4ecf4..7af92b2a 100644 --- a/flow/flow_test.go +++ b/flow/flow_test.go @@ -20,5 +20,5 @@ func check(t *testing.T) { for key := range gou.Flows { keys = append(keys, key) } - assert.Equal(t, 16, len(keys)) + assert.Equal(t, 18, len(keys)) } diff --git a/helper/array.go b/helper/array.go index 61142108..c8bc2653 100644 --- a/helper/array.go +++ b/helper/array.go @@ -5,6 +5,7 @@ import ( jsoniter "github.com/json-iterator/go" "github.com/yaoapp/kun/exception" + "github.com/yaoapp/kun/maps" ) // ArrayPluckValue ArrayPluck 参数 @@ -229,3 +230,25 @@ func (opt ArrayTreeOption) Tree(records []map[string]interface{}) []map[string]i } return res } + +// ArrayMapSet []map[string]interface{} 设定数值 +func ArrayMapSet(records []map[string]interface{}, key string, value interface{}) []map[string]interface{} { + res := []map[string]interface{}{} + for i := range records { + record := records[i] + record[key] = value + res = append(res, record) + } + return res +} + +// ArrayMapSetMapStr []map[string]interface{} 设定数值 +func ArrayMapSetMapStr(records []maps.MapStr, key string, value interface{}) []maps.MapStr { + res := []maps.MapStr{} + for i := range records { + record := records[i] + record[key] = value + res = append(res, record) + } + return res +} diff --git a/helper/array.process.go b/helper/array.process.go index 65fedfbb..4a3672c0 100644 --- a/helper/array.process.go +++ b/helper/array.process.go @@ -1,6 +1,9 @@ package helper -import "github.com/yaoapp/gou" +import ( + "github.com/yaoapp/gou" + "github.com/yaoapp/kun/maps" +) // ProcessArrayPluck xiang.helper.ArrayPluck 将多个数据记录集合,合并为一个数据记录集合 func ProcessArrayPluck(process *gou.Process) interface{} { @@ -54,3 +57,15 @@ func ProcessArrayUnique(process *gou.Process) interface{} { } return process.Args[0] } + +// ProcessArrayMapSet xiang.helper.ArrayMapSet 数组映射设定数值 +func ProcessArrayMapSet(process *gou.Process) interface{} { + process.ValidateArgNums(3) + arr, ok := process.Args[0].([]map[string]interface{}) + if ok { + return ArrayMapSet(arr, process.ArgsString(1), process.Args[2]) + } else if arr2, ok := process.Args[0].([]maps.MapStr); ok { + return ArrayMapSetMapStr(arr2, process.ArgsString(1), process.Args[2]) + } + return process.Args[0] +} diff --git a/helper/process.go b/helper/process.go index 16ffcf59..2f415735 100644 --- a/helper/process.go +++ b/helper/process.go @@ -13,6 +13,7 @@ func init() { gou.RegisterProcessHandler("xiang.helper.ArrayKeep", ProcessArrayKeep) gou.RegisterProcessHandler("xiang.helper.ArrayTree", ProcessArrayTree) gou.RegisterProcessHandler("xiang.helper.ArrayUnique", ProcessArrayUnique) + gou.RegisterProcessHandler("xiang.helper.ArrayMapSet", ProcessArrayMapSet) gou.RegisterProcessHandler("xiang.helper.MapKeys", ProcessMapKeys) gou.RegisterProcessHandler("xiang.helper.MapValues", ProcessMapValues) diff --git a/share/const.go b/share/const.go index f647c4c5..aa8ace6e 100644 --- a/share/const.go +++ b/share/const.go @@ -1,7 +1,7 @@ package share // VERSION 版本号 -const VERSION = "0.9.21" +const VERSION = "0.9.22" // DOMAIN 许可域(废弃) const DOMAIN = "*.iqka.com" diff --git a/table/process.go b/table/process.go index 8570134e..2533f6c6 100644 --- a/table/process.go +++ b/table/process.go @@ -313,5 +313,12 @@ func ProcessSelect(process *gou.Process) interface{} { if process.NumOfArgsIs(5) && api.IsAllow(process.Args[4]) { return nil } - return gou.NewProcess(api.Process, process.Args[1:]...).Run() + + // Before Hook + process.Args = table.Before(table.Hooks.BeforeSelect, process.Args) + + response := gou.NewProcess(api.Process, process.Args[1:]...).Run() + + // After Hook + return table.After(table.Hooks.AfterSelect, response) } diff --git a/table/process_test.go b/table/process_test.go index 7370d98d..610e6a68 100644 --- a/table/process_test.go +++ b/table/process_test.go @@ -436,3 +436,34 @@ func TestTableProcessSelect(t *testing.T) { // 清空数据 capsule.Query().Table("service").WhereIn("id", []int{id}).Delete() } + +func TestTableProcessSelectWithHook(t *testing.T) { + args := []interface{}{ + "service", + map[string]interface{}{ + "name": "腾讯黑岩云主机", + "short_name": "高性能云主机", + "kind_id": 3, + "manu_id": 1, + "price_options": []string{"按月订阅"}, + }, + } + process := gou.NewProcess("xiang.table.Save", args...) + response := ProcessSave(process) + assert.NotNil(t, response) + assert.True(t, any.Of(response).IsInt()) + + id := any.Of(response).CInt() + + args = []interface{}{"hooks.select"} + + data := gou.NewProcess("xiang.table.select", args...).Run().([]maps.MapStrAny) + assert.Greater(t, len(data), 1) + for _, row := range data { + assert.Contains(t, row.Get("name"), "腾讯") + assert.Equal(t, float64(100), row.Get("after")) + } + + // 清空数据 + capsule.Query().Table("service").WhereIn("id", []int{id}).Delete() +} diff --git a/table/table.go b/table/table.go index c7c7549a..bf2a5c08 100644 --- a/table/table.go +++ b/table/table.go @@ -176,8 +176,6 @@ func (table *Table) After(process string, data interface{}) interface{} { if process == "" { return data } - - fmt.Println("After", process) return gou.NewProcess(process, data).Run() } diff --git a/table/table_test.go b/table/table_test.go index ad4bbf90..62e3ff66 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -48,5 +48,5 @@ func check(t *testing.T) { for key := range Tables { keys = append(keys, key) } - assert.Equal(t, 7, len(keys)) + assert.Equal(t, 8, len(keys)) } diff --git a/tests/flows/hooks/after_select.flow.json b/tests/flows/hooks/after_select.flow.json new file mode 100644 index 00000000..edef02d0 --- /dev/null +++ b/tests/flows/hooks/after_select.flow.json @@ -0,0 +1,13 @@ +{ + "label": "After:Select", + "version": "1.0.0", + "description": "After:Select", + "nodes": [ + { + "name": "结果", + "process": "xiang.helper.ArrayMapSet", + "args": ["{{$in.0}}", "after", 100] + } + ], + "output": "{{$res.结果}}" +} diff --git a/tests/flows/hooks/before_select.flow.json b/tests/flows/hooks/before_select.flow.json new file mode 100644 index 00000000..f04cd736 --- /dev/null +++ b/tests/flows/hooks/before_select.flow.json @@ -0,0 +1,7 @@ +{ + "label": "Before:Select", + "version": "1.0.0", + "description": "Before:Select", + "nodes": [], + "output": ["腾讯", "name", "id"] +} diff --git a/tests/tables/hooks/select.tab.json b/tests/tables/hooks/select.tab.json new file mode 100644 index 00000000..ea4e031f --- /dev/null +++ b/tests/tables/hooks/select.tab.json @@ -0,0 +1,30 @@ +{ + "name": "云服务库", + "version": "1.0.0", + "decription": "云服务库", + "bind": { "model": "service" }, + "hooks": { + "before:select": "flows.hooks.before_select", + "after:select": "flows.hooks.after_select" + }, + "apis": {}, + "columns": {}, + "filters": {}, + "list": { + "primary": "id", + "layout": { + "columns": [{ "name": "ID", "width": 6 }], + "filters": [] + }, + "actions": {} + }, + "edit": { + "primary": "id", + "layout": { + "fieldset": [{ "columns": [{ "name": "ID", "width": 6 }] }] + }, + "actions": { "cancel": {} } + }, + "insert": {}, + "view": {} +}