refactor(response): 重构API响应结构体并优化配置管理
- 将Api结构体重命名为api并改为小写首字母 - 为Success、Error函数及SetMsg、SetData、SetCode、End方法添加详细注释 - 更新JSON字段名称从Json到JSON以符合Go命名规范 - 为配置相关函数SetDefault、LoadConfigFromFile、SetConfigValue等添加注释 - 在IDEA配置文件中更新结构体映射关系和最后修改时间戳 - 为配置结构体BaseConfig、ServerConfig等添加字段说明注释 - 将验证函数中的Bind方法替换为ShouldBind方法提升性能 - 为中间件ErrorHandler和CORSMiddleware函数添加功能注释main
parent
60f0d8053d
commit
7304f59b61
|
|
@ -3,13 +3,6 @@
|
||||||
<component name="GoORMHelperCache">
|
<component name="GoORMHelperCache">
|
||||||
<option name="schemaMapping">
|
<option name="schemaMapping">
|
||||||
<map>
|
<map>
|
||||||
<entry key="Api">
|
|
||||||
<value>
|
|
||||||
<set>
|
|
||||||
<option value="file://$PROJECT_DIR$/response/code.go" />
|
|
||||||
</set>
|
|
||||||
</value>
|
|
||||||
</entry>
|
|
||||||
<entry key="BaseConfig">
|
<entry key="BaseConfig">
|
||||||
<value>
|
<value>
|
||||||
<set>
|
<set>
|
||||||
|
|
@ -91,7 +84,6 @@
|
||||||
<value>
|
<value>
|
||||||
<set>
|
<set>
|
||||||
<option value="file://$PROJECT_DIR$/../gin_test/api/new.go" />
|
<option value="file://$PROJECT_DIR$/../gin_test/api/new.go" />
|
||||||
<option value="file://$PROJECT_DIR$/../gin_test/req/new.go" />
|
|
||||||
</set>
|
</set>
|
||||||
</value>
|
</value>
|
||||||
</entry>
|
</entry>
|
||||||
|
|
@ -99,7 +91,6 @@
|
||||||
<value>
|
<value>
|
||||||
<set>
|
<set>
|
||||||
<option value="file://$PROJECT_DIR$/../gin_test/api/new.go" />
|
<option value="file://$PROJECT_DIR$/../gin_test/api/new.go" />
|
||||||
<option value="file://$PROJECT_DIR$/../gin_test/req/new.go" />
|
|
||||||
</set>
|
</set>
|
||||||
</value>
|
</value>
|
||||||
</entry>
|
</entry>
|
||||||
|
|
@ -145,6 +136,13 @@
|
||||||
</set>
|
</set>
|
||||||
</value>
|
</value>
|
||||||
</entry>
|
</entry>
|
||||||
|
<entry key="api">
|
||||||
|
<value>
|
||||||
|
<set>
|
||||||
|
<option value="file://$PROJECT_DIR$/response/code.go" />
|
||||||
|
</set>
|
||||||
|
</value>
|
||||||
|
</entry>
|
||||||
<entry key="response">
|
<entry key="response">
|
||||||
<value>
|
<value>
|
||||||
<set>
|
<set>
|
||||||
|
|
@ -159,7 +157,7 @@
|
||||||
<entry key="file://$PROJECT_DIR$/config/fun.go">
|
<entry key="file://$PROJECT_DIR$/config/fun.go">
|
||||||
<value>
|
<value>
|
||||||
<ScannedPath>
|
<ScannedPath>
|
||||||
<option name="lastModified" value="1770100625919" />
|
<option name="lastModified" value="1770427966184" />
|
||||||
</ScannedPath>
|
</ScannedPath>
|
||||||
</value>
|
</value>
|
||||||
</entry>
|
</entry>
|
||||||
|
|
@ -173,7 +171,7 @@
|
||||||
<entry key="file://$PROJECT_DIR$/config/structs.go">
|
<entry key="file://$PROJECT_DIR$/config/structs.go">
|
||||||
<value>
|
<value>
|
||||||
<ScannedPath>
|
<ScannedPath>
|
||||||
<option name="lastModified" value="1770022381063" />
|
<option name="lastModified" value="1770444054990" />
|
||||||
<option name="schema">
|
<option name="schema">
|
||||||
<list>
|
<list>
|
||||||
<option value="BaseConfig" />
|
<option value="BaseConfig" />
|
||||||
|
|
@ -188,7 +186,7 @@
|
||||||
<entry key="file://$PROJECT_DIR$/curd/curd.go">
|
<entry key="file://$PROJECT_DIR$/curd/curd.go">
|
||||||
<value>
|
<value>
|
||||||
<ScannedPath>
|
<ScannedPath>
|
||||||
<option name="lastModified" value="1770102564408" />
|
<option name="lastModified" value="1770362565302" />
|
||||||
<option name="schema">
|
<option name="schema">
|
||||||
<list>
|
<list>
|
||||||
<option value="Paginate" />
|
<option value="Paginate" />
|
||||||
|
|
@ -236,14 +234,14 @@
|
||||||
<entry key="file://$PROJECT_DIR$/main.go">
|
<entry key="file://$PROJECT_DIR$/main.go">
|
||||||
<value>
|
<value>
|
||||||
<ScannedPath>
|
<ScannedPath>
|
||||||
<option name="lastModified" value="1770025551953" />
|
<option name="lastModified" value="1770362565303" />
|
||||||
</ScannedPath>
|
</ScannedPath>
|
||||||
</value>
|
</value>
|
||||||
</entry>
|
</entry>
|
||||||
<entry key="file://$PROJECT_DIR$/middleware/middleware.go">
|
<entry key="file://$PROJECT_DIR$/middleware/middleware.go">
|
||||||
<value>
|
<value>
|
||||||
<ScannedPath>
|
<ScannedPath>
|
||||||
<option name="lastModified" value="1770108038945" />
|
<option name="lastModified" value="1770444203947" />
|
||||||
</ScannedPath>
|
</ScannedPath>
|
||||||
</value>
|
</value>
|
||||||
</entry>
|
</entry>
|
||||||
|
|
@ -262,11 +260,11 @@
|
||||||
<entry key="file://$PROJECT_DIR$/response/code.go">
|
<entry key="file://$PROJECT_DIR$/response/code.go">
|
||||||
<value>
|
<value>
|
||||||
<ScannedPath>
|
<ScannedPath>
|
||||||
<option name="lastModified" value="1770014431840" />
|
<option name="lastModified" value="1770444657704" />
|
||||||
<option name="schema">
|
<option name="schema">
|
||||||
<list>
|
<list>
|
||||||
<option value="response" />
|
<option value="response" />
|
||||||
<option value="Api" />
|
<option value="api" />
|
||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</ScannedPath>
|
</ScannedPath>
|
||||||
|
|
@ -275,7 +273,7 @@
|
||||||
<entry key="file://$PROJECT_DIR$/server/server.go">
|
<entry key="file://$PROJECT_DIR$/server/server.go">
|
||||||
<value>
|
<value>
|
||||||
<ScannedPath>
|
<ScannedPath>
|
||||||
<option name="lastModified" value="1770026239951" />
|
<option name="lastModified" value="1770362565303" />
|
||||||
</ScannedPath>
|
</ScannedPath>
|
||||||
</value>
|
</value>
|
||||||
</entry>
|
</entry>
|
||||||
|
|
@ -342,7 +340,7 @@
|
||||||
<entry key="file://$PROJECT_DIR$/valid/valid.go">
|
<entry key="file://$PROJECT_DIR$/valid/valid.go">
|
||||||
<value>
|
<value>
|
||||||
<ScannedPath>
|
<ScannedPath>
|
||||||
<option name="lastModified" value="1770197737203" />
|
<option name="lastModified" value="1772004278917" />
|
||||||
</ScannedPath>
|
</ScannedPath>
|
||||||
</value>
|
</value>
|
||||||
</entry>
|
</entry>
|
||||||
|
|
@ -368,17 +366,10 @@
|
||||||
</ScannedPath>
|
</ScannedPath>
|
||||||
</value>
|
</value>
|
||||||
</entry>
|
</entry>
|
||||||
<entry key="file://$PROJECT_DIR$/../gin_test/api/home.go">
|
|
||||||
<value>
|
|
||||||
<ScannedPath>
|
|
||||||
<option name="lastModified" value="1770190095707" />
|
|
||||||
</ScannedPath>
|
|
||||||
</value>
|
|
||||||
</entry>
|
|
||||||
<entry key="file://$PROJECT_DIR$/../gin_test/api/new.go">
|
<entry key="file://$PROJECT_DIR$/../gin_test/api/new.go">
|
||||||
<value>
|
<value>
|
||||||
<ScannedPath>
|
<ScannedPath>
|
||||||
<option name="lastModified" value="1770191206242" />
|
<option name="lastModified" value="1772003979851" />
|
||||||
<option name="schema">
|
<option name="schema">
|
||||||
<list>
|
<list>
|
||||||
<option value="NewsOne" />
|
<option value="NewsOne" />
|
||||||
|
|
@ -388,44 +379,17 @@
|
||||||
</ScannedPath>
|
</ScannedPath>
|
||||||
</value>
|
</value>
|
||||||
</entry>
|
</entry>
|
||||||
<entry key="file://$PROJECT_DIR$/../gin_test/controller/home/houeRouter.go">
|
<entry key="file://$PROJECT_DIR$/../gin_test/router/homeRouter.go">
|
||||||
<value>
|
<value>
|
||||||
<ScannedPath>
|
<ScannedPath>
|
||||||
<option name="lastModified" value="1770191001042" />
|
<option name="lastModified" value="1772003574712" />
|
||||||
</ScannedPath>
|
|
||||||
</value>
|
|
||||||
</entry>
|
|
||||||
<entry key="file://$PROJECT_DIR$/../gin_test/controller/home/index.go">
|
|
||||||
<value>
|
|
||||||
<ScannedPath>
|
|
||||||
<option name="lastModified" value="1770255253603" />
|
|
||||||
</ScannedPath>
|
|
||||||
</value>
|
|
||||||
</entry>
|
|
||||||
<entry key="file://$PROJECT_DIR$/../gin_test/controller/home/router.go">
|
|
||||||
<value>
|
|
||||||
<ScannedPath>
|
|
||||||
<option name="lastModified" value="1770190557848" />
|
|
||||||
</ScannedPath>
|
|
||||||
</value>
|
|
||||||
</entry>
|
|
||||||
<entry key="file://$PROJECT_DIR$/../gin_test/req/new.go">
|
|
||||||
<value>
|
|
||||||
<ScannedPath>
|
|
||||||
<option name="lastModified" value="1770186504009" />
|
|
||||||
<option name="schema">
|
|
||||||
<list>
|
|
||||||
<option value="NewsOne" />
|
|
||||||
<option value="NewsSave" />
|
|
||||||
</list>
|
|
||||||
</option>
|
|
||||||
</ScannedPath>
|
</ScannedPath>
|
||||||
</value>
|
</value>
|
||||||
</entry>
|
</entry>
|
||||||
<entry key="file://$PROJECT_DIR$/../gin_test/router/router.go">
|
<entry key="file://$PROJECT_DIR$/../gin_test/router/router.go">
|
||||||
<value>
|
<value>
|
||||||
<ScannedPath>
|
<ScannedPath>
|
||||||
<option name="lastModified" value="1770191024510" />
|
<option name="lastModified" value="1772003574719" />
|
||||||
</ScannedPath>
|
</ScannedPath>
|
||||||
</value>
|
</value>
|
||||||
</entry>
|
</entry>
|
||||||
|
|
@ -433,7 +397,7 @@
|
||||||
</option>
|
</option>
|
||||||
<option name="tableStructMapping">
|
<option name="tableStructMapping">
|
||||||
<map>
|
<map>
|
||||||
<entry key="api" value="Api" />
|
<entry key="api" value="api" />
|
||||||
<entry key="base_config" value="BaseConfig" />
|
<entry key="base_config" value="BaseConfig" />
|
||||||
<entry key="client" value="Client" />
|
<entry key="client" value="Client" />
|
||||||
<entry key="config" value="Config" />
|
<entry key="config" value="Config" />
|
||||||
|
|
@ -456,6 +420,6 @@
|
||||||
<entry key="tcp_server" value="TCPServer" />
|
<entry key="tcp_server" value="TCPServer" />
|
||||||
</map>
|
</map>
|
||||||
</option>
|
</option>
|
||||||
<option name="lastTimeChecked" value="1770185677345" />
|
<option name="lastTimeChecked" value="1772003052939" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
|
|
@ -54,6 +54,7 @@ func init() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetDefault 设置默认配置信息
|
||||||
func SetDefault() {
|
func SetDefault() {
|
||||||
viper.Set("SERVER.addr", "127.0.0.1:8080")
|
viper.Set("SERVER.addr", "127.0.0.1:8080")
|
||||||
viper.Set("SERVER.mode", "release")
|
viper.Set("SERVER.mode", "release")
|
||||||
|
|
@ -63,6 +64,7 @@ func SetDefault() {
|
||||||
viper.Set("JWT.expire", 86400)
|
viper.Set("JWT.expire", 86400)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadConfigFromFile 在配置文件中加载配置
|
||||||
func LoadConfigFromFile() error {
|
func LoadConfigFromFile() error {
|
||||||
err := viper.ReadInConfig()
|
err := viper.ReadInConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -71,6 +73,7 @@ func LoadConfigFromFile() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetConfigValue 设置指定配置文件的值
|
||||||
func SetConfigValue(key string, value any) error {
|
func SetConfigValue(key string, value any) error {
|
||||||
viper.SetDefault(key, value)
|
viper.SetDefault(key, value)
|
||||||
err := viper.WriteConfig()
|
err := viper.WriteConfig()
|
||||||
|
|
@ -80,6 +83,7 @@ func SetConfigValue(key string, value any) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetConfigMap 使用Map的方式添加配置信息
|
||||||
func SetConfigMap(value map[string]any) error {
|
func SetConfigMap(value map[string]any) error {
|
||||||
if len(value) == 0 {
|
if len(value) == 0 {
|
||||||
log.Error("value is empty")
|
log.Error("value is empty")
|
||||||
|
|
@ -95,6 +99,7 @@ func SetConfigMap(value map[string]any) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetConfigValue 获取指定配置的值
|
||||||
func GetConfigValue(key string, def ...any) *gvar.Var {
|
func GetConfigValue(key string, def ...any) *gvar.Var {
|
||||||
value := gvar.New(viper.Get(key))
|
value := gvar.New(viper.Get(key))
|
||||||
if value.IsEmpty() && len(def) > 0 {
|
if value.IsEmpty() && len(def) > 0 {
|
||||||
|
|
@ -102,12 +107,15 @@ func GetConfigValue(key string, def ...any) *gvar.Var {
|
||||||
}
|
}
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unmarshal 将配置解析成指定的对象
|
||||||
func Unmarshal[T any]() (*T, error) {
|
func Unmarshal[T any]() (*T, error) {
|
||||||
var s T
|
var s T
|
||||||
err := viper.Unmarshal(&s)
|
err := viper.Unmarshal(&s)
|
||||||
return &s, err
|
return &s, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAllConfig 获取所有配置信息并返回Map
|
||||||
func GetAllConfig() map[string]any {
|
func GetAllConfig() map[string]any {
|
||||||
return viper.AllSettings()
|
return viper.AllSettings()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,25 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
|
// BaseConfig 基础配置信息
|
||||||
type BaseConfig struct {
|
type BaseConfig struct {
|
||||||
Server ServerConfig `mapstructure:"SERVER"`
|
Server ServerConfig `mapstructure:"SERVER"`
|
||||||
Database DataBaseConfig `mapstructure:"DATABASE"`
|
Database DataBaseConfig `mapstructure:"DATABASE"`
|
||||||
Jwt JwtConfig `mapstructure:"JWT"`
|
Jwt JwtConfig `mapstructure:"JWT"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServerConfig 服务配置
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
Addr string `mapstructure:"addr"`
|
Addr string `mapstructure:"addr"`
|
||||||
Mode string `mapstructure:"mode"`
|
Mode string `mapstructure:"mode"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DataBaseConfig 数据库配置
|
||||||
type DataBaseConfig struct {
|
type DataBaseConfig struct {
|
||||||
Dns string `mapstructure:"dns"`
|
Dns string `mapstructure:"dns"`
|
||||||
Type string `mapstructure:"type"`
|
Type string `mapstructure:"type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// JwtConfig JWT配置
|
||||||
type JwtConfig struct {
|
type JwtConfig struct {
|
||||||
Secret string `mapstructure:"secret"`
|
Secret string `mapstructure:"secret"`
|
||||||
Expire int64 `mapstructure:"expire"`
|
Expire int64 `mapstructure:"expire"`
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ErrorHandler 全局异常处理中间件
|
||||||
func ErrorHandler() gin.HandlerFunc {
|
func ErrorHandler() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|
@ -29,6 +30,8 @@ func ErrorHandler() gin.HandlerFunc {
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CORSMiddleware 跨域中间件
|
||||||
func CORSMiddleware() gin.HandlerFunc {
|
func CORSMiddleware() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
|
@ -40,7 +43,6 @@ func CORSMiddleware() gin.HandlerFunc {
|
||||||
c.AbortWithStatus(204)
|
c.AbortWithStatus(204)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,45 +13,71 @@ type response struct {
|
||||||
Timestamp int64 `json:"timestamp"`
|
Timestamp int64 `json:"timestamp"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Api struct {
|
// api 返回结构体
|
||||||
Json *response
|
type api struct {
|
||||||
|
JSON *response
|
||||||
c *gin.Context
|
c *gin.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func Success(c *gin.Context) *Api {
|
// Success 函数用于创建一个包含成功响应的 api 对象。
|
||||||
|
// 参数 c 是 Gin 框架的上下文对象,用于处理请求和响应。
|
||||||
|
// 返回值是一个指向 API 结构体的指针,其中包含 JSON 响应数据。
|
||||||
|
func Success(c *gin.Context) *api {
|
||||||
|
// 创建响应结构体,包含状态码和时间戳
|
||||||
|
json := &response{
|
||||||
|
Code: 200, // 成功的状态码
|
||||||
|
Timestamp: time.Now().Unix(), // 当前时间的时间戳
|
||||||
|
}
|
||||||
|
// 创建 api 结构体实例,并将响应数据和上下文对象赋值给它
|
||||||
|
r := &api{
|
||||||
|
JSON: json, // JSON 响应数据
|
||||||
|
c: c, // Gin 上下文对象
|
||||||
|
}
|
||||||
|
return r // 返回 api 对象的指针
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error 函数用于创建一个包含错误响应的 api 对象。
|
||||||
|
// 参数 c 是 Gin 框架的上下文对象,用于处理请求和响应。
|
||||||
|
// 返回值是一个指向 api 结构体的指针,其中包含 JSON 响应数据。
|
||||||
|
func Error(c *gin.Context) *api {
|
||||||
json := &response{
|
json := &response{
|
||||||
Code: 200,
|
Code: 200,
|
||||||
Timestamp: time.Now().Unix(),
|
Timestamp: time.Now().Unix(),
|
||||||
}
|
}
|
||||||
r := &Api{
|
r := &api{
|
||||||
Json: json,
|
JSON: json,
|
||||||
c: c,
|
c: c,
|
||||||
}
|
}
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
func Error(c *gin.Context) *Api {
|
|
||||||
json := &response{
|
// SetMsg 方法用于设置 api 对象的 JSON 响应数据中的 Msg 字段。
|
||||||
Code: 200,
|
// 参数 msg 是要设置的消息字符串。
|
||||||
Timestamp: time.Now().Unix(),
|
// 返回值是指向 api 结构体的指针,用于链式调用。
|
||||||
}
|
func (r *api) SetMsg(msg string) *api {
|
||||||
r := &Api{
|
r.JSON.Msg = msg
|
||||||
Json: json,
|
|
||||||
c: c,
|
|
||||||
}
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
func (r *Api) SetMsg(msg string) *Api {
|
|
||||||
r.Json.Msg = msg
|
// SetData 方法用于设置 api 对象的 JSON 响应数据中的 Data 字段。
|
||||||
|
// 参数 data 是要设置的数据,类型为 any(任意类型)。
|
||||||
|
// 返回值是指向 api 结构体的指针,用于链式调用。
|
||||||
|
func (r *api) SetData(data any) *api {
|
||||||
|
r.JSON.Data = data
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
func (r *Api) SetData(data any) *Api {
|
|
||||||
r.Json.Data = data
|
// SetCode 方法用于设置 api 对象的 JSON 响应数据中的 Code 字段。
|
||||||
|
// 参数 code 是要设置的状态码,类型为 int。
|
||||||
|
// 返回值是指向 api 结构体的指针,用于链式调用。
|
||||||
|
func (r *api) SetCode(code int) *api {
|
||||||
|
r.JSON.Code = code
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
func (r *Api) SetCode(code int) *Api {
|
|
||||||
r.Json.Code = code
|
// End 方法用于将 api 对象的 JSON 响应数据发送给客户端。
|
||||||
return r
|
// 它使用 Gin 上下文对象的 JSON 方法将 JSON 响应数据发送给客户端。
|
||||||
}
|
// 参数 r 是指向 api 结构体的指针,包含 JSON 响应数据和 Gin 上下文对象。
|
||||||
func (r *Api) End() {
|
func (r *api) End() {
|
||||||
r.c.JSON(r.Json.Code, r.Json)
|
r.c.JSON(r.JSON.Code, r.JSON)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import (
|
||||||
// ValidToStruct 验证参数并返回结构体
|
// ValidToStruct 验证参数并返回结构体
|
||||||
func ValidToStruct[T any](c *gin.Context) (object *T) {
|
func ValidToStruct[T any](c *gin.Context) (object *T) {
|
||||||
obj := new(T)
|
obj := new(T)
|
||||||
if err := c.Bind(obj); err != nil {
|
if err := c.ShouldBind(obj); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if err := g.Validator().Data(obj).Run(c); err != nil {
|
if err := g.Validator().Data(obj).Run(c); err != nil {
|
||||||
|
|
@ -22,7 +22,7 @@ func ValidToStruct[T any](c *gin.Context) (object *T) {
|
||||||
// ValidToMap 验证参数并返回结构体
|
// ValidToMap 验证参数并返回结构体
|
||||||
func ValidToMap[T any](c *gin.Context) (object map[string]any) {
|
func ValidToMap[T any](c *gin.Context) (object map[string]any) {
|
||||||
obj := new(T)
|
obj := new(T)
|
||||||
if err := c.Bind(obj); err != nil {
|
if err := c.ShouldBind(obj); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if err := g.Validator().Data(obj).Run(c); err != nil {
|
if err := g.Validator().Data(obj).Run(c); err != nil {
|
||||||
|
|
@ -34,7 +34,7 @@ func ValidToMap[T any](c *gin.Context) (object map[string]any) {
|
||||||
// ValidToStructAndMap 验证参数并返回map
|
// ValidToStructAndMap 验证参数并返回map
|
||||||
func ValidToStructAndMap[T any](c *gin.Context) (stru *T, object map[string]any) {
|
func ValidToStructAndMap[T any](c *gin.Context) (stru *T, object map[string]any) {
|
||||||
obj := new(T)
|
obj := new(T)
|
||||||
if err := c.Bind(obj); err != nil {
|
if err := c.ShouldBind(obj); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if err := g.Validator().Data(obj).Run(c); err != nil {
|
if err := g.Validator().Data(obj).Run(c); err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue