hi-server/pkg/tool/sliceReflectToStruct.go
shanshanzhong c582087c0f refactor: 更新项目引用路径从perfect-panel/ppanel-server到perfect-panel/server
feat: 添加版本和构建时间变量
fix: 修正短信队列类型注释错误
style: 清理未使用的代码和测试文件
docs: 更新安装文档中的下载链接
chore: 迁移数据库脚本添加日志和订阅配置
2025-10-13 01:33:03 -07:00

47 lines
1.1 KiB
Go

package tool
import (
"reflect"
"strconv"
"github.com/goccy/go-json"
"github.com/perfect-panel/server/internal/model/system"
)
func SystemConfigSliceReflectToStruct(slice []*system.System, structType any) {
v := reflect.ValueOf(structType).Elem()
for _, config := range slice {
field := v.FieldByName(config.Key)
if field.Kind() == reflect.Ptr && field.Type().Elem().Kind() == reflect.Bool {
if config.Value == "" {
field.Set(reflect.Zero(field.Type()))
} else {
boolValue, _ := strconv.ParseBool(config.Value)
field.Set(reflect.ValueOf(&boolValue))
}
continue
}
if field.IsValid() && field.CanSet() {
switch config.Type {
case "string":
field.SetString(config.Value)
case "bool":
boolValue, _ := strconv.ParseBool(config.Value)
field.SetBool(boolValue)
case "int":
intValue, _ := strconv.Atoi(config.Value)
field.SetInt(int64(intValue))
case "int64":
intValue, _ := strconv.ParseInt(config.Value, 10, 64)
field.SetInt(intValue)
case "interface":
_ = json.Unmarshal([]byte(config.Value), field.Addr().Interface())
default:
break
}
}
}
}