Documentation
¶
Overview ¶
Package utils provides common utility functions used throughout the SDK.
Index ¶
- Constants
- func AddSingleQuotes(s []string) []string
- func AppendSlices[T any](slices ...[]T) []T
- func Belongs[T comparable](input []T, elem T) bool
- func CleanupString(s string) string
- func CleanupStrings(s []string) []string
- func DeleteIndex[S ~[]E, E any](s S, i int) S
- func Detach(ctx context.Context) context.Context
- func DirExists(dirName string) bool
- func Err(errors ...error) error
- func ExpandHome(path string) string
- func FileExists(filename string) bool
- func FindErrorLogs(rootDirs ...string)
- func FormatAmount(amount *big.Int, decimals uint8) string
- func GetAPIContext() (context.Context, context.CancelFunc)
- func GetAPILargeContext() (context.Context, context.CancelFunc)
- func GetSHA256FromDisk(binPath string) (string, error)
- func GetStacktrace(all bool) string
- func GetTimedContext(timeout time.Duration) (context.Context, context.CancelFunc)
- func IsSortedAndUnique[T Sortable[T]](s []T) bool
- func IsSortedAndUniqueByHash[T ~[]byte](s []T) bool
- func IsSortedAndUniqueOrdered[T cmp.Ordered](s []T) bool
- func IsSortedBytes[T ~[]byte](s []T) bool
- func IsSubPath(childPath, parentPath string) bool
- func Map[T, U any](input []T, f func(T) U) []U
- func PointersSlice[T any](values []T) []*T
- func RandomBytes(n int) []byte
- func Retry[T any](fn func() (T, error), maxAttempts int, retryInterval time.Duration) (T, error)
- func RetryWithContextGen[T any](ctxGen func() (context.Context, context.CancelFunc), ...) (T, error)
- func SearchSHA256File(file []byte, toSearch string) (string, error)
- func Sort[T Sortable[T]](s []T)
- func SortByHash[T ~[]byte](s []T)
- func SplitComaSeparatedInt(s string) []int
- func SplitComaSeparatedString(s string) []string
- func SplitStringWithQuotes(str string, r rune) []string
- func TrimHexa(s string) string
- func Uint32Sort(arr []uint32)
- func Unique[T comparable](arr []T) []T
- func WrapContext[T any](f func() (T, error)) func(context.Context) (T, error)
- func Zero[T any]() T
- type Atomic
- type BytesPool
- type Sortable
Constants ¶
const CGOEnabled = true
CGOEnabled indicates whether CGO is available. This file is only compiled when CGO is enabled.
Variables ¶
This section is empty.
Functions ¶
func AddSingleQuotes ¶
AddSingleQuotes adds single quotes to each string in the given slice.
func AppendSlices ¶
func AppendSlices[T any](slices ...[]T) []T
AppendSlices appends multiple slices into a single slice.
func Belongs ¶
func Belongs[T comparable](input []T, elem T) bool
func CleanupString ¶
CleanupString cleans up a string by trimming \r and \n characters.
func CleanupStrings ¶
CleanupStrings cleans up a slice of strings by trimming \r and \n characters.
func DeleteIndex ¶
DeleteIndex moves the last element in the slice to index [i] and shrinks the size of the slice by 1.
This is an O(1) operation that allows the removal of an element from a slice when the order of the slice is not important.
If [i] is out of bounds, this function will panic.
func FindErrorLogs ¶
func FindErrorLogs(rootDirs ...string)
FindErrorLogs is a utility function, we will NOT do error handling, as this is supposed to be called during error handling itself we don't want to make it even more complex
func FormatAmount ¶
FormatAmount formats an amount of base units as a string representing the amount in the given denomination. (i.e. An amount of 54321 with a decimals value of 3 results in the string "54.321")
func GetAPIContext ¶
func GetAPIContext() (context.Context, context.CancelFunc)
GetAPIContext returns a context for API requests.
func GetAPILargeContext ¶
func GetAPILargeContext() (context.Context, context.CancelFunc)
GetAPILargeContext returns a context for API requests with large timeout.
func GetSHA256FromDisk ¶
func GetStacktrace ¶
func GetTimedContext ¶
GetTimedContext returns a context with the given timeout.
func IsSortedAndUnique ¶
Returns true iff the elements in [s] are unique and sorted.
func IsSortedAndUniqueByHash ¶
Returns true iff the elements in [s] are unique and sorted based by their hashes.
func IsSortedAndUniqueOrdered ¶
Returns true iff the elements in [s] are unique and sorted.
func IsSortedBytes ¶
Returns true iff the elements in [s] are sorted.
func IsSubPath ¶
IsSubPath checks if childPath is inside parentPath. Both paths should be absolute paths. Returns true if childPath is a subdirectory or file inside parentPath.
func PointersSlice ¶
func PointersSlice[T any](values []T) []*T
PointersSlice converts a slice of values to a slice of pointers
func RandomBytes ¶
RandomBytes returns a slice of n random bytes Intended for use in testing
func Retry ¶
Retry retries the given function until it succeeds or the maximum number of attempts is reached.
func RetryWithContextGen ¶
func RetryWithContextGen[T any]( ctxGen func() (context.Context, context.CancelFunc), fn func(context.Context) (T, error), maxAttempts int, retryInterval time.Duration, ) (T, error)
RetryWithContextGen retries the given function until it succeeds or the maximum number of attempts is reached. For each retry, it generates a fresh context to be used on the call.
func SortByHash ¶
func SortByHash[T ~[]byte](s []T)
Sorts the elements of [s] based on their hashes.
func SplitComaSeparatedInt ¶
SplitComaSeparatedInt splits a comma-separated string into a slice of integers.
func SplitComaSeparatedString ¶
SplitComaSeparatedString splits and trims a comma-separated string into a slice of strings.
func SplitStringWithQuotes ¶
SplitStringWithQuotes splits a string by the given rune, ignoring quoted segments.
func Uint32Sort ¶
func Uint32Sort(arr []uint32)
func Unique ¶
func Unique[T comparable](arr []T) []T
Unique returns a new slice containing only the unique elements from the input slice.
func WrapContext ¶
WrapContext adds a context based timeout to a given function
Types ¶
type Atomic ¶
Atomic is a re-export from the standalone atomic module for backward compatibility. Use github.com/luxfi/atomic directly for new code.
type BytesPool ¶
BytesPool tracks buckets of available buffers to be allocated. Each bucket allocates buffers of the following length:
0 1 3 7 15 31 63 127 ... MaxInt
In order to allocate a buffer of length 19 (for example), we calculate the number of bits required to represent 19 (5). And therefore allocate a slice from bucket 5, which has length 31. This is the bucket which produces the smallest slices that are at least length 19.
When replacing a buffer of length 19, we calculate the number of bits required to represent 20 (5). And therefore place the slice into bucket 4, which has length 15. This is the bucket which produces the largest slices that a length 19 slice can be used for.
func NewBytesPool ¶
func NewBytesPool() *BytesPool
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package bimap provides a bidirectional map implementation.
|
Package bimap provides a bidirectional map implementation. |
|
Package json provides JSON serialization utilities for numeric types and a gorilla/rpc Codec that normalizes lowercase method names.
|
Package json provides JSON serialization utilities for numeric types and a gorilla/rpc Codec that normalizes lowercase method names. |