helpers

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: MIT Imports: 23 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddToContext added in v1.3.7

func AddToContext(ctx context.Context, key models.ContextKey, value interface{}) context.Context

AddToContext adds a key-value pair to context and returns new context.

func CalculateOffset added in v1.3.7

func CalculateOffset(page, pageSize int) int

CalculateOffset calculates database offset for pagination.

func Coalesce added in v1.3.7

func Coalesce(values ...string) string

Coalesce returns the first non-empty string from the provided arguments.

func ContainsInt added in v1.3.7

func ContainsInt(slice []int, item int) bool

ContainsInt checks if slice contains integer.

func ContainsString added in v1.3.7

func ContainsString(slice []string, item string) bool

ContainsString checks if slice contains string.

func ConvertToUUID added in v1.2.6

func ConvertToUUID(dataString string) uuid.UUID

ConvertToUUID converts a string to UUID, returns uuid.Nil if conversion fails.

func CreateError

func CreateError(message string) error

CreateError returns a new error with the given message

func CreateErrorf added in v1.3.7

func CreateErrorf(format string, args ...interface{}) error

CreateErrorf returns a new formatted error with the given message and arguments

func DefaultIfEmpty added in v1.3.7

func DefaultIfEmpty(value string, defaultValue string) string

DefaultIfEmpty returns the value if not empty, otherwise returns default value.

func DirectoryExists added in v1.3.7

func DirectoryExists(dirPath string) bool

DirectoryExists checks if a directory exists.

func EnsureDirectory added in v1.3.7

func EnsureDirectory(dirPath string) error

EnsureDirectory creates directory if it doesn't exist.

func FileExists added in v1.3.7

func FileExists(filePath string) bool

FileExists checks if a file exists and is not a directory.

func FormatTimestamp added in v1.3.7

func FormatTimestamp(t time.Time, layout string) string

FormatTimestamp formats timestamp to specified layout.

func GenerateOTP added in v1.3.5

func GenerateOTP() (int, error)

GenerateOTP generates a random 6-digit OTP.

func GenerateRandomString added in v1.3.7

func GenerateRandomString(length int) (string, error)

GenerateRandomString generates a cryptographically secure random string of specified length.

func GenerateSecureOTPString added in v1.3.7

func GenerateSecureOTPString(length int) (string, error)

GenerateSecureOTPString generates a secure random OTP as string with fixed length.

func GenerateUUID added in v1.3.7

func GenerateUUID() uuid.UUID

GenerateUUID generates a new UUID v4.

func GenerateUUIDString added in v1.3.7

func GenerateUUIDString() string

GenerateUUIDString generates a new UUID v4 as string.

func GetClientIP added in v1.3.7

func GetClientIP(request *http.Request) string

GetClientIP extracts client IP address from request.

func GetContextData added in v1.3.5

func GetContextData(request *http.Request, key models.ContextKey) uuid.UUID

GetContextData extracts UUID from request context (alias for GetUUIDContextData).

func GetCurrentTimestamp added in v1.3.7

func GetCurrentTimestamp() time.Time

GetCurrentTimestamp returns current timestamp in various formats.

func GetCurrentTimestampString added in v1.3.7

func GetCurrentTimestampString() string

GetCurrentTimestampString returns current timestamp as string in RFC3339 format.

func GetENVBoolValue added in v1.3.7

func GetENVBoolValue(key string, defaultValue bool) bool

GetENVBoolValue loads an environment variable as a boolean with a default value.

func GetENVIntValue added in v1.3.7

func GetENVIntValue(key string, defaultValue int) int

GetENVIntValue loads an environment variable as an integer with a default value.

func GetENVValue

func GetENVValue(key string) string

GetENVValue loads the environment variable value for a given key (case insensitive, converts input key to UPPER_SNAKE_CASE), including those loaded from .env file.

func GetENVValueWithDefault added in v1.3.7

func GetENVValueWithDefault(key string, defaultValue string) string

GetENVValueWithDefault loads an environment variable with a default value if not set.

func GetFileSize added in v1.3.7

func GetFileSize(filePath string) (int64, error)

GetFileSize returns file size in bytes.

func GetFileType added in v1.2.6

func GetFileType(filename string) string

GetFileType determines if the file is an image, video, document, or unknown.

func GetFormFile added in v1.3.7

func GetFormFile(request *http.Request, fieldName string) (multipart.File, *multipart.FileHeader, error)

GetFormFile retrieves file from multipart form.

func GetFromContext added in v1.3.7

func GetFromContext(ctx context.Context, key models.ContextKey) (interface{}, bool)

GetFromContext retrieves value from context with type assertion.

func GetIntContextData added in v1.3.7

func GetIntContextData(request *http.Request, key models.ContextKey) int

GetIntContextData extracts integer from request context.

func GetIntFromContext added in v1.3.7

func GetIntFromContext(ctx context.Context, key models.ContextKey) int

GetIntFromContext retrieves integer value from context.

func GetPaginationParams added in v1.3.7

func GetPaginationParams(request *http.Request) (page int, pageSize int)

GetPaginationParams extracts pagination parameters from request.

func GetQueryBool added in v1.3.7

func GetQueryBool(request *http.Request, key string, defaultValue bool) bool

