add generic type
This commit is contained in:
parent
01a671dff8
commit
0c1c0da166
27
rest.go
27
rest.go
|
@ -652,28 +652,39 @@ func VisibleSchemas(ctx context.Context, db *gorm.DB, domain, moduleName, tableN
|
|||
}
|
||||
|
||||
// ModelTypes 查询指定模型的类型
|
||||
func ModelTypes(ctx context.Context, db *gorm.DB, model any, domainName, labelColumn, valueColumn string) (values []*types.TypeValue) {
|
||||
func ModelTypes[T any](ctx context.Context, db *gorm.DB, model any, domainName, labelColumn, valueColumn string) (values []*types.TypeValue[T], err error) {
|
||||
tx := db.WithContext(ctx)
|
||||
result := make([]map[string]any, 0, 10)
|
||||
if domainName == "" {
|
||||
tx.Model(model).Select(labelColumn, valueColumn).Scan(&result)
|
||||
err = tx.Model(model).Select(labelColumn, valueColumn).Scan(&result).Error
|
||||
} else {
|
||||
tx.Model(model).Select(labelColumn, valueColumn).Where("domain=?", domainName).Scan(&result)
|
||||
err = tx.Model(model).Select(labelColumn, valueColumn).Where("domain=?", domainName).Scan(&result).Error
|
||||
}
|
||||
values = make([]*types.TypeValue, 0, len(result))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
values = make([]*types.TypeValue[T], 0, len(result))
|
||||
for _, pairs := range result {
|
||||
feed := &types.TypeValue{}
|
||||
feed := &types.TypeValue[T]{}
|
||||
for k, v := range pairs {
|
||||
if k == labelColumn {
|
||||
feed.Label = v
|
||||
if s, ok := v.(string); ok {
|
||||
feed.Label = s
|
||||
} else {
|
||||
feed.Label = fmt.Sprint(s)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if k == valueColumn {
|
||||
feed.Value = v
|
||||
if p, ok := v.(T); ok {
|
||||
feed.Value = p
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
values = append(values, feed)
|
||||
}
|
||||
return values
|
||||
return values, nil
|
||||
}
|
||||
|
||||
// GetFieldValue 获取模型某个字段的值
|
||||
|
|
|
@ -119,16 +119,16 @@ type (
|
|||
}
|
||||
|
||||
// TypeValue 键值对数据
|
||||
TypeValue struct {
|
||||
Label any `json:"label"`
|
||||
Value any `json:"value"`
|
||||
TypeValue[T any] struct {
|
||||
Label string `json:"label"`
|
||||
Value T `json:"value"`
|
||||
}
|
||||
|
||||
// NestedValue 层级数据
|
||||
NestedValue struct {
|
||||
Label any `json:"label"`
|
||||
Value any `json:"value"`
|
||||
Children []*NestedValue `json:"children,omitempty"`
|
||||
NestedValue[T any] struct {
|
||||
Label string `json:"label"`
|
||||
Value T `json:"value"`
|
||||
Children []*NestedValue[T] `json:"children,omitempty"`
|
||||
}
|
||||
|
||||
//ValueLookupFunc 查找域的函数
|
||||
|
|
Loading…
Reference in New Issue