utils

package
v0.33.3 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2025 License: GPL-3.0 Imports: 19 Imported by: 29

Documentation

Index

Constants

View Source
const (
	CompareTypeErrorKey   = "$type"
	CompareLengthErrorKey = "$length"
	CompareAErrorKey      = "$A"
	CompareBErrorKey      = "$B"
)
View Source
const DIR_PERMISSION = 0744
View Source
const FILE_PERMISSION = 0644

These two const are needed because only 07xx (executable) directories can be accessed

Variables

View Source
var ErrorNotLoaded = errors.New("not loaded")

Functions

func AssignToT

func AssignToT[T any, U any](t *T, u U) error

func BoolPtr

func BoolPtr(b bool) *bool

func CheckFs

func CheckFs(afs afero.Fs, expected []TestFsEntry) (err error)

func CreateJsonPathChecker

func CreateJsonPathChecker(expression string) (checker func(document any) (bool, error), err error)

func CreateJsonPathCheckerFromEvaluable

func CreateJsonPathCheckerFromEvaluable(jp gval.Evaluable) (checker func(document any) (bool, error))

func CreateTemplateChecker

func CreateTemplateChecker(expression string) (checker func(document any) (bool, error), err error)

func CreateTemplateCheckerFromTemplate

func CreateTemplateCheckerFromTemplate(tmpl *template.Template) (checker func(document any) (bool, error))

func DecodeNewValue

func DecodeNewValue[T any](input any) (*T, error)

Attempts to decode the value passed in as input to the structure specified by the template Structs can be converted to maps and vice-versa, strings to integers, etc... The Tag used for specifying how to decode/encode is centralized in 'json', no need to use 'mapstructure'.

func DecodeValue

func DecodeValue[T any, U any](input T, output *U) (err error)

Attempts to decode the value passed in as input to the structure specified by the template Structs can be converted to maps and vice-versa, strings to integers, etc... The Tag used for specifying how to decode/encode is centralized in 'json', no need to use 'mapstructure'.

func ExecuteTemplateTrimmed

func ExecuteTemplateTrimmed(tmpl *template.Template, doc any) (s string, err error)

func GetJsonPath

func GetJsonPath(expression string, document any) (result any, err error)

func IsComparableEqual

func IsComparableEqual[V comparable](a, b V) bool

func IsComparablePointerEqual

func IsComparablePointerEqual[V comparable](a, b *V) bool

func IsPointerEqualFunc

func IsPointerEqualFunc[V any](a, b *V, equals func(*V, *V) bool) bool

equals is only called if both pointers are non-nil

func IsSameValueOrPointer

func IsSameValueOrPointer(a, b any) bool

Checks if the value is exactly the same, it's basically a "==" but handles map/slice and other non-comparable types as pointers, that is if their underlying pointer is the same. It DOES NOT check for map/slice similarities, only their addresses.

func NewJsonPath

func NewJsonPath(expression string) (jp gval.Evaluable, err error)

func NewLazyLoader

func NewLazyLoader[T any](loader func() T) func() T

func NewLazyLoaderWithArg

func NewLazyLoaderWithArg[T any, U comparable](loader func(U) T) func(U) T

Type U *MUST* be comparable

func NewLazyOnce

func NewLazyOnce(loader func()) func()

func NewTemplate

func NewTemplate(expression string) (tmpl *template.Template, err error)

func NewTemplateFilename

func NewTemplateFilename(expression string, fileName string) (tmpl *template.Template, err error)

func PrepareFs

func PrepareFs(afs afero.Fs, provided []TestFsEntry) (err error)

func ReflectValueDeepEqualCompare

func ReflectValueDeepEqualCompare(vA, vB reflect.Value) (err error)

func ReflectValueStructFieldsCompare

func ReflectValueStructFieldsCompare(vA, vB reflect.Value, cmp StructFieldsComparator) (err error)

func ReflectValueUnorderedSliceCompare

func ReflectValueUnorderedSliceCompare(vA, vB reflect.Value, compare ReflectValueCompareFn) (err error)