GetQueryBool extracts a boolean query parameter with optional default value.

func GetQueryID added in v1.3.5

func GetQueryID(request *http.Request) uuid.UUID

GetQueryID is an alias for GetQueryUUID for backward compatibility.

func GetQueryInt added in v1.3.7

func GetQueryInt(request *http.Request, key string, defaultValue int) int

GetQueryInt extracts an integer query parameter with optional default value.

func GetQueryParam added in v1.3.7

func GetQueryParam(request *http.Request, key string, defaultValue string) string

GetQueryParam extracts a string query parameter with optional default value.

func GetQueryUUID added in v1.2.6

func GetQueryUUID(request *http.Request) uuid.UUID

GetQueryUUID extracts UUID from query parameter "id".

func GetSearchKeyword added in v1.2.6

func GetSearchKeyword(request *http.Request) string

GetSearchKeyword extracts search keyword from query parameter "keyword".

func GetStringContextData added in v1.2.6

func GetStringContextData(request *http.Request, key models.ContextKey) string

GetStringContextData extracts string from request context.

func GetStringFromContext added in v1.3.7

func GetStringFromContext(ctx context.Context, key models.ContextKey) string

GetStringFromContext retrieves string value from context.

func GetUUIDContextData added in v1.2.6

func GetUUIDContextData(request *http.Request, key models.ContextKey) uuid.UUID

GetUUIDContextData extracts UUID from request context.

func GetUserAgent added in v1.3.7

func GetUserAgent(request *http.Request) string

GetUserAgent extracts user agent from request.

func IsDocumentFile added in v1.3.7

func IsDocumentFile(filename string) bool

IsDocumentFile checks if the file is a document based on extension.

func IsImageFile added in v1.3.7

func IsImageFile(filename string) bool

IsImageFile checks if the file is an image based on extension.

func IsValidUUID added in v1.3.0

func IsValidUUID(providedID string) bool

IsValidUUID checks if the provided string is a valid UUID.

func IsVideoFile added in v1.3.7

func IsVideoFile(filename string) bool

IsVideoFile checks if the file is a video based on extension.

func IsZeroUUID added in v1.3.0

func IsZeroUUID(ID uuid.UUID) bool

IsZeroUUID checks if UUID is the zero value.

func NormalizePhoneNumber added in v1.2.6

func NormalizePhoneNumber(phoneNumber string, toNormal bool) string

NormalizePhoneNumber normalizes phone numbers to standard format.

func ParseMultipartForm added in v1.3.7

func ParseMultipartForm(request *http.Request, maxMemory int64) error

ParseMultipartForm parses multipart form with size limits.

func ParseTimestamp added in v1.3.7

func ParseTimestamp(timestamp string, layout string) (time.Time, error)

ParseTimestamp parses timestamp string with specified layout.

func ReadFileContent added in v1.3.7

func ReadFileContent(filePath string) (string, error)

ReadFileContent reads entire file content as string.

func RemoveDuplicates added in v1.3.7

func RemoveDuplicates(slice []string) []string

RemoveDuplicates removes duplicate values from string slice.

func RespondWithJSON

func RespondWithJSON(w http.ResponseWriter, statusCode int, payload interface{})

RespondWithJSON writes a JSON response to the HTTP response writer. Constructs a standardized server response with payload and success flag. Sets the appropriate headers, status code, and writes the JSON data.

func RowToMap added in v1.4.2

func RowToMap(rows *sql.Rows) (map[string]any, error)

func RowsToMaps added in v1.4.2

func RowsToMaps(rows *sql.Rows) ([]map[string]any, error)

func SafeDeleteFile added in v1.3.7

func SafeDeleteFile(filePath string) error

SafeDeleteFile safely deletes a file with error handling.

func SaveUploadedFile added in v1.3.7

func SaveUploadedFile(fileHeader *multipart.FileHeader, destination string) error

SaveUploadedFile saves uploaded file to specified path.

func ToFormatedCurrency added in v1.3.9

func ToFormatedCurrency(value float64) string

func ToPlural added in v1.3.9

func ToPlural(word string) string

func ToSingular added in v1.3.9

func ToSingular(word string) string

func ToSnakeCase added in v1.4.0

func ToSnakeCase(input string) string

func TruncateString added in v1.3.7

func TruncateString(s string, maxLength int) string

TruncateString truncates string to specified length with ellipsis.

func ValidateEmail added in v1.3.7

func ValidateEmail(email string) bool

ValidateEmail validates an email address format.

func ValidatePhoneNumber added in v1.3.7

func ValidatePhoneNumber(phoneNumber string) bool

ValidatePhoneNumber validates phone number format.

func ValidateURL added in v1.3.7

func ValidateURL(urlString string) bool

ValidateURL validates a URL format.

func WrapError added in v1.3.7

func WrapError(err error, message string) error

WrapError wraps an existing error with additional context

func WrapErrorf added in v1.3.7

func WrapErrorf(err error, format string, args ...interface{}) error

WrapErrorf wraps an existing error with formatted additional context

func WriteFileContent added in v1.3.7

func WriteFileContent(filePath string, content string) error

WriteFileContent writes string content to file.

Types

This section is empty.

Jump to

Keyboard shortcuts

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