Documentation
¶
Index ¶
- Constants
- Variables
- func ChanReadAll[T any](ch <-chan T) []T
- func GobDecodeBytes[T any](data []byte) (T, error)
- func GobEncodeBytes(v any) ([]byte, error)
- func MaskStr(s string) string
- func MaskStrNotFirst(s string) string
- func MaskStrNotFirstLast(s string) string
- func Must[T any](v T, err error) T
- func PtrVal[T any](p *T, nilVal T) T
- func Ternary[T any](condition bool, rTrue T, rFalse T) T
- type Email
- type ErrorAlreadyDone
- type ErrorCancelled
- type ErrorNotFound
- type ErrorRuntime
- func ErrToRuntime(err error, methodOwner any, methodName string, methodInfo ...string) ErrorRuntime
- func ErrToRuntimePermanent(err error, methodOwner any, methodName string, methodInfo ...string) ErrorRuntime
- func ErrToRuntimeTemporary(err error, methodOwner any, methodName string, methodInfo ...string) ErrorRuntime
- func NewErrorRuntimeFf(msg string, msgArgs ...any) ErrorRuntime
- func NewErrorRuntimePermanentFf(msg string, msgArgs ...any) ErrorRuntime
- func NewErrorRuntimeTemporaryFf(msg string, msgArgs ...any) ErrorRuntime
- func WrapErrorToRuntime(err error, methodOwner any, methodName string, methodInfo ...string) ErrorRuntime
- type ErrorUnprocessable
- type ErrorValidation
- type SyncMap
- func (m *SyncMap[K, V]) Delete(k K)
- func (m *SyncMap[K, V]) DeleteAndGet(k K) (V, bool)
- func (m *SyncMap[K, V]) DeleteIf(k K, fn func(v V) bool)
- func (m *SyncMap[K, V]) Get(k K) (V, bool)
- func (m *SyncMap[K, V]) Keys() []K
- func (m *SyncMap[K, V]) Len() int
- func (m *SyncMap[K, V]) Modify(k K, fn func(v V) V)
- func (m *SyncMap[K, V]) Set(k K, v V)
- func (m *SyncMap[K, V]) Truncate()
Constants ¶
const EmailLengthMax = 255
const EmailLengthMin = 8 // Min 2 chars for each part + "@" and "."
Variables ¶
var EmailNil = Email{}
Functions ¶
func ChanReadAll ¶
func ChanReadAll[T any](ch <-chan T) []T
func GobDecodeBytes ¶ added in v0.6.0
GobDecodeBytes
See bytes.NewBuffer() See gob.NewDecoder().Decode().
func GobEncodeBytes ¶ added in v0.6.0
GobEncodeBytes
See bytes.NewBuffer() See gob.NewEncoder().Encode().
func MaskStr ¶ added in v0.2.0
MaskStr
Examples:
- "" --> ""
- "s" --> "*"
- "Secret-codE" --> "***********"
func MaskStrNotFirst ¶ added in v0.2.0
MaskStrNotFirst
Examples:
- "" --> ""
- "s" --> "s"
- "Secret-codE" --> "S**********"
func MaskStrNotFirstLast ¶ added in v0.2.0
MaskStrNotFirstLast
Examples:
- "" --> ""
- "s" --> "s"
- "se" --> "se"
- "Secret-codE" --> "S*********E"
Types ¶
type Email ¶
type Email struct {
// contains filtered or unexported fields
}
func EmailFromStringMust ¶
type ErrorAlreadyDone ¶
type ErrorAlreadyDone struct {
// contains filtered or unexported fields
}
func NewErrorAlreadyDoneFf ¶
func NewErrorAlreadyDoneFf(msg string, msgArgs ...any) ErrorAlreadyDone
NewErrorAlreadyDoneFf
Panics in case of empty arguments:
- msg
func (ErrorAlreadyDone) Error ¶
func (e ErrorAlreadyDone) Error() string
type ErrorCancelled ¶ added in v0.6.0
type ErrorCancelled struct {
// contains filtered or unexported fields
}
func NewErrorCancelledFf ¶ added in v0.6.0
func NewErrorCancelledFf(msg string, msgArgs ...any) ErrorCancelled
NewErrorCancelledFf
Panics in case of empty arguments:
- msg
func (ErrorCancelled) Error ¶ added in v0.6.0
func (e ErrorCancelled) Error() string
type ErrorNotFound ¶
type ErrorNotFound struct {
// contains filtered or unexported fields
}
func NewErrorNotFoundFf ¶
func NewErrorNotFoundFf(msg string, msgArgs ...any) ErrorNotFound
NewErrorNotFoundFf
Panics in case of empty arguments:
- msg
func (ErrorNotFound) Error ¶
func (e ErrorNotFound) Error() string
type ErrorRuntime ¶
type ErrorRuntime struct {
// contains filtered or unexported fields
}
ErrorRuntime
Client code should be able to distinguish business-logic errors from others. Moreover, each type of business-logic error may be handled differently. Runtime errors are typically handled uniformly (e.g: logged, reported, translated into an HTTP 500 response).
Go does not allow us to mark business-logic errors and then check that mark in the client code in a simple way, like some other OOP languages do (e.g., through inheritance and try‑catch statements), but it allows to get the same result in another way -- by wrapping all non-business-logic errors into a specific error type.
I.e:
try { ... }
catch (BusinessLogicException) { /* ... */ }
catch (Exception) { /* any other error -- in this case non-business-logic */ }
-->
switch err.(type) {
case nil:
case std.ErrorRuntime: /* any other error -- in this case non-business-logic */
default: /* ... */
}
--
For some cases it may be important to distinguish temporary runtime errors from permanent (e.g. for retry strategies). ErrorRuntime has a special flag for this -- IsTemporary() / IsPermanent().
--
Usage example:
package example
func SomeFunc() error {
v, err := ...
if err != nil {
return std.ErrToRuntime(err, "example", "SomeFunc")
}
...
}
type MyType struct { ... }
func (m *MyType) SomeMethod() (..., error) {
...
if err != nil {
return std.ErrToRuntime(err, m, "SomeMethod")
}
...
}
func ErrToRuntime ¶ added in v0.6.0
func ErrToRuntime(err error, methodOwner any, methodName string, methodInfo ...string) ErrorRuntime
ErrToRuntime
Wraps provided error into ErrorRuntime.
Adds this kind of prefix to the error to imitate stack trace: "{{`methodOwner`}}.{{`methodName`}}/{{joined by "/" elements of the `methodInfo`}}".
If `err` is a temporary ErrorRuntime (wrapped or not -- errors.As() is used as a checker), result error also will be marked as temporary, otherwise -- permanent. To mark result error by force use ErrToRuntimePermanent() or ErrToRuntimeTemporary().
Panics in case of empty arguments:
- err
- methodOwner
- methodName
Arguments:
- `methodOwner` -- string or any method owner instance. {{`methodOwner`}} will be replaced by the string value or method owner instance type accordingly. String value is allowed, for example, to accept package names, when a regular function is called, not a method.
- `methodName` -- name of the method / function, that was called.
- `methodInfo` -- any additional info.
func ErrToRuntimePermanent ¶ added in v0.6.0
func ErrToRuntimePermanent(err error, methodOwner any, methodName string, methodInfo ...string) ErrorRuntime
ErrToRuntimePermanent
Same as ErrToRuntime, but marks result error as permanent, even if source error was temporary.
func ErrToRuntimeTemporary ¶ added in v0.6.0
func ErrToRuntimeTemporary(err error, methodOwner any, methodName string, methodInfo ...string) ErrorRuntime
ErrToRuntimeTemporary
Same as ErrToRuntime, but marks result error as temporary, even if source error was permanent.
func NewErrorRuntimeFf ¶
func NewErrorRuntimeFf(msg string, msgArgs ...any) ErrorRuntime
NewErrorRuntimeFf
DEPRECATED: use NewErrorRuntimePermanentFf() or NewErrorRuntimeTemporaryFf() instead.
func NewErrorRuntimePermanentFf ¶ added in v0.6.0
func NewErrorRuntimePermanentFf(msg string, msgArgs ...any) ErrorRuntime
NewErrorRuntimePermanentFf
See ErrorRuntime for details.
func NewErrorRuntimeTemporaryFf ¶ added in v0.6.0
func NewErrorRuntimeTemporaryFf(msg string, msgArgs ...any) ErrorRuntime
NewErrorRuntimeTemporaryFf
See ErrorRuntime for details.
func WrapErrorToRuntime ¶
func WrapErrorToRuntime(err error, methodOwner any, methodName string, methodInfo ...string) ErrorRuntime
WrapErrorToRuntime
DEPRECATED: use ErrToRuntime() instead.
func (ErrorRuntime) Error ¶
func (e ErrorRuntime) Error() string
func (ErrorRuntime) IsPermanent ¶ added in v0.6.0
func (e ErrorRuntime) IsPermanent() bool
IsPermanent
Same as "not IsTemporary()".
See NewErrorRuntimePermanentFf(). See ErrToRuntimePermanent().
func (ErrorRuntime) IsTemporary ¶ added in v0.6.0
func (e ErrorRuntime) IsTemporary() bool
IsTemporary
See NewErrorRuntimeTemporaryFf(). See ErrToRuntimeTemporary().
func (ErrorRuntime) Unwrap ¶
func (e ErrorRuntime) Unwrap() error
type ErrorUnprocessable ¶
type ErrorUnprocessable struct {
// contains filtered or unexported fields
}
func NewErrorUnprocessableFf ¶
func NewErrorUnprocessableFf(msg string, msgArgs ...any) ErrorUnprocessable
NewErrorUnprocessableFf
Panics in case of empty arguments:
- msg
func (ErrorUnprocessable) Error ¶
func (e ErrorUnprocessable) Error() string
type ErrorValidation ¶
type ErrorValidation struct {
// contains filtered or unexported fields
}
func NewErrorValidationFf ¶
func NewErrorValidationFf(msg string, msgArgs ...any) ErrorValidation
NewErrorValidationFf
Panics in case of empty arguments:
- msg
func (ErrorValidation) Error ¶
func (e ErrorValidation) Error() string
type SyncMap ¶ added in v0.4.0
type SyncMap[K comparable, V any] struct { // contains filtered or unexported fields }
func NewSyncMap ¶ added in v0.4.0
func NewSyncMap[K comparable, V any]() *SyncMap[K, V]
func (*SyncMap[K, V]) Delete ¶ added in v0.4.0
func (m *SyncMap[K, V]) Delete(k K)
Delete
Removes association with the key `k`. Does nothing, if key `k` does not exist.
func (*SyncMap[K, V]) DeleteAndGet ¶ added in v0.4.0
DeleteAndGet
Removes association with the key `k` and then returns associated value like the Get() method does.
func (*SyncMap[K, V]) DeleteIf ¶ added in v0.6.0
DeleteIf
Removes association with the key `k`, if `fn` returns `true`. Does not call `fn`, if key `k` does not exist.
func (*SyncMap[K, V]) Modify ¶ added in v0.4.0
func (m *SyncMap[K, V]) Modify(k K, fn func(v V) V)
Modify
(Re-)Associates key `k` with value `v` modified by the `fn(v)`. If key `k` does not exist, `fn` gets zero-value argument.