update create model

This commit is contained in:
jagger 2025-03-21 15:04:27 +08:00
parent 7bd6650118
commit b3addd77a2
3 changed files with 65 additions and 0 deletions

View File

@ -48,6 +48,11 @@ func CreateModel(ctx *gin.Context) {
"name": param.Name,
"version": param.Version,
"modelSelectedFile": param.ModelSelectedFile,
"label": param.Label,
"engine": param.Engine,
"license": param.License,
"isPrivate": strconv.FormatBool(param.IsPrivate),
"description": param.Description,
}).
SetResult(&resp).
Post(reqUrl)

View File

@ -3,9 +3,11 @@ package common
import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/go-resty/resty/v2"
"io"
"mime/multipart"
"reflect"
"strconv"
"time"
)
@ -31,3 +33,56 @@ func GetFileMd5(file multipart.File) (string, error) {
func Bool2String(b bool) string {
return strconv.FormatBool(b)
}
// StructToMapWithTag 将结构体转换为 map[string]stringkey 使用指定标签的值
func StructToMapWithTag(obj interface{}, tagName string) (map[string]string, error) {
result := make(map[string]string)
// 获取值的反射对象
val := reflect.ValueOf(obj)
if val.Kind() == reflect.Ptr {
val = val.Elem() // 解引用指针
}
if val.Kind() != reflect.Struct {
return nil, fmt.Errorf("input is not a struct or pointer to struct")
}
// 获取类型信息
typ := val.Type()
// 遍历结构体字段
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
fieldType := typ.Field(i)
// 获取字段的标签值
tagValue := fieldType.Tag.Get(tagName)
if tagValue == "" {
continue // 如果标签不存在,跳过该字段
}
// 获取字段值并转换为字符串
var fieldValue string
switch field.Kind() {
case reflect.String:
fieldValue = field.String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fieldValue = strconv.FormatInt(field.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
fieldValue = strconv.FormatUint(field.Uint(), 10)
case reflect.Float32, reflect.Float64:
fieldValue = strconv.FormatFloat(field.Float(), 'f', -1, 64)
case reflect.Bool:
fieldValue = strconv.FormatBool(field.Bool())
default:
// 如果字段类型不支持,跳过
continue
}
// 将标签值和字段值存入 map
result[tagValue] = fieldValue
}
return result, nil
}

View File

@ -7,6 +7,11 @@ type CreateModelParam struct {
Name string `json:"name"`
Version string `json:"version"`
ModelSelectedFile string `json:"modelSelectedFile"`
Label string `json:"label"`
Engine string `json:"engine"`
License string `json:"license"`
IsPrivate bool `json:"isPrivate"`
Description string `json:"description"`
}
type CreateModel struct {