common

package
v0.0.0-...-1a3a2fa Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: BSD-3-Clause Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AppName   = "stillbox"
	EnvPrefix = "STILLBOX_"
)
View Source
const (
	DaysInWeek      = 7
	MonthsInQuarter = 3
)
View Source
const (
	TimeFormat = "Jan 2 15:04:05"
)

Variables

View Source
var (
	ErrPageOutOfRange = errors.New("requested page out of range")
	ErrBadOrder       = errors.New("invalid order")
	ErrBadDirection   = errors.New("invalid direction")
)
View Source
var (
	FuncMap = template.FuncMap{
		"f": fmtFloat,
		"dict": func(values ...any) (map[string]any, error) {
			if len(values)%2 != 0 {
				return nil, errors.New("invalid dict call")
			}
			dict := make(map[string]any, len(values)/2)
			for i := 0; i < len(values); i += 2 {
				key, ok := values[i].(string)
				if !ok {
					return nil, errors.New("dict keys must be strings")
				}
				dict[key] = values[i+1]
			}
			return dict, nil
		},
		"formTime": func(t jsontypes.Time) string {
			return time.Time(t).Format("2006-01-02T15:04")
		},
		"ago": func(s string) (string, error) {
			d, err := time.ParseDuration(s)
			if err != nil {
				return "", err
			}
			return time.Now().Add(-d).Format("2006-01-02T15:04"), nil
		},
	}
)

Functions

func Action

func Action(c cmdOptions) cli.ActionFunc

func AtoiU32

func AtoiU32(s string) (uint32, error)

AtoiU32 is atoi() that supports hex (0x) or dec.

func CollectStack

func CollectStack() []byte

func ConfigureAcronym

func ConfigureAcronym(key, val string)

ConfigureAcronym allows you to add additional words which will be considered acronyms

func ContentDisposition

func ContentDisposition(hdr http.Header, contentType, filename string, attachment bool)

func CryptRandSeq

func CryptRandSeq(n int) string

func DefaultIfNilOrZero

func DefaultIfNilOrZero[T comparable](v *T, def T) T

func FromPanicValue

func FromPanicValue(i any) error

FromPanicValue is used to recover errgroup panics.

func Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys returns an unsorted slice of the keys in m

func LoggingRoundTripper

func LoggingRoundTripper() http.RoundTripper

func NanoID

func NanoID(n int) string

func NilIfZero

func NilIfZero[T comparable](val T) *T

func PGUUID

func PGUUID(u *uuid.UUID) pgtype.UUID

func PtrTo

func PtrTo[T any](t T) *T

func RandSeq

func RandSeq(n int) string

func RemoteAddr

func RemoteAddr(as string) (netip.Addr, error)

func ToCamel

func ToCamel(s string) string

ToCamel converts a string to CamelCase

func ToDelimited

func ToDelimited(s string, delimiter uint8) string

ToDelimited converts a string to delimited.snake.case (in this case `delimiter = '.'`)

func ToKebab

func ToKebab(s string) string

ToKebab converts a string to kebab-case

func ToLowerCamel

func ToLowerCamel(s string) string

ToLowerCamel converts a string to lowerCamelCase

func ToScreamingDelimited

func ToScreamingDelimited(s string, delimiter uint8, ignore string, screaming bool) string

ToScreamingDelimited converts a string to SCREAMING.DELIMITED.SNAKE.CASE (in this case `delimiter = '.'; screaming = true`) or delimited.snake.case (in this case `delimiter = '.'; screaming = false`)

func ToScreamingKebab

func ToScreamingKebab(s string) string

ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE

func ToScreamingSnake

func ToScreamingSnake(s string) string

ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE

func ToSnake

func ToSnake(s string) string

ToSnake converts a string to snake_case

func ToSnakeWithIgnore

func ToSnakeWithIgnore(s string, ignore string) string

func WithDefaultBounds

func WithDefaultBounds(i Interval) tbOpt

func WithLocation

func WithLocation(l *time.Location) tbOpt

func ZeroFields

func ZeroFields(s any)

ZeroFields takes a struct or a pointer to struct and if any pointer fields point to a zero value for the pointed type, they are set to nil. It also recurses into embedded fields.

func ZeroIfNil

func ZeroIfNil[T any](v *T) T

Types

type ErrInvalidInterval

type ErrInvalidInterval string

func (ErrInvalidInterval) Error

func (in ErrInvalidInterval) Error() string

type Interval

type Interval string
const (
	Unknown   Interval = ""
	Daily     Interval = "daily"
	Weekly    Interval = "weekly"
	Monthly   Interval = "monthly"
	Quarterly Interval = "quarterly"
	Yearly    Interval = "yearly"
)

func (Interval) Duration

func (p Interval) Duration() time.Duration

func (Interval) IsValid

func (p Interval) IsValid() bool

type Pagination

type Pagination struct {
	Page    *int `json:"page"`
	PerPage *int `json:"perPage"`
}

Pagination describes a request for a particular page and records per page.

func (Pagination) OffsetPerPage

func (p Pagination) OffsetPerPage(perPageDefault int) (offset int32, perPage int32)

OffsetPerPage computes a sane offset and records per page based on p and perPageDefault.

type SortDirection

type SortDirection string
const (
	DirAsc  SortDirection = "asc"
	DirDesc SortDirection = "desc"
)

func (*SortDirection) DirString

func (t *SortDirection) DirString(def SortDirection) string

DirString returns the direction or a default.

func (*SortDirection) IsValid

func (t *SortDirection) IsValid() bool

IsValid returns whether t is valid.

type TimeBounder

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

func NewTimeBounder

func NewTimeBounder(opts ...tbOpt) *TimeBounder

func (*TimeBounder) Bounds

func (tb *TimeBounder) Bounds(t time.Time) (lowerBound, upperBound time.Time)

func (*TimeBounder) GetDailyBounds

func (tb *TimeBounder) GetDailyBounds(date time.Time) (lowerBound, upperBound time.Time)

func (*TimeBounder) GetMonthlyBounds

func (tb *TimeBounder) GetMonthlyBounds(date time.Time) (lowerBound, upperBound time.Time)

func (*TimeBounder) GetQuarterlyBounds

func (tb *TimeBounder) GetQuarterlyBounds(date time.Time) (lowerBound, upperBound time.Time)

func (*TimeBounder) GetWeeklyBounds

func (tb *TimeBounder) GetWeeklyBounds(date time.Time) (lowerBound, upperBound time.Time)

func (*TimeBounder) GetYearlyBounds

func (tb *TimeBounder) GetYearlyBounds(date time.Time) (lowerBound, upperBound time.Time)

type TimeRange

type TimeRange struct {
	Begin time.Time `json:"begin"`
	End   time.Time `json:"end"`
}

func (*TimeRange) TSTZRange

func (t *TimeRange) TSTZRange() pgtype.Range[pgtype.Timestamptz]

Jump to

Keyboard shortcuts

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