From 9686dd142e45c779e3a2363dcc346fe41cf9aaa7 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 29 Sep 2021 08:29:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 5 +-- go.sum | 4 +++ table/api.go | 92 ++++++++++++++++++++++++++++++++++++++++++++++++ table/process.go | 70 +++++------------------------------- 4 files changed, 107 insertions(+), 64 deletions(-) diff --git a/go.mod b/go.mod index 38dc170a..98b1fcf1 100644 --- a/go.mod +++ b/go.mod @@ -22,8 +22,8 @@ require ( github.com/robertkrimen/otto v0.0.0-20210927222213-f9375a256948 // indirect github.com/spf13/cobra v1.2.1 // indirect github.com/stretchr/testify v1.7.0 - github.com/yaoapp/gou v0.0.0-20210928160401-c3e8f1c2162c // indirect - github.com/yaoapp/kun v0.6.1 + github.com/yaoapp/gou v0.0.0-20210929002807-fbe45c391f18 // indirect + github.com/yaoapp/kun v0.6.2 github.com/yaoapp/xun v0.5.2 // indirect golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b // indirect @@ -34,4 +34,5 @@ require ( // go env -w GOPRIVATE=github.com/yaoapp/* +// replace github.com/yaoapp/kun => ../kun // kun local // replace github.com/yaoapp/gou => ../gou // gou local diff --git a/go.sum b/go.sum index 146797c2..7358e978 100644 --- a/go.sum +++ b/go.sum @@ -355,8 +355,12 @@ github.com/yaoapp/gou v0.0.0-20210928091747-b713e6eeaced h1:VcAXZFcP9AZI8Ou0Cucr github.com/yaoapp/gou v0.0.0-20210928091747-b713e6eeaced/go.mod h1:KwCRZDnEKLCxUNgMQ2acU7WI5zpt5SGOt1sBKqNAiaE= github.com/yaoapp/gou v0.0.0-20210928160401-c3e8f1c2162c h1:LCT2RmkbuNht0hm9ed1VhEsQMI71tIS7x6RuR7NeIQc= github.com/yaoapp/gou v0.0.0-20210928160401-c3e8f1c2162c/go.mod h1:KwCRZDnEKLCxUNgMQ2acU7WI5zpt5SGOt1sBKqNAiaE= +github.com/yaoapp/gou v0.0.0-20210929002807-fbe45c391f18 h1:diVG7thFYgqhttHbiGxXsrz/mndaFePLib1saFQBkyg= +github.com/yaoapp/gou v0.0.0-20210929002807-fbe45c391f18/go.mod h1:KwCRZDnEKLCxUNgMQ2acU7WI5zpt5SGOt1sBKqNAiaE= github.com/yaoapp/kun v0.6.1 h1:gbjP5wmAuJLe1RGZ2evH4rPCVWykf0iqbksaHikF/GI= github.com/yaoapp/kun v0.6.1/go.mod h1:igsTcWDnzpp0HtRN7sBP+XOEN2tzoC5hk2MyoPFs3xA= +github.com/yaoapp/kun v0.6.2 h1:QbtdZpVIklRDGEvL5YYRf/eR05YFZVFk/8rX2vy2K0Q= +github.com/yaoapp/kun v0.6.2/go.mod h1:igsTcWDnzpp0HtRN7sBP+XOEN2tzoC5hk2MyoPFs3xA= github.com/yaoapp/xun v0.5.2 h1:DedZ26FpcXfkLnPKXzJsrbVmpWDRx9adUo8AvOcNT+g= github.com/yaoapp/xun v0.5.2/go.mod h1:y107NMHO635nhqJxMt582i1iqLjnNxAoeYr5/500UBw= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/table/api.go b/table/api.go index aff2e798..a830102c 100644 --- a/table/api.go +++ b/table/api.go @@ -6,6 +6,8 @@ import ( "github.com/gin-gonic/gin" "github.com/yaoapp/gou" + "github.com/yaoapp/kun/any" + "github.com/yaoapp/kun/exception" ) // loadAPIs 加载数据管理 API @@ -150,3 +152,93 @@ func (api API) IsAllow(v interface{}) bool { } return false } + +// ValidateLoop 循环引用校验 +func (api API) ValidateLoop(name string) API { + if strings.ToLower(api.Process) == strings.ToLower(name) { + exception.New("循环引用 %s", 400, name).Throw() + } + return api +} + +// DefaultQueryParams 读取参数 QueryParam +func (api API) DefaultQueryParams(i int, defaults ...gou.QueryParam) gou.QueryParam { + param := gou.QueryParam{} + if len(defaults) > 0 { + param = defaults[0] + } + + if api.Default[i] == nil || len(api.Default) <= i { + return param + } + + param, ok := api.Default[i].(gou.QueryParam) + if !ok { + param, ok = gou.AnyToQueryParam(api.Default[i]) + } + + return param +} + +// DefaultInt 读取参数 Int +func (api API) DefaultInt(i int, defaults ...int) int { + value := 0 + ok := false + if len(defaults) > 0 { + value = defaults[0] + } + + if api.Default[i] == nil || len(api.Default) <= i { + return value + } + + value, ok = api.Default[i].(int) + if !ok { + value = any.Of(api.Default[i]).CInt() + } + + return value +} + +// DefaultString 读取参数 String +func (api API) DefaultString(i int, defaults ...string) string { + value := "" + ok := false + if len(defaults) > 0 { + value = defaults[0] + } + + if api.Default[i] == nil || len(api.Default) <= i { + return value + } + + value, ok = api.Default[i].(string) + if !ok { + value = any.Of(api.Default[i]).CString() + } + return value +} + +// MergeDefaultQueryParam 合并默认查询参数 +func (api API) MergeDefaultQueryParam(param gou.QueryParam, i int) gou.QueryParam { + if len(api.Default) > i && api.Default[i] != nil { + defaults, ok := gou.AnyToQueryParam(api.Default[i]) + if !ok { + exception.New("参数默认值数据结构错误", 400).Ctx(api.Default[i]).Throw() + } + if defaults.Withs != nil { + param.Withs = defaults.Withs + } + if defaults.Wheres != nil { + if param.Wheres == nil { + param.Wheres = []gou.QueryWhere{} + } + param.Wheres = append(param.Wheres, defaults.Wheres...) + } + + if defaults.Orders != nil { + param.Orders = append(param.Orders, defaults.Orders...) + } + } + return param +} diff --git a/table/process.go b/table/process.go index 45e28ef8..9f80231e 100644 --- a/table/process.go +++ b/table/process.go @@ -1,12 +1,7 @@ package table import ( - "strings" - "github.com/yaoapp/gou" - "github.com/yaoapp/kun/any" - "github.com/yaoapp/kun/exception" - "github.com/yaoapp/kun/str" ) func init() { @@ -18,67 +13,18 @@ func init() { // 按条件查询数据记录, 请求成功返回符合查询条件带有分页信息的数据对象 func ProcessSearch(process *gou.Process) interface{} { - // 参数处理 - process.ValidateArgNums(5) - name := any.Of(process.Args[0]).String() + process.ValidateArgNums(4) + name := process.ArgsString(0) table := Select(name) - api := table.APIs["search"] - if api.IsAllow(process.Args[4]) { + api := table.APIs["search"].ValidateLoop("xiang.table.search") + if process.NumOfArgsIs(5) && api.IsAllow(process.Args[4]) { return nil } - param := gou.QueryParam{} - ok := false - if process.Args[1] != nil { - param, ok = process.Args[1].(gou.QueryParam) - if !ok { - exception.New("参数不是QueryParam", 400).Throw() - } - } - - // 查询校验 - if strings.ToLower(api.Process) == "xiang.table.search" { - exception.New("循环引用 xiang.table.search", 400).Throw() - } - param = mergeQueryParam(param, api, 0) - page := 1 - if str.Of(api.Default[1]) != "" { - page = any.Of(api.Default[1]).CInt() - } - if str.Of(process.Args[2]) != "" { - page = any.Of(process.Args[2]).CInt() - } - pagesize := 15 - if str.Of(api.Default[2]) != "" { - pagesize = any.Of(api.Default[2]).CInt() - } - if str.Of(process.Args[3]) != "" { - pagesize = any.Of(process.Args[3]).CInt() - } + // 参数表 + param := api.MergeDefaultQueryParam(process.ArgsQueryParams(1), 0) + page := process.ArgsInt(2, api.DefaultInt(1)) + pagesize := process.ArgsInt(3, api.DefaultInt(2)) return gou.NewProcess(api.Process, param, page, pagesize).Run() } - -// 合并默认查询参数 -func mergeQueryParam(param gou.QueryParam, api API, i int) gou.QueryParam { - if len(api.Default) > i && api.Default[i] != nil { - defaults, ok := gou.AnyToQueryParam(api.Default[i]) - if !ok { - exception.New("参数默认值数据结构错误", 400).Ctx(api.Default[i]).Throw() - } - if defaults.Withs != nil { - param.Withs = defaults.Withs - } - if defaults.Wheres != nil { - if param.Wheres == nil { - param.Wheres = []gou.QueryWhere{} - } - param.Wheres = append(param.Wheres, defaults.Wheres...) - } - - if defaults.Orders != nil { - param.Orders = append(param.Orders, defaults.Orders...) - } - } - return param -}