utils

package module
v1.38.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 35 Imported by: 10

README

go-utils

Utility function for go

Documentation

Index

Constants

View Source
const (
	// BigPicture :nodoc:
	BigPicture = "big_picture"
	// LargeIcon :nodoc:
	LargeIcon = "large_icon"
	// IOSAttachment :nodoc:
	IOSAttachment = "ios_attahcment"
)

Variables

This section is empty.

Functions

func ArrayStringPointerToArrayInt64

func ArrayStringPointerToArrayInt64(s *[]*string) []int64

ArrayStringPointerToArrayInt64 :nodoc:

func BoolPointerToBool

func BoolPointerToBool(b *bool) bool

BoolPointerToBool :nodoc:

func BoolToString

func BoolToString(b bool) string

BoolToString :nodoc:

func Contains added in v1.21.0

func Contains[T comparable](a []T, x T) bool

Contains tells whether slice A contains x.

func ContainsInt64

func ContainsInt64(a []int64, x int64) bool

ContainsInt64 tells whether a slice contains x.

func ContainsString

func ContainsString(a []string, x string) bool

ContainsString tells whether a slice contains x.

func ConvertSlice added in v1.38.0

func ConvertSlice[T1 any, T2 any](in []T1, converter func(T1) T2) []T2

ConvertSlice can change slice data type or even manipulate the data using converter func

func DeleteByValue added in v1.28.0

func DeleteByValue[T comparable](a []T, x T) []T

DeleteByValue use for delete value in slice

func Difference added in v1.21.0

func Difference[T comparable](slice1 []T, slice2 []T) []T

Difference get discrepancies between 2 slices

func DifferenceInt64

func DifferenceInt64(slice1 []int64, slice2 []int64) []int64

DifferenceInt64 :nodoc:

func DifferenceString

func DifferenceString(slice1 []string, slice2 []string) []string

DifferenceString :nodoc:

func DoHTTPRequest added in v1.35.0

func DoHTTPRequest[T httpResponse](ctx context.Context, httpClient *http.Client, httpRequest *http.Request) (respStatusCode int, respBody *T, err error)

DoHTTPRequest generic do http request using ctxhttp

func Dump

func Dump(i interface{}) string

Dump to json using json marshal

func DumpIncomingContext

func DumpIncomingContext(c context.Context) string

DumpIncomingContext :nodoc:

func DumpOutGoingContext

func DumpOutGoingContext(c context.Context) string

DumpOutGoingContext :nodoc:

func EscapeQuote added in v1.25.0

func EscapeQuote(in string) string

EscapeQuote :nodoc:

func FindDifferencesFromSlices added in v1.32.0

func FindDifferencesFromSlices[T comparable](slices ...[]T) []T

FindDifferencesFromSlices find item that not exists in all slices but exists in one or more of them

func Float64PointerToFloat64

func Float64PointerToFloat64(f *float64) float64

Float64PointerToFloat64 :nodoc:

func FormatMoney added in v1.9.0

func FormatMoney(value decimal.Decimal, currencyCode string) string

FormatMoney format money by currency code (ISO 4217)

func FormatTimeRFC3339

func FormatTimeRFC3339(t *time.Time) (s string)

FormatTimeRFC3339 Format time according to RFC3339Nano

func FormatToIndonesianMoney added in v1.7.1

func FormatToIndonesianMoney(dec decimal.Decimal) string

FormatToIndonesianMoney format money into Indonesian example: Rp10.000.000

func FormatToWesternIndonesianTime added in v1.7.1

func FormatToWesternIndonesianTime(layout string, t *time.Time) string

FormatToWesternIndonesianTime format to western indonesian time expected format: 12 April 2020 14:30 WIB

func GenerateID

func GenerateID() int64

GenerateID based on current time

func GeneratePushNotificationMediaURL added in v1.1.0

func GeneratePushNotificationMediaURL(cdnURL, mediaSrcURL, imageType string) string

GeneratePushNotificationMediaURL Generates manipulated media URL for push notification purpose e.g. GeneratePushNotificationMediaURL("http://mycdn.com", "http://my.image.com/image/upload/v123/image.jpg", LargeIcon) => http://mycdn.com/image/upload/v123/image.jpg

func GenerateRandomAlphanumeric added in v1.4.0

func GenerateRandomAlphanumeric(n int) string

GenerateRandomAlphanumeric Generate random alphanumeric character adapted from https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go

func GenerateRandomBytes

func GenerateRandomBytes(n int) ([]byte, error)

GenerateRandomBytes adapted from https://elithrar.github.io/article/generating-secure-random-numbers-crypto-rand/ GenerateRandomBytes returns securely generated random bytes. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

func GenerateRandomString

func GenerateRandomString(n int) (string, error)

GenerateRandomString returns a securely generated random string. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

func GenerateRandomStringURLSafe

func GenerateRandomStringURLSafe(n int) (string, error)

GenerateRandomStringURLSafe returns a URL-safe, base64 encoded securely generated random string. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

func GenerateULIDFromTime added in v1.22.0

func GenerateULIDFromTime(t time.Time) string

GenerateULIDFromTime :nodoc:

