Skip to Content

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 }
MethodSignatureDescription
GetOffsetfunc (p *PreparePaginating) GetOffset() intCalculates the database offset from page and limit
GetLimitfunc (p *PreparePaginating) GetLimit() intReturns the limit (default: 10)
GetPagefunc (p *PreparePaginating) GetPage() intReturns the current page (default: 1)
GetSortfunc (p *PreparePaginating) GetSort() stringReturns the SQL ORDER BY clause (e.g., "created_at DESC")

String Helpers

FunctionSignatureDescription
CamelToSnakefunc CamelToSnake(s string) stringConverts CamelCase to snake_case (e.g., "UserProfile" to "user_profile")
ConvertUpperCamelToLowerCamelfunc ConvertUpperCamelToLowerCamel(input string) stringConverts PascalCase to camelCase (e.g., "UserName" to "userName")
ParseIdStringIsValidUUIDfunc ParseIdStringIsValidUUID(u string) (uuid.UUID, error)Parses and validates a UUID string

Other Utilities

FunctionSignatureDescription
GeneratePasswordfunc GeneratePassword(length int) (string, error)Generates a random password with mixed characters
ConvertStructToMapfunc ConvertStructToMap(obj interface{}) map[string]interface{}Converts a struct to a map using snake_case keys (non-nil pointer fields only)
BuildQueryForAnyModelfunc 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"}
Last updated on