优化代码结构
This commit is contained in:
parent
eb1cfef957
commit
9686dd142e
4 changed files with 107 additions and 64 deletions
5
go.mod
5
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
|
||||
|
|
|
|||
4
go.sum
4
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=
|
||||
|
|
|
|||
92
table/api.go
92
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue