Documentation
¶
Index ¶
- Constants
- func ProcessData[T AddData](c *core.Ctx) error
- func ProcessFilter(c *core.Ctx) error
- func ProcessPathID(c *core.Ctx) error
- func ProcessUpdateData[T UpdateData](c *core.Ctx) error
- func SanitizeString(input string) string
- func SanitizeStruct(target any)
- func ToListResponse[T any, R any](records []T, transformerFn func(T) R) []R
- type AddData
- type Error
- type Filter
- type List
- type ListApi
- type Meta
- type Success
- type UpdateData
Constants ¶
const ( // UserKey key in Context's Data for ID extracted from path parameter UserKey string = "__user__" // DataKey key in Context's Data for processed/transformed request data DataKey string = "__data__" // PathIDKey key in Context's Data for ID extracted from path parameter PathIDKey string = "__path_id__" // RequestKey key in Context's Data for raw request data RequestKey string = "__request__" // FilterKey key in Context's Data for filtering parameters FilterKey string = "__filter__" )
Variables ¶
This section is empty.
Functions ¶
func ProcessData ¶
ProcessData validates and processes create/add requests. It handles parsing the request body, converting to DTO, and validation and put to Ctx's Data.
Type Parameters:
- T: The type that implements the AddData interface.
Parameters:
- c: The context object containing the HTTP request/response data.
Returns:
- error: Returns nil if successful, otherwise returns an error response.
func ProcessFilter ¶
ProcessFilter validates and processes filter requests It handles parsing the query parameters, converting to DTO, and validation and put to Ctx's Data
Parameters:
- c: The context object containing the HTTP request/response data
Returns:
- error: Returns nil if successful, otherwise returns an error response
Example Usage:
func (h ListUserApi) Validate(c *core.Ctx) error {
return http.ProcessFilter(c)
}
func ProcessPathID ¶
ProcessPathID is a generic function that extracts a path ID parameter and stores it in the context. It handles the common pattern of validating a path ID parameter for API endpoints and putting it in Ctx's Data.
Parameters:
- c: The context object containing the HTTP request/response data
Returns:
- error: Returns nil if successful, otherwise returns an error response
Example Usage:
func (h DeleteUserApi) Validate(c *core.Ctx) error {
return http.ProcessPathID(c)
}
func ProcessUpdateData ¶
func ProcessUpdateData[T UpdateData](c *core.Ctx) error
ProcessUpdateData validates and processes update requests. It handles parsing the request body, setting the ID, converting to DTO, and validation and put to Ctx's Data.
Type Parameters:
- T: The type that implements the UpdateData interface.
Parameters:
- c: The context object containing the HTTP request/response data.
Returns:
- error: Returns nil if successful, otherwise returns an error response.
func SanitizeString ¶
func SanitizeStruct ¶
func SanitizeStruct(target any)
SanitizeStruct recursively sanitizes string fields to mitigate XSS payloads.
func ToListResponse ¶
ToListResponse generic function takes a list of records, and their transformer function; process then return a slice of response data
Types ¶
type Error ¶
type Error struct {
Code string `json:"code" example:"BAD_REQUEST"` // Error code
Message string `json:"message" example:"Bad request"` // Error message description
Data core.Data `json:"data"` // Useful for validation's errors
}
Error struct to describe login response. @Description Generic error response structure @Data Data is optional and can be used to return additional information related to the operation. @Code Code is the HTTP status code for the error. @Message Message is a description of the error that occurred. @Tags Error Responses
func Validate ¶
func Validate(structData any, msgForTagFunc ...validation.MsgForTagFunc) *Error
Validate perform data input checking.
type Filter ¶
type Filter struct {
Page int `json:"page" example:"1" validate:"number" doc:"Current page number for pagination"`
PerPage int `json:"per_page" example:"10" validate:"number" doc:"Number of items to display per page"`
Keyword string `json:"keyword" example:"search term" validate:"" doc:"Search keyword for filtering records"`
OrderBy string `json:"order_by" example:"-created_at" validate:"" doc:"Field to order by, prefix with '-' for descending order"`
}
Filter struct to describe filtering and pagination parameters for generic requests. @Description Generic filter structure for pagination, searching, and ordering @Page Page is the current page number for pagination (optional, starts from 1) @PerPage PerPage is the number of items to display per page (optional) @Keyword Keyword is used for searching/filtering records by text content @OrderBy OrderBy specifies the field to sort by, prefix with '-' for descending order @Tags Request Filters
func FilterData ¶
type List ¶
type List[T any] struct { Meta Meta `json:"meta" example:"{\"page\":1,\"per_page\":10,\"total\":100}" doc:"Metadata information for pagination"` Data []T `json:"data" example:"[]" doc:"List of category data"` }
List struct to describe a generic list response. @Description Generic list response structure @Meta Meta contains metadata information for pagination. @Data Data is a slice of type T, which can be any data type. @Tags Success Responses
type Meta ¶
type Meta struct {
Page int `json:"page,omitempty" example:"1" doc:"Current page number"`
PerPage int `json:"per_page,omitempty" example:"10" doc:"Number of items per page"`
Total int `json:"total" example:"1354" doc:"Total number of records"`
}
Meta struct to describe pagination metadata information. @Description Contains pagination metadata including current page, items per page, and total count @Page Page is the current page number (optional, starts from 1) @PerPage PerPage is the number of items displayed per page (optional) @Total Total is the total number of records available @Tags Info Responses
type Success ¶
type Success struct {
Message string `json:"message" example:"Operation completed successfully"` // Success message description
Data core.Data `json:"data" doc:"Additional data related to the operation"` // Optional data related to the success operation
}
Success struct to describe a generic success response. @Description Generic success response structure @Data Data is optional and can be used to return additional information related to the operation. @Message Message is a success message that describes the operation. @Tags Success Responses
type UpdateData ¶
type UpdateData interface {
// SetID sets the ID field of the request structure
// Must be implemented with a pointer receiver to modify the struct in place
// Parameters:
// - id: Integer ID value to set
SetID(int)
}
UpdateData is an interface for types that can be updated. IMPORTANT: SetID must be implemented with a pointer receiver (e.g., func (r *RequestType) SetID(id int)) to ensure the ID is properly set on the original struct, not a copy