toolbelt

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2023 License: MIT Imports: 36 Imported by: 5

README

toolbelt

A set of utilities used in every go project

wisshes mascot

Documentation

Index

Constants

View Source
const (
	UnixEpochJulianDay = 2440587.5
)

Variables

View Source
var (
	JulianZeroTime = JulianDayToTime(0)
)

Functions

func AliasHash

func AliasHash(alias string) int64

func AliasHashEncoded

func AliasHashEncoded(alias string) string

func AliasHashEncodedf

func AliasHashEncodedf(format string, args ...interface{}) string

func AliasHashf

func AliasHashf(format string, args ...interface{}) int64

func Camel

func Camel(s string) string

func Cased

func Cased(s string, fn ...CasedFn) string

func Clamp

func Clamp[T Float](v T, minimum T, maximum T) T

func Clamp01

func Clamp01[T Float](v T) T

func ClampFit

func ClampFit[T Float](
	x T,
	oldMin T,
	oldMax T,
	newMin T,
	newMax T,
) T

func ClampFit01

func ClampFit01[T Float](x T, newMin T, newMax T) T

func CompressMiddleware added in v0.0.6

func CompressMiddleware() func(next http.Handler) http.Handler

func CtxSlog added in v0.0.3

func CtxSlog(ctx context.Context) (logger *slog.Logger, ok bool)

func CtxWithSlog added in v0.0.3

func CtxWithSlog(ctx context.Context, slog *slog.Logger) context.Context

func Fit

func Fit[T Float](
	x T,
	oldMin T,
	oldMax T,
	newMin T,
	newMax T,
) T

func Fit01

func Fit01[T Float](x T, newMin T, newMax T) T

func FitMax

func FitMax[T Float](x T, newMax T) T

func JulianDayToTime

func JulianDayToTime(d float64) time.Time

JulianDayToTime converts a Julian day into a time.Time.

func JulianDayToTimestamp

func JulianDayToTimestamp(f float64) *timestamppb.Timestamp

func JulianNow

func JulianNow() float64

func Kebab

func Kebab(s string) string

func Lower

func Lower(s string) string

func MustProtoJSONMarshal added in v0.1.1

func MustProtoJSONMarshal(msg proto.Message) []byte

func MustProtoJSONUnmarshal added in v0.1.1

func MustProtoJSONUnmarshal(b []byte, msg proto.Message)

func MustProtoMarshal added in v0.1.1

func MustProtoMarshal(msg proto.Message) []byte

func MustProtoUnmarshal added in v0.1.1

func MustProtoUnmarshal(b []byte, msg proto.Message)

func NextEncodedID

func NextEncodedID() string

func NextID

func NextID() int64

func Pascal

func Pascal(s string) string

func RandIntRange

func RandIntRange[T Integer](r *rand.Rand, min, max T) T

func RandNegOneToOneClamped

func RandNegOneToOneClamped[T Float](r *rand.Rand) T

func RoundFit01

func RoundFit01[T Float](x T, newMin T, newMax T) T

func Snake

func Snake(s string) string

func StmtBytes added in v0.2.0

func StmtBytes(stmt *sqlite.Stmt, colName string) []byte

func StmtJulianToTime added in v0.2.0

func StmtJulianToTime(stmt *sqlite.Stmt, colName string) time.Time

func StmtJulianToTimestamp added in v0.2.0

func StmtJulianToTimestamp(stmt *sqlite.Stmt, colName string) *timestamppb.Timestamp

func TimeToJulianDay

func TimeToJulianDay(t time.Time) float64

TimeToJulianDay converts a time.Time into a Julian day.

func TimestampJulian

func TimestampJulian(ts *timestamppb.Timestamp) float64

func Upper

func Upper(s string) string

func UpsertStream

func UpsertStream(js nats.JetStreamContext, cfg *nats.StreamConfig) (si *nats.StreamInfo, err error)

Types

type CasedFn added in v0.0.26

type CasedFn func(string) string

type CasedString

type CasedString struct {
	Original string
	Pascal   string
	Camel    string
	Snake    string
	Kebab    string
	Upper    string
	Lower    string
}

func ToCasedString added in v0.0.26

func ToCasedString(s string) CasedString

type CtxErrFunc

type CtxErrFunc func(ctx context.Context) error

func RunHotReload added in v0.0.6

func RunHotReload(port int, onStartPath string) CtxErrFunc

type CtxKey added in v0.0.3

type CtxKey string
const CtxSlogKey CtxKey = "slog"

type Database

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

func NewDatabase

func NewDatabase(ctx context.Context, dbFilename string, migrations []string) (*Database, error)

func (*Database) Close

func (db *Database) Close() error

func (*Database) ReadTX

func (db *Database) ReadTX(ctx context.Context, fn TxFn) (err error)

func (*Database) WriteTX

func (db *Database) WriteTX(ctx context.Context, fn TxFn) (err error)

type EmbeddedNATsServer

type EmbeddedNATsServer struct {
	NatsServer *server.Server
}

func NewEmbeddedNATsServer

func NewEmbeddedNATsServer(ctx context.Context, clearData bool) (*EmbeddedNATsServer, error)

func (*EmbeddedNATsServer) Client

