utils

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: GPL-2.0 Imports: 15 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppName

func AppName() string

AppName returns the name of the executable that started this program (process).

func BatchSliceOfStrings

func BatchSliceOfStrings(ctx context.Context, keys []string, count int) <-chan []string

BatchSliceOfStrings groups the given keys into chunks of size count and streams them into a returned channel.

func ChanFromSlice added in v1.1.1

func ChanFromSlice[T any](values []T) <-chan T

ChanFromSlice takes a slice of values and returns a channel from which these values can be received. This channel is closed after the last value was sent.

func Checksum

func Checksum(data interface{}) []byte

Checksum returns the SHA-1 checksum of the data.

func ConvertCamelCase

func ConvertCamelCase(s string, _case int, sep byte) string

ConvertCamelCase converts a (lower) CamelCase string into various cases. _case must be unicode.Lower or unicode.Upper.

Example usage:

# snake_case
ConvertCamelCase(s, unicode.Lower, '_')

# SCREAMING_SNAKE_CASE
ConvertCamelCase(s, unicode.Upper, '_')

# kebab-case
ConvertCamelCase(s, unicode.Lower, '-')

# SCREAMING-KEBAB-CASE
ConvertCamelCase(s, unicode.Upper, '-')

# other.separator
ConvertCamelCase(s, unicode.Lower, '.')

func Ellipsize

func Ellipsize(s string, limit int) string

Ellipsize shortens s to <=limit runes and indicates shortening by "...".

func Fatal

func Fatal(err error)

Fatal panics with the given error.

func FromUnixMilli

func FromUnixMilli(ms int64) time.Time

FromUnixMilli creates and returns a time.Time value from the given milliseconds since the Unix epoch ms.

func IsContextCanceled

func IsContextCanceled(err error) bool

IsContextCanceled returns whether the given error is context.Canceled.

func IsDeadlock

func IsDeadlock(err error) bool

IsDeadlock returns whether the given error signals serialization failure.

func JoinHostPort added in v1.1.1

func JoinHostPort(host string, port int) string

JoinHostPort is like its equivalent in net., but handles UNIX sockets as well.

func Key

func Key(name string, sep byte) string

Key returns the name with all Unicode letters mapped to lower case letters, with an additional separator in front of each original upper case letter.

func MaxInt

func MaxInt(x, y int) int

MaxInt returns the larger of the given integers.

func Name

func Name(t interface{}) string

Name returns the declared name of type t. Name is used in combination with Key to automatically guess an entity's database table and Redis key.

func TableName

func TableName(t interface{}) string

TableName returns the table of t.

func Timed

func Timed(start time.Time, callback func(elapsed time.Duration))

Timed calls the given callback with the time that has elapsed since the start.

Timed should be installed by defer:

func TimedExample(logger *zap.SugaredLogger) {
	defer utils.Timed(time.Now(), func(elapsed time.Duration) {
		logger.Debugf("Executed job in %s", elapsed)
	})
	job()
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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