utils

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: Apache-2.0 Imports: 13 Imported by: 3

Documentation

Index

Constants

View Source
const (
	ISO8601Seconds      = "2006-01-02T15:04:05Z07:00" //time.RFC3339
	ISO8601Milliseconds = "2006-01-02T15:04:05.000Z07:00"
)

Variables

View Source
var (
	TRUE  = true
	FALSE = false
)
View Source
var (
	MaxTime = time.Unix(1<<63-1, 0).UTC()
)

Functions

func BoolPtr

func BoolPtr(v bool) *bool

BoolPtr Deprecated: make use of ToPtr instead

func CamelToSnakeCase

func CamelToSnakeCase(camelCase string) string

CamelToSnakeCase convert "camelCase" string to "snake-case"

func ConvertSlice

func ConvertSlice(slice []interface{}) interface{}

ConvertSlice attempt to convert []interface{} to []elementType using the first element's type. if given slice is empty, or any elements is not the same type of first one, same slice is returned

func Float64Ptr

func Float64Ptr(v float64) *float64

Float64Ptr Deprecated: make use of ToPtr instead

func FromPtr

func FromPtr[T primitives](t *T) T

FromPtr will take a pointer type and return its value if it is not nil. Otherwise, it will return the default value for that type. ex,

	var s *string
 FromPtr(s) // results in ""
 *s = "hello"
 FromPtr(s) // results in "hello"
 var b *bool
 FromPtr(b) // results in false
	...
 // Custom Types with underlying types of primitives
 type String string
 var s *String
 FromPtr(s) // results in "" - but typed String
 *s = String("hello")
 FromPtr(s) // results in "hello" - but typed String

func IntPtr

func IntPtr(v int) *int

IntPtr Deprecated: make use of ToPtr instead

func MustSetIfNotNil

func MustSetIfNotNil(dst interface{}, src interface{})

MustSetIfNotNil takes "src" pointer (e.g. *bool) and set its dereference value to "dst" if not nil this function panic if: - "dst" and "src" are not pointer - "src" is not convertable to "dst" - "dst" not point to a settable value

func MustSetIfNotZero

func MustSetIfNotZero(dst interface{}, src interface{})

MustSetIfNotZero takes "src" value (e.g. bool) and set its value to "dst" if not zero this function panic if: - "dst" is not pointer or not point to a settable value - "src" is not convertable to "dst"

func ParseDuration

func ParseDuration(v string) time.Duration

func ParseString

func ParseString(s string) interface{}

func ParseTime

func ParseTime(layout, v string) time.Time

func ParseTimeISO8601

func ParseTimeISO8601(v string) time.Time

func RandomInt64N

func RandomInt64N(n int64) int64

RandomInt64N returns, as an int64, a non-negative uniform number in the half-open interval [0,n). This function uses "crypto/rand" and fallback to "math/rand". It panics if n <= 0.

func RandomIntN

func RandomIntN(n int) int

RandomIntN returns, as an int64, a non-negative uniform number in the half-open interval [0,n). This function uses "crypto/rand" and fallback to "math/rand". It panics if n <= 0.

func RandomString

func RandomString(length int) string

RandomString returns a random Alphanumeric string of given "length" this function uses "crypto/rand" and fallback to "math/rand" It panics if len(charset) > 255, and returns empty string if length is non-positive

func RandomStringWithCharset

func RandomStringWithCharset(length int, charset RandomCharset) string

RandomStringWithCharset returns a random string of given "length" containing only characters from given "charset" this function uses "crypto/rand" and fallback to "math/rand" It returns empty string if length is non-positive, and only the first 256 chars in "charset" are used

func RecoverableFunc

func RecoverableFunc[T SupportedRecoverableFunc](panicingFunc T) func() error

RecoverableFunc wrap a panicing function with following signature - func() - func() error into a func() error, where the recovered value is converted to error This function panics if the given function has incorrect signature

func Remove

func Remove[T any](slice []T, index int) []T

Remove will not keep the ordering of the slice. It has a very fast operation. This function will automatically type itself using type inference

	intSlice := []int{1, 2, 3, 4}
	intSlice = Remove(intSlice, 1)
 	result: {1, 4, 3}

