utils

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

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

Go to latest
Published: May 11, 2024 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllCPUs

func AllCPUs() int

func BeginOfThisMonth

func BeginOfThisMonth(tz *time.Location) time.Time

func BeginOfThisWeek

func BeginOfThisWeek(tz *time.Location) time.Time

func BeginOfThisYear

func BeginOfThisYear(tz *time.Location) time.Time

func BeginOfToday

func BeginOfToday(tz *time.Location) time.Time

func CeilDate

func CeilDate(date time.Time) time.Time

CeilDate rounds date up to the start of next day if date is not already a start (00:00:00)

func CheckEmailMX

func CheckEmailMX(email string) bool

CheckEmailMX takes an e-mail address and verifies that an MX DNS record exists for its domain

func CloneStringMap

func CloneStringMap(m map[string]string, keysToLower bool) map[string]string

func CompareArgon2Id

func CompareArgon2Id(hashed, plain, pepper string) bool

func CompareBcrypt

func CompareBcrypt(hashed, plain, pepper string) bool

func ComparePassword

func ComparePassword(hashed, plain, pepper string) bool

func CronPadToSecondly

func CronPadToSecondly(expr string) string

func ExtractBasicAuth

func ExtractBasicAuth(r *http.Request) (username, password string, err error)

func ExtractBearerAuth

func ExtractBearerAuth(r *http.Request) (key string, err error)

func FadeColors

func FadeColors(color1, color2 color.RGBA, ratio float64) color.RGBA

func FindString

func FindString(needle string, haystack []string, defaultVal string) string

func HalfCPUs

func HalfCPUs() int

func HasConstraints

func HasConstraints(db *gorm.DB) bool

func HashArgon2Id

func HashArgon2Id(plain, pepper string) (string, error)

func HashBcrypt

func HashBcrypt(plain, pepper string) (string, error)

func HashPassword

func HashPassword(plain, pepper string) (string, error)

func HexToRGBA

func HexToRGBA(s string) (c color.RGBA)

func IsCleanDB

func IsCleanDB(db *gorm.DB) bool

func IsNoCache

func IsNoCache(r *http.Request, cacheTtl time.Duration) bool

IsNoCache checks whether returning a cached resource no older than cacheTtl is allowed given the incoming request

func Json

func Json(data interface{}) template.JS

func LocalTZOffset

func LocalTZOffset() time.Duration

LocalTZOffset returns the time difference between server local time and UTC

func MustParseTime

func MustParseTime(layout, value string) time.Time

func ParseJsonDropKeys

func ParseJsonDropKeys[T any](r io.Reader, dropKeys ...string) (T, error)

ParseJsonDropKeys parses the given JSON input object to an object of given type, while omitting the specified keys on the way. This can be useful if parsing would normally fail due to ambiguous typing of some key, but that key is not of interest and can be dropped to avoid parse errors. Dropping keys only works on top level of the object.

func ParseUserAgent

func ParseUserAgent(ua string) (string, string, error)

func ParseWeekday

func ParseWeekday(s string) time.Weekday

func QuoteDbIdentifier

func QuoteDbIdentifier(db *gorm.DB, identifier string) string

QuoteDbIdentifier quotes a column name used in a query.

func QuoteSql

func QuoteSql(db *gorm.DB, queryTemplate string, identifiers ...string) string

QuoteSql quotes a SQL statement with the given identifiers.

func RGBAToHex

func RGBAToHex(c color.RGBA) string

func RaiseForStatus

func RaiseForStatus(res *http.Response, err error) (*http.Response, error)

func SplitMulti

func SplitMulti(s string, delimiters ...string) []string

func SplitRangeByDays

func SplitRangeByDays(from time.Time, to time.Time) [][]time.Time

SplitRangeByDays creates a slice of intervals between from and to, each of which is at max of 24 hours length and has its split at midnight

func SubSlice

func SubSlice[T any](slice []T, from, to uint) []T

func ToRunes

func ToRunes(s string) (r []string)

func WaitTimeout

func WaitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool

WaitTimeout waits for the waitgroup for the specified max timeout. Returns true if waiting timed out. See // https://stackoverflow.com/a/32843750/3112139.

func WhereNullable

func WhereNullable(query *gorm.DB, col string, val any) *gorm.DB

func WithPaging

func WithPaging(query *gorm.DB, limit, skip int) *gorm.DB

func WithRecovery

func WithRecovery(fn func(...interface{}), args ...interface{}) (err error)

WithRecovery executes a given function while recovering from panics and returning them as an errors

func WithRecovery1

func WithRecovery1[T any](fn func(T), a1 T) (err error)

WithRecovery1 executes a given function with one parameter while recovering from panics and returning them as an errors

Types

type PageParams

type PageParams struct {
	Page     int `json:"page"`
	PageSize int `json:"page_size"`
}

func ParsePageParams

func ParsePageParams(r *http.Request) *PageParams

func ParsePageParamsWithDefault

func ParsePageParamsWithDefault(r *http.Request, page, size int) *PageParams

func (*PageParams) Limit

func (p *PageParams) Limit() int

func (*PageParams) Offset

func (p *PageParams) Offset() int

type TemplateMap

type TemplateMap map[string]*template.Template

func LoadTemplates

func LoadTemplates(templateFs fs.FS, funcs template.FuncMap) (TemplateMap, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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