Skip to content

Commit

Permalink
## 0.2.0-beta5 增加@alias+从json读取access/request配置
Browse files Browse the repository at this point in the history
  • Loading branch information
glennliao committed Jun 10, 2023
1 parent b2285c7 commit 9227091
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 37 deletions.
5 changes: 4 additions & 1 deletion @doc/v0.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
1. 由全局配置改成apijson实例,可创建不同实例对应不同的内容
2. 代码内部 传递的accessName 都为 _access 配置中的alias
3. access/request配置可从配置文件中获取
3. access/request配置可从配置文件中获取

# vX.x
- 给action 增加then/catch/before/after等方法?
11 changes: 6 additions & 5 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package action

import (
"context"
"strings"

"github.com/glennliao/apijson-go/config"
"github.com/glennliao/apijson-go/consts"
"github.com/glennliao/apijson-go/model"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"strings"
)

// Action 非get查询的request表中的请求
type Action struct {
ctx context.Context
tagRequest *config.Request
tagRequest *config.RequestConfig
method string

req model.Map
Expand All @@ -30,7 +31,7 @@ type Action struct {
// 关闭 request 验证开关, 默认否
NoRequestVerify bool

//Access *config.Access
// Access *config.Access

// dbFieldStyle 数据库字段命名风格 请求传递到数据库中
DbFieldStyle config.FieldStyle
Expand Down Expand Up @@ -75,7 +76,7 @@ func (a *Action) parse() error {
}
structure, ok := structures[key]
if !ok {
if structure, ok = structures[structuresKey]; !ok { //User[]可读取User或者User[]
if structure, ok = structures[structuresKey]; !ok { // User[]可读取User或者User[]
return gerror.New("structure错误: 400, 缺少" + key)
}
}
Expand Down Expand Up @@ -158,7 +159,7 @@ func (a *Action) Result() (model.Map, error) {
return ret, err
}

func checkTag(req model.Map, method string, requestCfg *config.ActionConfig) (*config.Request, error) {
func checkTag(req model.Map, method string, requestCfg *config.ActionConfig) (*config.RequestConfig, error) {
_tag, ok := req["tag"]
if !ok {
return nil, gerror.New("tag 缺失")
Expand Down
6 changes: 4 additions & 2 deletions action/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const (
)

type Hook struct {
For string //
For []string //
// Exec 事务外
BeforeNodeExec func(ctx context.Context, n *Node, method string) error
AfterNodeExec func(ctx context.Context, n *Node, method string) error
Expand All @@ -23,7 +23,9 @@ type Hook struct {
var hooksMap = map[string][]Hook{}

func RegHook(h Hook) {
hooksMap[h.For] = append(hooksMap[h.For], h)
for _, item := range h.For {
hooksMap[item] = append(hooksMap[item], h)
}
}

func EmitHook(ctx context.Context, hookAt int, node *Node, method string) error {
Expand Down
5 changes: 3 additions & 2 deletions config/action_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package config

import (
"context"

"github.com/glennliao/apijson-go/model"
)

type ActionConfig struct {
requestConfig *RequestConfig
requestConfig *RequestConfigs
access *Access
functions *functions
rowKeyGenFuncMap map[string]RowKeyGenFuncHandler
Expand All @@ -29,7 +30,7 @@ func (c *ActionConfig) Func(name string) Func {
return c.functions.funcMap[name]
}

func (c *ActionConfig) GetRequest(tag string, method string, version string) (*Request, error) {
func (c *ActionConfig) GetRequest(tag string, method string, version string) (*RequestConfig, error) {
return c.requestConfig.GetRequest(tag, method, version)
}

Expand Down
12 changes: 6 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func RegAccessListProvider(name string, provider AccessListProvider) {
accessListProviderMap[name] = provider
}

type RequestListProvider func(ctx context.Context) []Request
type RequestListProvider func(ctx context.Context) []RequestConfig

var requestListProviderMap = make(map[string]RequestListProvider)

Expand Down Expand Up @@ -50,9 +50,9 @@ type Config struct {

accessList []AccessConfig

requestConfig *RequestConfig
queryConfig *QueryConfig
actionConfig *ActionConfig
requestConfigs *RequestConfigs
queryConfig *QueryConfig
actionConfig *ActionConfig
}

func New() *Config {
Expand Down Expand Up @@ -117,7 +117,7 @@ func (c *Config) ReLoad() {
requestListProvider := requestListProviderMap[c.RequestListProvider]
if requestListProvider != nil {
requestList := requestListProvider(ctx)
c.requestConfig = NewRequestConfig(requestList)
c.requestConfigs = NewRequestConfig(requestList)
}

dbMetaProvider := dbMetaProviderMap[c.DbMetaProvider]
Expand All @@ -134,7 +134,7 @@ func (c *Config) ReLoad() {
}

c.actionConfig = &ActionConfig{
requestConfig: c.requestConfig,
requestConfig: c.requestConfigs,
access: c.Access,
functions: c.Functions,
rowKeyGenFuncMap: c.rowKeyGenFuncMap,
Expand Down
2 changes: 2 additions & 0 deletions config/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"context"
"fmt"

"github.com/glennliao/apijson-go/model"
"github.com/gogf/gf/v2/frame/g"
)
Expand All @@ -15,6 +16,7 @@ type ParamItem struct {

type Func struct {
ParamList []ParamItem
Batch bool // 是否为批量处理, 例如在获取列表后一次性将id传入, 然后按照传入的参数数组返回结果数组
Handler func(ctx context.Context, param model.Map) (res any, err error)
}

Expand Down
3 changes: 2 additions & 1 deletion config/query_config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package config

import (
"github.com/samber/lo"
"net/http"

"github.com/samber/lo"
)

type QueryConfig struct {
Expand Down
17 changes: 9 additions & 8 deletions config/request_config.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package config

import (
"strings"

"github.com/glennliao/apijson-go/consts"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
"strings"
)

type Request struct {
type RequestConfig struct {
Debug int8
Version string
Method string
Expand Down Expand Up @@ -38,13 +39,13 @@ type Structure struct {
Remove []string `json:"REMOVE,omitempty"`
}

type RequestConfig struct {
requestMap map[string]*Request
type RequestConfigs struct {
requestMap map[string]*RequestConfig
}

func NewRequestConfig(requestList []Request) *RequestConfig {
c := RequestConfig{}
requestMap := make(map[string]*Request)
func NewRequestConfig(requestList []RequestConfig) *RequestConfigs {
c := RequestConfigs{}
requestMap := make(map[string]*RequestConfig)

for _, _item := range requestList {
item := _item
Expand All @@ -71,7 +72,7 @@ func getRequestFullKey(tag string, method string, version string) string {
return tag + "@" + method + "@" + version
}

func (c *RequestConfig) GetRequest(tag string, method string, version string) (*Request, error) {
func (c *RequestConfigs) GetRequest(tag string, method string, version string) (*RequestConfig, error) {

if version == "" || version == "-1" || version == "0" {
version = "latest"
Expand Down
1 change: 1 addition & 0 deletions consts/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
Count = "count" // page size
Total = "total"
Query = "query"
Alias = "@alias"
)

const (
Expand Down
8 changes: 4 additions & 4 deletions drivers/goframe/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ var (
ProviderName = "db"
)

func RequestListProvider(ctx context.Context) []config.Request {
var requestList []config.Request
func RequestListProvider(ctx context.Context) []config.RequestConfig {
var requestList []config.RequestConfig
err := g.DB().Model(TableRequest).OrderAsc("version").Scan(&requestList)
if err != nil {
panic(err)
Expand All @@ -31,14 +31,14 @@ func RequestListProvider(ctx context.Context) []config.Request {
}

// provider处理
//if strings.ToLower(tag) != tag {
// if strings.ToLower(tag) != tag {
// // 本身大写, 如果没有外层, 则套一层
// if _, ok := item.Structure[tag]; !ok {
// item.Structure = map[string]any{
// tag: item.Structure,
// }
// }
//}
// }

for k, v := range item.Structure {
structure := config.Structure{}
Expand Down
47 changes: 47 additions & 0 deletions drivers/json/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package config

import (
"context"

"github.com/glennliao/apijson-go/config"
"github.com/gogf/gf/v2/util/gconv"
)

func RequestListProvider(ctx context.Context, jsonStr string) config.RequestListProvider {

return func(ctx context.Context) []config.RequestConfig {
var requestList []config.RequestConfig
err := gconv.Scan(jsonStr, &requestList)
if err != nil {
panic(err)
}
for i, request := range requestList {
if _, ok := request.Structure[request.Tag]; !ok {
requestList[i].Structure = map[string]*config.Structure{
request.Tag: {
Must: nil,
Refuse: nil,
Unique: nil,
Insert: nil,
Update: nil,
Replace: nil,
Remove: nil,
},
}
}
}
return requestList
}

}

func AccessListProvider(ctx context.Context, jsonStr string) config.AccessListProvider {
return func(ctx context.Context) []config.AccessConfig {
var accessList []config.AccessConfig
err := gconv.Scan(jsonStr, &accessList)
if err != nil {
panic(err)
}
return accessList
}
}
6 changes: 4 additions & 2 deletions query/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Node struct {

// 节点的请求数据
req model.Map
simpleReqVal string //非对象结构
simpleReqVal string // 非对象结构

// 节点数据执行器
executor executor.QueryExecutor
Expand All @@ -71,6 +71,8 @@ type Node struct {
// 执行完毕
finish bool

later bool // 后续执行

ret any
err error

Expand Down Expand Up @@ -178,7 +180,7 @@ func (n *Node) buildChild() error {
return nil
}

//最大深度检查
// 最大深度检查
maxDeep := n.queryContext.queryConfig.MaxTreeDeep()
if len(strings.Split(n.Path, "/")) > maxDeep {
return gerror.Newf("deep(%s) > %d", n.Path, maxDeep)
Expand Down
8 changes: 8 additions & 0 deletions query/node_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ func (h *funcNode) fetch() {

_func := queryConfig.Func(functionName)

if n.isList && _func.Batch {
n.later = true
return
}

param := model.Map{}

for i, item := range _func.ParamList {
valNode := n.queryContext.pathNodes[paramKeys[i]]
// if valNode == nil {
// continue
// }
if valNode.ret != nil {
param[item.Name] = valNode.ret
} else {
Expand Down
2 changes: 1 addition & 1 deletion query/node_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (q *queryNode) fetch() {
functionName, paramKeys := util.ParseFunctionsStr(v.(string))
_func := queryConfig.Func(functionName)

if n.isList {
if n.isList && n.ret != nil {
for i, item := range n.ret.([]model.Map) {

param := model.Map{}
Expand Down
12 changes: 7 additions & 5 deletions query/node_struct.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package query

import (
"path/filepath"
"strings"

"github.com/glennliao/apijson-go/consts"
"github.com/glennliao/apijson-go/model"
"github.com/gogf/gf/v2/errors/gerror"
"path/filepath"
"strings"
)

type structNode struct {
Expand Down Expand Up @@ -134,9 +135,9 @@ func (h *structNode) result() {
k = k[0 : len(k)-2]
}

// todo 增加alias 用来重命名返回的key,避免前端调整取值
if node.req["@alias"] != nil {
k = node.req["@alias"].(string)
// 增加alias 用来重命名返回的key,避免前端调整取值
if node.req[consts.Alias] != nil {
k = node.req[consts.Alias].(string)
}

retMap[k], err = node.Result()
Expand All @@ -148,6 +149,7 @@ func (h *structNode) result() {
n.err = err
}
}

n.ret = retMap
}
}
Expand Down

0 comments on commit 9227091

Please sign in to comment.