Skip to content

Commit

Permalink
+ Select Hook
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Jan 6, 2022
1 parent 70e50bc commit 10bf413
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 10 deletions.
6 changes: 3 additions & 3 deletions docker/yao/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion flow/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
23 changes: 23 additions & 0 deletions helper/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/maps"
)

// ArrayPluckValue ArrayPluck 参数
Expand Down Expand Up @@ -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
}
17 changes: 16 additions & 1 deletion helper/array.process.go
Original file line number Diff line number Diff line change
@@ -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{} {
Expand Down Expand Up @@ -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]
}
1 change: 1 addition & 0 deletions helper/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion share/const.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package share

// VERSION 版本号
const VERSION = "0.9.21"
const VERSION = "0.9.22"

// DOMAIN 许可域(废弃)
const DOMAIN = "*.iqka.com"
Expand Down
9 changes: 8 additions & 1 deletion table/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
31 changes: 31 additions & 0 deletions table/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
2 changes: 0 additions & 2 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 13 additions & 0 deletions tests/flows/hooks/after_select.flow.json
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 0 deletions tests/flows/hooks/before_select.flow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"label": "Before:Select",
"version": "1.0.0",
"description": "Before:Select",
"nodes": [],
"output": ["腾讯", "name", "id"]
}
30 changes: 30 additions & 0 deletions tests/tables/hooks/select.tab.json
Original file line number Diff line number Diff line change
@@ -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": {}
}

0 comments on commit 10bf413

Please sign in to comment.