util

package
v0.0.0-...-44f2bf6 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2024 License: Apache-2.0 Imports: 26 Imported by: 6

Documentation

Index

Constants

View Source
const TimeLayout = time.RFC3339

Variables

This section is empty.

Functions

func And

func And(in ...bool) bool

func ApplyMods

func ApplyMods[T any](in *T, mods ...Mod[T])

func BoolSwitch

func BoolSwitch[T interface{}](s bool, t, f T) T

BoolSwitch define bool switch for defined types - in case of true t T is returned, in case of false f T

func CheckConditionalNil

func CheckConditionalNil[T interface{}](in ConditionalFunction[T]) *T

func CheckConditionalP1Nil

func CheckConditionalP1Nil[T, P1 interface{}](in ConditionalP1Function[T, P1], p1 P1) *T

func CompareInt

func CompareInt(a, b int) bool

func CompareInt64

func CompareInt64(a, b int64) bool

func CompareInt64p

func CompareInt64p(a, b *int64) bool

func CompareIntp

func CompareIntp(a, b *int) bool

func CompareJSON

func CompareJSON[T interface{}](a, b T) (bool, error)

func CompareJSONP

func CompareJSONP[T interface{}](a, b *T) (bool, error)

func ContainsList

func ContainsList[A comparable](in []A, item A) bool

func CopyFullMap

func CopyFullMap[K comparable, V any](src map[K]V) map[K]V

func CopyList

func CopyList[A any](in []A) []A

func CreateSignalContext

func CreateSignalContext(ctx context.Context) context.Context

CreateSignalContext creates and returns the context which is closed when one of the provided signal occurs. SIGINT and SIGTERM is used by default.

func Default

func Default[T interface{}]() T

Default returns generic default value for type T

func DefaultInterface

func DefaultInterface[T interface{}]() interface{}

DefaultInterface returns generic default value for type T as Interface

func ExtractLabel

func ExtractLabel(obj meta.Object, label string) (string, bool)

func FileModTime

func FileModTime(file string) time.Time

func First

func First[T interface{}](input ...*T) *T

First returns first not nil value

func FirstNotDefault

func FirstNotDefault[T interface{}](input ...T) T

FirstNotDefault returns first not default value

func FlattenList

func FlattenList[A any](in [][]A) []A

func FormatList

func FormatList[A, B any](in []A, format func(A) B) []B

func FormatListErr

func FormatListErr[A, B any](in []A, format func(A) (B, error)) ([]B, error)

func InitOptional

func InitOptional[T any](in *T, ok bool) *T

func InitType

func InitType[T interface{}](in *T) *T

InitType initialise object if it is nil pointer

func IterateSorted

func IterateSorted[V any](m map[string]V, cb func(string, V))

func JSONRemarshal

func JSONRemarshal[A, B any](in A) (B, error)

func JsonOrYamlUnmarshal

func JsonOrYamlUnmarshal[T any](b []byte) (T, error)

func LastFromList

func LastFromList[T interface{}](in []T) T

LastFromList returns last element on the list

func ListAsMap

func ListAsMap[K comparable, V any](in []V, extract func(in V) K) map[K]V

func MD5

func MD5(data []byte) string

func MD5FromString

func MD5FromString(data string) string

func MapKeys

func MapKeys[K comparable, V any](m map[K]V) []K

func MapValues

func MapValues[K comparable, V any](m map[K]V) []V

func MergeMaps

func MergeMaps[K comparable, V any](override bool, maps ...map[K]V) map[K]V

func NewPointer

func NewPointer[T any](input *T) *T

NewPointer returns a reference to a copy of the pointer value

func NewType

func NewType[T interface{}](input T) *T

NewType returns a reference to a simple type with given value.

func NewTypeOrNil

func NewTypeOrNil[T interface{}](input *T) *T

NewTypeOrNil returns nil if input is nil, otherwise returns a clone of the given value.

func NormalizeEnv

func NormalizeEnv(env string) string

NormalizeEnv normalizes environment variables.

func OpenWithRead

func OpenWithRead(path string, size int) ([]byte, error)

func Optional

func Optional[K comparable, V any](m map[K]V, key K, def V) V

func OptionalType

func OptionalType[T any](in *T, v T) T

OptionalType returns the default value (or T default value)

func Or

func Or(in ...bool) bool

func ParallelInput

func ParallelInput[T any](in []T) <-chan T

func ParallelProcess

func ParallelProcess[T any](caller func(in T), threads int, in []T)

func ParallelThread

func ParallelThread(max int) (chan struct{}, func())

func ParseAgencyTime

func ParseAgencyTime(s string) (time.Time, bool)

func ParseTime

func ParseTime(s string, layouts ...string) (time.Time, int)

func PickFromList

func PickFromList[V any](in []V, q func(v V) bool) (V, bool)

