Skip to content

Commit

Permalink
feat: add functions param
Browse files Browse the repository at this point in the history
  • Loading branch information
glennliao committed Mar 14, 2023
1 parent ff2aa50 commit f403a04
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,14 @@ jobs:
simple-test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
cache: true

- name: test
run: cd test && go test -short
2 changes: 2 additions & 0 deletions action/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Hook struct {
AfterExecutorDo func(ctx context.Context, n *Node, method string) error
}

// todo hook可获取到executor的返回值

var hooksMap = map[string][]Hook{}

func RegHook(h Hook) {
Expand Down
1 change: 1 addition & 0 deletions config/executor/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type ActionExecutor interface {
Delete(ctx context.Context, table string, where model.Map) (count int64, err error)
}

// todo 调整executor
var actionExecutorMap = map[string]ActionExecutor{}

func RegActionExecutor(name string, e ActionExecutor) {
Expand Down
19 changes: 9 additions & 10 deletions config/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ import (
"github.com/gogf/gf/v2/frame/g"
)

type ParamItem struct {
Type string
Name string
Desc string
}

// todo Func会和下方functions混淆,不利于代码补全
type Func struct {
Handler func(ctx context.Context, param model.Map) (res any, err error)
ParamList []ParamItem
Handler func(ctx context.Context, param model.Map) (res any, err error)
}

type Functions struct {
Expand All @@ -22,15 +30,6 @@ func (f *Functions) Bind(name string, _func Func) {
f.funcMap[name] = _func
}

func (f *Functions) BindHandlerFunc(name string, handler func(ctx context.Context, param model.Map) (res any, err error)) {
if _, exists := f.funcMap[name]; exists {
panic(fmt.Errorf(" function %s has exists", name))
}
f.funcMap[name] = struct {
Handler func(ctx context.Context, param model.Map) (res any, err error)
}{Handler: handler}
}

func (f *Functions) Call(ctx context.Context, name string, param g.Map) (any, error) {
return f.funcMap[name].Handler(ctx, param)
}
Expand Down
2 changes: 1 addition & 1 deletion consts/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
const (
Role = "@role"
Page = "page" // page num
Count = "count" // page size
Count = "count" // page size // todo access中增加限制count,防止被恶意下载数据
Query = "query"
)

Expand Down
10 changes: 5 additions & 5 deletions test/z_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func App(ctx context.Context, a *apijson.ApiJson) {
panic(err)
}

a.Config().Functions.Bind("test", struct {
Handler func(ctx context.Context, param model.Map) (res any, err error)
}{Handler: func(ctx context.Context, param model.Map) (res any, err error) {
return "nihao", nil
}})
a.Config().Functions.Bind("test", config.Func{
Handler: func(ctx context.Context, param model.Map) (res any, err error) {
return "你好", nil
},
})

a.Config().AccessListProvider = "custom"

Expand Down

0 comments on commit f403a04

Please sign in to comment.