+ Select Hook
This commit is contained in:
parent
70e50bc162
commit
10bf413962
13 changed files with 135 additions and 10 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package share
|
||||
|
||||
// VERSION 版本号
|
||||
const VERSION = "0.9.21"
|
||||
const VERSION = "0.9.22"
|
||||
|
||||
// DOMAIN 许可域(废弃)
|
||||
const DOMAIN = "*.iqka.com"
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
13
tests/flows/hooks/after_select.flow.json
Normal file
13
tests/flows/hooks/after_select.flow.json
Normal file
|
|
@ -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.结果}}"
|
||||
}
|
||||
7
tests/flows/hooks/before_select.flow.json
Normal file
7
tests/flows/hooks/before_select.flow.json
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"label": "Before:Select",
|
||||
"version": "1.0.0",
|
||||
"description": "Before:Select",
|
||||
"nodes": [],
|
||||
"output": ["腾讯", "name", "id"]
|
||||
}
|
||||
30
tests/tables/hooks/select.tab.json
Normal file
30
tests/tables/hooks/select.tab.json
Normal file
|
|
@ -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": {}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue