From b2285c74ba3339ef01ddc4a9068441f9f2953327 Mon Sep 17 00:00:00 2001 From: glennliao Date: Mon, 5 Jun 2023 00:28:15 +0800 Subject: [PATCH] 0.2.0-beta4 (#20) * ## 0.2.0-beta3 * ## 0.2.0-beta4 --- @doc/access.md | 0 @doc/action.md | 0 @doc/doc_ref.md | 0 @doc/faq.md | 0 @doc/functions.md | 0 @doc/query.md | 0 @doc/roadmap.md | 0 @doc/v0.2.md | 0 LICENSE | 0 README.md | 0 README.zh-CN.md | 0 action/action.go | 0 action/hook.go | 0 action/node.go | 65 ++++---- apijson.go | 0 config/access.go | 0 config/access_config.go | 0 config/action_config.go | 0 config/config.go | 6 +- config/dbmeta.go | 0 config/executor/action.go | 0 config/executor/query.go | 0 config/field.go | 0 config/functions.go | 0 config/query_config.go | 0 config/request_config.go | 0 config/rowkeygen.go | 0 config/tables/table.go | 0 consts/access.go | 0 consts/errors.go | 0 consts/node.go | 0 drivers/config/goframe/config.go | 142 ------------------ drivers/executor/goframe/driver.go | 11 -- drivers/goframe/all.go | 21 +++ drivers/goframe/config/config.go | 139 +++++++++++++++++ .../executor}/action.go | 13 +- .../executor}/query.go | 7 +- .../{framework_goframe => goframe/web}/gf.go | 9 +- .../web}/mode.go | 2 +- go.mod | 0 go.sum | 0 model/model.go | 0 query/node.go | 12 +- query/node_func.go | 0 query/node_query.go | 39 ++++- query/node_ref.go | 0 query/node_struct.go | 7 +- query/query.go | 5 +- query/util.go | 0 test/config.toml | 0 test/func/z_test.go | 0 test/test.http | 0 test/z_app_test.go | 13 +- test/z_main_test.go | 34 ++++- util/conv.go | 0 util/functions.go | 0 util/node.go | 0 57 files changed, 307 insertions(+), 218 deletions(-) mode change 100644 => 100755 @doc/access.md mode change 100644 => 100755 @doc/action.md mode change 100644 => 100755 @doc/doc_ref.md mode change 100644 => 100755 @doc/faq.md mode change 100644 => 100755 @doc/functions.md mode change 100644 => 100755 @doc/query.md mode change 100644 => 100755 @doc/roadmap.md mode change 100644 => 100755 @doc/v0.2.md mode change 100644 => 100755 LICENSE mode change 100644 => 100755 README.md mode change 100644 => 100755 README.zh-CN.md mode change 100644 => 100755 action/action.go mode change 100644 => 100755 action/hook.go mode change 100644 => 100755 action/node.go mode change 100644 => 100755 apijson.go mode change 100644 => 100755 config/access.go mode change 100644 => 100755 config/access_config.go mode change 100644 => 100755 config/action_config.go mode change 100644 => 100755 config/config.go mode change 100644 => 100755 config/dbmeta.go mode change 100644 => 100755 config/executor/action.go mode change 100644 => 100755 config/executor/query.go mode change 100644 => 100755 config/field.go mode change 100644 => 100755 config/functions.go mode change 100644 => 100755 config/query_config.go mode change 100644 => 100755 config/request_config.go mode change 100644 => 100755 config/rowkeygen.go mode change 100644 => 100755 config/tables/table.go mode change 100644 => 100755 consts/access.go mode change 100644 => 100755 consts/errors.go mode change 100644 => 100755 consts/node.go delete mode 100644 drivers/config/goframe/config.go delete mode 100644 drivers/executor/goframe/driver.go create mode 100644 drivers/goframe/all.go create mode 100755 drivers/goframe/config/config.go rename drivers/{executor_goframe => goframe/executor}/action.go (94%) mode change 100644 => 100755 rename drivers/{executor_goframe => goframe/executor}/query.go (99%) mode change 100644 => 100755 rename drivers/{framework_goframe => goframe/web}/gf.go (99%) mode change 100644 => 100755 rename drivers/{framework_goframe => goframe/web}/mode.go (95%) mode change 100644 => 100755 mode change 100644 => 100755 go.mod mode change 100644 => 100755 go.sum mode change 100644 => 100755 model/model.go mode change 100644 => 100755 query/node.go mode change 100644 => 100755 query/node_func.go mode change 100644 => 100755 query/node_query.go mode change 100644 => 100755 query/node_ref.go mode change 100644 => 100755 query/node_struct.go mode change 100644 => 100755 query/query.go mode change 100644 => 100755 query/util.go mode change 100644 => 100755 test/config.toml mode change 100644 => 100755 test/func/z_test.go mode change 100644 => 100755 test/test.http mode change 100644 => 100755 test/z_app_test.go mode change 100644 => 100755 test/z_main_test.go mode change 100644 => 100755 util/conv.go mode change 100644 => 100755 util/functions.go mode change 100644 => 100755 util/node.go diff --git a/@doc/access.md b/@doc/access.md old mode 100644 new mode 100755 diff --git a/@doc/action.md b/@doc/action.md old mode 100644 new mode 100755 diff --git a/@doc/doc_ref.md b/@doc/doc_ref.md old mode 100644 new mode 100755 diff --git a/@doc/faq.md b/@doc/faq.md old mode 100644 new mode 100755 diff --git a/@doc/functions.md b/@doc/functions.md old mode 100644 new mode 100755 diff --git a/@doc/query.md b/@doc/query.md old mode 100644 new mode 100755 diff --git a/@doc/roadmap.md b/@doc/roadmap.md old mode 100644 new mode 100755 diff --git a/@doc/v0.2.md b/@doc/v0.2.md old mode 100644 new mode 100755 diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/README.zh-CN.md b/README.zh-CN.md old mode 100644 new mode 100755 diff --git a/action/action.go b/action/action.go old mode 100644 new mode 100755 diff --git a/action/hook.go b/action/hook.go old mode 100644 new mode 100755 diff --git a/action/node.go b/action/node.go old mode 100644 new mode 100755 index 4e4135d..b8e7a06 --- a/action/node.go +++ b/action/node.go @@ -41,6 +41,17 @@ func newNode(key string, req []model.Map, structure *config.Structure, executor Key: key, req: req, structure: structure, executor: executor, } + n.Data = []model.Map{} + n.Where = []model.Map{} + + // ? + for _ = range n.req { + + n.Data = append(n.Data, model.Map{}) + n.Where = append(n.Where, model.Map{}) + + } + if strings.HasSuffix(key, consts.ListKeySuffix) { n.Key = key[0 : len(key)-len(consts.ListKeySuffix)] n.IsList = true @@ -51,13 +62,11 @@ func newNode(key string, req []model.Map, structure *config.Structure, executor // parse req data to data/where func (n *Node) parseReq(method string) { - n.Data = []model.Map{} - n.Where = []model.Map{} 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 { @@ -101,8 +110,6 @@ func (n *Node) parse(ctx context.Context, method string) error { n.tableName = access.Name n.RowKey = access.RowKey - n.parseReq(method) - // 0. 角色替换 err = n.roleUpdate() @@ -137,6 +144,8 @@ func (n *Node) parse(ctx context.Context, method string) error { // 3. get where by accessCondition err = n.whereUpdate(ctx, method, accessRoles) + n.parseReq(method) + return err } @@ -329,11 +338,12 @@ func (n *Node) do(ctx context.Context, method string) (ret model.Map, err error) return nil, err } + var rowKeyVal model.Map + var rowKey string + switch method { case http.MethodPost: - var rowKeyVal model.Map - access, err := n.action.actionConfig.GetAccessConfig(n.Key, true) if err != nil { return nil, err @@ -358,30 +368,7 @@ func (n *Node) do(ctx context.Context, method string) (ret model.Map, err error) } } - ret, err := executor.GetActionExecutor(n.executor).Do(ctx, executor.ActionExecutorReq{ - Method: method, - Table: n.tableName, - Data: n.Data, - Where: nil, - }) - - if err != nil { - return nil, err - } - - if len(n.Data) > 0 { //多条插入时返回值已经应该无意义了 - - jsonStyle := n.action.JsonFieldStyle - if rowKeyVal != nil { - for k, v := range rowKeyVal { - if k == consts.RowKey { - ret[jsonStyle(ctx, n.tableName, access.RowKey)] = v - } else { - ret[jsonStyle(ctx, n.tableName, k)] = v - } - } - } - } + rowKey = access.RowKey case http.MethodPut: case http.MethodDelete: @@ -401,6 +388,20 @@ func (n *Node) do(ctx context.Context, method string) (ret model.Map, err error) return nil, err } + if len(n.Data) == 1 { + + jsonStyle := n.action.JsonFieldStyle + if rowKeyVal != nil { + for k, v := range rowKeyVal { + if k == consts.RowKey { + ret[jsonStyle(ctx, n.tableName, rowKey)] = v + } else { + ret[jsonStyle(ctx, n.tableName, k)] = v + } + } + } + } + n.Ret = ret err = EmitHook(ctx, AfterExecutorDo, n, method) diff --git a/apijson.go b/apijson.go old mode 100644 new mode 100755 diff --git a/config/access.go b/config/access.go old mode 100644 new mode 100755 diff --git a/config/access_config.go b/config/access_config.go old mode 100644 new mode 100755 diff --git a/config/action_config.go b/config/action_config.go old mode 100644 new mode 100755 diff --git a/config/config.go b/config/config.go old mode 100644 new mode 100755 index 5908492..0b823de --- a/config/config.go +++ b/config/config.go @@ -78,7 +78,7 @@ func New() *Config { func (c *Config) ReLoad() { - c.Access.accessConfigMap = make(map[string]AccessConfig) + accessConfigMap := make(map[string]AccessConfig) ctx := context.Background() @@ -108,10 +108,12 @@ func (c *Config) ReLoad() { access.FieldsGet[role].MaxCount = &defaultMaxCount } } - c.Access.accessConfigMap[access.Alias] = access + accessConfigMap[access.Alias] = access } } + c.Access.accessConfigMap = accessConfigMap + requestListProvider := requestListProviderMap[c.RequestListProvider] if requestListProvider != nil { requestList := requestListProvider(ctx) diff --git a/config/dbmeta.go b/config/dbmeta.go old mode 100644 new mode 100755 diff --git a/config/executor/action.go b/config/executor/action.go old mode 100644 new mode 100755 diff --git a/config/executor/query.go b/config/executor/query.go old mode 100644 new mode 100755 diff --git a/config/field.go b/config/field.go old mode 100644 new mode 100755 diff --git a/config/functions.go b/config/functions.go old mode 100644 new mode 100755 diff --git a/config/query_config.go b/config/query_config.go old mode 100644 new mode 100755 diff --git a/config/request_config.go b/config/request_config.go old mode 100644 new mode 100755 diff --git a/config/rowkeygen.go b/config/rowkeygen.go old mode 100644 new mode 100755 diff --git a/config/tables/table.go b/config/tables/table.go old mode 100644 new mode 100755 diff --git a/consts/access.go b/consts/access.go old mode 100644 new mode 100755 diff --git a/consts/errors.go b/consts/errors.go old mode 100644 new mode 100755 diff --git a/consts/node.go b/consts/node.go old mode 100644 new mode 100755 diff --git a/drivers/config/goframe/config.go b/drivers/config/goframe/config.go deleted file mode 100644 index 688012f..0000000 --- a/drivers/config/goframe/config.go +++ /dev/null @@ -1,142 +0,0 @@ -package goframe - -import ( - "context" - "github.com/glennliao/apijson-go/config" - "github.com/gogf/gf/v2/frame/g" - "github.com/gogf/gf/v2/util/gconv" - "strings" -) - -// 设置 _access/_request 自定义表名 -var ( - TableAccess = "_access" - TableRequest = "_request" - ProviderName = "db" -) - -func init() { - config.RegAccessListProvider(ProviderName, func(ctx context.Context) []config.AccessConfig { - // access - var accessList []config.AccessConfig - - db := g.DB() - - err := db.Model(TableAccess).Scan(&accessList) - if err != nil { - panic(err) - } - - for _, access := range accessList { - if len(access.Get) == 1 { - access.Get = strings.Split(access.Get[0], ",") - } - if len(access.Head) == 1 { - access.Head = strings.Split(access.Head[0], ",") - } - if len(access.Gets) == 1 { - access.Gets = strings.Split(access.Gets[0], ",") - } - if len(access.Heads) == 1 { - access.Heads = strings.Split(access.Heads[0], ",") - } - if len(access.Post) == 1 { - access.Post = strings.Split(access.Post[0], ",") - } - if len(access.Put) == 1 { - access.Put = strings.Split(access.Put[0], ",") - } - if len(access.Delete) == 1 { - access.Delete = strings.Split(access.Delete[0], ",") - } - - g.Dump(access.FieldsGet) - - accessList = append(accessList, access) - } - - return accessList - }) - - config.RegRequestListProvider(ProviderName, func(ctx context.Context) []config.Request { - var requestList []config.Request - err := g.DB().Model(TableRequest).OrderAsc("version").Scan(&requestList) - if err != nil { - panic(err) - } - - for i, item := range requestList { - item := item - - if item.Structure == nil { - item.Structure = make(map[string]*config.Structure) - } - - // provider处理 - //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{} - err := gconv.Scan(v, &structure) - if err != nil { - panic(err) - } - - if structure.Must != nil { - structure.Must = strings.Split(structure.Must[0], ",") - } - if structure.Refuse != nil { - structure.Refuse = strings.Split(structure.Refuse[0], ",") - } - - item.Structure[k] = &structure - } - - if len(item.ExecQueue) > 0 { - item.ExecQueue = strings.Split(item.ExecQueue[0], ",") - } - - requestList[i] = item - - } - - return requestList - }) - - config.RegDbMetaProvider(ProviderName, func(ctx context.Context) []config.Table { - var _tables []config.Table - - db := g.DB() - tables, err := db.Tables(ctx) - if err != nil { - panic(err) - } - - for _, table := range tables { - fields, err := db.TableFields(ctx, table) - if err != nil { - panic(err) - } - - var columns []config.Column - for field, _ := range fields { - columns = append(columns, config.Column{Name: field}) - } - - _tables = append(_tables, config.Table{ - Name: table, - Columns: columns, - }) - - } - - return _tables - }) -} diff --git a/drivers/executor/goframe/driver.go b/drivers/executor/goframe/driver.go deleted file mode 100644 index bfb55b1..0000000 --- a/drivers/executor/goframe/driver.go +++ /dev/null @@ -1,11 +0,0 @@ -package goframe - -import ( - "github.com/glennliao/apijson-go/config/executor" - "github.com/glennliao/apijson-go/drivers/executor_goframe" -) - -func init() { - executor.RegQueryExecutor("default", executor_goframe.New) - executor.RegActionExecutor("default", &executor_goframe.ActionExecutor{}) -} diff --git a/drivers/goframe/all.go b/drivers/goframe/all.go new file mode 100644 index 0000000..e1f2739 --- /dev/null +++ b/drivers/goframe/all.go @@ -0,0 +1,21 @@ +package goframe + +import ( + "github.com/glennliao/apijson-go/config" + "github.com/glennliao/apijson-go/config/executor" + gfConfig "github.com/glennliao/apijson-go/drivers/goframe/config" + gfExecutor "github.com/glennliao/apijson-go/drivers/goframe/executor" +) + +func init() { + + config.RegAccessListProvider(gfConfig.ProviderName, gfConfig.AccessListDBProvider) + config.RegRequestListProvider(gfConfig.ProviderName, gfConfig.RequestListProvider) + + config.RegDbMetaProvider(gfConfig.ProviderName, gfConfig.DbMetaProvider) + + executor.RegQueryExecutor("default", gfExecutor.New) + executor.RegActionExecutor("default", &gfExecutor.ActionExecutor{}) + + // todo 添加未配置driver时的报错信息, 而不是 invalid memory address or nil pointer dereference +} diff --git a/drivers/goframe/config/config.go b/drivers/goframe/config/config.go new file mode 100755 index 0000000..fb9686b --- /dev/null +++ b/drivers/goframe/config/config.go @@ -0,0 +1,139 @@ +package config + +import ( + "context" + "strings" + + "github.com/glennliao/apijson-go/config" + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/util/gconv" +) + +// 设置 _access/_request 自定义表名 +var ( + TableAccess = "_access" + TableRequest = "_request" + ProviderName = "db" +) + +func RequestListProvider(ctx context.Context) []config.Request { + var requestList []config.Request + err := g.DB().Model(TableRequest).OrderAsc("version").Scan(&requestList) + if err != nil { + panic(err) + } + + for i, item := range requestList { + item := item + + if item.Structure == nil { + item.Structure = make(map[string]*config.Structure) + } + + // provider处理 + //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{} + err := gconv.Scan(v, &structure) + if err != nil { + panic(err) + } + + if structure.Must != nil { + structure.Must = strings.Split(structure.Must[0], ",") + } + if structure.Refuse != nil { + structure.Refuse = strings.Split(structure.Refuse[0], ",") + } + + item.Structure[k] = &structure + } + + if len(item.ExecQueue) > 0 { + item.ExecQueue = strings.Split(item.ExecQueue[0], ",") + } + + requestList[i] = item + + } + + return requestList +} + +func DbMetaProvider(ctx context.Context) []config.Table { + var _tables []config.Table + + db := g.DB() + tables, err := db.Tables(ctx) + if err != nil { + panic(err) + } + + for _, table := range tables { + fields, err := db.TableFields(ctx, table) + if err != nil { + panic(err) + } + + var columns []config.Column + for field, _ := range fields { + columns = append(columns, config.Column{Name: field}) + } + + _tables = append(_tables, config.Table{ + Name: table, + Columns: columns, + }) + + } + + return _tables +} + +func AccessListDBProvider(ctx context.Context) []config.AccessConfig { + // access + var accessList []config.AccessConfig + + db := g.DB() + + err := db.Model(TableAccess).Scan(&accessList) + if err != nil { + panic(err) + } + + for _, access := range accessList { + if len(access.Get) == 1 { + access.Get = strings.Split(access.Get[0], ",") + } + if len(access.Head) == 1 { + access.Head = strings.Split(access.Head[0], ",") + } + if len(access.Gets) == 1 { + access.Gets = strings.Split(access.Gets[0], ",") + } + if len(access.Heads) == 1 { + access.Heads = strings.Split(access.Heads[0], ",") + } + if len(access.Post) == 1 { + access.Post = strings.Split(access.Post[0], ",") + } + if len(access.Put) == 1 { + access.Put = strings.Split(access.Put[0], ",") + } + if len(access.Delete) == 1 { + access.Delete = strings.Split(access.Delete[0], ",") + } + + accessList = append(accessList, access) + } + + return accessList +} diff --git a/drivers/executor_goframe/action.go b/drivers/goframe/executor/action.go old mode 100644 new mode 100755 similarity index 94% rename from drivers/executor_goframe/action.go rename to drivers/goframe/executor/action.go index 10d5314..95ba22e --- a/drivers/executor_goframe/action.go +++ b/drivers/goframe/executor/action.go @@ -1,7 +1,10 @@ -package executor_goframe +package executor import ( "context" + "net/http" + "strings" + "github.com/glennliao/apijson-go/config/executor" "github.com/glennliao/apijson-go/consts" "github.com/glennliao/apijson-go/model" @@ -10,8 +13,6 @@ import ( "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" - "net/http" - "strings" ) type ActionExecutor struct { @@ -91,13 +92,13 @@ func (a *ActionExecutor) Update(ctx context.Context, table string, data model.Ma continue } if k == consts.Raw { - m = m.Where(v.(model.Map)) + m = m.Where(v.(map[string]any)) delete(where, k) continue } - if v.(string) == "" || v == nil { //暂只处理字符串为空的情况 - return nil, gerror.New("where的值不能为空") + if v == nil || gconv.String(v) == "" { //暂只处理字符串为空的情况 + return nil, gerror.New("where的值不能为空:" + k) } } diff --git a/drivers/executor_goframe/query.go b/drivers/goframe/executor/query.go old mode 100644 new mode 100755 similarity index 99% rename from drivers/executor_goframe/query.go rename to drivers/goframe/executor/query.go index 0105d30..8a6ea7f --- a/drivers/executor_goframe/query.go +++ b/drivers/goframe/executor/query.go @@ -1,7 +1,10 @@ -package executor_goframe +package executor import ( "context" + "regexp" + "strings" + "github.com/glennliao/apijson-go/config" "github.com/glennliao/apijson-go/config/executor" "github.com/glennliao/apijson-go/consts" @@ -11,8 +14,6 @@ import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" "github.com/samber/lo" - "regexp" - "strings" ) type SqlExecutor struct { diff --git a/drivers/framework_goframe/gf.go b/drivers/goframe/web/gf.go old mode 100644 new mode 100755 similarity index 99% rename from drivers/framework_goframe/gf.go rename to drivers/goframe/web/gf.go index 5ead390..a121558 --- a/drivers/framework_goframe/gf.go +++ b/drivers/goframe/web/gf.go @@ -1,8 +1,12 @@ -package framework_goframe +package web import ( "context" "fmt" + "net/http" + "strings" + "time" + "github.com/glennliao/apijson-go" "github.com/glennliao/apijson-go/model" "github.com/gogf/gf/v2/container/gmap" @@ -10,9 +14,6 @@ import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" "github.com/iancoleman/orderedmap" - "net/http" - "strings" - "time" ) type GF struct { diff --git a/drivers/framework_goframe/mode.go b/drivers/goframe/web/mode.go old mode 100644 new mode 100755 similarity index 95% rename from drivers/framework_goframe/mode.go rename to drivers/goframe/web/mode.go index ad56790..bf3c02d --- a/drivers/framework_goframe/mode.go +++ b/drivers/goframe/web/mode.go @@ -1,4 +1,4 @@ -package framework_goframe +package web import "github.com/gogf/gf/v2/container/gmap" diff --git a/go.mod b/go.mod old mode 100644 new mode 100755 diff --git a/go.sum b/go.sum old mode 100644 new mode 100755 diff --git a/model/model.go b/model/model.go old mode 100644 new mode 100755 diff --git a/query/node.go b/query/node.go old mode 100644 new mode 100755 index 44c1e4d..a548cc5 --- a/query/node.go +++ b/query/node.go @@ -2,6 +2,10 @@ package query import ( "context" + "path/filepath" + "strings" + "time" + "github.com/glennliao/apijson-go/config" "github.com/glennliao/apijson-go/config/executor" "github.com/glennliao/apijson-go/consts" @@ -11,9 +15,6 @@ import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" "github.com/samber/lo" - "path/filepath" - "strings" - "time" ) const ( @@ -46,6 +47,9 @@ type Node struct { // 节点类型 Type int + // 字段映射 + Column map[string]string + // 是否为列表节点 isList bool @@ -149,6 +153,8 @@ func newNode(query *Query, key string, path string, nodeReq any) *Node { node.simpleReqVal = gconv.String(nodeReq) } + node.Column = map[string]string{} + return node } diff --git a/query/node_func.go b/query/node_func.go old mode 100644 new mode 100755 diff --git a/query/node_query.go b/query/node_query.go old mode 100644 new mode 100755 index deb73ed..43041e6 --- a/query/node_query.go +++ b/query/node_query.go @@ -1,15 +1,19 @@ package query import ( + "fmt" + "net/http" + "path/filepath" + "regexp" + "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" - "net/http" - "path/filepath" - "strings" + "github.com/gogf/gf/v2/util/gconv" ) type queryNode struct { @@ -82,6 +86,30 @@ func (q *queryNode) parse() { n.executor.ParseCtrl(ctrlMap) + if v, exists := ctrlMap["@column"]; exists { + var exp = regexp.MustCompile(`^[\s\w][\w()]+`) // 匹配 field, COUNT(field) + + fieldStr := strings.ReplaceAll(gconv.String(v), ";", ",") + + fieldList := strings.Split(fieldStr, ",") + + for i, item := range fieldList { + fieldList[i] = exp.ReplaceAllStringFunc(item, func(field string) string { + return field + }) + } + + for _, item := range fieldList { + if strings.Contains(item, ":") { + n.Column[item] = item + } else { + n.Column[item] = item + } + } + + fmt.Println(fieldList) + } + err = n.executor.ParseCondition(conditionMap, true) if err != nil { n.err = err @@ -208,8 +236,11 @@ func (q *queryNode) fetch() { count = 0 } - n.ret, n.err = n.executor.List(page, count) n.total, n.err = n.executor.Count() + if n.total > 0 { + n.ret, n.err = n.executor.List(page, count) + } + } else { n.ret, n.err = n.executor.One() } diff --git a/query/node_ref.go b/query/node_ref.go old mode 100644 new mode 100755 diff --git a/query/node_struct.go b/query/node_struct.go old mode 100644 new mode 100755 index 0e4132b..384c64a --- a/query/node_struct.go +++ b/query/node_struct.go @@ -134,13 +134,18 @@ func (h *structNode) result() { k = k[0 : len(k)-2] } + // todo 增加alias ?用来重命名返回的key,避免前端调整取值 + if node.req["@alias"] != nil { + k = node.req["@alias"].(string) + } + retMap[k], err = node.Result() if node.Type == NodeTypeFunc && retMap[k] == nil { delete(retMap, k) } if err != nil { - n.ret = nil + n.err = err } } n.ret = retMap diff --git a/query/query.go b/query/query.go old mode 100644 new mode 100755 index 954b5cc..2664d1c --- a/query/query.go +++ b/query/query.go @@ -3,14 +3,15 @@ package query import ( "context" "fmt" + "strings" + "time" + "github.com/glennliao/apijson-go/config" "github.com/glennliao/apijson-go/model" "github.com/glennliao/apijson-go/util" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" "github.com/samber/lo" - "strings" - "time" ) type Query struct { diff --git a/query/util.go b/query/util.go old mode 100644 new mode 100755 diff --git a/test/config.toml b/test/config.toml old mode 100644 new mode 100755 diff --git a/test/func/z_test.go b/test/func/z_test.go old mode 100644 new mode 100755 diff --git a/test/test.http b/test/test.http old mode 100644 new mode 100755 diff --git a/test/z_app_test.go b/test/z_app_test.go old mode 100644 new mode 100755 index ad9ab8f..3e945bf --- a/test/z_app_test.go +++ b/test/z_app_test.go @@ -2,13 +2,14 @@ package main import ( "context" + "time" + "github.com/glennliao/apijson-go" "github.com/glennliao/apijson-go/config" "github.com/glennliao/apijson-go/config/tables" "github.com/glennliao/apijson-go/model" "github.com/glennliao/table-sync/tablesync" "github.com/gogf/gf/v2/frame/g" - "time" ) type User struct { @@ -34,6 +35,16 @@ func init() { Alias: "User", Get: []string{"UNKNOWN"}, RowKey: "id", + FieldsGet: map[string]*config.FieldsGetValue{ + "default": { + In: nil, + Out: map[string]string{ + "id": "", + "username": "", + }, + MaxCount: nil, + }, + }, }, { Name: "user", diff --git a/test/z_main_test.go b/test/z_main_test.go old mode 100644 new mode 100755 index ccd4a20..12b5c55 --- a/test/z_main_test.go +++ b/test/z_main_test.go @@ -2,16 +2,16 @@ package main import ( "context" + "log" + "testing" + "github.com/glennliao/apijson-go" - _ "github.com/glennliao/apijson-go/drivers/config/goframe" // need import for executor with goframe - _ "github.com/glennliao/apijson-go/drivers/executor/goframe" // need import for executor with goframe - "github.com/glennliao/apijson-go/drivers/framework_goframe" + _ "github.com/glennliao/apijson-go/drivers/goframe" + "github.com/glennliao/apijson-go/drivers/goframe/web" "github.com/glennliao/apijson-go/model" _ "github.com/gogf/gf/contrib/drivers/sqlite/v2" // need import for sqlite "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" - "log" - "testing" ) var a *apijson.ApiJson @@ -25,7 +25,7 @@ func TestServer(t *testing.T) { if testing.Short() { t.Skip() } - s := framework_goframe.New(a) + s := web.New(a) s.Run() // then test in test.http } @@ -68,6 +68,28 @@ func TestQuery(t *testing.T) { } +func TestAlias(t *testing.T) { + + ctx := gctx.New() + + q := a.NewQuery(ctx, model.Map{ + "User[]": model.Map{ + "@column": "id,password:username", + //"userId": "123", + }, + }) + + q.NoAccessVerify = false + + result, err := q.Result() + + if err != nil { + log.Fatalf("%+v", err) + } + + g.Dump(result) +} + func BenchmarkName(b *testing.B) { for i := 0; i < b.N; i++ { ctx := context.Background() diff --git a/util/conv.go b/util/conv.go old mode 100644 new mode 100755 diff --git a/util/functions.go b/util/functions.go old mode 100644 new mode 100755 diff --git a/util/node.go b/util/node.go old mode 100644 new mode 100755