func RemoveStable

func RemoveStable[T any](slice []T, index int) []T

RemoveStable will remove an element from the slice and keep its order. This operation can be potentially costly depending on how large the slice is since it needs to shift all elements that appear after index i over by 1.

This function will automatically type itself using type inference.

If the given index is not within the bounds of the slice, then the function will panic

func Reverse

func Reverse[T any](input []T)

Reverse will reverse the order of the given slice

func SetIfNotNil

func SetIfNotNil(dst interface{}, src interface{}) (err error)

SetIfNotNil is equivalent of MustSetIfNotNil, this function returns error instead of panic

func SetIfNotZero

func SetIfNotZero(dst interface{}, src interface{}) (err error)

SetIfNotZero is equivalent of MustSetIfNotZero, this function returns error instead of panic

func StringPtr

func StringPtr(v string) *string

StringPtr Deprecated: Make use of ToPtr instead

func ToPtr

func ToPtr[T any](t T) *T

ToPtr will return a pointer to any given input Example usage:

 var stringPtr *string
	stringPtr = ToPtr("hello world")

	// or some complex types
 var funcPtr *[]func(arg *argType)
 funcPtr = ToPtr([]func(arg *argType){})

func UIntPtr

func UIntPtr(v uint) *uint

UIntPtr Deprecated: make use of ToPtr instead

func UnQuote

func UnQuote(s string) string

func UuidPtr

func UuidPtr(v uuid.UUID) *uuid.UUID

UuidPtr Deprecated: make use of ToPtr instead

Types

type CommaSeparatedSlice

type CommaSeparatedSlice []string

CommaSeparatedSlice alias of []string that can deserialize from comma delimited string

func (CommaSeparatedSlice) MarshalText

func (s CommaSeparatedSlice) MarshalText() ([]byte, error)

MarshalText encoding.TextMarshaler

func (CommaSeparatedSlice) String

func (s CommaSeparatedSlice) String() string

fmt.Stringer

func (*CommaSeparatedSlice) UnmarshalJSON

func (s *CommaSeparatedSlice) UnmarshalJSON(data []byte) error

UnmarshalJSON json.Unmarshaler

func (*CommaSeparatedSlice) UnmarshalText

func (s *CommaSeparatedSlice) UnmarshalText(data []byte) error

UnmarshalText encoding.TextUnmarshaler

type ContextValuer

type ContextValuer func(key interface{}) interface{}

ContextValuer is an additional source of context.Context.Value(any)) used by MutableContext to search values with key. When MutableContext cannot find given key in its internal store, it will go through all ContextValuers before pass along the key-value searching to its parent context. See NewMutableContext and MakeMutableContext

type Duration

type Duration time.Duration

func (Duration) MarshalText

func (d Duration) MarshalText() (text []byte, err error)

MarshalText implements encoding.TextMarshaler

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler

type GenericSet

type GenericSet[T comparable] map[T]void

func NewGenericSet

func NewGenericSet[T comparable](values ...T) GenericSet[T]

func (GenericSet[T]) Add

func (s GenericSet[T]) Add(values ...T) GenericSet[T]

func (GenericSet[T]) Copy

func (s GenericSet[T]) Copy() GenericSet[T]

func (GenericSet[T]) Equals

func (s GenericSet[T]) Equals(another GenericSet[T]) bool

func (GenericSet[T]) Has

func (s GenericSet[T]) Has(value T) bool

func (GenericSet[T]) HasAll

func (s GenericSet[T]) HasAll(values ...T) bool

func (GenericSet[T]) MarshalJSON

func (s GenericSet[T]) MarshalJSON() ([]byte, error)

MarshalJSON json.Marshaler

func (GenericSet[T]) Remove

func (s GenericSet[T]) Remove(values ...T) GenericSet[T]

func (*GenericSet[T]) UnmarshalJSON