func Rand

func Rand() *rand.Rand

func RandomElement

func RandomElement[T interface{}](in []T) (T, bool)

func Read

func Read(in io.Reader, buff []byte) (int, error)

func RecentFileModTime

func RecentFileModTime(files ...string) time.Time

func RunParallel

func RunParallel(max int, actions ...func() error) error

RunParallel runs actions parallelly throttling them to the given maximum number.

func SHA256

func SHA256(data []byte) string

func SHA256FromExtract

func SHA256FromExtract[T any](extract func(T) string, obj ...T) string

func SHA256FromFile

func SHA256FromFile(file string) (string, error)

func SHA256FromHashArray

func SHA256FromHashArray[T Hash](data []T) string

func SHA256FromIO

func SHA256FromIO(in io.Reader) (string, error)

func SHA256FromJSON

func SHA256FromJSON[T interface{}](a T) (string, error)

func SHA256FromString

func SHA256FromString(data string) string

func SHA256FromStringArray

func SHA256FromStringArray(data ...string) string

func SHA256FromStringMap

func SHA256FromStringMap(data map[string]string) string

func Sort

func Sort[IN any](in []IN, cmp func(i, j IN) bool) []IN

func SortKeys

func SortKeys[V any](m map[string]V) []string

func TimeAgencyLayouts

func TimeAgencyLayouts() []string

func TimeCompareEqual

func TimeCompareEqual(a, b meta.Time) bool

TimeCompareEqual compares two times, allowing an error of 1s

func TimeCompareEqualPointer

func TimeCompareEqualPointer(a, b *meta.Time) bool

TimeCompareEqualPointer compares two times, allowing an error of 1s Returns true if both pointers are nil

func TypeOrDefault

func TypeOrDefault[T interface{}](input *T, defaultValue ...T) T

TypeOrDefault returns the default value (or T default value) if input is nil, otherwise returns the referenced value.

func UniqueList

func UniqueList[A comparable](in []A) []A

func WithContextTimeoutP1A1

func WithContextTimeoutP1A1[P1, A1 interface{}](ctx context.Context, timeout time.Duration, f func(context.Context, A1) P1, a1 A1) P1

func WithContextTimeoutP1A2

func WithContextTimeoutP1A2[P1, A1, A2 interface{}](ctx context.Context, timeout time.Duration, f func(context.Context, A1, A2) P1, a1 A1, a2 A2) P1

func WithContextTimeoutP1A4

func WithContextTimeoutP1A4[P1, A1, A2, A3, A4 interface{}](ctx context.Context, timeout time.Duration, f func(context.Context, A1, A2, A3, A4) P1, a1 A1, a2 A2, a3 A3, a4 A4) P1

func WithContextTimeoutP2A2

func WithContextTimeoutP2A2[P1, P2, A1, A2 interface{}](ctx context.Context, timeout time.Duration, f func(context.Context, A1, A2) (P1, P2), a1 A1, a2 A2) (P1, P2)

func WithContextTimeoutP2A4

func WithContextTimeoutP2A4[P1, P2, A1, A2, A3, A4 interface{}](ctx context.Context, timeout time.Duration, f func(context.Context, A1, A2, A3, A4) (P1, P2), a1 A1, a2 A2, a3 A3, a4 A4) (P1, P2)

func WithContextTimeoutP4A3

func WithContextTimeoutP4A3[P1, P2, P3, P4, A1, A2, A3 interface{}](ctx context.Context, timeout time.Duration, f func(context.Context, A1, A2, A3) (P1, P2, P3, P4), a1 A1, a2 A2, a3 A3) (P1, P2, P3, P4)

func WithDefault

func WithDefault[T interface{}](in *T) T

WithDefault returns generic default value for type T if in is nil

func WithKubernetesContextTimeoutP1A1

func WithKubernetesContextTimeoutP1A1[P1, A1 interface{}](ctx context.Context, f func(context.Context, A1) P1, a1 A1) P1

func WithKubernetesContextTimeoutP1A2

func WithKubernetesContextTimeoutP1A2[P1, A1, A2 interface{}](ctx context.Context, f func(context.Context, A1, A2) P1, a1 A1, a2 A2) P1

func WithKubernetesContextTimeoutP1A4

func WithKubernetesContextTimeoutP1A4[P1, A1, A2, A3, A4 interface{}](ctx context.Context, f func(context.Context, A1, A2, A3, A4) P1, a1 A1, a2 A2, a3 A3, a4 A4) P1

func WithKubernetesContextTimeoutP2A2

func WithKubernetesContextTimeoutP2A2[P1, P2, A1, A2 interface{}](ctx context.Context, f func(context.Context, A1, A2) (P1, P2), a1 A1, a2 A2) (P1, P2)

func WithKubernetesContextTimeoutP2A4

