Documentation
¶
Overview ¶
Package utils is an assortment of independent utilities.
Index ¶
- func AnyToString(o any) string
- func CatchPanic(f func() error) (err error)
- func EvaluateBoolExp(boolExp string, symbols map[string]any) (bool, error)
- func IsLowerCaseIdentifier(id string) bool
- func IsNil(x any) bool
- func IsUpperCaseIdentifier(id string) bool
- func LooksLikeJWT(token string) bool
- func SourceCodeSHA256(directory string) (string, error)
- func StringClaimFromJWT(token string, name string) (value string, ok bool)
- func ToKebabCase(id string) string
- func ToSnakeCase(id string) string
- func UnsafeBytesToString(b []byte) string
- func UnsafeStringToBytes(s string) []byte
- func ValidateConfigName(name string) error
- func ValidateHostname(hostname string) error
- func ValidateTickerName(name string) error
- type SyncMap
- func (sm *SyncMap[K, V]) Delete(key K) (value V, deleted bool)
- func (sm *SyncMap[K, V]) DoUnderLock(callback func(m map[K]V))
- func (sm *SyncMap[K, V]) Keys() (keys []K)
- func (sm *SyncMap[K, V]) Load(key K) (value V, ok bool)
- func (sm *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
- func (sm *SyncMap[K, V]) LoadOrStoreFunc(key K, value func() V) (actual V, loaded bool)
- func (sm *SyncMap[K, V]) Snapshot() (copy map[K]V)
- func (sm *SyncMap[K, V]) Store(key K, value V)
- func (sm *SyncMap[K, V]) Values() (values []V)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AnyToString ¶ added in v1.13.1
AnyToString returns the string representation of the object. It looks for a TextMarshaler or Stringer interfaces first before defaulting to fmt.Sprintf.
func CatchPanic ¶
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
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 ¶
IsLowerCaseIdentifier accepts only lowerCaseIdentifiers.
func IsUpperCaseIdentifier ¶
IsUpperCaseIdentifier accepts only UpperCaseIdentifiers.
func LooksLikeJWT ¶ added in v1.13.1
LooksLikeJWT checks if the token is likely to be a signed representation of a JWT.
func SourceCodeSHA256 ¶
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
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 ¶
ToKebabCase converts a CamelCase identifier to kebab-case. Consecutive non-letters or numbers are compressed into a single hyphen.
func ToSnakeCase ¶
ToSnakeCase converts a CamelCase identifier to snake_case. Consecutive non-letters are compressed into a single underscore.
func UnsafeBytesToString ¶ added in v1.13.1
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
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 ¶
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 ¶
ValidateHostname indicates if the hostname is a valid microservice hostname. Hostnames must contain only alphanumeric characters, hyphens, underscores and dot separators.
func ValidateTickerName ¶
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]) 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 ¶
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 ¶
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
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.