系统常规设置完成

This commit is contained in:
Liujian
2024-10-17 12:00:19 +08:00
parent e1c7c16d91
commit 8ba0d49fb3
12 changed files with 256 additions and 11 deletions
+40
View File
@@ -0,0 +1,40 @@
package system_dto
import (
"net/url"
"reflect"
)
type InputSetting struct {
InvokeAddress string `json:"invoke_address" key:"system.node.invoke_address"`
}
func (i *InputSetting) Validate() error {
_, err := url.Parse(i.InvokeAddress)
if err != nil {
return err
}
return nil
}
func ToKeyMap(i interface{}) map[string]string {
result := make(map[string]string)
val := reflect.ValueOf(i)
typ := reflect.TypeOf(i)
if typ.Kind() == reflect.Ptr {
val = val.Elem()
typ = typ.Elem()
}
switch typ.Kind() {
case reflect.Struct:
{
for i := 0; i < typ.NumField(); i++ {
f := typ.Field(i)
if f.Tag.Get("key") != "" {
result[f.Tag.Get("key")] = val.Field(i).String()
}
}
}
}
return result
}
+18
View File
@@ -0,0 +1,18 @@
package system_dto
import (
"log"
"testing"
)
func TestMap(t *testing.T) {
input := &InputSetting{
InvokeAddress: "http://127.0.0.1:8080",
}
err := input.Validate()
if err != nil {
t.Error(err)
}
log.Println(ToKeyMap(input))
}
+63
View File
@@ -0,0 +1,63 @@
package system_dto
import (
"reflect"
"strconv"
)
type Setting struct {
InvokeAddress string `json:"invoke_address" key:"system.node.invoke_address"`
}
func MapStringToStruct[T any](m map[string]string) *T {
var result T
val := reflect.ValueOf(&result).Elem()
// 获取结构体的类型
t := val.Type()
// 查找结构体中与键名匹配的字段
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
key := field.Tag.Get("key")
if key == "" {
continue
}
v, ok := m[key]
if !ok {
continue
}
// 获取字段的值
fieldVal := val.Field(i)
if !fieldVal.CanSet() {
continue
}
fieldVal.SetString(v)
// 如果字段不可设置,跳过
if !fieldVal.CanSet() {
continue
}
// 根据字段的类型,进行类型转换
switch fieldVal.Kind() {
case reflect.Float64:
// 如果是 string 类型且非空,转换为 float64
if floatVal, err := strconv.ParseFloat(v, 64); err == nil {
fieldVal.SetFloat(floatVal)
}
case reflect.Int:
if intVal, err := strconv.Atoi(v); err == nil {
fieldVal.SetInt(int64(intVal))
}
case reflect.String:
fieldVal.SetString(v)
default:
// 其他类型不进行转换
}
}
return &result
}
+48
View File
@@ -0,0 +1,48 @@
package system
import (
"context"
"github.com/eolinker/go-common/store"
"github.com/eolinker/go-common/utils"
system_dto "github.com/APIParkLab/APIPark/module/system/dto"
"github.com/APIParkLab/APIPark/service/setting"
)
var (
_ ISettingModule = (*imlSettingModule)(nil)
)
type imlSettingModule struct {
settingService setting.ISettingService `autowired:""`
transaction store.ITransaction `autowired:""`
}
func (i *imlSettingModule) Get(ctx context.Context) *system_dto.Setting {
v, err := i.settingService.All(ctx)
if err != nil {
return &system_dto.Setting{}
}
return system_dto.MapStringToStruct[system_dto.Setting](v)
}
func (i *imlSettingModule) Set(ctx context.Context, input *system_dto.InputSetting) error {
err := input.Validate()
if err != nil {
return err
}
return i.transaction.Transaction(ctx, func(ctx context.Context) error {
keyMap := system_dto.ToKeyMap(input)
userId := utils.UserId(ctx)
for k, v := range keyMap {
err = i.settingService.Set(ctx, k, v, userId)
if err != nil {
return err
}
}
return nil
})
}
+20 -1
View File
@@ -1,7 +1,26 @@
package system
import "context"
import (
"context"
"reflect"
"github.com/eolinker/go-common/autowire"
system_dto "github.com/APIParkLab/APIPark/module/system/dto"
)
type IExportModule[T any] interface {
ExportAll(ctx context.Context) ([]*T, error)
}
type ISettingModule interface {
Get(ctx context.Context) *system_dto.Setting
Set(ctx context.Context, input *system_dto.InputSetting) error
}
func init() {
autowire.Auto[ISettingModule](func() reflect.Value {
return reflect.ValueOf(new(imlSettingModule))
})
}