func WithKubernetesContextTimeoutP2A4[P1, P2, A1, A2, A3, A4 interface{}](ctx context.Context, f func(context.Context, A1, A2, A3, A4) (P1, P2), a1 A1, a2 A2, a3 A3, a4 A4) (P1, P2)

func WithKubernetesContextTimeoutP4A3

func WithKubernetesContextTimeoutP4A3[P1, P2, P3, P4, A1, A2, A3 interface{}](ctx context.Context, f func(context.Context, A1, A2, A3) (P1, P2, P3, P4), a1 A1, a2 A2, a3 A3) (P1, P2, P3, P4)

func WithKubernetesPatch

func WithKubernetesPatch[P1 meta.Object](ctx context.Context, obj string, client PatchInterface[P1], p ...patch.Item) (P1, error)

func WriteAll

func WriteAll(out io.Writer, data []byte) (int, error)

Types

type ConditionalFunction

type ConditionalFunction[T interface{}] func() (T, bool)

type ConditionalP1Function

type ConditionalP1Function[T, P1 interface{}] func(p1 P1) (T, bool)

type EnvironmentVariable

type EnvironmentVariable string

EnvironmentVariable is a wrapper to get environment variables

func (EnvironmentVariable) Exists

func (e EnvironmentVariable) Exists() bool

Exists check if variable is defined

func (EnvironmentVariable) Get

func (e EnvironmentVariable) Get() string

Get fetch variable. If variable does not exist empty string is returned

func (EnvironmentVariable) GetOrDefault

func (e EnvironmentVariable) GetOrDefault(d string) string

GetOrDefault fetch variable. If variable is not defined default value is returned

func (EnvironmentVariable) Lookup

func (e EnvironmentVariable) Lookup() (string, bool)

Lookup for environment variable

func (EnvironmentVariable) String

func (e EnvironmentVariable) String() string

String return string representation of environment variable name

type Hash

type Hash interface {
	Hash() string
}

type Interval

type Interval time.Duration

Interval is a specialization of Duration so we can add some helper functions to that.

func (Interval) After

func (i Interval) After() <-chan time.Time

After waits for the interval to elapse and then sends the current time on the returned channel.

func (Interval) Backoff

func (i Interval) Backoff(factor float64, maxInt Interval) Interval

Backoff returns an interval that is equal to min(i*factor, maxInt).

func (Interval) IncreaseTo

func (i Interval) IncreaseTo(x Interval) Interval

IncreaseTo returns an interval that is equal to max(x, i).

func (Interval) ReduceTo

func (i Interval) ReduceTo(x Interval) Interval

ReduceTo returns an interval that is equal to min(x, i).

func (Interval) String

func (i Interval) String() string

type KV

type KV[K comparable, V any] struct {
	K K
	V V
}

func Extract

func Extract[K comparable, V any](in map[K]V) []KV[K, V]

func ExtractWithSort

func ExtractWithSort[K comparable, V any](in map[K]V, cmp func(i, j K) bool) []KV[K, V]

type List

type List[T any] []T

func MapList

func MapList[T, V comparable](in List[T], fn func(T) V) List[V]

func (List[T]) Append

func (l List[T]) Append(items ...T) List[T]

func (List[T]) Contains

func (l List[T]) Contains(fn func(T) bool) bool

func (List[T]) Count

func (l List[T]) Count(fn func(T) bool) int

func (List[T]) Filter

func (l List[T]) Filter(fn func(T) bool) List[T]

func (List[T]) List

func (l List[T]) List() []T

func (List[T]) Sort

func (l List[T]) Sort(fn func(T, T) bool) List[T]

func (List[T]) Unique

func (l List[T]) Unique(f func(existing List[T], a T) bool) List[T]

type Loader

type Loader[T any] interface {
	MustGet() T

	Get() (T, error)
}

func NewLoader

func NewLoader[T any](gen LoaderGen[T]) Loader[T]

func NewYamlLoader

func NewYamlLoader[T any](data []byte) Loader[T]

type LoaderGen

type LoaderGen[T any] func() (T, error)

type Mod

type Mod[T any] func(in *T)

func WithMods

func WithMods[T any](mods ...Mod[T]) Mod[T]

func (Mod[T]) Optional

func (m Mod[T]) Optional() Mod[T]

type NextIterator

type NextIterator[T any] interface {
	Next(ctx context.Context) (T, error)
}

type PatchInterface

type PatchInterface[P1 meta.Object] interface {
	Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts meta.PatchOptions, subresources ...string) (P1, error)
}

type Registerer

type Registerer[K comparable, V any] interface {
	Register(key K, value V) bool
	MustRegister(key K, value V)

	Items() []KV[K, V]
}

func NewRegisterer

func NewRegisterer[K comparable, V any]() Registerer[K, V]

Jump to

Keyboard shortcuts

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