Documentation
¶
Overview ¶
Package jet is a pragmatic toolkit for building Go microservices.
It provides the building blocks most services re-implement from scratch:
- CLogger — a structured, context-aware logger on top of log/slog.
- ConfigLoader — a typed YAML + environment-variable configuration loader.
- RequestContext — a request context (request id, user, session, roles, …) that propagates across HTTP, gRPC and Kafka boundaries.
- AppError — a structured error model with codes, business/system/panic types and HTTP/gRPC status hints.
- Healthcheck, JWT, crypto, validation, watchdog and generics helpers.
Sub-packages add the rest of a typical service: storage adapters (storages/pg, storages/redis, …), messaging (kafka, event), transport servers (http, grpc), service lifecycle (cluster), concurrency helpers (goroutine, retry) and observability (monitoring, profile).
The toolkit favors boring, explicit building blocks over magic, and is extracted from an internal kit that has run 20+ production services.
Index ¶
- Constants
- Variables
- func Await(fn func() (bool, error), tick, timeout time.Duration) chan error
- func BoolPtr(b bool) *bool
- func BytesToMapInterfaces(bytes []byte) map[string]interface{}
- func ContainsIntersection(slice1, slice2 []string) bool
- func ContextToGrpcMD(ctx context.Context) (metadata.MD, bool)
- func ConvertFromAny[T any](data any) (*T, error)
- func ConvertFromMap[T any](data map[string]interface{}) (*T, error)
- func ConvertMapValues[T any](m map[string]interface{}, converter func(value any) T) map[string]T
- func ConvertSlice[TSrc any, TRes any](src []*TSrc, converter func(*TSrc) *TRes) []*TRes
- func ConvertToMap(data any) (map[string]interface{}, error)
- func Copy(ctx context.Context) context.Context
- func Date(date time.Time) time.Time
- func DecryptString(ctx context.Context, key, val string) (string, error)
- func Diff(a, b time.Time) (year, month, day, hour, min, sec int)
- func Digits(s string) bool
- func EncryptString(ctx context.Context, key, val string) (string, error)
- func Equal(m1, m2 any) bool
- func ExtractUrlExtension(s string) (string, error)
- func Filter[TItem any](slice []TItem, filterFn func(TItem) bool) []TItem
- func First[TItem any](slice []*TItem, selectFn func(*TItem) bool) *TItem
- func Float32Ptr(i float32) *float32
- func Float64Ptr(i float64) *float64
- func ForAll[TItem any](slice []TItem, fn func(TItem)) []TItem
- func FromGrpcMD(ctx context.Context, md metadata.MD) context.Context
- func FromMap(ctx context.Context, mp map[string]interface{}) (context.Context, error)
- func FromSet[TKey comparable](m map[TKey]struct{}) []TKey
- func GenJwtToken(ctx context.Context, rq *JwtRequest) (string, error)
- func GenerateInternalAccessToken(ctx context.Context, secret []byte, ttl int, callerName string) (string, error)
- func GenerateTimeSeries(from, to time.Time, period time.Duration) []time.Time
- func GetDefault[T any]() T
- func GetTzLocation(tz string) *time.Location
- func GroupBy[TItem any, TKey comparable](slice []TItem, keyFn func(TItem) TKey) map[TKey][]TItem
- func HashObj(obj any) uint64
- func Int32ToIntPtr(i *int32) *int
- func Int64ToIntPtr(i *int64) *int
- func IntPtr(i int) *int
- func IntToInt32Ptr(i *int) *int32
- func IntToInt64Ptr(i *int) *int64
- func IsAppErrCode(e error, code string) bool
- func IsCoordinateValid(c string) bool
- func IsEmailValid(email string) bool
- func IsEmpty(object interface{}) bool
- func IsIpV4Valid(ip string) bool
- func IsIpV6Valid(ip string) bool
- func IsPhoneValid(phone string) bool
- func IsPhoneWithCountryCodeValid(code, phone string) bool
- func IsRussianPhoneValid(phone string) bool
- func IsTelegramChannelValid(channel string) bool
- func IsTelegramUsernameValid(username string) bool
- func IsTimeZoneIANA(tz string) bool
- func IsUrlValid(url string) bool
- func IsValidISO639_1(lang string) bool
- func JsonDecode[T any](payload []byte) (*T, error)
- func JsonDecodePlainSlice[T any](payload []byte) ([]T, error)
- func JsonDecodeSlice[T any](payload []byte) ([]*T, error)
- func JsonEncode(v any) ([]byte, error)
- func LeftExclusive[T comparable](left, right []T) []T
- func Map[TItem any, TRes any](slice []TItem, mapFn func(TItem) TRes) []TRes
- func MapInterfacesToBytes(m map[string]interface{}) []byte
- func MapKeys[TKey comparable, TItem any](m map[TKey]TItem) []TKey
- func MapToLowerCamelKeys(m map[string]interface{}) map[string]interface{}
- func MapValues[TKey comparable, TItem any](m map[TKey]TItem) []TItem
- func MapsEqual(m1, m2 map[string]interface{}) bool
- func MaxTime(times ...time.Time) *time.Time
- func Millis(t time.Time) int64
- func MillisFromTime(t time.Time) int64
- func MinTime(times ...time.Time) *time.Time
- func Modified[T comparable](modified bool, cur T, update T) (T, bool)
- func ModifiedNillable[T any](modified bool, cur *T, update *Nillable[T]) (*T, bool)
- func ModifiedPlain[T PlainType](modified bool, cur T, update T) (T, bool)
- func ModifiedPlainNillable[T PlainType](modified bool, cur T, update *T) (T, bool)
- func ModifiedSliceNillable[T constraints.Ordered](modified bool, cur []T, update []T) ([]T, bool)
- func ModifiedSliceStructured[T comparable](modified bool, sort func([]T), cur []T, update []T) ([]T, bool)
- func NanoId() string
- func NewAppError(code string, format string, args ...interface{}) error
- func NewId() string
- func NewRandString() string
- func Nil() string
- func NilOrInMap[T comparable](value *T, m map[T]struct{}) bool
- func Now() time.Time
- func NowDate() time.Time
- func NowMillis() int64
- func NowNanos() int64
- func NowPtr() *time.Time
- func NullToString(s *string) string
- func NumCode(size int) string
- func Overlapped(startA, endA, startB, endB time.Time) bool
- func PaginateSlice[T any](items []T, paging PagingRequest) []T
- func ParseDateAny(s string) *time.Time
- func ParseFloat32(s string) *float32
- func ParseFloat64(s string) *float64
- func ParseInternalAccessToken(ctx context.Context, secret []byte, token string) (string, error)
- func Reduce[TItem any, TRes any, TKey comparable](slice []TItem, grpFn func(TItem) TKey, accFn func(TItem, TRes) TRes) map[TKey]TRes
- func RemoveNonAlfaDigital(str string) string
- func Retry[TIn, TOut any](ctx context.Context, fn func(ctx context.Context, in TIn) (TOut, error), ...) (TOut, error)
- func Round100(value float64) float64
- func Round10000(value float64) float64
- func SliceToMap[TItem any, TKey comparable](slice []TItem, grpFn func(TItem) TKey) map[TKey]TItem
- func SplitArr[T any](data []T, size int) [][]T
- func SplitArrByItemLen[T ~string](data []T, max int) ([][]T, error)
- func StrToInt64(s string) (int64, error)
- func StringPtr(s string) *string
- func StringToNull(s string) *string
- func StringsToInterfaces(sl []string) []interface{}
- func TimeFromMillis(millis int64) time.Time
- func TimePtr(t time.Time) *time.Time
- func ToSet[TItem any, TKey comparable](slice []TItem, keyFn func(TItem) TKey) map[TKey]struct{}
- func ToSlicePtr[T any](src []T) []*T
- func ToStringDate(date *time.Time) string
- func ToTz(t time.Time, tz string) (time.Time, error)
- func TzValid(tz string) bool
- func UInt32Ptr(i uint32) *uint32
- func UInt64ToInt32Ptr(i *uint64) *int32
- func UUID(size int) string
- func ValidateUUIDs(uuids ...string) error
- func VerifyJwtToken(ctx context.Context, token string, secret []byte) (*jwt.Token, jwt.MapClaims, error)
- type Adapter
- type AdapterListener
- type AppErrBuilder
- type AppError
- func (e *AppError) Cause() error
- func (e *AppError) Code() string
- func (e *AppError) Error() string
- func (e *AppError) Fields() KV
- func (e *AppError) GrpcStatus() *uint32
- func (e *AppError) HttpStatus() *uint32
- func (e *AppError) Message() string
- func (e *AppError) Type() string
- func (e *AppError) WithStack() string
- func (e *AppError) WithStackErr() error
- type CLogger
- type CLoggerFunc
- type Check
- type ConfigLoader
- type DayOfWeek
- type DaysOfWeek
- type Error
- type ErrorHook
- type Flags
- func (f Flags[T]) HasAll(flags ...T) bool
- func (f Flags[T]) HasAny(flags ...T) bool
- func (f Flags[T]) MarshalJSON() ([]byte, error)
- func (f Flags[T]) Ptr() *Flags[T]
- func (f Flags[T]) Set(flags ...T) Flags[T]
- func (f Flags[T]) String() string
- func (f Flags[T]) Toggle(flags ...T) Flags[T]
- func (f Flags[T]) Uint() T
- func (f *Flags[T]) UnmarshalJSON(data []byte) error
- func (f Flags[T]) Unset(flags ...T) Flags[T]
- type Healthcheck
- type HealthcheckConfig
- type HourMinTime
- func (t HourMinTime) Before(other HourMinTime) bool
- func (t HourMinTime) FromTime(tm time.Time) HourMinTime
- func (t HourMinTime) Hour() int
- func (t *HourMinTime) MarshalJSON() ([]byte, error)
- func (t HourMinTime) Minute() int
- func (t HourMinTime) MustParse(s string) HourMinTime
- func (t HourMinTime) Parse(s string) (HourMinTime, error)
- func (t HourMinTime) String() string
- func (t HourMinTime) Unix() int64
- func (t *HourMinTime) UnmarshalJSON(b []byte) error
- type JwtRequest
- type KV
- type LogConfig
- type Logger
- type Nillable
- type PageReader
- type PagingRequest
- type PagingRequestG
- type PagingResponse
- type PagingResponseG
- type PlainType
- type RequestContext
- func (r *RequestContext) EN() *RequestContext
- func (r *RequestContext) Empty() *RequestContext
- func (r *RequestContext) GetApp() string
- func (r *RequestContext) GetClientIp() string
- func (r *RequestContext) GetKv() KV
- func (r *RequestContext) GetLang() language.Tag
- func (r *RequestContext) GetRequestId() string
- func (r *RequestContext) GetRoles() []string
- func (r *RequestContext) GetSessionId() string
- func (r *RequestContext) GetUserId() string
- func (r *RequestContext) GetUsername() string
- func (r *RequestContext) TestApp() *RequestContext
- func (r *RequestContext) ToContext(parent context.Context) context.Context
- func (r *RequestContext) ToMap() map[string]interface{}
- func (r *RequestContext) WithApp(app string) *RequestContext
- func (r *RequestContext) WithClientIp(ip string) *RequestContext
- func (r *RequestContext) WithKv(key string, val interface{}) *RequestContext
- func (r *RequestContext) WithLang(lang language.Tag) *RequestContext
- func (r *RequestContext) WithNewRequestId() *RequestContext
- func (r *RequestContext) WithRequestId(requestId string) *RequestContext
- func (r *RequestContext) WithRoles(roles ...string) *RequestContext
- func (r *RequestContext) WithSessionId(sessionId string) *RequestContext
- func (r *RequestContext) WithUser(userId, username string) *RequestContext
- type RetryCfg
- type SafeMap
- type Searchable
- type SortRequest
- type String
- type Strings
- type Suite
- func (s *Suite) AfterTest(suiteName, testName string)
- func (s *Suite) AssertAppErr(err error, code string)
- func (s *Suite) AssertCalled(mock *mock.Mock, methodName string, arguments ...interface{})
- func (s *Suite) AssertNotCalled(mock *mock.Mock, methodName string, arguments ...interface{})
- func (s *Suite) AssertNumberOfCalls(mock *mock.Mock, methodName string, expectedCalls int)
- func (s *Suite) BeforeTest(suiteName, testName string)
- func (s *Suite) Condition(comp assert.Comparison, msgAndArgs ...interface{})
- func (s *Suite) Contains(el, contains interface{}, msgAndArgs ...interface{})
- func (s *Suite) DefaultLogger()
- func (s *Suite) DirExists(dir string, msgAndArgs ...interface{})
- func (s *Suite) ElementsMatch(listA, listB interface{}, msgAndArgs ...interface{})
- func (s *Suite) Empty(object interface{}, msgAndArgs ...interface{})
- func (s *Suite) Equal(expected interface{}, actual interface{}, msgAndArgs ...interface{})
- func (s *Suite) EqualError(theError error, errString string, msgAndArgs ...interface{})
- func (s *Suite) EqualValues(expected interface{}, actual interface{}, msgAndArgs ...interface{})
- func (s *Suite) Error(err error, msgAndArgs ...interface{})
- func (s *Suite) ErrorAs(err, target error, msgAndArgs ...interface{})
- func (s *Suite) ErrorIs(err, target error, msgAndArgs ...interface{})
- func (s *Suite) Eventually(condition func() bool, waitFor time.Duration, tick time.Duration, ...)
- func (s *Suite) Exactly(expected interface{}, actual interface{}, msgAndArgs ...interface{})
- func (s *Suite) False(value bool, msgAndArgs ...interface{})
- func (s *Suite) Fatal(msgAndArgs ...interface{})
- func (s *Suite) FileExists(path string, msgAndArgs ...interface{})
- func (s *Suite) Implements(interfaceObject interface{}, object interface{}, msgAndArgs ...interface{})
- func (s *Suite) InDelta(expected, actual interface{}, delta float64, msgAndArgs ...interface{})
- func (s *Suite) InDeltaMapValues(expected, actual interface{}, delta float64, msgAndArgs ...interface{})
- func (s *Suite) InDeltaSlice(expected, actual interface{}, delta float64, msgAndArgs ...interface{})
- func (s *Suite) InEpsilon(expected, actual interface{}, epsilon float64, msgAndArgs ...interface{})
- func (s *Suite) InEpsilonSlice(expected, actual interface{}, epsilon float64, msgAndArgs ...interface{})
- func (s *Suite) Init(logger CLoggerFunc)
- func (s *Suite) IsType(expectedType interface{}, object interface{}, msgAndArgs ...interface{})
- func (s *Suite) JSONEq(expected string, actual string, msgAndArgs ...interface{})
- func (s *Suite) L() CLogger
- func (s *Suite) Len(object interface{}, length int, msgAndArgs ...interface{})
- func (s *Suite) Never(condition func() bool, waitFor time.Duration, tick time.Duration, ...)
- func (s *Suite) Nil(object interface{}, msgAndArgs ...interface{})
- func (s *Suite) NoDirExists(dir string, msgAndArgs ...interface{})
- func (s *Suite) NoError(err error, msgAndArgs ...interface{})
- func (s *Suite) NoFileExists(path string, msgAndArgs ...interface{})
- func (s *Suite) NotContains(el, contains interface{}, msgAndArgs ...interface{})
- func (s *Suite) NotEmpty(object interface{}, msgAndArgs ...interface{})
- func (s *Suite) NotEqual(expected interface{}, actual interface{}, msgAndArgs ...interface{})
- func (s *Suite) NotEqualValues(expected interface{}, actual interface{}, msgAndArgs ...interface{})
- func (s *Suite) NotErrorIs(err, target error, msgAndArgs ...interface{})
- func (s *Suite) NotNil(object interface{}, msgAndArgs ...interface{})
- func (s *Suite) NotPanics(f assert.PanicTestFunc, msgAndArgs ...interface{})
- func (s *Suite) NotRegexp(rx interface{}, str interface{}, msgAndArgs ...interface{})
- func (s *Suite) NotSame(expected, actual interface{}, msgAndArgs ...interface{})
- func (s *Suite) NotSubset(list, subset interface{}, msgAndArgs ...interface{})
- func (s *Suite) NotZero(v interface{}, msgAndArgs ...interface{})
- func (s *Suite) Panics(f assert.PanicTestFunc, msgAndArgs ...interface{})
- func (s *Suite) PanicsWithError(errString string, f assert.PanicTestFunc, msgAndArgs ...interface{})
- func (s *Suite) PanicsWithValue(expected interface{}, f assert.PanicTestFunc, msgAndArgs ...interface{})
- func (s *Suite) RandPhone() string
- func (s *Suite) Regexp(rx interface{}, str interface{}, msgAndArgs ...interface{})
- func (s *Suite) Same(expected, actual interface{}, msgAndArgs ...interface{})
- func (s *Suite) Subset(list, subset interface{}, msgAndArgs ...interface{})
- func (s *Suite) True(value bool, msgAndArgs ...interface{})
- func (s *Suite) WithinDuration(expected, actual time.Time, delta time.Duration, msgAndArgs ...interface{})
- func (s *Suite) YAMLEq(expected string, actual string, msgAndArgs ...interface{})
- func (s *Suite) Zero(v interface{}, msgAndArgs ...interface{})
- type TFlags
- type TimePeriod
- type TimeRange
- func (t *TimeRange) EndTime() string
- func (t TimeRange) MustParse(from, to string) TimeRange
- func (t TimeRange) Parse(from, to string) (*TimeRange, error)
- func (t TimeRange) ParseOrEmpty(from, to string) *TimeRange
- func (t *TimeRange) StartTime() string
- func (t TimeRange) Valid() bool
- func (t TimeRange) ValidRange() bool
- func (t TimeRange) Within(tm HourMinTime) bool
- func (t TimeRange) WithinExcl(tm HourMinTime) bool
- type Validator
- type WaitGroup
- type Watchdog
Examples ¶
Constants ¶
const ( ErrTypeBusiness = "business" ErrTypeSystem = "system" ErrTypePanic = "panic" ErrCodePanic = "PANIC-001" )
const ( ErrCodeCfgRead = "CFG-001" ErrCodeCfgMerge = "CFG-002" ErrCodeCfgUnmarshal = "CFG-003" ErrCodeNoPathSpecified = "CFG-004" ErrCodeConfigNotLoaded = "CFG-005" )
const ( ErrCodeCryptoEncrypt = "CRP-001" ErrCodeCryptoDecrypt = "CRP-002" )
const ( TzUTC = "UTC" TzP13 = "UTC+13" TzP12 = "UTC+12" TzP11 = "UTC+11" TzP10 = "UTC+10" TzP9 = "UTC+9" TzP8 = "UTC+8" TzP7 = "UTC+7" TzP6p5 = "UTC+6:30" TzP6 = "UTC+6" TzP5 = "UTC+5" TzP4 = "UTC+4" TzP3 = "UTC+3" TzP2 = "UTC+2" TzP1 = "UTC+1" TzM1 = "UTC-1" TzM2 = "UTC-2" TzM3 = "UTC-3" TzM4 = "UTC-4" TzM5 = "UTC-5" TzM6 = "UTC-6" TzM7 = "UTC-7" TzM8 = "UTC-8" TzM9 = "UTC-9" TzM10 = "UTC-10" TzM11 = "UTC-11" )
const ( ClaimCallerName = "caller_name" ErrCodeJwtParse = "JWT-001" ErrCodeJwtMalformed = "JWT-002" ErrCodeJwtTokenGen = "JWT-003" ErrCodeJwtWrongSigningMethod = "JWT-004" )
const ( // PanicLevel level. Nothing below a panic is logged; reserved as the highest threshold. PanicLevel = "panic" // FatalLevel level. Logs and then calls os.Exit(1). FatalLevel = "fatal" // ErrorLevel level. Used for errors that should definitely be noted. ErrorLevel = "error" // WarnLevel level. Non-critical entries that deserve eyes. WarnLevel = "warning" // InfoLevel level. General operational entries about what's going on inside the application. InfoLevel = "info" // DebugLevel level. Usually only enabled when debugging. Very verbose logging. DebugLevel = "debug" // TraceLevel level. Finer-grained informational events than Debug. TraceLevel = "trace" FormatterText = "plain" FormatterJson = "json" )
const ( ErrCodeSysUtilsRetryMaxAttempts = "SU-001" ErrCodeSysUtilsRetryFnEmpty = "SU-002" )
const ( ErrCodeJsonEncode = "TP-001" ErrCodeJsonDecode = "TP-002" )
const (
AppTest = "test"
)
const (
ErrCodeAdapterConfigInvalid = "KIT-001"
)
const (
ErrCodeNotSupportedTz = "DTU-001"
)
const (
ErrCodeSplitLengthExceeded = "ARR-001"
)
const (
ErrCodeValidatorNotEmpty = "VAL-001"
)
const (
ErrCodeWatchdogWorkerStuck = "WDG-001"
)
Variables ¶
var ( ErrCfgRead = func(cause error, path string) error { return NewAppErrBuilder(ErrCodeCfgRead, "read config error: %s", path).Wrap(cause).Err() } ErrCfgMerge = func(cause error, path string) error { return NewAppErrBuilder(ErrCodeCfgMerge, "merge config error: %s", path).Wrap(cause).Err() } ErrCfgUnmarshal = func(cause error) error { return NewAppErrBuilder(ErrCodeCfgUnmarshal, "merge config error").Wrap(cause).Err() } ErrNoPathSpecified = func() error { return NewAppErrBuilder(ErrCodeNoPathSpecified, "neither env nor explicit path is specified").Business().Err() } ErrConfigNotLoaded = func() error { return NewAppErrBuilder(ErrCodeConfigNotLoaded, "config not loaded").Business().Err() } )
var ( ErrCryptoEncrypt = func(ctx context.Context, cause error) error { return NewAppErrBuilder(ErrCodeCryptoEncrypt, "encryption error").C(ctx).Wrap(cause).Err() } ErrCryptoDecrypt = func(ctx context.Context, cause error) error { return NewAppErrBuilder(ErrCodeCryptoDecrypt, "decryption error").C(ctx).Wrap(cause).Err() } )
var ( ErrJwtParse = func(cause error) error { return NewAppErrBuilder(ErrCodeJwtParse, "").Wrap(cause).Err() } ErrJwtMalformed = func() error { return NewAppErrBuilder(ErrCodeJwtMalformed, "").Err() } ErrJwtTokenGen = func(ctx context.Context, cause error) error { return NewAppErrBuilder(ErrCodeJwtTokenGen, "jwt gen").Wrap(cause).Err() } ErrJwtWrongSigningMethod = func(ctx context.Context) error { return NewAppErrBuilder(ErrCodeJwtWrongSigningMethod, "wrong signing method").Err() } )
var ( ErrSysUtilsRetryMaxAttempts = func(ctx context.Context) error { return NewAppErrBuilder(ErrCodeSysUtilsRetryMaxAttempts, "max attempts reached").C(ctx).Err() } ErrSysUtilsRetryFnEmpty = func(ctx context.Context) error { return NewAppErrBuilder(ErrCodeSysUtilsRetryFnEmpty, "function empty").C(ctx).Err() } )
var ( ErrJsonEncode = func(cause error) error { return NewAppErrBuilder(ErrCodeJsonEncode, "encode JSON").Wrap(cause).Err() } ErrJsonDecode = func(cause error) error { return NewAppErrBuilder(ErrCodeJsonDecode, "decode JSON").Wrap(cause).Err() } )
var ( ErrAdapterConfigInvalid = func(ctx context.Context) error { return NewAppErrBuilder(ErrCodeAdapterConfigInvalid, "invalid config").Err() } )
var ( ErrNotSupportedTz = func(err error, tz string) error { return NewAppErrBuilder(ErrCodeNotSupportedTz, "timezone isn't supported").Wrap(err).F(KV{"tz": tz}).Business().Err() } )
var ( ErrPanic = func(ctx context.Context, cause interface{}) error { var causeErr error switch v := cause.(type) { case nil: causeErr = fmt.Errorf("panic") case string: causeErr = fmt.Errorf("%s", v) case error: causeErr = v default: causeErr = fmt.Errorf("%v", v) } return NewAppErrBuilder(ErrCodePanic, "").Wrap(causeErr).C(ctx).Panic().Err() } )
var ( ErrSplitLengthExceeded = func() error { return NewAppErrBuilder(ErrCodeSplitLengthExceeded, "split length exceeded").Business().Err() } )
var ( ErrValidatorNotEmpty = func(ctx context.Context, method, attr string) error { return NewAppErrBuilder(ErrCodeValidatorNotEmpty, "empty: %s", method, attr).F(KV{"method": method}).C(ctx).Business().Err() } )
var ErrWatchdogWorkerStuck = func(workerName string) error { return NewAppErrBuilder(ErrCodeWatchdogWorkerStuck, "worker is stuck"). F(KV{"worker": workerName}). Business(). Err() }
var LanguageISO639_1 = map[string]struct{}{}/* 184 elements not displayed */
LanguageISO639_1 ISO 639-1 language codes set
Functions ¶
func Await ¶
Await allows awaiting some state by periodically hitting fn unless either it returns true or error or timeout It returns nil when fn results true
func BytesToMapInterfaces ¶
func ContainsIntersection ¶
func ConvertFromAny ¶
func ConvertFromMap ¶
func ConvertMapValues ¶
func ConvertSlice ¶
ConvertSlice is a generic function to convert one slice to another with help of converter func
func ConvertToMap ¶
func ExtractUrlExtension ¶
ExtractUrlExtension extract extension from url
func Filter ¶
Example ¶
package main
import (
"fmt"
"github.com/zloevil/jet"
)
func main() {
even := jet.Filter([]int{1, 2, 3, 4}, func(i int) bool { return i%2 == 0 })
fmt.Println(even)
}
Output: [2 4]
func Float32Ptr ¶
func Float64Ptr ¶
func FromSet ¶
func FromSet[TKey comparable](m map[TKey]struct{}) []TKey
func GenJwtToken ¶
func GenJwtToken(ctx context.Context, rq *JwtRequest) (string, error)
GenJwtToken generates a new JWT token
func GenerateInternalAccessToken ¶
func GenerateInternalAccessToken(ctx context.Context, secret []byte, ttl int, callerName string) (string, error)
GenerateInternalAccessToken generates token for internal communications between services
func GenerateTimeSeries ¶
GenerateTimeSeries takes period of time and generates slice of timestamps with the given period step
func GetTzLocation ¶
func GroupBy ¶
func GroupBy[TItem any, TKey comparable](slice []TItem, keyFn func(TItem) TKey) map[TKey][]TItem
GroupBy groups by slice by the key
func Int32ToIntPtr ¶
func Int64ToIntPtr ¶
func IntToInt32Ptr ¶
func IntToInt64Ptr ¶
func IsAppErrCode ¶
IsAppErrCode checks if error interface is asserted to *AppError and contains specific code
func IsCoordinateValid ¶
IsCoordinateValid checks if coordinate valid
func IsEmpty ¶
func IsEmpty(object interface{}) bool
IsEmpty gets whether the specified object is considered empty or not
func IsPhoneValid ¶
IsPhoneValid checks phone format (with country code without special characters)
func IsPhoneWithCountryCodeValid ¶
IsPhoneWithCountryCodeValid checks phone with country code
func IsRussianPhoneValid ¶
IsRussianPhoneValid checks Russian phone format (with country code without special characters)
func IsTelegramChannelValid ¶
func IsTelegramUsernameValid ¶
func IsTimeZoneIANA ¶
IsTimeZoneIANA returns true if provided valid IANA time zone
func IsUrlValid ¶
func IsValidISO639_1 ¶
IsValidISO639_1 if the language is in ISO 639-1 format
func JsonDecode ¶
JsonDecode decodes type from json bytes
func JsonDecodePlainSlice ¶
JsonDecodePlainSlice decodes type from json bytes to slice
func JsonDecodeSlice ¶
JsonDecodeSlice decodes type from json bytes to slice
func LeftExclusive ¶
func LeftExclusive[T comparable](left, right []T) []T
func Map ¶
Example ¶
package main
import (
"fmt"
"github.com/zloevil/jet"
)
func main() {
doubled := jet.Map([]int{1, 2, 3}, func(i int) int { return i * 2 })
fmt.Println(doubled)
}
Output: [2 4 6]
func MapInterfacesToBytes ¶
func MapKeys ¶
func MapKeys[TKey comparable, TItem any](m map[TKey]TItem) []TKey
func MapToLowerCamelKeys ¶
func MapValues ¶
func MapValues[TKey comparable, TItem any](m map[TKey]TItem) []TItem
func Millis ¶
Millis is a convenience method to get milliseconds since epoch for provided HourMinTime.
func MillisFromTime ¶
func Modified ¶
func Modified[T comparable](modified bool, cur T, update T) (T, bool)
Modified returns result value and boolean flag that shows if the value was modified or not can be used for structs. For plain type use ModifiedPlain instead
func ModifiedNillable ¶
ModifiedNillable returns result value and boolean flag that shows if an object was modified or not if the 'update' parameter is nil means the value has not been updated []int{1,2,3} and []int{3,2,1} is NOT equal For plain types use ModifiedPlainNillable
func ModifiedPlain ¶
ModifiedPlain returns the result value, and a boolean flag that shows if the value was modified or not can be used for plain types: integer, float, string
func ModifiedPlainNillable ¶
ModifiedPlainNillable returns result value and boolean flag that shows if the value was modified or not if the 'update' parameter is nil means the value has not been updated
func ModifiedSliceNillable ¶
func ModifiedSliceNillable[T constraints.Ordered](modified bool, cur []T, update []T) ([]T, bool)
ModifiedSliceNillable return result slice and boolean flag that shows if the slice was modified or not if the 'update' parameter is nil means value has not been updated if the 'update' parameter is empty means value has been updated to nil []int{1,2,3} and []int{3,2,1} is equal Side effect: result value can be sorted
func ModifiedSliceStructured ¶
func ModifiedSliceStructured[T comparable](modified bool, sort func([]T), cur []T, update []T) ([]T, bool)
ModifiedSliceStructured return result slice and boolean flag that shows if the slice was modified or not if the 'update' parameter is nil means value has not been updated if the 'update' parameter is empty means value has been updated to nil []int{1,2,3} and []int{3,2,1} is equal Side effect: result value can be sorted
func NewAppError ¶
NewAppError creates a new AppError and returns error interface
func NilOrInMap ¶
func NilOrInMap[T comparable](value *T, m map[T]struct{}) bool
func NullToString ¶
NullToString transforms NULL to empty string
func Overlapped ¶
func PaginateSlice ¶
func PaginateSlice[T any](items []T, paging PagingRequest) []T
PaginateSlice paginates the given slice of items according to the provided paging request. Returns a slice containing a subset of the original items based on the specified page size and index.
func ParseDateAny ¶
ParseDateAny parses multiple formats of dates
func ParseFloat32 ¶
func ParseFloat64 ¶
func ParseInternalAccessToken ¶
ParseInternalAccessToken parses an internal token
func Reduce ¶
func Reduce[TItem any, TRes any, TKey comparable](slice []TItem, grpFn func(TItem) TKey, accFn func(TItem, TRes) TRes) map[TKey]TRes
func RemoveNonAlfaDigital ¶
func Retry ¶
func Retry[TIn, TOut any](ctx context.Context, fn func(ctx context.Context, in TIn) (TOut, error), in TIn, cfg RetryCfg) (TOut, error)
Retry takes a generic function with parameters and run it with retry wrapper retry params are configured with RetryCfg
func Round10000 ¶
func SliceToMap ¶
func SliceToMap[TItem any, TKey comparable](slice []TItem, grpFn func(TItem) TKey) map[TKey]TItem
SliceToMap converts a passed slice to a map by the given definition of key Caution!!! if specified key isn't unique for the slice, only last slice item for the given key is taken to the resulted map
func SplitArrByItemLen ¶
func StrToInt64 ¶
func StringToNull ¶
StringToNull transforms empty string to nil string
func StringsToInterfaces ¶
func StringsToInterfaces(sl []string) []interface{}
func TimeFromMillis ¶
func ToSet ¶
func ToSet[TItem any, TKey comparable](slice []TItem, keyFn func(TItem) TKey) map[TKey]struct{}
func ToStringDate ¶
func UInt64ToInt32Ptr ¶
func ValidateUUIDs ¶
ValidateUUIDs check UUID format nad return error if at least one UUID isn't in a correct format
Types ¶
type Adapter ¶
type Adapter[T any] interface { // Init initializes adapter Init(ctx context.Context, cfg *T) error // Close closes storage Close(ctx context.Context) error }
Adapter common interface for adapters
type AdapterListener ¶
type AdapterListener[T any] interface { Adapter[T] // ListenAsync runs async listening ListenAsync(ctx context.Context) error }
AdapterListener common interface for adapters with listeners
type AppErrBuilder ¶
type AppErrBuilder interface {
// C attaches a request context to AppError
C(ctx context.Context) AppErrBuilder
// F attaches additional fields to AppError object
// if type of passed field isn't valid, it's just silently ignored
F(fields KV) AppErrBuilder
// GrpcSt attaches gRPC status
// when converting to grpc error it will be checked and if populated, corresponding grpc status is set
GrpcSt(status uint32) AppErrBuilder
// HttpSt attaches HTTP status
// it gives some hint to API gateway layer what HTTP status to return client
HttpSt(status uint32) AppErrBuilder
// Business marks error as business validation error
Business() AppErrBuilder
// System marks error as system error (this is by default)
System() AppErrBuilder
// Panic marks error as panic error
Panic() AppErrBuilder
// Type sets type of error
Type(t string) AppErrBuilder
// Err builds error with all specified attributes
Err() error
// Wrap wraps another error
Wrap(cause error) AppErrBuilder
}
AppErrBuilder allows building AppError object
func NewAppErrBuilder ¶
func NewAppErrBuilder(code string, format string, args ...interface{}) AppErrBuilder
NewAppErrBuilder creates a new AppErrBuilder and default AppError object
Example ¶
package main
import (
"fmt"
"github.com/zloevil/jet"
)
func main() {
err := jet.NewAppErrBuilder("ORD-001", "order not found: %s", "42").Business().Err()
appErr, _ := jet.IsAppErr(err)
fmt.Println(appErr.Code())
fmt.Println(appErr.Type())
fmt.Println(appErr.Message())
}
Output: ORD-001 business order not found: 42
type AppError ¶
type AppError struct {
// contains filtered or unexported fields
}
AppError specifies application error object
func IsAppErr ¶
IsAppErr checks if error interface is asserted to *AppError if true, it returns *AppError
func (*AppError) GrpcStatus ¶
GrpcStatus returns grpc status
func (*AppError) HttpStatus ¶
HttpStatus returns http status
func (*AppError) WithStack ¶
WithStack return error message with stack trace attached if you need split fields, assert to *AppError
func (*AppError) WithStackErr ¶
type CLogger ¶
type CLogger interface {
// C - adds request context to log
//
// don't put context when logging error, as it makes sense a context of where error happens rather than a context of where error log is invoked
// otherwise, context will be logged twice
C(ctx context.Context) CLogger
// F - adds fields to log
F(fields KV) CLogger
// E - adds error to log
E(err error) CLogger
// St - adds stack to log (if err is already set)
St() CLogger
// Cmp - adds component
Cmp(c string) CLogger
// Mth - adds method
Mth(m string) CLogger
// Pr - adds protocol
Pr(m string) CLogger
// Srv - adds unique service code
Srv(s string) CLogger
// Node - adds service instance code
Node(n string) CLogger
Inf(args ...interface{}) CLogger
InfF(format string, args ...interface{}) CLogger
Err(args ...interface{}) CLogger
ErrF(format string, args ...interface{}) CLogger
Dbg(args ...interface{}) CLogger
DbgF(format string, args ...interface{}) CLogger
Trc(args ...interface{}) CLogger
TrcF(format string, args ...interface{}) CLogger
// TrcObj marshals all args only if loglevel = Trace, otherwise bypass
// Note that only Exported fields of objects are logged (due to nature of json.Marshal)
TrcObj(format string, args ...interface{}) CLogger
Warn(args ...interface{}) CLogger
WarnF(format string, args ...interface{}) CLogger
Fatal(args ...interface{}) CLogger
FatalF(format string, args ...interface{}) CLogger
Clone() CLogger
Printf(string, ...interface{})
PrintfErr(f string, args ...interface{})
// Write writer implementation
Write(p []byte) (n int, err error)
}
CLogger provides structured logging abilities. !!!! Not thread safe. Don't share one CLogger instance through multiple goroutines.
type CLoggerFunc ¶
type CLoggerFunc func() CLogger
type Check ¶
type Check = healthcheck.Check
Check is a health check function that returns nil if healthy, or an error if unhealthy.
type ConfigLoader ¶
type ConfigLoader[T any] struct { // contains filtered or unexported fields }
func NewConfigLoader ¶
func NewConfigLoader[T any]() *ConfigLoader[T]
Example ¶
ExampleNewConfigLoader loads a typed config from YAML with env overrides.
package main
import (
"github.com/zloevil/jet"
)
func main() {
type Config struct {
HTTP struct{ Port string }
}
cfg, err := jet.NewConfigLoader[Config]().WithPath("./config.yml").WithPrefix("MYSVC").Load()
if err != nil {
return
}
_ = cfg
}
Output:
func (*ConfigLoader[T]) Load ¶
func (c *ConfigLoader[T]) Load() (*T, error)
Load loads config by provided parameters
func (*ConfigLoader[T]) WithEnv ¶
func (c *ConfigLoader[T]) WithEnv(env string) *ConfigLoader[T]
func (*ConfigLoader[T]) WithPath ¶
func (c *ConfigLoader[T]) WithPath(path string) *ConfigLoader[T]
func (*ConfigLoader[T]) WithPrefix ¶
func (c *ConfigLoader[T]) WithPrefix(prefix string) *ConfigLoader[T]
type DaysOfWeek ¶
type DaysOfWeek map[DayOfWeek]struct{}
func (DaysOfWeek) IsValid ¶
func (d DaysOfWeek) IsValid() bool
type Error ¶
type Error struct {
Message string `json:"message"` // Message is error description
Code string `json:"code,omitempty"` // Code is error code provided by error producer
Type string `json:"type,omitempty"` // Type is error type (panic, system, business)
HttpStatus *uint32 `json:"http_status,omitempty"` // HttpStatus http status
Details map[string]interface{} `json:"details,omitempty"` // Details is additional info provided by error producer
Stack string `json:"stack,omitempty"` // Stack trace
GrpcStatus *uint32 `json:"grpc_status,omitempty"` // GrpcStatus grpc status
}
Error error for events
func ToErrorFromString ¶
type ErrorHook ¶
type ErrorHook interface {
Error(err error)
}
ErrorHook allows specifying a hook invoked for every logged error.
type Flags ¶
type Flags[T TFlags] struct { // contains filtered or unexported fields }
func (Flags[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface
func (*Flags[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface
type Healthcheck ¶
type Healthcheck struct {
// contains filtered or unexported fields
}
Healthcheck provides HTTP endpoints for Kubernetes liveness and readiness probes. Endpoints:
- GET /live - liveness probe (indicates if app should be restarted)
- GET /ready - readiness probe (indicates if app can serve traffic)
func NewHealthCheck ¶
func NewHealthCheck(cfg *HealthcheckConfig) *Healthcheck
NewHealthCheck creates a new Healthcheck instance.
func (*Healthcheck) AddLivenessCheck ¶
func (h *Healthcheck) AddLivenessCheck(name string, check Check)
AddLivenessCheck adds a check that indicates whether this instance of the application should be destroyed or restarted.
A failed liveness check indicates that this instance is unhealthy and should be killed and restarted by Kubernetes. Use for detecting internal failures like deadlocks, infinite loops, or stuck workers.
Note: All liveness checks are automatically included as readiness checks.
Example:
hc.AddLivenessCheck("worker", func() error {
if time.Since(workerLastActivity) > 5*time.Minute {
return errors.New("worker is stuck")
}
return nil
})
func (*Healthcheck) AddReadinessCheck ¶
func (h *Healthcheck) AddReadinessCheck(name string, check Check)
AddReadinessCheck adds a check that indicates whether this instance of the application is currently ready to serve requests.
A failed readiness check indicates that this instance should temporarily not receive traffic (e.g., during startup, when dependencies are unavailable). Kubernetes will stop routing traffic but won't restart the container.
Example:
hc.AddReadinessCheck("database", func() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
return db.PingContext(ctx)
})
func (*Healthcheck) Start ¶
func (h *Healthcheck) Start()
Start starts the healthcheck HTTP server in a background goroutine. The server will automatically retry on failure with 5 second delay.
func (*Healthcheck) Stop ¶
func (h *Healthcheck) Stop()
Stop gracefully stops the healthcheck HTTP server.
type HealthcheckConfig ¶
type HealthcheckConfig struct {
Port string
}
HealthcheckConfig configuration for healthcheck server.
type HourMinTime ¶
type HourMinTime struct {
// contains filtered or unexported fields
}
HourMinTime - time (hour:min) representation in format 15:04
func (HourMinTime) Before ¶
func (t HourMinTime) Before(other HourMinTime) bool
func (HourMinTime) FromTime ¶
func (t HourMinTime) FromTime(tm time.Time) HourMinTime
func (HourMinTime) Hour ¶
func (t HourMinTime) Hour() int
func (*HourMinTime) MarshalJSON ¶
func (t *HourMinTime) MarshalJSON() ([]byte, error)
func (HourMinTime) Minute ¶
func (t HourMinTime) Minute() int
func (HourMinTime) MustParse ¶
func (t HourMinTime) MustParse(s string) HourMinTime
func (HourMinTime) Parse ¶
func (t HourMinTime) Parse(s string) (HourMinTime, error)
func (HourMinTime) String ¶
func (t HourMinTime) String() string
func (HourMinTime) Unix ¶
func (t HourMinTime) Unix() int64
func (*HourMinTime) UnmarshalJSON ¶
func (t *HourMinTime) UnmarshalJSON(b []byte) error
type JwtRequest ¶
JwtRequest request to generate a new jwt token
type LogConfig ¶
type LogConfig struct {
Level string // Level logging level
Format string // Format (plain, json)
Context bool // Context if true, request context params are part of logging
Service bool // Service if true, service params are part of logging
}
LogConfig represents logging configuration.
type Logger ¶
type Logger struct {
Cfg *LogConfig
// contains filtered or unexported fields
}
Logger holds the configured slog logger and is the factory source for CLogger.
func InitLogger ¶
InitLogger creates and configures a Logger from cfg.
Example ¶
ExampleInitLogger shows logger setup and the chainable, context-aware API.
package main
import (
"github.com/zloevil/jet"
)
func main() {
logger := jet.InitLogger(&jet.LogConfig{Level: jet.InfoLevel, Format: jet.FormatterJson})
log := jet.L(logger)
log.Cmp("orders").Mth("Create").F(jet.KV{"id": "42"}).Inf("order created")
// log lines are timestamped and written to stdout, so they are not asserted here.
}
Output:
func (*Logger) SetErrorHook ¶
SetErrorHook registers a hook invoked for every error logged at Error level or above.
type PageReader ¶
type PageReader[TItem any, TRq any] interface { // GetPage retrieves one page GetPage(ctx context.Context, rq TRq) chan PagingResponseG[TItem] }
PageReader is a generic reader allowing read from remote repository
func NewPageReader ¶
func NewPageReader[TItem any, TRq any](fn func(context.Context, PagingRequestG[TRq]) (PagingResponseG[TItem], error), pageSize int, logger CLogger) PageReader[TItem, TRq]
type PagingRequest ¶
type PagingRequest struct {
Size int `json:"size"` // Size page size
Index int `json:"index"` // Index page index
SortBy []*SortRequest `json:"sortBy"` // SortBy sort by fields
}
PagingRequest paging request
type PagingRequestG ¶
type PagingRequestG[T any] struct { PagingRequest Request T `json:"request"` // Request }
PagingRequestG generic paging request
type PagingResponse ¶
type PagingResponse struct {
Total int `json:"total"` // Total number of rows
Limit int `json:"limit"` // Limit applied
}
PagingResponse paging response
type PagingResponseG ¶
type PagingResponseG[T any] struct { PagingResponse Items []*T `json:"response"` // Items }
PagingResponseG generic paging response
type PlainType ¶
type PlainType interface {
constraints.Integer | constraints.Float | ~string | ~bool | time.Time
}
type RequestContext ¶
type RequestContext struct {
Rid string `json:"_ctx.rid,omitempty" mapstructure:"_ctx.rid"` // Rid request ID
Sid string `json:"_ctx.sid,omitempty" mapstructure:"_ctx.sid"` // Sid session ID
Uid string `json:"_ctx.uid,omitempty" mapstructure:"_ctx.uid"` // Uid user ID
Un string `json:"_ctx.un,omitempty" mapstructure:"_ctx.un"` // Un username
App string `json:"_ctx.app,omitempty" mapstructure:"_ctx.app"` // App application
ClIp string `json:"_ctx.clIp,omitempty" mapstructure:"_ctx.clIp"` // ClIp client IP
Roles []string `json:"_ctx.rl,omitempty" mapstructure:"_ctx.rl"` // Roles list of roles
Lang language.Tag `json:"_ctx.lang,omitempty" mapstructure:"_ctx.lang"` // Lang client language
Kv KV `json:"_ctx.kv,omitempty" mapstructure:"_ctx.kv"` // Kv arbitrary key-value
}
Example ¶
package main
import (
"context"
"fmt"
"github.com/zloevil/jet"
)
func main() {
ctx := jet.NewRequestCtx().WithRequestId("req-1").WithUser("u-1", "alice").ToContext(context.Background())
rc, _ := jet.Request(ctx)
fmt.Println(rc.GetRequestId(), rc.GetUserId(), rc.GetUsername())
}
Output: req-1 u-1 alice
func MustRequest ¶
func MustRequest(context context.Context) (*RequestContext, error)
func NewRequestCtx ¶
func NewRequestCtx() *RequestContext
func (*RequestContext) EN ¶
func (r *RequestContext) EN() *RequestContext
func (*RequestContext) Empty ¶
func (r *RequestContext) Empty() *RequestContext
func (*RequestContext) GetApp ¶
func (r *RequestContext) GetApp() string
func (*RequestContext) GetClientIp ¶
func (r *RequestContext) GetClientIp() string
func (*RequestContext) GetKv ¶
func (r *RequestContext) GetKv() KV
func (*RequestContext) GetLang ¶
func (r *RequestContext) GetLang() language.Tag
func (*RequestContext) GetRequestId ¶
func (r *RequestContext) GetRequestId() string
func (*RequestContext) GetRoles ¶
func (r *RequestContext) GetRoles() []string
func (*RequestContext) GetSessionId ¶
func (r *RequestContext) GetSessionId() string
func (*RequestContext) GetUserId ¶
func (r *RequestContext) GetUserId() string
func (*RequestContext) GetUsername ¶
func (r *RequestContext) GetUsername() string
func (*RequestContext) TestApp ¶
func (r *RequestContext) TestApp() *RequestContext
func (*RequestContext) ToContext ¶
func (r *RequestContext) ToContext(parent context.Context) context.Context
func (*RequestContext) ToMap ¶
func (r *RequestContext) ToMap() map[string]interface{}
func (*RequestContext) WithApp ¶
func (r *RequestContext) WithApp(app string) *RequestContext
func (*RequestContext) WithClientIp ¶
func (r *RequestContext) WithClientIp(ip string) *RequestContext
func (*RequestContext) WithKv ¶
func (r *RequestContext) WithKv(key string, val interface{}) *RequestContext
func (*RequestContext) WithLang ¶
func (r *RequestContext) WithLang(lang language.Tag) *RequestContext
func (*RequestContext) WithNewRequestId ¶
func (r *RequestContext) WithNewRequestId() *RequestContext
func (*RequestContext) WithRequestId ¶
func (r *RequestContext) WithRequestId(requestId string) *RequestContext
func (*RequestContext) WithRoles ¶
func (r *RequestContext) WithRoles(roles ...string) *RequestContext
func (*RequestContext) WithSessionId ¶
func (r *RequestContext) WithSessionId(sessionId string) *RequestContext
func (*RequestContext) WithUser ¶
func (r *RequestContext) WithUser(userId, username string) *RequestContext
type SafeMap ¶
type SafeMap[TK comparable, TV any] struct { sync.RWMutex // contains filtered or unexported fields }
func NewSafeMap ¶
func NewSafeMap[TK comparable, TV any]() *SafeMap[TK, TV]
type Searchable ¶
type Searchable[TItem any, Rq any] interface { // Search searches items by criteria in a pageable manner Search(ctx context.Context, rq PagingRequestG[Rq]) (PagingResponseG[TItem], error) }
Searchable interface provides a search abilities
type SortRequest ¶
type SortRequest struct {
Field string `json:"field"` // Field which is sorted
Desc bool `json:"desc"` // Desc if true order by desc
NullsLast bool `json:"nullsLast"` // NullsLast if true nulls go last
}
SortRequest request to sort data
type String ¶
type String string
func (String) RemoveNewlines ¶
func (String) TrimCodeEnd ¶
func (String) TrimCodeStart ¶
type Strings ¶
type Strings []string
Strings represents slice of strings
func (Strings) ToSliceAny ¶
type Suite ¶
func (*Suite) AssertAppErr ¶
func (*Suite) AssertCalled ¶
AssertCalled asserts that the method was called. It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method.
func (*Suite) AssertNotCalled ¶
AssertNotCalled asserts that the method was not called. It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method.
func (*Suite) AssertNumberOfCalls ¶
AssertNumberOfCalls asserts that the method was called expectedCalls times.
func (*Suite) BeforeTest ¶
func (*Suite) Condition ¶
func (s *Suite) Condition(comp assert.Comparison, msgAndArgs ...interface{})
Condition uses a Comparison to assert a complex condition.
func (*Suite) Contains ¶
func (s *Suite) Contains(el, contains interface{}, msgAndArgs ...interface{})
Contains asserts that the specified string, list(array, slice...) or map contains the specified substring or element.
s.Contains(t, "Hello World", "World")
s.Contains(t, ["Hello", "World"], "World")
s.Contains(t, {"Hello": "World"}, "Hello")
func (*Suite) DefaultLogger ¶
func (s *Suite) DefaultLogger()
func (*Suite) DirExists ¶
DirExists checks whether a directory exists in the given path. It also fails if the path is a file rather a directory or there is an error checking whether it exists.
func (*Suite) ElementsMatch ¶
func (s *Suite) ElementsMatch(listA, listB interface{}, msgAndArgs ...interface{})
ElementsMatch asserts that the specified listA(array, slice...) is equal to specified listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, the number of appearances of each of them in both lists should match.
s.ElementsMatch(t, [1, 3, 2, 3], [1, 3, 3, 2])
func (*Suite) Empty ¶
func (s *Suite) Empty(object interface{}, msgAndArgs ...interface{})
Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either a slice or a channel with len == 0.
s.Empty(t, obj)
func (*Suite) Equal ¶
func (s *Suite) Equal(expected interface{}, actual interface{}, msgAndArgs ...interface{})
Equal asserts that two objects are equal.
s.Equal(t, 123, 123)
Pointer variable equality is determined based on the equality of the referenced values (as opposed to the memory addresses). Function equality cannot be determined and will always fail.
func (*Suite) EqualError ¶
EqualError asserts that a function returned an error (i.e. not `nil`) and that it is equal to the provided error.
actualObj, err := SomeFunction() s.EqualError(t, err, expectedErrorString)
func (*Suite) EqualValues ¶
func (s *Suite) EqualValues(expected interface{}, actual interface{}, msgAndArgs ...interface{})
EqualValues asserts that two objects are equal or convertable to the same types and equal.
s.EqualValues(t, uint32(123), int32(123))
func (*Suite) Error ¶
Error asserts that a function returned an error (i.e. not `nil`).
actualObj, err := SomeFunction()
if s.Error(t, err) {
s.Equal(t, expectedError, err)
}
func (*Suite) ErrorAs ¶
ErrorAs asserts that at least one of the errors in err's chain matches target, and if so, sets target to that error value. This is a wrapper for errors.As.
func (*Suite) ErrorIs ¶
ErrorIs asserts that at least one of the errors in err's chain matches target. This is a wrapper for errors.IsAppErr.
func (*Suite) Eventually ¶
func (s *Suite) Eventually(condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{})
Eventually asserts that given condition will be met in waitFor time, periodically checking target function each tick.
s.Eventually(t, func() bool { return true; }, time.Second, 10*time.Millisecond)
func (*Suite) Exactly ¶
func (s *Suite) Exactly(expected interface{}, actual interface{}, msgAndArgs ...interface{})
Exactly asserts that two objects are equal in value and type.
s.Exactly(t, int32(123), int64(123))
func (*Suite) FileExists ¶
FileExists checks whether a file exists in the given path. It also fails if the path points to a directory or there is an error when trying to check the file.
func (*Suite) Implements ¶
func (s *Suite) Implements(interfaceObject interface{}, object interface{}, msgAndArgs ...interface{})
Implements asserts that an object is implemented by the specified interface.
s.Implements(t, (*MyInterface)(nil), new(MyObject))
func (*Suite) InDelta ¶
InDelta asserts that the two numerals are within delta of each other.
s.InDelta(t, math.Pi, 22/7.0, 0.01)
func (*Suite) InDeltaMapValues ¶
func (s *Suite) InDeltaMapValues(expected, actual interface{}, delta float64, msgAndArgs ...interface{})
InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys.
func (*Suite) InDeltaSlice ¶
func (s *Suite) InDeltaSlice(expected, actual interface{}, delta float64, msgAndArgs ...interface{})
InDeltaSlice is the same as InDelta, except it compares two slices.
func (*Suite) InEpsilon ¶
InEpsilon asserts that expected and actual have a relative error less than epsilon
func (*Suite) InEpsilonSlice ¶
func (s *Suite) InEpsilonSlice(expected, actual interface{}, epsilon float64, msgAndArgs ...interface{})
InEpsilonSlice is the same as InEpsilon, except it compares each value from two slices.
func (*Suite) Init ¶
func (s *Suite) Init(logger CLoggerFunc)
func (*Suite) IsType ¶
func (s *Suite) IsType(expectedType interface{}, object interface{}, msgAndArgs ...interface{})
IsType asserts that the specified objects are of the same type.
func (*Suite) JSONEq ¶
JSONEq asserts that two JSON strings are equivalent.
s.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
func (*Suite) Len ¶
Len asserts that the specified object has specific length. Len also fails if the object has a type that len() not accept.
s.Len(t, mySlice, 3)
func (*Suite) Never ¶
func (s *Suite) Never(condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{})
Never asserts that the given condition doesn't satisfy in waitFor time, periodically checking the target function each tick.
s.Never(t, func() bool { return false; }, time.Second, 10*time.Millisecond)
func (*Suite) Nil ¶
func (s *Suite) Nil(object interface{}, msgAndArgs ...interface{})
Exactly asserts that two objects are equal in value and type.
s.Exactly(t, int32(123), int64(123))
func (*Suite) NoDirExists ¶
NoDirExists checks whether a directory does not exist in the given path. It fails if the path points to an existing _directory_ only.
func (*Suite) NoError ¶
NoError asserts that a function returned no error (i.e. `nil`).
actualObj, err := SomeFunction()
if s.NoError(t, err) {
s.Equal(t, expectedObj, actualObj)
}
func (*Suite) NoFileExists ¶
NoFileExists checks whether a file does not exist in a given path. It fails if the path points to an existing _file_ only.
func (*Suite) NotContains ¶
func (s *Suite) NotContains(el, contains interface{}, msgAndArgs ...interface{})
NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the specified substring or element.
s.NotContains(t, "Hello World", "Earth")
s.NotContains(t, ["Hello", "World"], "Earth")
s.NotContains(t, {"Hello": "World"}, "Earth")
func (*Suite) NotEmpty ¶
func (s *Suite) NotEmpty(object interface{}, msgAndArgs ...interface{})
NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either a slice or a channel with len == 0.
if s.NotEmpty(t, obj) {
s.Equal(t, "two", obj[1])
}
func (*Suite) NotEqual ¶
func (s *Suite) NotEqual(expected interface{}, actual interface{}, msgAndArgs ...interface{})
NotEqual asserts that the specified values are NOT equal.
s.NotEqual(t, obj1, obj2)
Pointer variable equality is determined based on the equality of the referenced values (as opposed to the memory addresses).
func (*Suite) NotEqualValues ¶
func (s *Suite) NotEqualValues(expected interface{}, actual interface{}, msgAndArgs ...interface{})
NotEqualValues asserts that two objects are not equal even when converted to the same type
s.NotEqualValues(t, obj1, obj2)
func (*Suite) NotErrorIs ¶
NotErrorIs asserts that at none of the errors in err's chain matches target. This is a wrapper for errors.IsAppErr.
func (*Suite) NotNil ¶
func (s *Suite) NotNil(object interface{}, msgAndArgs ...interface{})
NotNil asserts that the specified object is not nil.
s.NotNil(t, err)
func (*Suite) NotPanics ¶
func (s *Suite) NotPanics(f assert.PanicTestFunc, msgAndArgs ...interface{})
NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic.
s.NotPanics(t, func(){ RemainCalm() })
func (*Suite) NotRegexp ¶
func (s *Suite) NotRegexp(rx interface{}, str interface{}, msgAndArgs ...interface{})
NotRegexp asserts that a specified regexp does not match a string.
s.NotRegexp(t, regexp.MustCompile("starts"), "it's starting")
s.NotRegexp(t, "^start", "it's not starting")
func (*Suite) NotSame ¶
func (s *Suite) NotSame(expected, actual interface{}, msgAndArgs ...interface{})
NotSame asserts that two pointers do not reference the same object.
s.NotSame(t, ptr1, ptr2)
Both arguments must be pointer variables. Pointer variable sameness is determined based on the equality of both type and value.
func (*Suite) NotSubset ¶
func (s *Suite) NotSubset(list, subset interface{}, msgAndArgs ...interface{})
NotSubset asserts that the specified list(array, slice...) contains not all elements given in the specified subset(array, slice...).
s.NotSubset(t, [1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]")
func (*Suite) NotZero ¶
func (s *Suite) NotZero(v interface{}, msgAndArgs ...interface{})
NotZero asserts that i is not the zero value for its type.
func (*Suite) Panics ¶
func (s *Suite) Panics(f assert.PanicTestFunc, msgAndArgs ...interface{})
Panics asserts that the code inside the specified PanicTestFunc panics.
s.Panics(t, func(){ GoCrazy() })
func (*Suite) PanicsWithError ¶
func (s *Suite) PanicsWithError(errString string, f assert.PanicTestFunc, msgAndArgs ...interface{})
PanicsWithError asserts that the code inside the specified PanicTestFunc panics, and that the recovered panic value is an error that satisfies the EqualError comparison.
s.PanicsWithError(t, "crazy error", func(){ GoCrazy() })
func (*Suite) PanicsWithValue ¶
func (s *Suite) PanicsWithValue(expected interface{}, f assert.PanicTestFunc, msgAndArgs ...interface{})
PanicsWithValue asserts that the code inside the specified PanicTestFunc panics, and that the recovered panic value equals the expected panic value.
s.PanicsWithValue(t, "crazy error", func(){ GoCrazy() })
func (*Suite) Regexp ¶
func (s *Suite) Regexp(rx interface{}, str interface{}, msgAndArgs ...interface{})
Regexp asserts that a specified regexp matches a string.
s.Regexp(t, regexp.MustCompile("start"), "it's starting")
s.Regexp(t, "start...$", "it's not starting")
func (*Suite) Same ¶
func (s *Suite) Same(expected, actual interface{}, msgAndArgs ...interface{})
Same asserts that two pointers reference the same object.
s.Same(t, ptr1, ptr2)
Both arguments must be pointer variables. Pointer variable sameness is determined based on the equality of both type and value.
func (*Suite) Subset ¶
func (s *Suite) Subset(list, subset interface{}, msgAndArgs ...interface{})
Subset asserts that the specified list(array, slice...) contains all elements given in the specified subset(array, slice...).
s.Subset(t, [1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]")
func (*Suite) WithinDuration ¶
func (s *Suite) WithinDuration(expected, actual time.Time, delta time.Duration, msgAndArgs ...interface{})
WithinDuration asserts that the two times are within duration delta of each other.
s.WithinDuration(t, time.Now(), time.Now(), 10*time.Second)
type TimePeriod ¶
TimePeriod represents period of time
type TimeRange ¶
type TimeRange [2]HourMinTime
TimeRange represents time interval in format [15:00, 18:00]
func (TimeRange) ParseOrEmpty ¶
func (TimeRange) ValidRange ¶
func (TimeRange) Within ¶
func (t TimeRange) Within(tm HourMinTime) bool
func (TimeRange) WithinExcl ¶
func (t TimeRange) WithinExcl(tm HourMinTime) bool
WithinExcl with left inclusive and right exclusive
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
func NewValidator ¶
type WaitGroup ¶
type WaitGroup struct {
// contains filtered or unexported fields
}
WaitGroup wait group with duration timeout
type Watchdog ¶
type Watchdog struct {
// contains filtered or unexported fields
}
Watchdog tracks worker activity. Each worker must call Ping() periodically to confirm it is alive. If a worker does not report for longer than timeout, Check() returns an error.
func NewWatchdog ¶
NewWatchdog creates a new Watchdog with the given timeout. A worker that does not call Ping() within timeout is considered stuck.
func (*Watchdog) Check ¶
Check inspects all registered workers. Returns an error if at least one worker is stuck (has not reported within timeout).
func (*Watchdog) Disable ¶
func (w *Watchdog) Disable()
Disable turns off the watchdog check. Used during graceful shutdown so liveness does not fail while stopping.
func (*Watchdog) Ping ¶
Ping updates the worker's last-activity time. A worker should call this on every iteration of its loop.
func (*Watchdog) Register ¶
Register starts tracking a worker. After registration the worker must call Ping() periodically.
func (*Watchdog) Unregister ¶
Unregister stops tracking a worker. Used during a worker's graceful shutdown.
Source Files
¶
- apperrors.go
- arr_utils.go
- business_utils.go
- common_errors.go
- common_types.go
- config.go
- context.go
- crypto.go
- datetime_utils.go
- doc.go
- func.go
- healthcheck.go
- jwt.go
- language_utils.go
- logformatter.go
- logger.go
- modified_utils.go
- notifyerrors.go
- page_reader.go
- string_utils.go
- system_utils.go
- testsuite.go
- types_utils.go
- validator.go
- watchdog.go
- watchdog_errors.go
Directories
¶
| Path | Synopsis |
|---|---|
|
Package aws holds the shared AWS SDK configuration (credentials, region, endpoint) used by the aws sub-packages such as s3 and sqs.
|
Package aws holds the shared AWS SDK configuration (credentials, region, endpoint) used by the aws sub-packages such as s3 and sqs. |
|
s3
Package s3 provides a client for Amazon S3 (and S3-compatible) object storage.
|
Package s3 provides a client for Amazon S3 (and S3-compatible) object storage. |
|
sqs
Package sqs provides a client and a message subscriber for Amazon SQS queues.
|
Package sqs provides a client and a message subscriber for Amazon SQS queues. |
|
Package batch provides a generic worker that accumulates items and flushes them in batches.
|
Package batch provides a generic worker that accumulates items and flushes them in batches. |
|
Package centrifugo wraps the Centrifugo real-time messaging client, exposing a server-side publisher and a subscriber client.
|
Package centrifugo wraps the Centrifugo real-time messaging client, exposing a server-side publisher and a subscriber client. |
|
Package cluster wires a service's lifecycle.
|
Package cluster wires a service's lifecycle. |
|
Package elasticsearch wraps the Elasticsearch client with helpers for building index mappings and settings and for querying.
|
Package elasticsearch wraps the Elasticsearch client with helpers for building index mappings and settings and for querying. |
|
Package event provides an in-process publish/subscribe event bus.
|
Package event provides an in-process publish/subscribe event bus. |
|
Package excel reads tabular data (rows) from XLSX spreadsheets.
|
Package excel reads tabular data (rows) from XLSX spreadsheets. |
|
Package google provides integrations with Google services: OAuth2 token handling and reCAPTCHA verification.
|
Package google provides integrations with Google services: OAuth2 token handling and reCAPTCHA verification. |
|
Package goroutine provides panic-safe goroutines and error groups.
|
Package goroutine provides panic-safe goroutines and error groups. |
|
Package grpc provides a gRPC server and client preconfigured with middleware.
|
Package grpc provides a gRPC server and client preconfigured with middleware. |
|
Package http provides an HTTP server built on gorilla/mux.
|
Package http provides an HTTP server built on gorilla/mux. |
|
Package kafka provides Kafka producers and subscribers built on segmentio/kafka-go.
|
Package kafka provides Kafka producers and subscribers built on segmentio/kafka-go. |
|
Package memcache provides a simple in-memory cache with per-item TTL, backed by patrickmn/go-cache.
|
Package memcache provides a simple in-memory cache with per-item TTL, backed by patrickmn/go-cache. |
|
Package monitoring exposes Prometheus metrics over HTTP.
|
Package monitoring exposes Prometheus metrics over HTTP. |
|
Package notification builds notification receivers from permission and resource policies.
|
Package notification builds notification receivers from permission and resource policies. |
|
Package profile exposes the Go net/http/pprof profiling endpoints over a dedicated HTTP server.
|
Package profile exposes the Go net/http/pprof profiling endpoints over a dedicated HTTP server. |
|
Package retry runs functions with bounded retries using exponential backoff with jitter.
|
Package retry runs functions with bounded retries using exponential backoff with jitter. |
|
Package rpc defines a request/response RPC protocol carried over Kafka.
|
Package rpc defines a request/response RPC protocol carried over Kafka. |
|
client
Package client implements rpc.Client: it sends RPC calls over Kafka and correlates incoming responses back to their pending requests.
|
Package client implements rpc.Client: it sends RPC calls over Kafka and correlates incoming responses back to their pending requests. |
|
server
Package server implements rpc.Server: it handles incoming RPC requests from Kafka and sends responses back to the calling client.
|
Package server implements rpc.Server: it handles incoming RPC requests from Kafka and sends responses back to the calling client. |
|
storages
|
|
|
aerospike
Package aerospike provides an Aerospike client connection.
|
Package aerospike provides an Aerospike client connection. |
|
clickhouse
Package clickhouse provides a ClickHouse connection, both through the native client and the standard database/sql interface.
|
Package clickhouse provides a ClickHouse connection, both through the native client and the standard database/sql interface. |
|
migration
Package migration runs SQL schema migrations via goose for PostgreSQL and ClickHouse.
|
Package migration runs SQL schema migrations via goose for PostgreSQL and ClickHouse. |
|
minio
Package minio provides a client for MinIO (S3-compatible) object storage.
|
Package minio provides a client for MinIO (S3-compatible) object storage. |
|
mongodb
Package mongodb provides a MongoDB client connection with optional TLS.
|
Package mongodb provides a MongoDB client connection with optional TLS. |
|
pg
Package pg provides a PostgreSQL connection via GORM.
|
Package pg provides a PostgreSQL connection via GORM. |
|
redis
Package redis provides a Redis client (built on go-redis) with helpers for distributed locks and priority queues.
|
Package redis provides a Redis client (built on go-redis) with helpers for distributed locks and priority queues. |