+ Find Hook

This commit is contained in:
Max 2022-01-06 19:25:11 +08:00
parent b1b21a527b
commit 77047aa3e4
7 changed files with 75 additions and 13 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -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.结果}}"
}

View file

@ -0,0 +1,7 @@
{
"label": "Before:Find",
"version": "1.0.0",
"description": "Before:Find",
"nodes": [],
"output": [1, {}]
}

View file

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