Compares the slices ignoring their order (quadratic: O(len(a) * O(len(b)))

func ReflectValueUnorderedSliceCompareDeepEqual

func ReflectValueUnorderedSliceCompareDeepEqual(vA, vB reflect.Value) (err error)

func SimplifyAny

func SimplifyAny(value any) (converted any, err error)

This function takes any Go value (simple types, arrays, maps, structs, pointers...) and converts it into the simplest possible native types. Pointers are converted into their respective underlying value, structs are converted into map[string]any, etc. This operation is recursive and will convert the passed value in its entirety, and not just the first depth level

func StructFieldsCompare

func StructFieldsCompare[T any](a, b T, cmp StructFieldsComparator) (err error)

func StructFieldsEqual

func StructFieldsEqual[T any](a, b T, cmp StructFieldsComparator) bool

func UnorderedSliceCompareDeepEqual

func UnorderedSliceCompareDeepEqual[T any, S ~[]T](a, b S) (err error)

func UnorderedSliceDeepEqual

func UnorderedSliceDeepEqual[T any, S ~[]T](a, b S) bool

Types

type COW

type COW[T any] interface {
	// Compare current value to another.
	//
	// May be called on nil COW handle, compares to the empty value of T.
	Equals(other T) bool
	// Return the current value and if it was changed, resets the internal pointers
	//
	// May be called on nil COW handle, returns the empty value of T.
	Release() (value T, changed bool)
	// Return the current value, do not modify it!
	//
	// May be called on nil COW handle, returns the empty value of T
	Peek() (value T)
	// Checks if the COW was mutated from its original value.
	//
	// May be called on nil COW handle, returns false.
	IsChanged() (changed bool)

	// Replace the internal value with another, if it's different.
	//
	// **CANNOT** be called on nil COW handle.
	Replace(other T) bool
}

type COWContainer

type COWContainer[K comparable, V any] interface {
	// Iterates over all items of the container.
	//
	// If cb returns false the loop will stop and this function will also return false.
	// Otherwise this function returns true (meaning it looped through all items)
	//
	// May be called on nil COW handle, won't iterate and return true.
	ForEach(cb func(key K, value V) (run bool)) (finished bool)

	// Get the item at key.
	//
	// May be called on nil COW handle, returns the empty value of V and ok == false.
	Get(key K) (value V, ok bool)

	// Get the current container length.
	//
	// May be called on nil COW handle, returns 0.
	Len() int

	// Checks if the given value exists at the target key
	//
	// May be called on nil COW handle, returns false.
	ExistsAt(key K, value V) bool

	// Set is smart to not modify the map if value is the same
	//
	// Returns true if the value was modified, false otherwise.
	//
	// **CANNOT** be called on nil COW handle.
	Set(key K, value V) bool

	// Deletes the key (if it exists).
	//
	// May be called on nil COW handle, nothing is done
	Delete(key K) bool
}

COW that is a container of other values (ie: slice, map)

type COWContainerOfCOW

type COWContainerOfCOW[K comparable, V any, C COW[V]] interface {
	// Iterates over all COW items of the container.
	//
	// If cb returns false the loop will stop and this function will also return false.
	// Otherwise this function returns true (meaning it looped through all items)
	//
	// May be called on nil COW handle, won't iterate and return true.
	ForEachCOW(cb func(key K, cow C) (run bool)) (finished bool)

	// Get the COW item at key.
	//
	// May be called on nil COW handle, returns the empty COW of C and ok == false.
	GetCOW(key K) (cow C, ok bool)
}

COW that is a container of other values (ie: slice, map) that are exposed as children COW.

type COWMap

type COWMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map that will be copied before its first write, the original value is used if no writes were done

func NewCOWMapComparable

func NewCOWMapComparable[K comparable, V comparable](m map[K]V) *COWMap[K, V]

func NewCOWMapFunc

func NewCOWMapFunc[K comparable, V any](m map[K]V, equals func(V, V) bool) *COWMap[K, V]

func (*COWMap[K, V]) Delete

func (c *COWMap[K, V]) Delete(key K) (mutated bool)

Deletes the key (if it exists).

May be called on nil COW handle, nothing is done

func (*COWMap[K, V]) Equals

func (c *COWMap[K, V]) Equals(other map[K]V) bool

func (*COWMap[K, V]) ExistsAt

func (c *COWMap[K, V]) ExistsAt(key K, value V) bool

Checks if the given value exists at the target key

May be called on nil COW handle, returns false.

func (*COWMap[K, V]) ForEach

func (c *COWMap[K, V]) ForEach(cb func(key K, value V) (run bool)) (finished bool)

Iterates over all map items.

If cb returns false the loop will stop and this function will also return false. Otherwise this function returns true (meaning it looped through all items)

May be called on nil COW handle, won't iterate and return true.

func (*COWMap[K, V]) Get

func (c *COWMap[K, V]) Get(key K) (value V, ok bool)

Get the item at key.

May be called on nil COW handle, returns the empty value of V and ok == false.

func (*COWMap[K, V]) IsChanged

func (c *COWMap[K, V]) IsChanged() (changed bool)

func (*COWMap[K, V]) Len

func (c *COWMap[K, V]) Len() int

Get the current map length.

May be called on nil COW handle, returns 0.

func (*COWMap[K, V]) Peek

func (c *COWMap[K, V]) Peek() (m map[K]V)

Get the pointer to the internal reference.

DO NOT MODIFY THE RETURNED MAP

func (*COWMap[K, V]) Release

func (c *COWMap[K, V]) Release() (m map[K]V, changed bool)

func (*COWMap[K, V]) Replace

func (c *COWMap[K, V]) Replace(other map[K]V) bool

Only does it if the maps are not equal.

The COWMap will be set as changed and other will be COPIED.

**CANNOT** be called on nil COW handle.

func (*COWMap[K, V]) Set

func (c *COWMap[K, V]) Set(key K, value V) (mutated bool)

Set is smart to not modify the map if value is the same

Returns true if the value was modified, false otherwise.

**CANNOT** be called on nil COW handle.

type COWMapOfCOW

type COWMapOfCOW[K comparable, V any, C COW[V]] struct {
	// contains filtered or unexported fields
}

Map that will be copied before its first write, the original value is used if no writes were done

This variant will create a COW for each element, making it easy for users. Writes to the element's COW will update the parent (container) map (m) given at construction.

func NewCOWMapOfCOW

func NewCOWMapOfCOW[K comparable, V any, C COW[V]](m map[K]V, cowFactory func(V) C) *COWMapOfCOW[K, V, C]

func (*COWMapOfCOW[K, V, C]) Delete

func (c *COWMapOfCOW[K, V, C]) Delete(key K) (mutated bool)

Deletes the key (if it exists).

May be called on nil COW handle, nothing is done

func (*COWMapOfCOW[K, V, C]) Equals

func (c *COWMapOfCOW[K, V, C]) Equals(other map[K]V) bool

func (*COWMapOfCOW[K, V, C]) ExistsAt

func (c *COWMapOfCOW[K, V, C]) ExistsAt(key K, value V) bool

Checks if the given value exists at the target key

May be called on nil COW handle, returns false.

func (*COWMapOfCOW[K, V, C]) ForEach

func (c *COWMapOfCOW[K, V, C]) ForEach(cb func(key K, item V) (run bool)) (finished bool)

Iterates over all map items.

If cb returns false the loop will stop and this function will also return false. Otherwise this function returns true (meaning it looped through all items)

May be called on nil COW handle, won't iterate and return true.

func (*COWMapOfCOW[K, V, C]) ForEachCOW

func (c *COWMapOfCOW[K, V, C]) ForEachCOW(cb func(key K, cow C) (run bool)) (finished bool)

Iterates over all map COW items.

If cb returns false the loop will stop and this function will also return false. Otherwise this function returns true (meaning it looped through all items)

May be called on nil COW handle, won't iterate and return true.

func (*COWMapOfCOW[K, V, C]) Get

func (c *COWMapOfCOW[K, V, C]) Get(key K) (v V, ok bool)

Gets the plain value at the given key. Do not mutate it, if you want to mutate use GetCOW()

func (*COWMapOfCOW[K, V, C]) GetCOW

func (c *COWMapOfCOW[K, V, C]) GetCOW(key K) (cow C, ok bool)

Gets the Copy-on-Write value at the given key.

If the COW is mutated, the COWMapOfCOW (c) will reflect the new value automatically.

For the plain value, use Get().

func (*COWMapOfCOW[K, V, C]) IsChanged

func (c *COWMapOfCOW[K, V, C]) IsChanged() (changed bool)

func (*COWMapOfCOW[K, V, C]) Len

func (c *COWMapOfCOW[K, V, C]) Len() int

Get the current map length.

May be called on nil COW handle, returns 0.

func (*COWMapOfCOW[K, V, C]) Peek

func (c *COWMapOfCOW[K, V, C]) Peek() (m map[K]V)

Get the pointer to the internal reference.

DO NOT MODIFY THE RETURNED MAP

func (*COWMapOfCOW[K, V, C]) Release

func (c *COWMapOfCOW[K, V, C]) Release() (m map[K]V, changed bool)

func (*COWMapOfCOW[K, V, C]) Replace

func (c *COWMapOfCOW[K, V, C]) Replace(other map[K]V) bool

Only does it if the maps are not equal.

The COWMap will be set as changed and other will be COPIED.

**CANNOT** be called on nil COW handle.

func (*COWMapOfCOW[K, V, C]) Set

func (c *COWMapOfCOW[K, V, C]) Set(key K, value V) (mutated bool)

Set is smart to not modify the map if value is the same

Returns true if the value was modified, false otherwise.

**CANNOT** be called on nil COW handle.

type COWSlice

type COWSlice[V any] struct {
	// contains filtered or unexported fields
}

Slice that will be copied before its first write, the original value is used if no writes were done

func NewCOWSliceComparable

func NewCOWSliceComparable[V comparable](s []V) *COWSlice[V]

func NewCOWSliceFunc

func NewCOWSliceFunc[V any](s []V, equals func(V, V) bool) *COWSlice[V]

func (*COWSlice[V]) Add

func (c *COWSlice[V]) Add(value V)

If not Contains(value), then Append(value)

**CANNOT** be called on nil COW handle.

func (*COWSlice[V]) Append

func (c *COWSlice[V]) Append(value V)

Unconditionally appends the value.

**CANNOT** be called on nil COW handle.

func (*COWSlice[V]) Contains

func (c *COWSlice[V]) Contains(value V) bool

Checks if the given value exists in the slice

May be called on nil COW handle, returns false.

func (*COWSlice[V]) Delete

func (c *COWSlice[V]) Delete(i int) (mutated bool)

Deletes the index (if it exists).

May be called on nil COW handle, nothing is done

func (*COWSlice[V]) Equals

func (c *COWSlice[V]) Equals(other []V) bool

func (*COWSlice[V]) ExistsAt

func (c *COWSlice[V]) ExistsAt(i int, value V) bool

Checks if the given value exists at the target position

May be called on nil COW handle, returns false.

func (*COWSlice[V]) ForEach

func (c *COWSlice[V]) ForEach(cb func(index int, value V) (run bool)) (finished bool)

Iterates over all slice items.

If cb returns false the loop will stop and this function will also return false. Otherwise this function returns true (meaning it looped through all items)

May be called on nil COW handle, won't iterate and returns true.

func (*COWSlice[V]) Get

func (c *COWSlice[V]) Get(i int) (value V, ok bool)

Get the item at index.

May be called on nil COW handle, returns the empty value of V and ok == false.

func (*COWSlice[V]) IsChanged

func (c *COWSlice[V]) IsChanged() (changed bool)

func (*COWSlice[V]) Len

func (c *COWSlice[V]) Len() int

Get the current slice length.

May be called on nil COW handle, returns 0.

func (*COWSlice[V]) Peek

func (c *COWSlice[V]) Peek() (s []V)

Get the pointer to the internal reference.

DO NOT MODIFY THE RETURNED SLICE

func (*COWSlice[V]) Release

func (c *COWSlice[V]) Release() (s []V, changed bool)

func (*COWSlice[V]) Replace

func (c *COWSlice[V]) Replace(other []V) bool

Only does it if the slices are not equal.

The COWSlice will be set as changed and other will be COPIED.

**CANNOT** be called on nil COW handle.

func (*COWSlice[V]) Resize

func (c *COWSlice[V]) Resize(newSize int)

Resize the slice, if needed

**CANNOT** be called on nil COW handle

func (*COWSlice[V]) Set

func (c *COWSlice[V]) Set(i int, value V) (mutated bool)

Set is smart to not modify the slice if value is the same

Will grow the slice if needed.

Returns true if the value was modified, false otherwise.

**CANNOT** be called on nil COW handle.

type COWSliceOfCOW

type COWSliceOfCOW[V any, C COW[V]] struct {
	// contains filtered or unexported fields
}

Slice that will be copied before its first write, the original value is used if no writes were done

This variant will create a COW for each element, making it easy for users. Writes to the element's COW will update the parent (container) slice (s) given at construction.

func NewCOWSliceOfCOW

func NewCOWSliceOfCOW[V any, C COW[V]](s []V, cowFactory func(V) C) *COWSliceOfCOW[V, C]

func (*COWSliceOfCOW[V, C]) Add

func (c *COWSliceOfCOW[V, C]) Add(value V)

If not Contains(value), then Append(value)

**CANNOT** be called on nil COW handle.

func (*COWSliceOfCOW[V, C]) Append

func (c *COWSliceOfCOW[V, C]) Append(value V)

Unconditionally appends the value.

**CANNOT** be called on nil COW handle.

func (*COWSliceOfCOW[V, C]) Contains

func (c *COWSliceOfCOW[V, C]) Contains(value V) bool

Checks if the given value exists in the slice

May be called on nil COW handle, returns false.

func (*COWSliceOfCOW[V, C]) Delete

func (c *COWSliceOfCOW[V, C]) Delete(i int) (mutated bool)

Deletes the index (if it exists).

May be called on nil COW handle, nothing is done

func (*COWSliceOfCOW[V, C]) Equals

func (c *COWSliceOfCOW[V, C]) Equals(other []V) bool

func (*COWSliceOfCOW[V, C]) ExistsAt

func (c *COWSliceOfCOW[V, C]) ExistsAt(i int, value V) bool

Checks if the given value exists at the target position

May be called on nil COW handle, returns false.

func (*COWSliceOfCOW[V, C]) ForEach

func (c *COWSliceOfCOW[V, C]) ForEach(cb func(index int, item V) (run bool)) (finished bool)

Iterates over all slice items.

If cb returns false the loop will stop and this function will also return false. Otherwise this function returns true (meaning it looped through all items)

May be called on nil COW handle, won't iterate and returns true.

func (*COWSliceOfCOW[V, C]) ForEachCOW

func (c *COWSliceOfCOW[V, C]) ForEachCOW(cb func(index int, cow C) (run bool)) (finished bool)

Iterates over all slice COW items.

If cb returns false the loop will stop and this function will also return false. Otherwise this function returns true (meaning it looped through all items)

May be called on nil COW handle, won't iterate and returns true.

func (*COWSliceOfCOW[V, C]) Get

func (c *COWSliceOfCOW[V, C]) Get(i int) (v V, ok bool)

Get the item at index.

May be called on nil COW handle, returns the empty value of V and ok == false.

func (*COWSliceOfCOW[V, C]) GetCOW

func (c *COWSliceOfCOW[V, C]) GetCOW(i int) (cow C, ok bool)

Gets the Copy-on-Write value at the given index.

If the COW is mutated, the COWSliceOfCOW (c) will reflect the new value automatically.

For the plain value, use Get().

May be called on nil COW handle, returns the empty value of C and ok == false.

func (*COWSliceOfCOW[V, C]) IsChanged

func (c *COWSliceOfCOW[V, C]) IsChanged() (changed bool)

func (*COWSliceOfCOW[V, C]) Len

func (c *COWSliceOfCOW[V, C]) Len() int

Get the current slice length.

May be called on nil COW handle, returns 0.

func (*COWSliceOfCOW[V, C]) Peek

func (c *COWSliceOfCOW[V, C]) Peek() (s []V)

Get the pointer to the internal reference.

DO NOT MODIFY THE RETURNED SLICE

func (*COWSliceOfCOW[V, C]) Release

func (c *COWSliceOfCOW[V, C]) Release() (s []V, changed bool)

func (*COWSliceOfCOW[V, C]) Replace

func (c *COWSliceOfCOW[V, C]) Replace(other []V) bool

Only does it if the slices are not equal.

The COWSlice will be set as changed and other will be COPIED

**CANNOT** be called on nil COW handle.

func (*COWSliceOfCOW[V, C]) Resize

func (c *COWSliceOfCOW[V, C]) Resize(newSize int)

Resize the slice, if needed

**CANNOT** be called on nil COW handle

func (*COWSliceOfCOW[V, C]) Set

func (c *COWSliceOfCOW[V, C]) Set(i int, value V) (mutated bool)

Set is smart to not modify the slice if value is the same

Will grow the slice if needed.

Returns true if the value was modified, false otherwise.

**CANNOT** be called on nil COW handle.

type ChainedError

type ChainedError struct {
	Name string
	Err  error
}

Chained error that builds a path in the error message

It walks Err chain looking for other ChainedError, building a JSON Pointer-compatible path ("/" and "~" are escaped with "~0/1") and outputs the final error message

It's particularly useful to validate specifications and nested trees.

func (*ChainedError) Error

func (e *ChainedError) Error() string

func (*ChainedError) Is

func (e *ChainedError) Is(target error) bool

func (*ChainedError) Unwrap

func (e *ChainedError) Unwrap() error

type CompareError

type CompareError struct {
	A, B    any
	Op      string
	Message string
}

func (*CompareError) Error

func (e *CompareError) Error() string

func (*CompareError) Is

func (e *CompareError) Is(target error) bool

type LoadWithError

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

func NewLazyLoaderWithError

func NewLazyLoaderWithError[T any](loader LoadWithError[T]) LoadWithError[T]

type MapEntry

type MapEntry[K comparable, V any] struct {
	Key   K
	Value V
}

func SortedMapIterator

func SortedMapIterator[K constraints.Integer | string, V any](m map[K]V) []MapEntry[K, V]

type MultiError

type MultiError []error

func (MultiError) Error

func (e MultiError) Error() string

func (MultiError) Unwrap

func (e MultiError) Unwrap() []error

type OnceWithError

type OnceWithError func() error

func NewLazyOnceWithError

func NewLazyOnceWithError(loader OnceWithError) OnceWithError

type ReflectValueCompareFn

type ReflectValueCompareFn func(a, b reflect.Value) error

type StructFieldsComparator

type StructFieldsComparator interface {
	Type() reflect.Type
	// field index at reflect.Type
	Compare(field int, vA, vB reflect.Value) (err error)
}

func NewMapStructFieldsComparator

func NewMapStructFieldsComparator[T any](fields map[string]ReflectValueCompareFn) StructFieldsComparator

type TestFsEntry

type TestFsEntry struct {
	Path string
	Mode fs.FileMode
	Data []byte
}

func AutoMkdirAll

func AutoMkdirAll(entries []TestFsEntry) (expanded []TestFsEntry)

func FindFsEntry

func FindFsEntry(path string, entries []TestFsEntry) (TestFsEntry, error)

func MergeFsEntries

func MergeFsEntries(toBeMerged ...[]TestFsEntry) (merged []TestFsEntry)

Jump to

Keyboard shortcuts

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