修复键和值一样的情况会出现没有数据的情况

This commit is contained in:
Yavolte 2025-07-29 16:09:19 +08:00
parent b9c8112a7c
commit 0e4226fd84
2 changed files with 8 additions and 14 deletions

View File

@ -2,6 +2,7 @@ package rest
import ( import (
"context" "context"
"git.nobla.cn/golang/rest/types" "git.nobla.cn/golang/rest/types"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -133,7 +134,6 @@ func (hook *hookManager) afterSave(ctx context.Context, tx *gorm.DB, model any,
cb(ctx, tx, model, diff) cb(ctx, tx, model, diff)
} }
} }
return
} }
func (hook *hookManager) beforeDelete(ctx context.Context, tx *gorm.DB, model any) (err error) { func (hook *hookManager) beforeDelete(ctx context.Context, tx *gorm.DB, model any) (err error) {
@ -161,7 +161,6 @@ func (hook *hookManager) afterDelete(ctx context.Context, tx *gorm.DB, model any
cb(ctx, tx, model) cb(ctx, tx, model)
} }
} }
return
} }
func (hook *hookManager) afterExport(ctx context.Context, filename string) { func (hook *hookManager) afterExport(ctx context.Context, filename string) {
@ -174,7 +173,6 @@ func (hook *hookManager) afterExport(ctx context.Context, filename string) {
cb(ctx, filename) cb(ctx, filename)
} }
} }
return
} }
func (hook *hookManager) afterImport(ctx context.Context, ret *types.ImportResult) { func (hook *hookManager) afterImport(ctx context.Context, ret *types.ImportResult) {
@ -187,7 +185,6 @@ func (hook *hookManager) afterImport(ctx context.Context, ret *types.ImportResul
cb(ctx, ret) cb(ctx, ret)
} }
} }
return
} }
func (hook *hookManager) BeforeCreate(cb BeforeCreate) { func (hook *hookManager) BeforeCreate(cb BeforeCreate) {

17
rest.go
View File

@ -673,13 +673,12 @@ func ModelTypes[T any](ctx context.Context, db *gorm.DB, model any, domainName,
} else { } else {
feed.Label = fmt.Sprint(s) feed.Label = fmt.Sprint(s)
} }
continue //这里不直接返回, 有可能key和value是同一个字段
} }
if k == valueColumn { if k == valueColumn {
if p, ok := v.(T); ok { if p, ok := v.(T); ok {
feed.Value = p feed.Value = p
} }
continue
} }
} }
values = append(values, feed) values = append(values, feed)
@ -703,25 +702,23 @@ func ModelTiers[T comparable](ctx context.Context, db *gorm.DB, model any, domai
for _, pairs := range result { for _, pairs := range result {
feed := &types.TierValue[T]{} feed := &types.TierValue[T]{}
for k, v := range pairs { for k, v := range pairs {
if k == parentColumn {
if p, ok := v.(T); ok {
feed.Parent = p
}
continue
}
if k == labelColumn { if k == labelColumn {
if s, ok := v.(string); ok { if s, ok := v.(string); ok {
feed.Label = s feed.Label = s
} else { } else {
feed.Label = fmt.Sprint(s) feed.Label = fmt.Sprint(s)
} }
continue
} }
if k == valueColumn { if k == valueColumn {
if p, ok := v.(T); ok { if p, ok := v.(T); ok {
feed.Value = p feed.Value = p
} }
continue
}
if k == parentColumn {
if p, ok := v.(T); ok {
feed.Parent = p
}
continue
} }
} }
values = append(values, feed) values = append(values, feed)