utils

package
v1.3.24 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package utils provides utility functions for converting types to strings and integers.

Index

Constants

This section is empty.

Variables

View Source
var ValidEmailRegexp = regexp.MustCompile(`^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$`)

ValidEmailRegexp is the standard email validation pattern.

Functions

func BoolToByte

func BoolToByte(b bool) byte

--- Conversion Helpers ---

func ByteToBool

func ByteToBool(b byte) bool

func CurrentTimeStr

func CurrentTimeStr() string

CurrentTimeStr returns the current time formatted as "YYYY-MM-DD HH:MM:SS".

func GetDateRange

func GetDateRange(value string) (fromDate, toDate string)

GetDateRange returns the date range based on the given value keyword. Supported: "today", "yesterday", "this_week", "last_week", "this_month", "last_month", "this_year", "last_year".

func IfThen

func IfThen[T any](condition bool, a T) (b T)

IfThen returns 'a' if condition is true, otherwise the zero value of T. Note: This is less safe than IfThenElse because the zero value might be meaningful.

func IfThenElse

func IfThenElse[T any](condition bool, a T, b T) T

IfThenElse returns 'a' if condition is true, otherwise 'b'.

func IsValidEmail

func IsValidEmail(email string) bool

func Pluck

func Pluck[T any, K any](subject *[]T, key string, destination *[]K)

Pluck extracts a specific field from a slice of structs.

func Ptr

func Ptr[T any](value T) *T

Ptr returns a pointer to the given value.

func Round

func Round(val float64, precision int) float64

--- Math Helpers ---

func SliceToMap

func SliceToMap[T any](slice []T) []map[string]any

SliceToMap converts a slice of structs to a slice of maps.

func StringClean

func StringClean(str string, limit int) string

StringClean truncates a string to a given rune-count limit and trims whitespace. Truncation is performed at rune (Unicode code point) boundaries to avoid producing invalid UTF-8.

func StringContainsIgnoreCase

func StringContainsIgnoreCase(str, substr string) bool

func StringSliceMergeUnique

func StringSliceMergeUnique(original, slice *[]string) []string

--- Slice Helpers ---

func ToInt

func ToInt[T int | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | float32 | float64 | string](value T) int

ToInt converts various types to int.

func ToJsonMap

func ToJsonMap[T any](item T) map[string]any

ToJsonMap converts a struct to a map using JSON tags.

func ToMap

func ToMap[T any](item T) map[string]any

ToMap converts a struct to a map.

func ToString

func ToString[T any](value T) string

ToString converts various types to string.

func WithDefault

func WithDefault[T int | string](value T, defaultValue T) T

WithDefault returns defaultValue if value is the zero value for its type (0 or ""). Currently supports int and string.

Types

type Pipeline

type Pipeline[T any] struct {
	// contains filtered or unexported fields
}

func NewPipeline

func NewPipeline[T any](subject *T) *Pipeline[T]

func (*Pipeline[T]) AddStep

func (p *Pipeline[T]) AddStep(step PipelineStep[T]) *Pipeline[T]

func (*Pipeline[T]) GetSubject

func (p *Pipeline[T]) GetSubject() *T

func (*Pipeline[T]) Run

func (p *Pipeline[T]) Run() error

func (*Pipeline[T]) WithContext

func (p *Pipeline[T]) WithContext(ctx *PipelineContext) *Pipeline[T]

type PipelineContext

type PipelineContext struct {
	// contains filtered or unexported fields
}

func NewPipelineContext

func NewPipelineContext() *PipelineContext

func (*PipelineContext) Get

func (pc *PipelineContext) Get(key string) (any, bool)

func (*PipelineContext) Is

func (pc *PipelineContext) Is(key string, value any) bool

func (*PipelineContext) Set

func (pc *PipelineContext) Set(key string, value any)

type PipelineStep

type PipelineStep[T any] func(*T, *PipelineContext) error

Jump to

Keyboard shortcuts

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