helper

package module
v0.0.0-...-50b0264 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2022 License: MIT Imports: 11 Imported by: 0

README

go-helper

Golang project common helper functions

Documentation

Index

Constants

View Source
const (
	ErrorBadRequest          int = 400
	ErrorUnauthorized        int = 403
	ErrorNotFound            int = 404
	ErrorDuplicate           int = 409
	ErrorUnprocessableEntity int = 422
	ErrorInternalService     int = 500
)
View Source
const ContextKeyRequestId key = "requestId"

Variables

This section is empty.

Functions

func ContextWithRequestId

func ContextWithRequestId(ctx context.Context, requestId string) context.Context

func EmptyStringWhenNil

func EmptyStringWhenNil(str *string) string

EmptyStringWhenNil will return empty string when arg is nil, or arg when not nil

func EqualPointerValue

func EqualPointerValue[T comparable](a *T, b *T) bool

func GetLogger

func GetLogger(c context.Context) *logrus.Entry

func GetRequestIdFromMessage

func GetRequestIdFromMessage(message []byte) string

func InSlice

func InSlice[T comparable](list []T, str T) bool

func IsDuplicateError

func IsDuplicateError(e error) bool

func IsEmptyTrimString

func IsEmptyTrimString(str *string) bool

func IsNotFoundError

func IsNotFoundError(e error) bool

func IsParameterError

func IsParameterError(e error) bool

func IsRetyableError

func IsRetyableError(e error) bool

func IsStringContainsPattern

func IsStringContainsPattern(str string, pattern string) bool

IsStringContainsPattern will check based on regex

func IsStringInArray

func IsStringInArray(str string, arr []string) bool

func IsStringPhoneNumber

func IsStringPhoneNumber(contact string) bool

func IsUnprocessableEntityError

func IsUnprocessableEntityError(e error) bool

func JsonParse

func JsonParse(jsonBytes []byte, receiver interface{}) error

func JsonString

func JsonString(obj interface{}) (*string, error)

func MaskEmail

func MaskEmail(str string) string

func MaskPhone

func MaskPhone(str string) string

func MergeSlices

func MergeSlices[T any](vals ...[]T) (merged []T)

func MustJsonString

func MustJsonString(obj interface{}) string

func NormalizeEmail

func NormalizeEmail(email string) string

func NormalizePhoneNumber

func NormalizePhoneNumber(phone string) string

func NormalizeSimple

func NormalizeSimple(str *string) string

NormalizeSimple will remove all non alphanumeric chars, lower, and trim given string will return empty string if str is nil

func NormalizeSimpleKeepSpace

func NormalizeSimpleKeepSpace(str *string) string

keep space intact

func Pointer

func Pointer[T any](data T) *T

func RemoveNonAlphaNumChar

func RemoveNonAlphaNumChar(str string) string

RemoveNonAlphaNumChar will return string with removed non alphanumeric chars or empty string if nil was given

func RemoveNonAlphaNumSpaceChar

func RemoveNonAlphaNumSpaceChar(str string) string

func StrLimit

func StrLimit(str *string, limit int) *string

func StrReplace

func StrReplace(str string, before string, after string) string

func StrToLower

func StrToLower(str string) string

StrToLower returns `str` with all Unicode letters mapped to their lower case.

https://golang.org/pkg/strings/#ToLower

func StringToTime

func StringToTime(timeStr *string) *time.Time

func TimeToMilisecond

func TimeToMilisecond(t *time.Time) int64

func ToInt64

func ToInt64[T NumberType](data *T) (result *int64)

func ToString

func ToString[T NumberType](data *T) *string

func TrimAndLowerString

func TrimAndLowerString(str string) string

TrimAndLowerString will return whitespace-trimmed and lowered-case version of the given str

func UuidV4Fn

func UuidV4Fn() func() string

func Val

func Val[T any](pointer *T) T

func ValTimeUnix

func ValTimeUnix(val *time.Time) int64

Types

type Error

type Error struct {
	Message   string
	Code      int
	Retryable bool
}

func NewDuplicateError

func NewDuplicateError() Error

func NewError

func NewError(message string, code int) Error

func NewLimitReachedError

func NewLimitReachedError() Error

func NewNotFoundError

func NewNotFoundError() Error

func NewParameterError

func NewParameterError(msg *string) Error

func NewUnauthorizedError

func NewUnauthorizedError() Error

func (Error) Error

func (e Error) Error() string

type NumberType

type NumberType interface {
	~string | int | int32 | int64 | float32 | float64 | bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL