diff --git a/flow/flow_test.go b/flow/flow_test.go index fcbd66c1..f4789a8c 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, 12, len(keys)) + assert.Equal(t, 14, len(keys)) } diff --git a/table/process.go b/table/process.go index 6750756f..f9e749b0 100644 --- a/table/process.go +++ b/table/process.go @@ -57,7 +57,8 @@ func ProcessSearch(process *gou.Process) interface{} { // ProcessFind xiang.table.Find // 按主键值查询单条数据, 请求成功返回对应主键的数据记录 func ProcessFind(process *gou.Process) interface{} { - process.ValidateArgNums(2) + + process.ValidateArgNums(1) name := process.ArgsString(0) table := Select(name) api := table.APIs["find"].ValidateLoop("xiang.table.find") @@ -65,10 +66,19 @@ func ProcessFind(process *gou.Process) interface{} { return nil } + // Before Hook + process.Args = table.Before(table.Hooks.BeforeFind, process.Args) + // 参数表 + process.ValidateArgNums(2) id := process.Args[1] param := api.MergeDefaultQueryParam(gou.QueryParam{}, 1) - return gou.NewProcess(api.Process, id, param).Run() + + // 查询数据 + response := gou.NewProcess(api.Process, id, param).Run() + + // After Hook + return table.After(table.Hooks.AfterFind, response) } // ProcessSave xiang.table.Save diff --git a/table/process_test.go b/table/process_test.go index b6d20a60..74313d52 100644 --- a/table/process_test.go +++ b/table/process_test.go @@ -1,7 +1,6 @@ package table import ( - "fmt" "path/filepath" "testing" @@ -27,11 +26,6 @@ func init() { query.Load(config.Conf) flow.LoadFrom(filepath.Join(config.Conf.Root, "flows", "hooks"), "hooks.") Load(config.Conf) - - for name := range gou.Flows { - fmt.Println(name) - } - fmt.Println(filepath.Join(config.Conf.Root, "tables", "hooks")) } func TestTableProcessSearch(t *testing.T) { @@ -86,11 +80,19 @@ func TestTableProcessFind(t *testing.T) { 1, &gin.Context{}, } - process := gou.NewProcess("xiang.table.Find", args...) - response := ProcessFind(process) + response := gou.NewProcess("xiang.table.Find", args...).Run() assert.NotNil(t, response) res := any.Of(response).Map() - assert.Equal(t, any.Of(res.Get("id")).CInt(), 1) + assert.Equal(t, 1, any.Of(res.Get("id")).CInt()) +} + +func TestTableProcessFindWithHook(t *testing.T) { + args := []interface{}{"hooks.find"} + response := gou.NewProcess("xiang.table.Find", args...).Run() + assert.NotNil(t, response) + res := any.Of(response).Map() + assert.Equal(t, 1, any.Of(res.Get("id")).CInt()) + assert.Equal(t, float64(100), res.Get("after")) } func TestTableProcessSave(t *testing.T) { diff --git a/table/table_test.go b/table/table_test.go index bbbf86fe..a26d8ec9 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, 5, len(keys)) + assert.Equal(t, 6, len(keys)) } diff --git a/tests/flows/hooks/after_find.flow.json b/tests/flows/hooks/after_find.flow.json new file mode 100644 index 00000000..59a161ba --- /dev/null +++ b/tests/flows/hooks/after_find.flow.json @@ -0,0 +1,13 @@ +{ + "label": "After:Find", + "version": "1.0.0", + "description": "After:Find", + "nodes": [ + { + "name": "结果", + "process": "xiang.helper.MapSet", + "args": ["{{$in.0}}", "after", 100] + } + ], + "output": "{{$res.结果}}" +} diff --git a/tests/flows/hooks/before_find.flow.json b/tests/flows/hooks/before_find.flow.json new file mode 100644 index 00000000..18359cd1 --- /dev/null +++ b/tests/flows/hooks/before_find.flow.json @@ -0,0 +1,7 @@ +{ + "label": "Before:Find", + "version": "1.0.0", + "description": "Before:Find", + "nodes": [], + "output": [1, {}] +} diff --git a/tests/tables/hooks/find.tab.json b/tests/tables/hooks/find.tab.json new file mode 100644 index 00000000..b0016c95 --- /dev/null +++ b/tests/tables/hooks/find.tab.json @@ -0,0 +1,30 @@ +{ + "name": "云服务库", + "version": "1.0.0", + "decription": "云服务库", + "bind": { "model": "service" }, + "hooks": { + "before:find": "flows.hooks.before_find", + "after:find": "flows.hooks.after_find" + }, + "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": {} +}