add GetStringSlice

This commit is contained in:
Havoc412 2024-10-20 11:45:26 +08:00
parent 81729bc718
commit 20962f00b6
4 changed files with 41 additions and 0 deletions

View File

@ -1,9 +1,11 @@
package data_transfer
import (
"catface/app/global/consts"
"catface/app/global/variable"
"catface/app/http/validator/core/interf"
"encoding/json"
"reflect"
"time"
"github.com/gin-gonic/gin"
@ -36,3 +38,38 @@ func DataAddContext(validatorInterface interf.ValidatorInterface, extraAddDataPr
}
return nil
}
// getSlice 是一个通用的辅助函数,用于从 context 中获取切片。
func getSlice(context *gin.Context, ValidatorPrefix string, key string, elemType reflect.Type) interface{} {
if val, ok := context.Get(ValidatorPrefix + key); ok && val != nil {
if slice, ok := val.([]interface{}); ok {
result := reflect.MakeSlice(reflect.SliceOf(elemType), 0, len(slice))
for _, item := range slice {
if reflect.TypeOf(item) == elemType {
result = reflect.Append(result, reflect.ValueOf(item))
}
}
return result.Interface()
}
}
return nil
}
// GetStringSlice 从 context 中获取字符串切片。
func GetStringSlice(context *gin.Context, key string) (ss []string) {
if val := getSlice(context, consts.ValidatorPrefix, key, reflect.TypeOf("")); val != nil {
ss = val.([]string)
}
// INFO 同时重新装载。
context.Set(consts.ValidatorPrefix+key, ss)
return
}
// GetIntSlice 从 context 中获取整数切片。
func GetIntSlice(context *gin.Context, key string) (ss []int) {
if val := getSlice(context, consts.ValidatorPrefix, key, reflect.TypeOf(0)); val != nil {
ss = val.([]int)
}
context.Set(consts.ValidatorPrefix+key, ss)
return
}

View File

@ -0,0 +1 @@
package upload_file

View File

@ -69,6 +69,7 @@ FileUploadSetting:
- "text/plain; charset=utf-8" #txt log json等文本文件
# - "video/mp4" #视频文件例如mp4
# - "audio/mpeg" #音频文件,例如: mp3
AvatarWidth: 200
# casbin 权限控制api接口
Casbin:

View File

@ -3,10 +3,12 @@ package routers
import (
"catface/app/global/consts"
"catface/app/global/variable"
// "catface/app/http/controller/captcha" // 验证码组件
// "catface/app/http/middleware/authorization"
"catface/app/http/middleware/cors"
validatorFactory "catface/app/http/validator/core/factory"
// TODO validatorFactory "catface/app/http/validator/core/factory"
"catface/app/utils/gin_release"
"net/http"