Skip to content

Commit

Permalink
## 0.2.0-beta8 导出Action字段
Browse files Browse the repository at this point in the history
  • Loading branch information
glennliao committed Jul 2, 2023
1 parent 40b950a commit 8476aff
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 64 deletions.
10 changes: 5 additions & 5 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Action struct {
// jsonFieldStyle 数据库返回的字段
JsonFieldStyle config.FieldStyle

actionConfig *config.ActionConfig
ActionConfig *config.ActionConfig
}

func New(ctx context.Context, actionConfig *config.ActionConfig, method string, req model.Map) *Action {
Expand All @@ -49,7 +49,7 @@ func New(ctx context.Context, actionConfig *config.ActionConfig, method string,
panic(err)
}

delete(req, "tag")
delete(req, consts.Tag)
delete(req, "version")

a := &Action{
Expand All @@ -59,7 +59,7 @@ func New(ctx context.Context, actionConfig *config.ActionConfig, method string,
req: req,
children: map[string]*Node{},
keyNode: map[string]*Node{},
actionConfig: actionConfig,
ActionConfig: actionConfig,
}
return a
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func (a *Action) parse() error {

node := newNode(key, list, structure, a.tagRequest.Executor[key])
node.ctx = a.ctx
node.action = a
node.Action = a
a.keyNode[key] = &node
node.keyNode = a.keyNode
err := node.parse(a.ctx, a.method)
Expand Down Expand Up @@ -160,7 +160,7 @@ func (a *Action) Result() (model.Map, error) {
}

func checkTag(req model.Map, method string, requestCfg *config.ActionConfig) (*config.RequestConfig, error) {
_tag, ok := req["tag"]
_tag, ok := req[consts.Tag]
if !ok {
return nil, gerror.New("tag 缺失")
}
Expand Down
45 changes: 24 additions & 21 deletions action/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ package action

import (
"context"
"net/http"
"strings"

"github.com/glennliao/apijson-go/config"
"github.com/glennliao/apijson-go/config/executor"
"github.com/glennliao/apijson-go/consts"
"github.com/glennliao/apijson-go/model"
"github.com/glennliao/apijson-go/util"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/samber/lo"
"net/http"
"strings"
)

type Node struct {
req []model.Map
ctx context.Context
action *Action
Action *Action
Key string
IsList bool
tableName string
Expand All @@ -32,7 +33,7 @@ type Node struct {

keyNode map[string]*Node

//access *config.Access
// access *config.Access
}

func newNode(key string, req []model.Map, structure *config.Structure, executor string) Node {
Expand Down Expand Up @@ -65,8 +66,8 @@ func (n *Node) parseReq(method string) {

for i, item := range n.req {

//n.Data = append(n.Data, model.Map{})
//n.Where = append(n.Where, model.Map{})
// n.Data = append(n.Data, model.Map{})
// n.Where = append(n.Where, model.Map{})

for key, val := range item {

Expand All @@ -75,7 +76,7 @@ func (n *Node) parseReq(method string) {
continue
}

key = n.action.DbFieldStyle(n.ctx, n.tableName, key)
key = n.Action.DbFieldStyle(n.ctx, n.tableName, key)

switch method {
case http.MethodPost:
Expand All @@ -101,7 +102,7 @@ func (n *Node) parse(ctx context.Context, method string) error {
if strings.HasSuffix(key, consts.ListKeySuffix) {
key = key[0 : len(key)-2]
}
access, err := n.action.actionConfig.GetAccessConfig(key, true)
access, err := n.Action.ActionConfig.GetAccessConfig(key, true)

if err != nil {
return err
Expand All @@ -117,7 +118,7 @@ func (n *Node) parse(ctx context.Context, method string) error {
return err
}
var accessRoles []string
if n.action.NoAccessVerify == false {
if n.Action.NoAccessVerify == false {
// 1. 检查权限, 无权限就不用做参数检查了

switch method {
Expand Down Expand Up @@ -167,7 +168,7 @@ func (n *Node) roleUpdate() error {

func (n *Node) checkAccess(ctx context.Context, method string, accessRoles []string) error {

role, err := n.action.actionConfig.DefaultRoleFunc()(ctx, config.RoleReq{
role, err := n.Action.ActionConfig.DefaultRoleFunc()(ctx, config.RoleReq{
AccessName: n.tableName,
Method: method,
NodeRole: n.Role,
Expand Down Expand Up @@ -204,7 +205,7 @@ func (n *Node) whereUpdate(ctx context.Context, method string, accessRoles []str
NodeReq: item,
}

err := n.action.actionConfig.ConditionFunc(ctx, conditionReq, condition)
err := n.Action.ActionConfig.ConditionFunc(ctx, conditionReq, condition)

if err != nil {
return err
Expand Down Expand Up @@ -270,7 +271,7 @@ func (n *Node) reqUpdate() error {

// call functions
{
queryConfig := n.action.actionConfig
queryConfig := n.Action.ActionConfig

functionName, paramKeys := util.ParseFunctionsStr(updateVal.(string))

Expand Down Expand Up @@ -320,9 +321,9 @@ func (n *Node) reqUpdateBeforeDo() error {
if strings.HasSuffix(k, consts.RefKeySuffix) {
refNodeKey, refCol := util.ParseRefCol(v.(string))
if strings.HasSuffix(refNodeKey, consts.ListKeySuffix) { // 双列表
n.Data[i][k] = n.keyNode[refNodeKey].Data[i][n.action.DbFieldStyle(n.ctx, n.tableName, refCol)]
n.Data[i][k] = n.keyNode[refNodeKey].Data[i][n.Action.DbFieldStyle(n.ctx, n.tableName, refCol)]
} else {
n.Data[i][k] = n.keyNode[refNodeKey].Data[0][n.action.DbFieldStyle(n.ctx, n.tableName, refCol)]
n.Data[i][k] = n.keyNode[refNodeKey].Data[0][n.Action.DbFieldStyle(n.ctx, n.tableName, refCol)]
}
}
}
Expand All @@ -341,18 +342,18 @@ func (n *Node) do(ctx context.Context, method string) (ret model.Map, err error)
var rowKeyVal model.Map
var rowKey string

access, err := n.Action.ActionConfig.GetAccessConfig(n.Key, true)
if err != nil {
return nil, err
}

switch method {
case http.MethodPost:

access, err := n.action.actionConfig.GetAccessConfig(n.Key, true)
if err != nil {
return nil, err
}

if access.RowKeyGen != "" {
for i, _ := range n.Data {

rowKeyVal, err = n.action.actionConfig.RowKeyGen(ctx, access.RowKeyGen, n.Key, n.Data[i])
rowKeyVal, err = n.Action.ActionConfig.RowKeyGen(ctx, access.RowKeyGen, n.Key, n.Data[i])
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -382,6 +383,8 @@ func (n *Node) do(ctx context.Context, method string) (ret model.Map, err error)
Table: n.tableName,
Data: n.Data,
Where: n.Where,
Access: access,
Config: n.Action.ActionConfig,
})

if err != nil {
Expand All @@ -390,7 +393,7 @@ func (n *Node) do(ctx context.Context, method string) (ret model.Map, err error)

if len(n.Data) == 1 {

jsonStyle := n.action.JsonFieldStyle
jsonStyle := n.Action.JsonFieldStyle
if rowKeyVal != nil {
for k, v := range rowKeyVal {
if k == consts.RowKey {
Expand Down
4 changes: 4 additions & 0 deletions config/executor/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package executor

import (
"context"

"github.com/glennliao/apijson-go/config"
"github.com/glennliao/apijson-go/model"
"github.com/samber/lo"
)
Expand All @@ -15,6 +17,8 @@ type ActionExecutorReq struct {
Table string
Data []model.Map
Where []model.Map
Access *config.AccessConfig
Config *config.ActionConfig
}

var actionExecutorMap = map[string]ActionExecutor{}
Expand Down
1 change: 1 addition & 0 deletions consts/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
Total = "total"
Query = "query"
Alias = "@alias"
Tag = "tag"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions drivers/goframe/executor/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ func (a *ActionExecutor) Delete(ctx context.Context, table string, where model.M

for k, v := range where {

if k == "@raw" {
if k == consts.Raw {
m = m.Where(v)
continue
}

if strings.HasSuffix(k, "{}") {
if strings.HasSuffix(k, consts.OpIn) {
m = m.WhereIn(k[0:len(k)-2], v)
delete(where, k)
continue
Expand Down
6 changes: 3 additions & 3 deletions drivers/goframe/executor/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type SqlExecutor struct {

Role string

//保存where条件 [ ["user_id",">", 123], ["user_id","<=",345] ]
// 保存where条件 [ ["user_id",">", 123], ["user_id","<=",345] ]
Where [][]any
accessCondition model.Map

Expand Down Expand Up @@ -54,8 +54,8 @@ func (e *SqlExecutor) ParseCondition(conditions model.MapStrAny, accessVerify bo

for key, condition := range conditions {
switch {
case strings.HasSuffix(key, "{}"):
e.parseMultiCondition(util.RemoveSuffix(key, "{}"), condition)
case strings.HasSuffix(key, consts.OpIn):
e.parseMultiCondition(util.RemoveSuffix(key, consts.OpIn), condition)

case strings.HasSuffix(key, consts.OpLike):
e.Where = append(e.Where, []any{key[0 : len(key)-1], consts.SqlLike, gconv.String(condition)})
Expand Down
7 changes: 4 additions & 3 deletions drivers/goframe/web/gf.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/glennliao/apijson-go"
"github.com/glennliao/apijson-go/consts"
"github.com/glennliao/apijson-go/model"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/errors/gerror"
Expand Down Expand Up @@ -138,14 +139,14 @@ func sortMap(ctx context.Context, body []byte, res *gmap.ListMap, ret model.Map)
}

for _, k := range reqSortMap.Keys() {
if strings.HasPrefix(k, "@") {
if strings.HasPrefix(k, consts.RefKeySuffix) {
continue
}
if k == "tag" {
if k == consts.Tag {
continue
}

if strings.HasSuffix(k, "@") {
if strings.HasSuffix(k, consts.RefKeySuffix) {
k = k[:len(k)-1]
}

Expand Down
63 changes: 33 additions & 30 deletions query/node_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,28 +248,47 @@ func (q *queryNode) fetch() {
return
}

queryConfig := n.queryContext.queryConfig
if n.ret != nil { // parse 时判断是否有functions, 有则处理
queryConfig := n.queryContext.queryConfig

// 需优化调整
// 处理functions调用
for k, v := range n.req {
if !strings.HasSuffix(k, consts.FunctionsKeySuffix) {
continue
}

// 需优化调整
for k, v := range n.req {
if !strings.HasSuffix(k, consts.FunctionsKeySuffix) {
continue
}
k = k[0 : len(k)-2]

k = k[0 : len(k)-2]
functionName, paramKeys := util.ParseFunctionsStr(v.(string))
_func := queryConfig.Func(functionName)

functionName, paramKeys := util.ParseFunctionsStr(v.(string))
_func := queryConfig.Func(functionName)
if n.isList {
for i, item := range n.ret.([]model.Map) {

if n.isList && n.ret != nil {
for i, item := range n.ret.([]model.Map) {
param := model.Map{}
for paramI, paramItem := range _func.ParamList {
if paramItem.Name == consts.FunctionOriReqParam {
param[paramItem.Name] = item
} else {
param[paramItem.Name] = item[paramKeys[paramI]]
}
}

val, err := _func.Handler(n.ctx, param)
if err != nil {
n.err = err
return
}
n.ret.([]model.Map)[i][k] = val
}
} else {
param := model.Map{}
for paramI, paramItem := range _func.ParamList {
if paramItem.Name == consts.FunctionOriReqParam {
param[paramItem.Name] = item
param[paramItem.Name] = n.ret.(model.Map)
} else {
param[paramItem.Name] = item[paramKeys[paramI]]
param[paramItem.Name] = n.ret.(model.Map)[paramKeys[paramI]]
}
}

Expand All @@ -278,24 +297,8 @@ func (q *queryNode) fetch() {
n.err = err
return
}
n.ret.([]model.Map)[i][k] = val
}
} else {
param := model.Map{}
for paramI, paramItem := range _func.ParamList {
if paramItem.Name == consts.FunctionOriReqParam {
param[paramItem.Name] = n.ret.(model.Map)
} else {
param[paramItem.Name] = n.ret.(model.Map)[paramKeys[paramI]]
}
}

val, err := _func.Handler(n.ctx, param)
if err != nil {
n.err = err
return
n.ret.(model.Map)[k] = val
}
n.ret.(model.Map)[k] = val
}
}

Expand Down

0 comments on commit 8476aff

Please sign in to comment.