req

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultPage  = 1
	DefaultSize  = 50
	MaxSizeLimit = 1000 // 默认最大分页大小,防止内存耗尽攻击
	MinPage      = 1
	MinSize      = 1
)

分页默认配置

Variables

This section is empty.

Functions

func ApplyDefaults

func ApplyDefaults(obj any) error

ApplyDefaults 根据结构体字段的 `default` tag 为零值字段填充默认值。 在 Bind / ShouldBind 中于校验前自动调用;也可在手动绑定后单独调用。

func Bind

func Bind(c *gin.Context, obj any) (err error)

func ShouldBind

func ShouldBind(c *gin.Context, obj any) error

ShouldBind 从多个来源(path/body/query)绑定数据到同一个结构体 设计意图:尽力绑定模式,忽略各来源的绑定错误,最终只返回验证结果 适用场景:同一个 Req 结构体需要同时接收路径参数、请求体和查询参数

Types

type Captcha

type Captcha struct {
	UUID string `json:"uuid" form:"uuid" binding:"required"`
	Code string `json:"code" form:"code" binding:"required"`
}

type IDReq

type IDReq[T uint64 | string] struct {
	ID T `json:"id" yaml:"id" form:"id" binding:"required"`
}

type LikeReq

type LikeReq struct {
	Keyword string `json:"keyword" form:"keyword"`
}

type NameReq

type NameReq struct {
	Name string `form:"name" json:"name" binding:"required"`
}

type PaginationReq

type PaginationReq struct {
	Page int `json:"page" form:"page"`
	Size int `json:"size" form:"size"`
}

func (*PaginationReq) FindPage

func (p *PaginationReq) FindPage(sizeLimit ...int) (offset int, limit int)

FindPage 返回分页的 offset 和 limit sizeLimit 可选,指定最大分页大小,默认使用 MaxSizeLimit

func (*PaginationReq) StartEnd

func (p *PaginationReq) StartEnd() (start, end int)

StartEnd 返回分页的 start 和 end 索引

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL