utils

package
v1.19.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package utils is an assortment of independent utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyToString added in v1.13.1

func AnyToString(o any) string

AnyToString returns the string representation of the object. It looks for a TextMarshaler or Stringer interfaces first before defaulting to fmt.Sprintf.

func CatchPanic

func CatchPanic(f func() error) (err error)

CatchPanic calls the given function and returns any panic as a standard error. Deprecated: Use errors.CatchPanic instead.

func EvaluateBoolExp added in v1.13.1

func EvaluateBoolExp(boolExp string, symbols map[string]any) (bool, error)

EvaluateBoolExp evaluates a boolean expression such as "foo=='bar' && (y==3 || y<0)". String literals must be quoted using single or double quotes. Logical operators: &&, ||, !. Comparison operators: ==, !=, <, <=, >, >=, =~ (regexp), !~ (negate regexp).

func IsLowerCaseIdentifier

func IsLowerCaseIdentifier(id string) bool

IsLowerCaseIdentifier accepts only lowerCaseIdentifiers.

func IsNil added in v1.17.0

func IsNil(x any) bool

IsNil returns true if x is nil or an interface holding nil.

func IsUpperCaseIdentifier

func IsUpperCaseIdentifier(id string) bool

IsUpperCaseIdentifier accepts only UpperCaseIdentifiers.

func LooksLikeJWT added in v1.13.1

func LooksLikeJWT(token string) bool

LooksLikeJWT checks if the token is likely to be a signed representation of a JWT.

func SourceCodeSHA256

func SourceCodeSHA256(directory string) (string, error)

SourceCodeSHA256 generates a SHA256 of the source code files in the indicated directory and its sub-directories. The directory is interpreted relative to the current working directory. Use "." to hash the current working directory.

func StringClaimFromJWT added in v1.13.1

func StringClaimFromJWT(token string, name string) (value string, ok bool)

StringClaimFromJWT extracts a claim from a JWT with minimal memory allocations without fully parsing it. The claim must be a string, i.e. appear as "name":"value" in the claim part.

func ToKebabCase

func ToKebabCase(id string) string

ToKebabCase converts a CamelCase identifier to kebab-case. Consecutive non-letters or numbers are compressed into a single hyphen.

func ToSnakeCase

func ToSnakeCase(id string) string

ToSnakeCase converts a CamelCase identifier to snake_case. Consecutive non-letters are compressed into a single underscore.

func UnsafeBytesToString added in v1.13.1

func UnsafeBytesToString(b []byte) string

UnsafeBytesToString converts a slice of bytes to a string with no memory allocation. The original byte slice data should not be modified.

func UnsafeStringToBytes added in v1.13.1

func UnsafeStringToBytes(s string) []byte

UnsafeStringToBytes converts a string to a slice of bytes with no memory allocation. The slice points to the original data of the string and should not be modified.

func ValidateConfigName

func ValidateConfigName(name string) error

ValidateConfigName indicates if the name can be used for a config. Config names must start with a letter and contain only alphanumeric characters, hyphens or underscores.

func ValidateHostname

func ValidateHostname(hostname string) error

ValidateHostname indicates if the hostname is a valid microservice hostname. Hostnames must contain only alphanumeric characters, hyphens, underscores and dot separators.

func ValidateTickerName

func ValidateTickerName(name string) error

ValidateTickerName indicates if the name can be used for a ticker. Ticker names must start with a letter and contain only alphanumeric characters, hyphens or underscores.

Types

type SyncMap

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

SyncMap is a map protected by a mutex.

func (*SyncMap[K, V]) Delete

func (sm *SyncMap[K, V]) Delete(key K) (value V, deleted bool)

Delete deletes the value for a key.

func (*SyncMap[K, V]) DoUnderLock added in v1.19.0

func (sm *SyncMap[K, V]) DoUnderLock(callback func(m map[K]V))

DoUnderLock obtains a lock and passes the internal map to the callback.

func (*SyncMap[K, V]) Keys added in v1.19.0

func (sm *SyncMap[K, V]) Keys() (keys []K)

Keys returns all keys.

func (*SyncMap[K, V]) Load

func (sm *SyncMap[K, V]) Load(key K) (value V, ok bool)

Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.

func (*SyncMap[K, V]) LoadOrStore

func (sm *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*SyncMap[K, V]) LoadOrStoreFunc added in v1.14.0

func (sm *SyncMap[K, V]) LoadOrStoreFunc(key K, value func() V) (actual V, loaded bool)

LoadOrStoreFunc returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*SyncMap[K, V]) Snapshot added in v1.19.0

func (sm *SyncMap[K, V]) Snapshot() (copy map[K]V)

Snapshot returns a shallow copy of the internal map.

func (*SyncMap[K, V]) Store

func (sm *SyncMap[K, V]) Store(key K, value V)

Store sets the value for a key.

func (*SyncMap[K, V]) Values added in v1.19.0

func (sm *SyncMap[K, V]) Values() (values []V)

Values returns all values.

Jump to

Keyboard shortcuts

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