fix(admin): 修复错误日志列表返回空元素
This commit is contained in:
1 parent
49eb8d07cc
commit
8a8a96a012
3 files changed
+58
-23
No files matched your search
@@ -95,6 +95,15 @@ func queryPage(ctx context.Context, table, fields, where, orderBy string, page,
|
||||
if where != "" {
|
||||
m = m.Where(where, args...)
|
||||
}
|
||||
|
||||
total, err := m.Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if total == 0 {
|
||||
return []map[string]any{}, 0, nil
|
||||
}
|
||||
|
||||
if fields != "" {
|
||||
m = m.Fields(fields)
|
||||
}
|
||||
@@ -102,16 +111,23 @@ func queryPage(ctx context.Context, table, fields, where, orderBy string, page,
|
||||
m = m.Order(orderBy)
|
||||
}
|
||||
|
||||
var items []map[string]any
|
||||
var total int
|
||||
err := m.Page(page, pageSize).ScanAndCount(&items, &total, false)
|
||||
result, err := m.Page(page, pageSize).All()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if items == nil {
|
||||
items = []map[string]any{}
|
||||
return toMapList(result), total, nil
|
||||
}
|
||||
|
||||
// toMapList converts database records to JSON-safe maps.
|
||||
func toMapList(result gdb.Result) []map[string]any {
|
||||
if len(result) == 0 {
|
||||
return []map[string]any{}
|
||||
}
|
||||
return items, total, nil
|
||||
list := make([]map[string]any, 0, len(result))
|
||||
for _, row := range result {
|
||||
list = append(list, row.Map())
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
// queryAuditPage 查询审计表分页数据,自动计数、分页、转换并批量填充关联名称。
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
)
|
||||
|
||||
func TestToMapListProducesJSONObjects(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
result := gdb.Result{
|
||||
gdb.Record{
|
||||
"id": gvar.New(9),
|
||||
"error_message": gvar.New("request failed"),
|
||||
},
|
||||
}
|
||||
|
||||
list := toMapList(result)
|
||||
if len(list) != 1 {
|
||||
t.Fatalf("toMapList() length = %d, want 1", len(list))
|
||||
}
|
||||
if list[0] == nil {
|
||||
t.Fatal("toMapList() returned a nil map")
|
||||
}
|
||||
|
||||
data, err := json.Marshal(list)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal list: %v", err)
|
||||
}
|
||||
if got, want := string(data), `[{"error_message":"request failed","id":9}]`; got != want {
|
||||
t.Fatalf("marshal list = %s, want %s", got, want)
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
|
||||
@@ -196,19 +195,3 @@ func (s *sAdmin) ChannelErrorCategories(ctx context.Context, req *v1.ChannelErro
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// toMapList 将 Result 转为 []map[string]any
|
||||
func toMapList(result gdb.Result) []map[string]any {
|
||||
if result == nil || len(result) == 0 {
|
||||
return []map[string]any{}
|
||||
}
|
||||
list := make([]map[string]any, len(result))
|
||||
for i, row := range result {
|
||||
data := make(map[string]any, len(row))
|
||||
for k, v := range row {
|
||||
data[k] = v.Val()
|
||||
}
|
||||
list[i] = data
|
||||
}
|
||||
return list
|
||||
}
|
||||
Reference in new issue
Block a user