Documentation
¶
Index ¶
- Constants
- func Bind(ctx *gin.Context, req interface{}) (err *errors.Error)
- func BindJSON[T any](ctx *gin.Context) (*T, *errors.Error)
- func BindParams(ctx *gin.Context, req interface{}, noPrint ...bool) (err *errors.Error)
- func BindQuery[T any](ctx *gin.Context) (*T, *errors.Error)
- func BindReq[T any](ctx *gin.Context, noPrint ...bool) (*T, *errors.Error)
- func BindURI[T any](ctx *gin.Context) (*T, *errors.Error)
- func EnsureTraceID(ctx *gin.Context) string
- func GetBearerToken(ctx *gin.Context) string
- func GetClientIP(ctx *gin.Context) string
- func GetContextVal[T any](ctx *gin.Context, key string) (val T, exists bool)
- func GetFullURL(ctx *gin.Context) string
- func GetHeader(ctx *gin.Context, key string, defValue ...string) string
- func GetHost(ctx *gin.Context) string
- func GetPageReq(ctx *gin.Context) (pageReq *load.Page, err *errors.Error)
- func GetParam(ctx *gin.Context, key string, defValue ...string) (value interface{}, err *errors.Error)
- func GetParamArray[t any](ctx *gin.Context, key string, force Forcible, defValue ...[]t) (value []t, err *errors.Error)
- func GetParamBool(ctx *gin.Context, key string, force Forcible, defValue ...bool) (value bool, err *errors.Error)
- func GetParamFloat64(ctx *gin.Context, key string, force Forcible, defValue ...float64) (value float64, err *errors.Error)
- func GetParamForce(ctx *gin.Context, key string) (value string, err *errors.Error)
- func GetParamInt(ctx *gin.Context, key string, force Forcible, defValue ...int) (value int, err *errors.Error)
- func GetParamInt64(ctx *gin.Context, key string, force Forcible, defValue ...int64) (value int64, err *errors.Error)
- func GetParamInt64Slice(ctx *gin.Context, key string, force Forcible, defValue ...[]int64) (value []int64, err *errors.Error)
- func GetParamStr(ctx *gin.Context, key string, defValue ...string) (value string, err *errors.Error)
- func GetParamType[t ParamType](ctx *gin.Context, key string, force Forcible, defValue ...t) (value t, err *errors.Error)
- func GetParams(ctx *gin.Context, requestType ...RequestType) map[string]interface{}
- func GetScheme(ctx *gin.Context) string
- func GetTraceID(ctx *gin.Context) string
- func GetUserID(ctx *gin.Context) string
- func GetUserIDInt(ctx *gin.Context) int
- func GetUserIDInt64(ctx *gin.Context) int64
- func GetUserInfo(ctx *gin.Context) map[string]interface{}
- func GetUserInfoAs[T any](ctx *gin.Context) (*T, error)
- func GetUserInfoValue(ctx *gin.Context, key string) any
- func GetValidator() *validator.Validate
- func Handle(ctx *gin.Context, code int, err error)
- func HandleData(ctx *gin.Context, code int, data interface{}, err error)
- func HandleError(ctx *gin.Context, code int, data any, err error)
- func HandleOk(ctx *gin.Context)
- func HandlePage[T any](ctx *gin.Context, code int, pageResp *load.PageRespT[T], err error)
- func HandlePanic(ctx *gin.Context)
- func HandleResult[T any](ctx *gin.Context, code int, data T, err error)
- func NewCtx() *gin.Context
- func PanicNotify(ctx *gin.Context, err interface{})
- func Param[T any](ctx *gin.Context, key string, defValue ...T) (T, *errors.Error)
- func ParamReq[T any](ctx *gin.Context, key string) (T, *errors.Error)
- func ParamSlice[T any](ctx *gin.Context, key string, defValue ...[]T) ([]T, *errors.Error)
- func ParamSliceReq[T any](ctx *gin.Context, key string) ([]T, *errors.Error)
- func PutParams(ctx *gin.Context, params map[string]interface{})
- func RegisterErrorHook(h ErrorHandler)
- func RegisterPanicHook(h PanicHandler)
- func RegisterValidation(tag string, fn validator.Func) error
- func SendError(ctx *gin.Context, code int, message string, err ...error)
- func SetContextVal[T any](ctx *gin.Context, key string, val T)
- func SetTraceID(ctx *gin.Context, customTraceID ...string) string
- func SetUserID(ctx *gin.Context, userID string)
- func SetUserInfo(ctx *gin.Context, userInfo map[string]interface{})
- func ValidateStruct(obj interface{}) *errors.Error
- func ValidateVar(field any, tag string) *errors.Error
- type ErrorHandler
- type Forcible
- type PanicHandler
- type ParamType
- type RequestType
- type ValueExtractor
Constants ¶
const (
ErrorKey = "error_g"
)
Variables ¶
This section is empty.
Functions ¶
func BindParams ¶
BindParams binds and validates request data into req struct pointer.
func BindReq ¶
BindReq is a generic constructor and binder: allocates a new struct T, binds, validates, and returns *T.
func EnsureTraceID ¶
EnsureTraceID retrieves the trace ID or generates and sets a new one if missing.
func GetBearerToken ¶
GetBearerToken extracts Bearer token from Authorization header or 'token' query/header parameter.
func GetClientIP ¶
GetClientIP gets client IP address checking headers with fallback.
func GetContextVal ¶
GetContextVal safely retrieves a typed value from Gin Context.
func GetFullURL ¶
GetFullURL returns the full request URL string including scheme and host.
func GetPageReq ¶
GetPageReq parses standard pagination parameters (page, size, lastID) from request.
func GetParam ¶
func GetParam(ctx *gin.Context, key string, defValue ...string) (value interface{}, err *errors.Error)
GetParam retrieves a raw parameter.
func GetParamArray ¶
func GetParamArray[t any](ctx *gin.Context, key string, force Forcible, defValue ...[]t) (value []t, err *errors.Error)
GetParamArray retrieves an array parameter.
func GetParamBool ¶
func GetParamBool(ctx *gin.Context, key string, force Forcible, defValue ...bool) (value bool, err *errors.Error)
GetParamBool retrieves a bool parameter.
func GetParamFloat64 ¶
func GetParamFloat64(ctx *gin.Context, key string, force Forcible, defValue ...float64) (value float64, err *errors.Error)
GetParamFloat64 retrieves a float64 parameter.
func GetParamForce ¶
GetParamForce retrieves a required string parameter.
func GetParamInt ¶
func GetParamInt(ctx *gin.Context, key string, force Forcible, defValue ...int) (value int, err *errors.Error)
GetParamInt retrieves an int parameter.
func GetParamInt64 ¶
func GetParamInt64(ctx *gin.Context, key string, force Forcible, defValue ...int64) (value int64, err *errors.Error)
GetParamInt64 retrieves an int64 parameter.
func GetParamInt64Slice ¶
func GetParamInt64Slice(ctx *gin.Context, key string, force Forcible, defValue ...[]int64) (value []int64, err *errors.Error)
GetParamInt64Slice retrieves an []int64 parameter (supporting comma separated strings).
func GetParamStr ¶
func GetParamStr(ctx *gin.Context, key string, defValue ...string) (value string, err *errors.Error)
GetParamStr retrieves a string parameter.
func GetParamType ¶
func GetParamType[t ParamType](ctx *gin.Context, key string, force Forcible, defValue ...t) (value t, err *errors.Error)
GetParamType retrieves a parameter conforming to ParamType.
func GetParams ¶
func GetParams(ctx *gin.Context, requestType ...RequestType) map[string]interface{}
GetParams extracts and merges all request parameters (URL query, form, and JSON body).
func GetTraceID ¶
GetTraceID extracts the Trace ID from Gin Context, Request Context, or HTTP headers.
func GetUserIDInt ¶
GetUserIDInt returns the User ID as int.
func GetUserIDInt64 ¶
GetUserIDInt64 returns the User ID as int64.
func GetUserInfo ¶
GetUserInfo returns the UserInfo map from context.
func GetUserInfoAs ¶
GetUserInfoAs converts user info into a typed struct pointer.
func GetUserInfoValue ¶
GetUserInfoValue gets a specific field from UserInfo map.
func GetValidator ¶
GetValidator returns the global validator instance (defaults to binding tag).
func HandleData ¶
HandleData renders success with data when err is nil, or handles the error when non-nil.
func HandleError ¶
HandleError handles errors, distinguishes between System & Application errors, and renders the JSON response.
func HandlePage ¶
HandlePage renders paginated response or handles error.
func HandlePanic ¶
HandlePanic recovers from panics, logs stack traces, and renders 500 JSON response.
func HandleResult ¶
HandleResult is a generic helper for standard handler returns.
func PanicNotify ¶
PanicNotify logs and sends panic alert notifications.
func ParamReq ¶
ParamReq retrieves a required parameter by key converted to type T, triggering validation error if missing.
func ParamSlice ¶
ParamSlice retrieves a slice parameter by key with optional default.
func ParamSliceReq ¶
ParamSliceReq retrieves a required slice parameter by key, triggering validation error if missing.
func RegisterErrorHook ¶
func RegisterErrorHook(h ErrorHandler)
RegisterErrorHook adds a custom system error notification hook.
func RegisterPanicHook ¶
func RegisterPanicHook(h PanicHandler)
RegisterPanicHook adds a custom panic notification hook.
func RegisterValidation ¶
RegisterValidation registers a custom validation function to both binding and validate validators.
func SetContextVal ¶
SetContextVal sets a typed value into Gin Context.
func SetTraceID ¶
SetTraceID sets a new or given Trace ID into both Gin Context and standard Request Context.
func SetUserInfo ¶
SetUserInfo sets the UserInfo map into context.
func ValidateStruct ¶
ValidateStruct validates a struct using validation tags (supports both binding:"..." and validate:"...").
Types ¶
type ErrorHandler ¶
ErrorHandler is a callback hook invoked when a system error occurs.
type Forcible ¶
type Forcible bool
Forcible indicates whether a missing parameter triggers a validation error response.
type PanicHandler ¶
PanicHandler is a callback hook invoked when a panic occurs.
type ParamType ¶
type ParamType interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
~float32 | ~float64 | ~string | ~bool | ~uintptr
}
ParamType defines supported primitive constraint types for generic parameter extraction.
type RequestType ¶
type RequestType string
const ( Get RequestType = "Get" PostForm RequestType = "PostForm" PostBody RequestType = "PostBody" )
type ValueExtractor ¶
type ValueExtractor struct {
// contains filtered or unexported fields
}
ValueExtractor provides a fluent interface for extracting single parameter values.
func Value ¶
func Value(ctx *gin.Context, key string) ValueExtractor
Value creates a fluent ValueExtractor for a parameter key.
func (ValueExtractor) Bool ¶
func (e ValueExtractor) Bool(def ...bool) bool
Bool returns parameter as bool or fallback default.
func (ValueExtractor) Float64 ¶
func (e ValueExtractor) Float64(def ...float64) float64
Float64 returns parameter as float64 or fallback default.
func (ValueExtractor) Int ¶
func (e ValueExtractor) Int(def ...int) int
Int returns parameter as int or fallback default.
func (ValueExtractor) Int64 ¶
func (e ValueExtractor) Int64(def ...int64) int64
Int64 returns parameter as int64 or fallback default.
func (ValueExtractor) String ¶
func (e ValueExtractor) String(def ...string) string
String returns parameter as string or fallback default.