ToyBoomServer/common/request.go
2024-09-02 18:07:30 +00:00

35 lines
672 B
Go

package common
import (
"github.com/nose7en/ToyBoomServer/defs"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
type ReqType interface {
gin.H |
defs.CommonPaginationRequest | defs.CommonQueryPaginationRequest |
defs.CommonQueryRequest | defs.CommonRequest
}
func GetProtoRequest[T ReqType](c *gin.Context) (r *T, err error) {
r = new(T)
if c.Request.ContentLength == 0 {
return r, nil
}
if c.ContentType() == "application/x-protobuf" {
err = c.Copy().ShouldBindWith(r, binding.ProtoBuf)
if err != nil {
return nil, err
}
} else {
err = c.Copy().ShouldBindJSON(r)
if err != nil {
return nil, err
}
}
return r, nil
}