Skip to content

Commit

Permalink
just run ok
Browse files Browse the repository at this point in the history
  • Loading branch information
glennliao committed Mar 6, 2023
1 parent 5d1ab9f commit 0651e60
Show file tree
Hide file tree
Showing 27 changed files with 522 additions and 388 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
*.exe
*.exe
*.sqlite3
19 changes: 13 additions & 6 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package action
import (
"context"
"github.com/glennliao/apijson-go/config"
"github.com/glennliao/apijson-go/config/db"
"github.com/glennliao/apijson-go/consts"
"github.com/glennliao/apijson-go/model"
"github.com/gogf/gf/v2/database/gdb"
Expand All @@ -16,7 +15,7 @@ import (
// Action 非get查询的request表中的请求
type Action struct {
ctx context.Context
tagRequest *db.Request
tagRequest *config.Request
method string

req model.Map
Expand All @@ -32,11 +31,19 @@ type Action struct {
NoRequestVerify bool

Access *config.Access

// dbFieldStyle 数据库字段命名风格 请求传递到数据库中
DbFieldStyle config.FieldStyle

// jsonFieldStyle 数据库返回的字段
JsonFieldStyle config.FieldStyle

Functions *config.Functions
}

func New(ctx context.Context, method string, req model.Map) *Action {
func New(ctx context.Context, method string, req model.Map, requestCfg *config.RequestConfig) *Action {

request, err := checkTag(req, method)
request, err := checkTag(req, method, requestCfg)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -150,7 +157,7 @@ func (a *Action) Result() (model.Map, error) {
return ret, err
}

func checkTag(req model.Map, method string) (*db.Request, error) {
func checkTag(req model.Map, method string, requestCfg *config.RequestConfig) (*config.Request, error) {
_tag, ok := req["tag"]
if !ok {
return nil, gerror.New("tag 缺失")
Expand All @@ -159,7 +166,7 @@ func checkTag(req model.Map, method string) (*db.Request, error) {
tag := gconv.String(_tag)
version := req["version"]

request, err := db.GetRequest(tag, method, gconv.String(version))
request, err := requestCfg.GetRequest(tag, method, gconv.String(version))
if err != nil {
return nil, err
}
Expand Down
20 changes: 9 additions & 11 deletions action/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package action
import (
"context"
"github.com/glennliao/apijson-go/config"
"github.com/glennliao/apijson-go/config/db"
"github.com/glennliao/apijson-go/config/executor"
"github.com/glennliao/apijson-go/config/functions"
"github.com/glennliao/apijson-go/consts"
"github.com/glennliao/apijson-go/model"
"github.com/glennliao/apijson-go/util"
Expand All @@ -27,15 +25,15 @@ type Node struct {
Where []model.Map // 条件
RowKey string // 主键

structure *db.Structure
structure *config.Structure
executor string

keyNode map[string]*Node

access *config.Access
}

func newNode(key string, req []model.Map, structure *db.Structure, executor string) Node {
func newNode(key string, req []model.Map, structure *config.Structure, executor string) Node {
return Node{
Key: key, req: req, structure: structure, executor: executor,
}
Expand All @@ -54,7 +52,7 @@ func (n *Node) parseReq(method string) {
if key == consts.Role {
n.Role = util.String(val)
} else {
key = config.GetDbFieldStyle()(n.ctx, n.TableName, key)
key = n.action.DbFieldStyle(n.ctx, n.TableName, key)

if method == http.MethodDelete {
n.Where[i][key] = val
Expand Down Expand Up @@ -82,7 +80,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 := db.GetAccess(key, true)
access, err := n.access.GetAccess(key, true)

if err != nil {
return err
Expand Down Expand Up @@ -245,7 +243,7 @@ func (n *Node) reqUpdate() error {
}
}
k := key[0 : len(key)-2]
val, err := functions.Call(n.ctx, functionName, param)
val, err := n.action.Functions.Call(n.ctx, functionName, param)
if err != nil {
return err
}
Expand Down Expand Up @@ -278,9 +276,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][config.GetDbFieldStyle()(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][config.GetDbFieldStyle()(n.ctx, n.TableName, refCol)]
n.Data[i][k] = n.keyNode[refNodeKey].Data[0][n.action.DbFieldStyle(n.ctx, n.TableName, refCol)]
}
}
}
Expand All @@ -303,7 +301,7 @@ func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret model.

var rowKeyVal model.Map

access, err := db.GetAccess(n.Key, true)
access, err := n.access.GetAccess(n.Key, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -343,7 +341,7 @@ func (n *Node) do(ctx context.Context, method string, dataIndex int) (ret model.

if len(n.Data) > 0 { //多条插入时返回值已经应该无意义了

jsonStyle := config.GetJsonFieldStyle()
jsonStyle := n.action.JsonFieldStyle
if rowKeyVal != nil {
for k, v := range rowKeyVal {
if k == consts.RowKey {
Expand Down
8 changes: 6 additions & 2 deletions apijson.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ func New() *ApiJson {
}

// Load load for defaultApiJson, 简化使用
func Load(app func(ctx context.Context, a *ApiJson)) *ApiJson {
DefaultApiJson.Use(app)
func Load(apps ...func(ctx context.Context, a *ApiJson)) *ApiJson {

for _, app := range apps {
DefaultApiJson.Use(app)
}

DefaultApiJson.Load()
return DefaultApiJson
}
Expand Down
2 changes: 2 additions & 0 deletions config/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type Access struct {
DefaultRoleFunc DefaultRole

roleList []string

accessConfigMap map[string]AccessConfig
}

func NewAccess() *Access {
Expand Down
98 changes: 98 additions & 0 deletions config/access_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package config

import (
"github.com/glennliao/apijson-go/util"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gtime"
"github.com/samber/lo"
"net/http"
)

type FieldsGetValue struct {
In map[string][]string
Out map[string]string
}

type AccessConfig struct {
Debug int8
Name string
Alias string
Get []string
Head []string
Gets []string
Heads []string
Post []string
Put []string
Delete []string
CreatedAt *gtime.Time
Detail string

RowKeyGen string // 主键生成策略
RowKey string
FieldsGet map[string]FieldsGetValue
Executor string
}

func (a *AccessConfig) GetFieldsGetOutByRole(role string) []string {
var fieldsMap map[string]string

if val, exists := a.FieldsGet[role]; exists {
fieldsMap = val.Out
} else {
fieldsMap = a.FieldsGet["default"].Out
}
return lo.Keys(fieldsMap)
}

func (a *AccessConfig) GetFieldsGetInByRole(role string) map[string][]string {
var inFieldsMap map[string][]string

if val, exists := a.FieldsGet[role]; exists {
inFieldsMap = val.In
} else {
inFieldsMap = a.FieldsGet["default"].In
}

return inFieldsMap
}

func (a *Access) GetAccess(tableAlias string, accessVerify bool) (*AccessConfig, error) {
tableAlias, _ = util.ParseNodeKey(tableAlias)
access, ok := a.accessConfigMap[tableAlias]

if !ok {
if accessVerify {
return nil, gerror.Newf("access[%s]: 404", tableAlias)
}
return &AccessConfig{
Debug: 0,
Name: tableAlias,
Alias: tableAlias,
}, nil
}

return &access, nil
}

func (a *Access) GetAccessRole(table string, method string) ([]string, string, error) {
access, ok := a.accessConfigMap[table]

if !ok {
return nil, "", gerror.Newf("access[%s]: 404", table)
}

switch method {
case http.MethodGet:
return access.Get, access.Name, nil
case http.MethodHead:
return access.Head, access.Name, nil
case http.MethodPost:
return access.Post, access.Name, nil
case http.MethodPut:
return access.Put, access.Name, nil
case http.MethodDelete:
return access.Delete, access.Name, nil
}

return []string{}, access.Name, nil
}
27 changes: 24 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
package config

type Config struct {
Access *Access
MaxTreeWidth int
MaxTreeDeep int
Access *Access

Functions *Functions

MaxTreeWidth int
MaxTreeDeep int

rowKeyGenFuncMap map[string]RowKeyGenFuncHandler

// dbFieldStyle 数据库字段命名风格 请求传递到数据库中
DbFieldStyle FieldStyle

// jsonFieldStyle 数据库返回的字段
JsonFieldStyle FieldStyle

DbMeta *DBMeta

AccessList []AccessConfig // todo to access

RequestConfig *RequestConfig
}

func New() *Config {
Expand All @@ -16,5 +32,10 @@ func New() *Config {

a.rowKeyGenFuncMap = make(map[string]RowKeyGenFuncHandler)

a.DbFieldStyle = CaseSnake
a.JsonFieldStyle = CaseCamel

a.Functions = &Functions{}

return a
}
Loading

0 comments on commit 0651e60

Please sign in to comment.