From 70e50bc1625a3408288ac43ed7a92d19ec52ce63 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 6 Jan 2022 19:37:58 +0800 Subject: [PATCH] + Save Hook --- flow/flow_test.go | 2 +- table/process.go | 17 ++++++++++++-- table/process_test.go | 17 ++++++++++++-- table/table_test.go | 2 +- tests/flows/hooks/after_save.flow.json | 7 ++++++ tests/flows/hooks/before_save.flow.json | 15 +++++++++++++ tests/tables/hooks/save.tab.json | 30 +++++++++++++++++++++++++ 7 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 tests/flows/hooks/after_save.flow.json create mode 100644 tests/flows/hooks/before_save.flow.json create mode 100644 tests/tables/hooks/save.tab.json diff --git a/flow/flow_test.go b/flow/flow_test.go index f4789a8c..71b4ecf4 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, 14, len(keys)) + assert.Equal(t, 16, len(keys)) } diff --git a/table/process.go b/table/process.go index f9e749b0..8570134e 100644 --- a/table/process.go +++ b/table/process.go @@ -84,14 +84,27 @@ func ProcessFind(process *gou.Process) interface{} { // ProcessSave xiang.table.Save // 保存单条记录。如数据记录中包含主键字段则更新,不包含主键字段则创建记录;返回创建或更新的记录主键值 func ProcessSave(process *gou.Process) interface{} { - process.ValidateArgNums(2) + + // 读取表格名称 + process.ValidateArgNums(1) name := process.ArgsString(0) table := Select(name) api := table.APIs["save"].ValidateLoop("xiang.table.save") if process.NumOfArgsIs(3) && api.IsAllow(process.Args[2]) { return nil } - return gou.NewProcess(api.Process, process.Args[1]).Run() + + // Before Hook + process.Args = table.Before(table.Hooks.BeforeSave, process.Args) + + // 参数处理 + process.ValidateArgNums(2) + + // 查询数据 + response := gou.NewProcess(api.Process, process.Args[1]).Run() + + // After Hook + return table.After(table.Hooks.AfterSave, response) } // ProcessDelete xiang.table.Delete diff --git a/table/process_test.go b/table/process_test.go index 74313d52..7370d98d 100644 --- a/table/process_test.go +++ b/table/process_test.go @@ -106,8 +106,7 @@ func TestTableProcessSave(t *testing.T) { "price_options": []string{"按月订阅"}, }, } - process := gou.NewProcess("xiang.table.Save", args...) - response := ProcessSave(process) + response := gou.NewProcess("xiang.table.Save", args...).Run() assert.NotNil(t, response) assert.True(t, any.Of(response).IsInt()) @@ -117,6 +116,20 @@ func TestTableProcessSave(t *testing.T) { capsule.Query().Table("service").Where("id", id).Delete() } +func TestTableProcessSaveWithHook(t *testing.T) { + args := []interface{}{"hooks.save"} + response := gou.NewProcess("xiang.table.Save", args...).Run() + assert.NotNil(t, response) + res := any.Of(response).Map() + assert.True(t, any.Of(res.Get("id")).IsInt()) + assert.Equal(t, float64(100), res.Get("after")) + + id := any.Of(res.Get("id")).CInt() + + // 清空数据 + capsule.Query().Table("service").Where("id", id).Delete() +} + func TestTableProcessDelete(t *testing.T) { args := []interface{}{ "service", diff --git a/table/table_test.go b/table/table_test.go index a26d8ec9..ad4bbf90 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, 6, len(keys)) + assert.Equal(t, 7, len(keys)) } diff --git a/tests/flows/hooks/after_save.flow.json b/tests/flows/hooks/after_save.flow.json new file mode 100644 index 00000000..9488fcd4 --- /dev/null +++ b/tests/flows/hooks/after_save.flow.json @@ -0,0 +1,7 @@ +{ + "label": "After:Save", + "version": "1.0.0", + "description": "After:Save", + "nodes": [], + "output": { "id": "{{$in.0}}", "after": 100 } +} diff --git a/tests/flows/hooks/before_save.flow.json b/tests/flows/hooks/before_save.flow.json new file mode 100644 index 00000000..0d66c10c --- /dev/null +++ b/tests/flows/hooks/before_save.flow.json @@ -0,0 +1,15 @@ +{ + "label": "Before:Save", + "version": "1.0.0", + "description": "Before:Save", + "nodes": [], + "output": [ + { + "name": "腾讯黑岩云主机", + "short_name": "高性能云主机", + "kind_id": 3, + "manu_id": 1, + "price_options": "按月订阅" + } + ] +} diff --git a/tests/tables/hooks/save.tab.json b/tests/tables/hooks/save.tab.json new file mode 100644 index 00000000..d0e1c859 --- /dev/null +++ b/tests/tables/hooks/save.tab.json @@ -0,0 +1,30 @@ +{ + "name": "云服务库", + "version": "1.0.0", + "decription": "云服务库", + "bind": { "model": "service" }, + "hooks": { + "before:save": "flows.hooks.before_save", + "after:save": "flows.hooks.after_save" + }, + "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": {} +}