Skip to content

Commit

Permalink
update some
Browse files Browse the repository at this point in the history
  • Loading branch information
glennliao committed Mar 10, 2023
1 parent 5c43c8e commit ff2aa50
Show file tree
Hide file tree
Showing 19 changed files with 293 additions and 269 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ on:

jobs:
simple-test:
run: cd test && go test -short
runs-on: ubuntu-latest
steps:
- name: test
run: cd test && go test -short
45 changes: 0 additions & 45 deletions .github/workflows/todo.yml

This file was deleted.

21 changes: 21 additions & 0 deletions @doc/functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## 1. 调用方式一 来自返回的字段 -> 因为默认simple字段会返回 -> 需要返回吗
```json
{
"name": "hi",
"aaa": "demo",
"ref()": "sayHello(name,aaa)",
"@a": 0,
"ref2()": "ret(@a)",
"User": {
"pic()": "getPic(userId)" // 来自当前User的字段, 需要分析函数依赖的字段和依赖函数字段的节点
}
}
```

## 2
```json
{
"msg()": "sayHi",
"msg2()": "sayHi()"
}
```
2 changes: 1 addition & 1 deletion action/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func RegHook(h Hook) {

func EmitHook(ctx context.Context, hookAt int, node *Node, method string) error {

hooks := append(hooksMap["*"], hooksMap[node.TableName]...)
hooks := append(hooksMap["*"], hooksMap[node.AccessName]...)
for _, hook := range hooks {

var handler func(ctx context.Context, n *Node, method string) error
Expand Down
48 changes: 26 additions & 22 deletions action/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (
)

type Node struct {
req []model.Map
ctx context.Context
action *Action
Key string
TableName string
Role string
req []model.Map
ctx context.Context
action *Action
Key string
tableName string
AccessName string
Role string

Data []model.Map // 需写入数据库的数据
Where []model.Map // 条件
Expand All @@ -34,8 +35,14 @@ type Node struct {
}

func newNode(key string, req []model.Map, structure *config.Structure, executor string) Node {

accessName := key
if strings.HasSuffix(accessName, "[]") {
accessName = accessName[0 : len(accessName)-2]
}

return Node{
Key: key, req: req, structure: structure, executor: executor,
Key: key, req: req, structure: structure, executor: executor, AccessName: accessName,
}
}

Expand All @@ -52,7 +59,7 @@ func (n *Node) parseReq(method string) {
if key == consts.Role {
n.Role = util.String(val)
} else {
key = n.action.DbFieldStyle(n.ctx, n.TableName, key)
key = n.action.DbFieldStyle(n.ctx, n.tableName, key)

if method == http.MethodDelete {
n.Where[i][key] = val
Expand All @@ -63,7 +70,6 @@ func (n *Node) parseReq(method string) {
} else {
n.Data[i][key] = val
}
// Post 暂原则上不让传递这个rowKey值 // todo 可传递
} else {
n.Data[i][key] = val
}
Expand All @@ -86,7 +92,7 @@ func (n *Node) parse(ctx context.Context, method string) error {
return err
}

n.TableName = access.Name
n.tableName = access.Name
n.RowKey = access.RowKey

n.parseReq(method)
Expand Down Expand Up @@ -143,10 +149,8 @@ func (n *Node) roleUpdate() error {

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

// todo 可配置单次的内容, 而非直接使用整个的

role, err := n.action.actionConfig.DefaultRoleFunc()(ctx, config.RoleReq{
AccessName: n.TableName,
AccessName: n.tableName,
Method: method,
NodeRole: n.Role,
})
Expand All @@ -170,7 +174,7 @@ func (n *Node) checkAccess(ctx context.Context, method string, accessRoles []str
condition := config.NewConditionRet()

conditionReq := config.ConditionReq{
AccessName: n.TableName,
AccessName: n.tableName,
TableAccessRoleList: accessRoles,
Method: method,
NodeRole: n.Role,
Expand Down Expand Up @@ -281,9 +285,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 Down Expand Up @@ -314,7 +318,7 @@ func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret model.
if access.RowKeyGen != "" {
for i, _ := range n.Data {

rowKeyVal, err = n.action.actionConfig.RowKeyGen(ctx, access.RowKeyGen, n.TableName, n.Data[i])
rowKeyVal, err = n.action.actionConfig.RowKeyGen(ctx, access.RowKeyGen, n.AccessName, n.Data[i])
if err != nil {
return nil, err
}
Expand All @@ -332,7 +336,7 @@ func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret model.

var id int64

id, count, err = executor.GetActionExecutor(n.executor).Insert(ctx, n.TableName, n.Data)
id, count, err = executor.GetActionExecutor(n.executor).Insert(ctx, n.tableName, n.Data)

if err != nil {
return nil, err
Expand All @@ -350,16 +354,16 @@ func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret model.
if rowKeyVal != nil {
for k, v := range rowKeyVal {
if k == consts.RowKey {
ret[jsonStyle(ctx, n.TableName, access.RowKey)] = v
ret[jsonStyle(ctx, n.tableName, access.RowKey)] = v
} else {
ret[jsonStyle(ctx, n.TableName, k)] = v
ret[jsonStyle(ctx, n.tableName, k)] = v
}
}
}
}

case http.MethodPut:
count, err = executor.GetActionExecutor(n.executor).Update(ctx, n.TableName, n.Data[dataIndex], n.Where[dataIndex])
count, err = executor.GetActionExecutor(n.executor).Update(ctx, n.tableName, n.Data[dataIndex], n.Where[dataIndex])
if err != nil {
return nil, err
}
Expand All @@ -369,7 +373,7 @@ func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret model.
"count": count,
}
case http.MethodDelete:
count, err = executor.GetActionExecutor(n.executor).Delete(ctx, n.TableName, n.Where[dataIndex])
count, err = executor.GetActionExecutor(n.executor).Delete(ctx, n.tableName, n.Where[dataIndex])
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions config/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ func NewConditionRet() *ConditionRet {
return &c
}

func (c *ConditionRet) Add(k string, v any) { // todo any?
func (c *ConditionRet) Add(k string, v any) {
c.condition[k] = v
}

func (c *ConditionRet) AddRaw(k string, v any) { // todo any?
func (c *ConditionRet) AddRaw(k string, v any) {
c.rawCondition[k] = v
}

Expand Down
76 changes: 0 additions & 76 deletions config/action_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,79 +50,3 @@ func (c *ActionConfig) RowKeyGen(ctx context.Context, genFuncName string, access

return nil, nil
}

//
//type ExecutorConfig struct {
// NoVerify bool
// accessConfig *AccessConfig
// method string
// role string
// DBMeta *DBMeta
// DbFieldStyle FieldStyle
// JsonFieldStyle FieldStyle
//}
//
//func NewExecutorConfig(accessConfig *AccessConfig, method string, noVerify bool) *ExecutorConfig {
// return &ExecutorConfig{
// accessConfig: accessConfig,
// method: method,
// NoVerify: noVerify,
// }
//}
//
//func (c *ExecutorConfig) SetRole(role string) {
// c.role = role
//}
//
//func (c *ExecutorConfig) TableName() string {
// return c.accessConfig.Name
//}
//
//func (c *ExecutorConfig) TableColumns() []string {
// return c.DBMeta.GetTableColumns(c.accessConfig.Name)
//}
//
//func (c *ExecutorConfig) GetFieldsGetOutByRole() []string {
// var fieldsMap map[string]string
//
// if val, exists := c.accessConfig.FieldsGet[c.role]; exists {
// fieldsMap = val.Out
// } else {
// fieldsMap = c.accessConfig.FieldsGet["default"].Out
// }
// return lo.Keys(fieldsMap)
//}
//
//func (c *ExecutorConfig) GetFieldsGetInByRole() map[string][]string {
// var inFieldsMap map[string][]string
//
// if val, exists := c.accessConfig.FieldsGet[c.role]; exists {
// inFieldsMap = val.In
// } else {
// inFieldsMap = c.accessConfig.FieldsGet["default"].In
// }
//
// return inFieldsMap
//}
//
//func (c *ExecutorConfig) AccessRoles() []string {
// switch c.method {
// case http.MethodGet:
// return c.accessConfig.Get
// case http.MethodHead:
// return c.accessConfig.Head
// case http.MethodPost:
// return c.accessConfig.Post
// case http.MethodPut:
// return c.accessConfig.Put
// case http.MethodDelete:
// return c.accessConfig.Delete
// }
// return []string{}
//
//}
//
//func (c *ExecutorConfig) Executor() string {
// return c.accessConfig.Executor
//
//}
7 changes: 3 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ type Config struct {
RequestListProvider string
DbMetaProvider string

accessList []AccessConfig // todo to access
accessList []AccessConfig

requestConfig *RequestConfig

queryConfig *QueryConfig
actionConfig *ActionConfig
queryConfig *QueryConfig
actionConfig *ActionConfig
}

func New() *Config {
Expand Down
1 change: 0 additions & 1 deletion config/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

type Func struct {
// todo 调整成结构体
Handler func(ctx context.Context, param model.Map) (res any, err error)
}

Expand Down
Loading

0 comments on commit ff2aa50

Please sign in to comment.