func GenerateUUID added in v1.34.0

func GenerateUUID() string

GenerateUUID generate random UUID v4

func GetCronNextAt added in v1.27.0

func GetCronNextAt(cronTab string) string

GetCronNextAt supports

  • Standard crontab specs, e.g. "* * * * ?"
  • Descriptors, e.g. "@midnight", "@every 1h30m"

if cron parsing error then return current time

func GetDifferenceDaysForHumans added in v1.13.0

func GetDifferenceDaysForHumans(startsAt time.Time, endsAt time.Time) string

GetDifferenceDaysForHumans return difference days for humans in indonesian language

func GetIDFromSlug added in v1.6.0

func GetIDFromSlug(slug string) string

GetIDFromSlug in base62

func GraphQLIDPointerToInt32

func GraphQLIDPointerToInt32(id *graphql.ID) int32

GraphQLIDPointerToInt32 :nodoc:

func GraphQLIDPointerToInt64

func GraphQLIDPointerToInt64(id *graphql.ID) int64

GraphQLIDPointerToInt64 :nodoc:

func GraphQLIDPointerToString

func GraphQLIDPointerToString(id *graphql.ID) string

GraphQLIDPointerToString :nodoc:

func GraphQLIDToInt32

func GraphQLIDToInt32(id graphql.ID) int32

GraphQLIDToInt32 :nodoc:

func GraphQLIDToInt64

func GraphQLIDToInt64(id graphql.ID) int64

GraphQLIDToInt64 :nodoc:

func GraphQLIDToString

func GraphQLIDToString(id graphql.ID) string

GraphQLIDToString :nodoc:

func Int32PointerToInt32

func Int32PointerToInt32(i *int32) int32

Int32PointerToInt32 :nodoc:

func Int32PointerToInt64

func Int32PointerToInt64(i *int32) int64

Int32PointerToInt64 :nodoc:

func Int64MillisToPointerTime added in v1.11.0

func Int64MillisToPointerTime(millis int64) *time.Time

Int64MillisToPointerTime convert millis to pointer time in UTC

func Int64MillisToTime added in v1.11.0

func Int64MillisToTime(millis int64) time.Time

Int64MillisToTime convert millis to time in UTC

func Int64PointerToInt64 added in v1.23.0

func Int64PointerToInt64(i *int64) int64

Int64PointerToInt64 :nodoc:

func Int64ToGraphQLID

func Int64ToGraphQLID(i int64) graphql.ID

Int64ToGraphQLID :nodoc:

func Int64ToString

func Int64ToString(i int64) string

Int64ToString :nodoc:

func Int64WithLimit

func Int64WithLimit(input int64, limit int64) int64

Int64WithLimit -> Check req value bigger or not from limit.

func Int64WithMinAndMaxLimit added in v1.20.0

func Int64WithMinAndMaxLimit(input, min, max int64) int64

Int64WithMinAndMaxLimit check input value. if bigger than max, then return max. if smaller than min, then return min. else return input.

func InterfaceBytesToInt64

func InterfaceBytesToInt64(i interface{}) int64

InterfaceBytesToInt64 will transform cached value that get from the redis to Int64

func InterfaceBytesToType added in v1.21.0

func InterfaceBytesToType[T any](i interface{}) (out T)

InterfaceBytesToType will transform cached value that get from the redis to any types

func IsEmailValid

func IsEmailValid(email string) bool

IsEmailValid -> validate email using regex

func IsNumeric

func IsNumeric(s string) bool

IsNumeric -> Check if input string is int

func IsSameSliceIgnoreOrder

func IsSameSliceIgnoreOrder(a, b []int64) bool

IsSameSliceIgnoreOrder to compare slice without order

func IsURLReachable added in v1.36.0

func IsURLReachable(url string) bool

IsURLReachable check is the url reachable

func IsUniqueSliceItem added in v1.37.0

func IsUniqueSliceItem[T comparable](data []T) bool

IsUniqueSliceItem :nodoc:

func JoinURL

func JoinURL(baseURL string, pathElements ...string) (string, error)

JoinURL joins URL with the path elements

func LowerMapStringKey added in v1.3.0

func LowerMapStringKey(v map[string]interface{}) map[string]interface{}

LowerMapStringKey :nodoc:

func MapValuesToOrderedSlice added in v1.31.0

func MapValuesToOrderedSlice[K comparable, V any](src map[K]V, keysOrder []K) (orderedMapValues []V)

MapValuesToOrderedSlice convert map values to ordered slice. If the map didn't contain one or more key in the keysOrder slice, then it will be filled with zero value.

func MyCaller added in v1.2.0

func MyCaller(skip int) string

MyCaller will return the method caller. skip value defines how many steps to be skipped. skip=0 will always return the MyCaller skip=1 returns the caller of the MyCaller and so on...

func Offset

func Offset(page, limit int64) int64

Offset to get offset from page and limit, min value for page = 1

func PaginateSlice added in v1.29.0

func PaginateSlice[T comparable](data []T, page, size int64) []T

PaginateSlice :nodoc:

func ParseDurationWithDefault added in v1.23.0

