Utils
The utils package provides a collection of general-purpose utility functions including pagination math, string case conversion, struct-to-map conversion, search query building, and random password generation.
Import
import "github.com/gofastadev/gofasta/pkg/utils"Pagination
PreparePaginating
A helper struct that computes offset, limit, page, and sort order from pagination and sorting DTOs.
type PreparePaginating struct {
PageFilters *types.TPaginationInputDto
Sorting *types.TSortingInputDto
}| Method | Signature | Description |
|---|---|---|
GetOffset | func (p *PreparePaginating) GetOffset() int | Calculates the database offset from page and limit |
GetLimit | func (p *PreparePaginating) GetLimit() int | Returns the limit (default: 10) |
GetPage | func (p *PreparePaginating) GetPage() int | Returns the current page (default: 1) |
GetSort | func (p *PreparePaginating) GetSort() string | Returns the SQL ORDER BY clause (e.g., "created_at DESC") |
String Helpers
| Function | Signature | Description |
|---|---|---|
CamelToSnake | func CamelToSnake(s string) string | Converts CamelCase to snake_case (e.g., "UserProfile" to "user_profile") |
ConvertUpperCamelToLowerCamel | func ConvertUpperCamelToLowerCamel(input string) string | Converts PascalCase to camelCase (e.g., "UserName" to "userName") |
ParseIdStringIsValidUUID | func ParseIdStringIsValidUUID(u string) (uuid.UUID, error) | Parses and validates a UUID string |
Other Utilities
| Function | Signature | Description |
|---|---|---|
GeneratePassword | func GeneratePassword(length int) (string, error) | Generates a random password with mixed characters |
ConvertStructToMap | func ConvertStructToMap(obj interface{}) map[string]interface{} | Converts a struct to a map using snake_case keys (non-nil pointer fields only) |
BuildQueryForAnyModel | func BuildQueryForAnyModel(db *gorm.DB, filters map[string]interface{}) (*gorm.DB, error) | Builds a GORM query with ILIKE filters for string pointer fields |
Usage
Pagination Calculations
paginator := utils.PreparePaginating{
PageFilters: &types.TPaginationInputDto{
Page: utils.Ptr(3),
Limit: utils.Ptr(20),
},
Sorting: &types.TSortingInputDto{
SortByField: "createdAt",
SortOrientation: &types.SortOrientationDesc,
},
}
offset := paginator.GetOffset() // 40
limit := paginator.GetLimit() // 20
page := paginator.GetPage() // 3
sort := paginator.GetSort() // "created_at DESC"String Case Conversion
snake := utils.CamelToSnake("UserProfileSettings")
// -> "user_profile_settings"
camel := utils.ConvertUpperCamelToLowerCamel("UserName")
// -> "userName"
// Special case for "ID"
camel = utils.ConvertUpperCamelToLowerCamel("ID")
// -> "id"UUID Validation
id, err := utils.ParseIdStringIsValidUUID("550e8400-e29b-41d4-a716-446655440000")
if err != nil {
// invalid UUID
}Building Search Queries
name := "john"
filters := map[string]interface{}{
"name": &name,
"email": (*string)(nil), // nil pointers are skipped
}
query, err := utils.BuildQueryForAnyModel(db, filters)
// Produces: WHERE name ILIKE '%john%'Random Password Generation
password, err := utils.GeneratePassword(16)
if err != nil {
return err
}
// e.g., "aB3$kL9!mN2@pQ7&"Struct to Map Conversion
user := UserUpdate{
Name: utils.Ptr("Jane"),
Email: utils.Ptr("jane@example.com"),
Phone: nil, // nil fields are excluded
}
m := utils.ConvertStructToMap(user)
// -> {"name": "Jane", "email": "jane@example.com"}Related Pages
- Types — Type definitions that utilities operate on
- HTTP Utilities — Higher-level pagination helpers
- Validators — String validation rules
Last updated on