func (n *EmbeddedNATsServer) Client() (*nats.Conn, error)

func (*EmbeddedNATsServer) Close

func (n *EmbeddedNATsServer) Close() error

func (*EmbeddedNATsServer) WaitForServer

func (n *EmbeddedNATsServer) WaitForServer()

type ErrGroupSeparateCtx

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

func NewErrGroupSeparateCtx

func NewErrGroupSeparateCtx() *ErrGroupSeparateCtx

func (*ErrGroupSeparateCtx) Go

func (egc *ErrGroupSeparateCtx) Go(ctx context.Context, funcs ...CtxErrFunc)

func (*ErrGroupSeparateCtx) Wait

func (egc *ErrGroupSeparateCtx) Wait() error

type ErrGroupSharedCtx

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

func NewErrGroupSharedCtx

func NewErrGroupSharedCtx(ctx context.Context, funcs ...CtxErrFunc) *ErrGroupSharedCtx

func (*ErrGroupSharedCtx) Go

func (egc *ErrGroupSharedCtx) Go(funcs ...CtxErrFunc)

func (*ErrGroupSharedCtx) Wait

func (egc *ErrGroupSharedCtx) Wait() error

type Float

type Float interface {
	~float32 | ~float64
}

type Integer

type Integer interface {
	~int | ~uint8 | ~int8 | ~uint16 | ~int16 | ~uint32 | ~int32 | ~uint64 | ~int64
}

type MustProtobufHandler added in v0.2.0

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

func NewProtobufHandler added in v0.2.0

func NewProtobufHandler(isJSON bool) *MustProtobufHandler

func (*MustProtobufHandler) Marshal added in v0.2.0

func (h *MustProtobufHandler) Marshal(msg proto.Message) []byte

func (*MustProtobufHandler) Unmarshal added in v0.2.0

func (h *MustProtobufHandler) Unmarshal(b []byte, msg proto.Message)

type SSEEvent added in v0.0.21

type SSEEvent struct {
	Id                string
	Event             string
	Data              []string
	Retry             time.Duration
	SkipMinBytesCheck bool
}

type SSEEventOption added in v0.0.21

type SSEEventOption func(*SSEEvent)

func WithSSEEvent added in v0.0.32

func WithSSEEvent(event string) SSEEventOption

func WithSSEId added in v0.0.32

func WithSSEId(id string) SSEEventOption

func WithSSERetry added in v0.0.32

func WithSSERetry(retry time.Duration) SSEEventOption

func WithSSESkipMinBytesCheck added in v0.0.32

func WithSSESkipMinBytesCheck(skip bool) SSEEventOption

type ServerSentEventsHandler added in v0.0.21

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

func NewSSE added in v0.0.21

func (*ServerSentEventsHandler) Send added in v0.0.21

func (sse *ServerSentEventsHandler) Send(data string, opts ...SSEEventOption)

func (*ServerSentEventsHandler) SendMultiData added in v0.0.32

func (sse *ServerSentEventsHandler) SendMultiData(dataArr []string, opts ...SSEEventOption)

type TxFn

type TxFn func(tx *sqlite.Conn) error

type TypedIdFunc

type TypedIdFunc[T protoreflect.ProtoMessage] func(T) string

type TypedKV

type TypedKV[T proto.Message] struct {
	// contains filtered or unexported fields
}

func UpsertTypedKV

func UpsertTypedKV[T protoreflect.ProtoMessage](js nats.JetStreamContext, cfg *nats.KeyValueConfig, newFn TypedNewFunc[T], idFn TypedIdFunc[T]) (*TypedKV[T], error)

func (*TypedKV[T]) All

func (tkv *TypedKV[T]) All() (out []T, err error)

func (*TypedKV[T]) Batch

func (tkv *TypedKV[T]) Batch(values ...T) (err error)

func (*TypedKV[T]) Delete

func (tkv *TypedKV[T]) Delete(value T) (err error)

func (*TypedKV[T]) DeleteKey

func (tkv *TypedKV[T]) DeleteKey(key string) (err error)

func (*TypedKV[T]) Get

func (tkv *TypedKV[T]) Get(key string) (T, uint64, error)

func (*TypedKV[T]) Keys

func (tkv *TypedKV[T]) Keys() ([]string, error)

func (*TypedKV[T]) Load

func (tkv *TypedKV[T]) Load(keys ...string) (loaded []T, err error)

func (*TypedKV[T]) Set

func (tkv *TypedKV[T]) Set(value T) (revision uint64, err error)

func (*TypedKV[T]) Update

func (tkv *TypedKV[T]) Update(value T, last uint64) (revision uint64, err error)

func (*TypedKV[T]) Watch added in v0.0.20

func (tkv *TypedKV[T]) Watch(ctx context.Context, key string, opts ...nats.WatchOpt) (values <-chan T, stop func() error, err error)

func (*TypedKV[T]) WatchAll added in v0.0.20

func (tkv *TypedKV[T]) WatchAll(ctx context.Context, opts ...nats.WatchOpt) (values <-chan T, stop func() error, err error)

type TypedNewFunc

type TypedNewFunc[T protoreflect.ProtoMessage] func() T

Directories

Path Synopsis
hot

Jump to

Keyboard shortcuts

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