util

package
v0.15.2 Latest Latest
Warning

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

Go to latest
Published: May 16, 2023 License: MPL-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// StopIteration can be returned by the RingBuffer.Iter or MapRingBuffer callbacks to stop iteration immediately.
	StopIteration = errors.New("stop iteration")

	// SkipItem can be returned by the MapRingBuffer callback to skip adding a specific item.
	SkipItem = errors.New("skip item")
)
View Source
var GJSONEscaper = strings.NewReplacer(
	`\`, `\\`,
	".", `\.`,
	"|", `\|`,
	"#", `\#`,
	"@", `\@`,
	"*", `\*`,
	"?", `\?`)
View Source
var MimeExtensionSanityOverrides = map[string]string{
	"image/png":  ".png",
	"image/webp": ".webp",
	"image/jpeg": ".jpg",
	"image/tiff": ".tiff",
	"image/heif": ".heic",
	"image/heic": ".heic",

	"audio/mpeg":  ".mp3",
	"audio/ogg":   ".ogg",
	"audio/webm":  ".webm",
	"audio/x-caf": ".caf",
	"video/mp4":   ".mp4",
	"video/mpeg":  ".mpeg",
	"video/webm":  ".webm",

	"text/plain": ".txt",
	"text/html":  ".html",

	"application/xml": ".xml",
}

MimeExtensionSanityOverrides includes extensions for various common mimetypes.

This is necessary because sometimes the OS mimetype database and Go interact in weird ways, which causes very obscure extensions to be first in the array for common mimetypes (e.g. image/jpeg -> .jpe, text/plain -> ,v).

Functions

func CallerWithFunctionName added in v0.15.2

func CallerWithFunctionName(pc uintptr, file string, line int) string

CallerWithFunctionName is an implementation for zerolog.CallerMarshalFunc that includes the caller function name in addition to the file and line number.

Use as

zerolog.CallerMarshalFunc = util.CallerWithFunctionName

func ExtensionFromMimetype

func ExtensionFromMimetype(mimetype string) string

func GJSONPath added in v0.12.2

func GJSONPath(path ...string) string

func MapRingBuffer added in v0.15.1

func MapRingBuffer[Key comparable, Value, Output any](rb *RingBuffer[Key, Value], callback func(key Key, val Value) (Output, error)) ([]Output, error)

func MarshalAndDeleteEmpty added in v0.12.1

func MarshalAndDeleteEmpty(marshalable interface{}, paths []string) ([]byte, error)

MarshalAndDeleteEmpty marshals a JSON object, then uses gjson to delete empty objects at the given gjson paths.

This can be used as a convenient way to create a marshaler that omits empty non-pointer structs. See mautrix.RespSync for example.

func RandomBytes added in v0.12.0

func RandomBytes(n int) []byte

func RandomString added in v0.12.0

func RandomString(n int) string

RandomString generates a random string of the given length.

func RandomToken added in v0.12.1

func RandomToken(namespace string, randomLength int) string

Types

type DualError added in v0.12.0

type DualError struct {
	High error
	Low  error
}

func NewDualError added in v0.12.0

func NewDualError(high, low error) DualError

func (DualError) Error added in v0.12.0

func (err DualError) Error() string

func (DualError) Is added in v0.12.0

func (err DualError) Is(other error) bool

func (DualError) Unwrap added in v0.12.0

func (err DualError) Unwrap() error

type ReturnableOnce added in v0.14.0

type ReturnableOnce[Value any] struct {
	// contains filtered or unexported fields
}

ReturnableOnce is a wrapper for sync.Once that can return a value

func (*ReturnableOnce[Value]) Do added in v0.14.0

func (ronce *ReturnableOnce[Value]) Do(fn func() (Value, error)) (Value, error)

type RingBuffer added in v0.14.0

type RingBuffer[Key comparable, Value any] struct {
	// contains filtered or unexported fields
}

func NewRingBuffer added in v0.14.0

func NewRingBuffer[Key comparable, Value any](size int) *RingBuffer[Key, Value]

func (*RingBuffer[Key, Value]) Contains added in v0.14.0

func (rb *RingBuffer[Key, Value]) Contains(val Key) bool

func (*RingBuffer[Key, Value]) Get added in v0.14.0

func (rb *RingBuffer[Key, Value]) Get(key Key) (val Value, found bool)

func (*RingBuffer[Key, Value]) Iter added in v0.15.1

func (rb *RingBuffer[Key, Value]) Iter(callback func(key Key, val Value) error) error

func (*RingBuffer[Key, Value]) Push added in v0.14.0

func (rb *RingBuffer[Key, Value]) Push(key Key, val Value)

func (*RingBuffer[Key, Value]) Replace added in v0.14.0

func (rb *RingBuffer[Key, Value]) Replace(key Key, val Value) bool

func (*RingBuffer[Key, Value]) Size added in v0.15.1

func (rb *RingBuffer[Key, Value]) Size() int

type SyncMap added in v0.14.0

type SyncMap[Key comparable, Value any] struct {
	// contains filtered or unexported fields
}

SyncMap is a simple map with a built-in mutex.

func NewSyncMap added in v0.14.0

func NewSyncMap[Key comparable, Value any]() *SyncMap[Key, Value]

func (*SyncMap[Key, Value]) Clone added in v0.14.0

func (sm *SyncMap[Key, Value]) Clone() *SyncMap[Key, Value]

Clone returns a copy of the map.

func (*SyncMap[Key, Value]) CopyData added in v0.14.0

func (sm *SyncMap[Key, Value]) CopyData() map[Key]Value

CopyData returns a copy of the data in the map as a normal (non-atomic) map.

func (*SyncMap[Key, Value]) Delete added in v0.14.0

func (sm *SyncMap[Key, Value]) Delete(key Key)

Delete removes a key from the map.

func (*SyncMap[Key, Value]) Get added in v0.14.0

func (sm *SyncMap[Key, Value]) Get(key Key) (value Value, ok bool)

Get gets a value in the map.

The boolean return parameter is the same as with normal Go map access (true if the key exists, false if not).

func (*SyncMap[Key, Value]) GetOrSet added in v0.14.0

func (sm *SyncMap[Key, Value]) GetOrSet(key Key, value Value) (actual Value, wasGet bool)

GetOrSet gets a value in the map if the key already exists, otherwise inserts the given value and returns it.

The boolean return parameter is true if the key already exists, and false if the given value was inserted.

func (*SyncMap[Key, Value]) Pop added in v0.14.0

func (sm *SyncMap[Key, Value]) Pop(key Key) (value Value, ok bool)

Pop removes a key from the map and returns the old value.

The boolean return parameter is the same as with normal Go map access (true if the key exists, false if not).

func (*SyncMap[Key, Value]) Set added in v0.14.0

func (sm *SyncMap[Key, Value]) Set(key Key, value Value)

Set stores a value in the map.

func (*SyncMap[Key, Value]) Swap added in v0.14.0

func (sm *SyncMap[Key, Value]) Swap(key Key, value Value) (oldValue Value, wasReplaced bool)

Swap sets a value in the map and returns the old value.

The boolean return parameter is true if the value already existed, false if not.

Directories

Path Synopsis
Package base58 provides an API for working with modified base58 and Base58Check encodings.
Package base58 provides an API for working with modified base58 and Base58Check encodings.
Package variationselector provides utility functions for adding and removing emoji variation selectors (16) that matches the suggestions in the Matrix spec.
Package variationselector provides utility functions for adding and removing emoji variation selectors (16) that matches the suggestions in the Matrix spec.

Jump to

Keyboard shortcuts

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