func ParseDurationWithDefault(in string, defaultDuration time.Duration) time.Duration

ParseDurationWithDefault self explained

func RegexEmail

func RegexEmail() *regexp.Regexp

RegexEmail returns regex for email

func Retry

func Retry(attempts int, sleep time.Duration, fn func() error) error

Retry :nodoc:

func SliceAtoi

func SliceAtoi(s []string) ([]int, error)

SliceAtoi -> convert array of string to array of integer

func SlicePointerInt32PointerToSliceInt64

func SlicePointerInt32PointerToSliceInt64(i *[]*int32) (result []int64)

SlicePointerInt32PointerToSliceInt64 :nodoc:

func StandardizeSpaces

func StandardizeSpaces(s string) string

StandardizeSpaces -> JoinURL long query to one line query

func StringMillisToPointerTime added in v1.10.0

func StringMillisToPointerTime(millis string) *time.Time

StringMillisToPointerTime convert millis to pointer time in UTC

func StringMillisToTime added in v1.10.0

func StringMillisToTime(millis string) time.Time

StringMillisToTime convert millis to time in UTC

func StringPointerToFloat64

func StringPointerToFloat64(s *string) float64

StringPointerToFloat64 :nodoc:

func StringPointerToInt64

func StringPointerToInt64(s *string) int64

StringPointerToInt64 :nodoc:

func StringPointerToString

func StringPointerToString(s *string) string

StringPointerToString :nodoc:

func StringToBool

func StringToBool(s string) bool

StringToBool :nodoc:

func StringToInt64

func StringToInt64(s string) int64

StringToInt64 :nodoc:

func StringToInt64WithDefault

func StringToInt64WithDefault(s string, d int64) int64

StringToInt64WithDefault :nodoc:

func StripHTML added in v1.12.0

func StripHTML(s string) string

StripHTML strips all HTML from a string, duh

func Ternary

func Ternary(condition bool, a, b interface{}) interface{}

Ternary if condition is true, return a else b

func TimeFromObjectIDHex added in v1.30.0

func TimeFromObjectIDHex(s string) (t time.Time, err error)

TimeFromObjectIDHex extracts time data from bson's objectID

func ToByte

func ToByte(i interface{}) []byte

ToByte :nodoc:

func TrimSpacePointerString added in v1.33.0

func TrimSpacePointerString(s *string)

TrimSpacePointerString :nodoc:

func TruncateString added in v1.7.0

func TruncateString(str string, num int) string

TruncateString by length, and add ellipsis at the end

func UnescapeString

func UnescapeString(str string) (ustr string)

UnescapeString UTF-8 string e.g. convert "\u0e27\u0e23\u0e0d\u0e32" to "วรญา"

func Unique added in v1.21.0

func Unique[T comparable](elements []T) (result []T)

Unique returns unique value in as slice

func UniqueInt64

func UniqueInt64(elements []int64) []int64

UniqueInt64 :nodoc:

func UniqueString

func UniqueString(elements []string) []string

UniqueString :nodoc:

func ValueOfPointer added in v1.33.0

func ValueOfPointer[T comparable](i *T) T

ValueOfPointer return value of pointer T

func ValueOrDefault added in v1.26.0

func ValueOrDefault[T comparable](value, defaultValue T) T

ValueOrDefault use the given value or use default value if the value = empty value

Types

type Hook added in v1.15.0

type Hook struct {
	// contains filtered or unexported fields
}

Hook "the hook"

func NewHook added in v1.15.0

func NewHook(options sentry.ClientOptions, levels ...logrus.Level) (*Hook, error)

NewHook Initiate new hook

func (*Hook) AddTag added in v1.15.0

func (hook *Hook) AddTag(key, value string)

AddTag implements AddTag

func (*Hook) Fire added in v1.15.0

func (hook *Hook) Fire(entry *logrus.Entry) error

Fire implements Fire

func (*Hook) Flush added in v1.15.0

func (hook *Hook) Flush()

Flush implements Flush

func (*Hook) Levels added in v1.15.0

func (hook *Hook) Levels() []logrus.Level

Levels implements Levels

func (*Hook) SetEnvironment added in v1.15.0

func (hook *Hook) SetEnvironment(environment string)

SetEnvironment implements SetEnvironment

func (*Hook) SetFlushTimeout added in v1.15.0

func (hook *Hook) SetFlushTimeout(timeout time.Duration)

SetFlushTimeout implements SetFlushTimeout

func (*Hook) SetPrefix added in v1.15.0

func (hook *Hook) SetPrefix(prefix string)

SetPrefix implements SetPrefix

func (*Hook) SetRelease added in v1.15.0

func (hook *Hook) SetRelease(release string)

SetRelease implements SetRelease

func (*Hook) SetTags added in v1.15.0

func (hook *Hook) SetTags(tags map[string]string)

SetTags implements SetTags

type RetryStopper

type RetryStopper struct {
	// contains filtered or unexported fields
}

RetryStopper :nodoc:

func NewRetryStopper added in v1.3.1

func NewRetryStopper(err error) RetryStopper

NewRetryStopper :nodoc:

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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