func (s *GenericSet[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON json.Unmarshaler

func (GenericSet[T]) Values

func (s GenericSet[T]) Values() []T

type ListableContext

type ListableContext interface {
	context.Context
	Values() map[interface{}]interface{}
}

ListableContext is supplementary interface of MutableContext, listing all values stored in the context

type MutableContext

type MutableContext interface {
	context.Context
	Set(key, value any)
}

MutableContext wraps context.Context with an internal KV pairs storage. KV pairs stored in this context can be changed in later time. To change/list KV pairs on any context.Context that inherit from MutableContext, use FindMutableContext to obtain a MutableContextAccessor. See FindMutableContext and MutableContextAccessor for more details

func MakeMutableContext

func MakeMutableContext(parent context.Context, valuers ...ContextValuer) MutableContext

MakeMutableContext return the context itself if it's already a MutableContext and no additional ContextValuer are specified. Otherwise, wrap the given context as MutableContext. Note: If the given context itself is not a MutableContext but its hierarchy contains MutableContext as parent context,

a new MutableContext is created and its mutable store (map) is not shared with the one from the hierarchy.

func NewMutableContext

func NewMutableContext(parent context.Context, valuers ...ContextValuer) MutableContext

NewMutableContext Wrap given context.Context with a mutable store and optionally additional KV sources defined as ContextValuer

type MutableContextAccessor

type MutableContextAccessor interface {
	Set(key, value any)
	Values() (values map[any]any)
}

func FindMutableContext

func FindMutableContext(ctx context.Context) MutableContextAccessor

FindMutableContext search for MutableContext from given context.Context's inheritance hierarchy, and return a MutableContextAccessor for key-values manipulation. If MutableContext is not found, nil is returned.

Important: This function may returns parent context of the given one. Therefore, changing values may affect parent context.

type RandomCharset

type RandomCharset string

RandomCharset is a string containing all acceptable UTF-8 characters for random string generation

const (
	CharsetAlphanumeric RandomCharset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	CharsetAlphabetic   RandomCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)

type Set

type Set map[interface{}]void

func NewSet

func NewSet(values ...interface{}) Set

func NewSetFrom

func NewSetFrom(i interface{}) Set

func NewSetFromStringSet

func NewSetFromStringSet(stringSet StringSet) Set

func (Set) Add

func (s Set) Add(values ...interface{}) Set

func (Set) Copy

func (s Set) Copy() Set

func (Set) Equals

func (s Set) Equals(another Set) bool

func (Set) Has

func (s Set) Has(value interface{}) bool

func (Set) HasAll

func (s Set) HasAll(values ...interface{}) bool

func (Set) MarshalJSON

func (s Set) MarshalJSON() ([]byte, error)

MarshalJSON json.Marshaler

func (Set) Remove

func (s Set) Remove(values ...interface{}) Set

func (*Set) UnmarshalJSON

func (s *Set) UnmarshalJSON(data []byte) error

UnmarshalJSON json.Unmarshaler

func (Set) Values

func (s Set) Values() []interface{}

type StringSet

type StringSet map[string]void

func NewStringSet

func NewStringSet(values ...string) StringSet

func NewStringSetFrom

func NewStringSetFrom(i interface{}) StringSet

func NewStringSetFromSet

func NewStringSetFromSet(set Set) StringSet

func (StringSet) Add

func (s StringSet) Add(values ...string) StringSet

func (StringSet) Copy

func (s StringSet) Copy() StringSet

func (StringSet) Equals

func (s StringSet) Equals(another StringSet) bool

func (StringSet) Has

func (s StringSet) Has(value string) bool

func (StringSet) HasAll

func (s StringSet) HasAll(values ...string) bool

func (StringSet) MarshalJSON

func (s StringSet) MarshalJSON() ([]byte, error)

MarshalJSON json.Marshaler

func (StringSet) Remove

func (s StringSet) Remove(values ...string) StringSet

func (StringSet) ToSet

func (s StringSet) ToSet() Set

func (*StringSet) UnmarshalJSON

func (s *StringSet) UnmarshalJSON(data []byte) error

UnmarshalJSON json.Unmarshaler

func (StringSet) Values

func (s StringSet) Values() []string

type SupportedRecoverableFunc

type SupportedRecoverableFunc interface {
	~func() | ~func() error
}

A SupportedRecoverableFunc is a function that can be converted by RecoverableFunc

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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