Skip to content

Commit 9227091

Browse files
committed
## 0.2.0-beta5 增加@alias+从json读取access/request配置
1 parent b2285c7 commit 9227091

File tree

15 files changed

+108
-37
lines changed

15 files changed

+108
-37
lines changed

@doc/v0.2.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
55
1. 由全局配置改成apijson实例,可创建不同实例对应不同的内容
66
2. 代码内部 传递的accessName 都为 _access 配置中的alias
7-
3. access/request配置可从配置文件中获取
7+
3. access/request配置可从配置文件中获取
8+
9+
# vX.x
10+
- 给action 增加then/catch/before/after等方法?

action/action.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ package action
22

33
import (
44
"context"
5+
"strings"
6+
57
"github.com/glennliao/apijson-go/config"
68
"github.com/glennliao/apijson-go/consts"
79
"github.com/glennliao/apijson-go/model"
810
"github.com/gogf/gf/v2/database/gdb"
911
"github.com/gogf/gf/v2/errors/gerror"
1012
"github.com/gogf/gf/v2/frame/g"
1113
"github.com/gogf/gf/v2/util/gconv"
12-
"strings"
1314
)
1415

1516
// Action 非get查询的request表中的请求
1617
type Action struct {
1718
ctx context.Context
18-
tagRequest *config.Request
19+
tagRequest *config.RequestConfig
1920
method string
2021

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

33-
//Access *config.Access
34+
// Access *config.Access
3435

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

161-
func checkTag(req model.Map, method string, requestCfg *config.ActionConfig) (*config.Request, error) {
162+
func checkTag(req model.Map, method string, requestCfg *config.ActionConfig) (*config.RequestConfig, error) {
162163
_tag, ok := req["tag"]
163164
if !ok {
164165
return nil, gerror.New("tag 缺失")

action/hook.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const (
1010
)
1111

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

2525
func RegHook(h Hook) {
26-
hooksMap[h.For] = append(hooksMap[h.For], h)
26+
for _, item := range h.For {
27+
hooksMap[item] = append(hooksMap[item], h)
28+
}
2729
}
2830

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

config/action_config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package config
22

33
import (
44
"context"
5+
56
"github.com/glennliao/apijson-go/model"
67
)
78

89
type ActionConfig struct {
9-
requestConfig *RequestConfig
10+
requestConfig *RequestConfigs
1011
access *Access
1112
functions *functions
1213
rowKeyGenFuncMap map[string]RowKeyGenFuncHandler
@@ -29,7 +30,7 @@ func (c *ActionConfig) Func(name string) Func {
2930
return c.functions.funcMap[name]
3031
}
3132

32-
func (c *ActionConfig) GetRequest(tag string, method string, version string) (*Request, error) {
33+
func (c *ActionConfig) GetRequest(tag string, method string, version string) (*RequestConfig, error) {
3334
return c.requestConfig.GetRequest(tag, method, version)
3435
}
3536

config/config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func RegAccessListProvider(name string, provider AccessListProvider) {
1010
accessListProviderMap[name] = provider
1111
}
1212

13-
type RequestListProvider func(ctx context.Context) []Request
13+
type RequestListProvider func(ctx context.Context) []RequestConfig
1414

1515
var requestListProviderMap = make(map[string]RequestListProvider)
1616

@@ -50,9 +50,9 @@ type Config struct {
5050

5151
accessList []AccessConfig
5252

53-
requestConfig *RequestConfig
54-
queryConfig *QueryConfig
55-
actionConfig *ActionConfig
53+
requestConfigs *RequestConfigs
54+
queryConfig *QueryConfig
55+
actionConfig *ActionConfig
5656
}
5757

5858
func New() *Config {
@@ -117,7 +117,7 @@ func (c *Config) ReLoad() {
117117
requestListProvider := requestListProviderMap[c.RequestListProvider]
118118
if requestListProvider != nil {
119119
requestList := requestListProvider(ctx)
120-
c.requestConfig = NewRequestConfig(requestList)
120+
c.requestConfigs = NewRequestConfig(requestList)
121121
}
122122

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

136136
c.actionConfig = &ActionConfig{
137-
requestConfig: c.requestConfig,
137+
requestConfig: c.requestConfigs,
138138
access: c.Access,
139139
functions: c.Functions,
140140
rowKeyGenFuncMap: c.rowKeyGenFuncMap,

config/functions.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"context"
55
"fmt"
6+
67
"github.com/glennliao/apijson-go/model"
78
"github.com/gogf/gf/v2/frame/g"
89
)
@@ -15,6 +16,7 @@ type ParamItem struct {
1516

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

config/query_config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package config
22

33
import (
4-
"github.com/samber/lo"
54
"net/http"
5+
6+
"github.com/samber/lo"
67
)
78

89
type QueryConfig struct {

config/request_config.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package config
22

33
import (
4+
"strings"
5+
46
"github.com/glennliao/apijson-go/consts"
57
"github.com/gogf/gf/v2/errors/gerror"
68
"github.com/gogf/gf/v2/frame/g"
79
"github.com/gogf/gf/v2/os/gtime"
810
"github.com/gogf/gf/v2/util/gconv"
9-
"strings"
1011
)
1112

12-
type Request struct {
13+
type RequestConfig struct {
1314
Debug int8
1415
Version string
1516
Method string
@@ -38,13 +39,13 @@ type Structure struct {
3839
Remove []string `json:"REMOVE,omitempty"`
3940
}
4041

41-
type RequestConfig struct {
42-
requestMap map[string]*Request
42+
type RequestConfigs struct {
43+
requestMap map[string]*RequestConfig
4344
}
4445

45-
func NewRequestConfig(requestList []Request) *RequestConfig {
46-
c := RequestConfig{}
47-
requestMap := make(map[string]*Request)
46+
func NewRequestConfig(requestList []RequestConfig) *RequestConfigs {
47+
c := RequestConfigs{}
48+
requestMap := make(map[string]*RequestConfig)
4849

4950
for _, _item := range requestList {
5051
item := _item
@@ -71,7 +72,7 @@ func getRequestFullKey(tag string, method string, version string) string {
7172
return tag + "@" + method + "@" + version
7273
}
7374

74-
func (c *RequestConfig) GetRequest(tag string, method string, version string) (*Request, error) {
75+
func (c *RequestConfigs) GetRequest(tag string, method string, version string) (*RequestConfig, error) {
7576

7677
if version == "" || version == "-1" || version == "0" {
7778
version = "latest"

consts/node.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const (
1818
Count = "count" // page size
1919
Total = "total"
2020
Query = "query"
21+
Alias = "@alias"
2122
)
2223

2324
const (

drivers/goframe/config/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ var (
1616
ProviderName = "db"
1717
)
1818

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

3333
// provider处理
34-
//if strings.ToLower(tag) != tag {
34+
// if strings.ToLower(tag) != tag {
3535
// // 本身大写, 如果没有外层, 则套一层
3636
// if _, ok := item.Structure[tag]; !ok {
3737
// item.Structure = map[string]any{
3838
// tag: item.Structure,
3939
// }
4040
// }
41-
//}
41+
// }
4242

4343
for k, v := range item.Structure {
4444
structure := config.Structure{}

0 commit comments

Comments
 (0)