approveflow/app/utils/convert.go

30 lines
555 B
Go
Raw Normal View History

2024-11-14 17:02:41 +08:00
package utils
import (
"approveflow/app/http/base"
"encoding/json"
"github.com/Superdanda/hade/framework/gin"
"net/http"
)
func QuickBind[T any](c *gin.Context) *T {
var params T
if err := c.ShouldBindJSON(&params); err != nil {
c.ISetStatus(http.StatusBadRequest).IJson(base.Fail("参数错误"))
return nil
}
return &params
}
func Convert(origin interface{}, target interface{}) error {
data, err := json.Marshal(origin)
if err != nil {
return err
}
err = json.Unmarshal(data, target)
if err != nil {
return err
}
return nil
}