Documentation
¶
Overview ¶
Package utils is an assortment of independent utilities.
Index ¶
- func AnyToString(o any) string
- func IsLowerCaseIdentifier(id string) bool
- func IsNil(x any) bool
- func IsUpperCaseIdentifier(id string) bool
- func LooksLikeJWT(token string) bool
- func RandomIdentifier(length int) string
- func Testing() (testFuncName string, underTest bool)
- func ToKebabCase(id string) string
- func ToSnakeCase(id string) string
- func UnsafeBytesToString(b []byte) string
- func UnsafeStringToBytes(s string) []byte
- 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 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 RandomIdentifier ¶ added in v1.22.0
RandomIdentifier generates a random string of the specified length. The string will include only alphanumeric characters a-z, A-Z, 0-9. Digits 0 and 1 are slightly overrepresented (2/64 vs 1/64) due to padding the 62-character alphabet to a power of two.
func Testing ¶ added in v1.21.0
Testing indicates if the code is running inside a unit test, and if so, the test function name as well.
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.
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.