types

package module
v0.0.0-...-8363780 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 11 Imported by: 3

README

go-types

Mainly financial Go types used throughout the Domonda App

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidValue = errors.New("invalid value")

ErrInvalidValue means that a value is not valid, returned by Validate() and ValidatorAsValidatErr.Validate().

Functions

func CanMarshalJSON

func CanMarshalJSON(t reflect.Type) bool

CanMarshalJSON returns if a type can be marshalled as JSON

func DeepValidate

func DeepValidate(v any) error

DeepValidate validates all fields of a struct, all elements of a slice or array, and all values of a map by recursively calling Validate or Valid methods.

func ReduceSet

func ReduceSet[S ~map[T]struct{}, T cmp.Ordered, R any](set S, reduceFunc func(last R, val T) R) (result R)

func ReduceSlice

func ReduceSlice[S ~[]T, T cmp.Ordered, R any](slice S, reduceFunc func(last R, val T) R) (result R)

func ReflectCompare

func ReflectCompare(a, b reflect.Value) int

ReflectCompare compares two reflect.Values of the same type. The function panics if the types of a and b are not idential or not orderable. Orderable types are variantes of integers, floats, and strings.

func SetToRandomizedSlice

func SetToRandomizedSlice[S ~map[T]struct{}, T cmp.Ordered](set S) []T

func SetToSortedSlice

func SetToSortedSlice[S ~map[T]struct{}, T cmp.Ordered](set S) []T

func SliceContainsAll

func SliceContainsAll[T comparable](outer []T, inner ...T) bool

SliceContainsAll returns true if outer contains all elements of inner.

func SliceContainsAny

func SliceContainsAny[T comparable](outer []T, inner ...T) bool

SliceContainsAny returns true if outer contains any element of inner.

func TryValidate

func TryValidate(v any) (err error, isValidatable bool)

TryValidate returns an error and true if v implements ValidatErr or Validator and the methods ValidatErr.Validate() or Validator.Valid() indicate an invalid value. The error from ValidatErr.Validate() is returned directly, and ErrInvalidValue is returned if Validator.Valid() is false. If v does not implement ValidatErr or Validator then nil and false will be returned.

func Validate

func Validate(v any) error

Validate returns an error if v implements ValidatErr or Validator and the methods ValidatErr.Validate() or Validator.Valid() indicate an invalid value. The error from ValidatErr.Validate() is returned directly, and ErrInvalidValue is returned if Validator.Valid() is false. If v does not implement ValidatErr or Validator then nil will be returned.

Types

type Finder

type Finder interface {
	FindAllIndex(str []byte, n int) [][]int
}

type KeyMutex

type KeyMutex[T comparable] struct {
	// contains filtered or unexported fields
}

KeyMutex manages a unique mutex for every locked key. The mutex for a key exists as long as there are any locks waiting to be unlocked. This is equivalent to declaring a mutex variable for every key, except that the key and the number of mutexes are dynamic.

func NewKeyMutex

func NewKeyMutex[T comparable]() *KeyMutex[T]

NewKeyMutex returns a new KeyMutex

func (*KeyMutex[T]) IsLocked

func (m *KeyMutex[T]) IsLocked(key T) bool

IsLocked tells wether a key is locked.

func (*KeyMutex[T]) Lock

func (m *KeyMutex[T]) Lock(key T)

Lock the mutex for a given key

func (*KeyMutex[T]) Unlock

func (m *KeyMutex[T]) Unlock(key T)

Unlock the mutex for a given key.

type LenString

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

LenString holds a string together with a minimum and maximum length. Validate returns an error if the string length does not fit the minium-maxium length. LenString implements the encoding.UnmarshalText, json.Unmarshaler, and strfmt.StringAssignable interfaces that will do length validation.

func MustLenString

func MustLenString(str string, min, max int) LenString

MustLenString returns a LenString or panics on errors from Validate.

func NewLenString

func NewLenString(str string, min, max int) *LenString

NewLenString returns a new LenString without validating it.

func (*LenString) MarshalJSON

func (s *LenString) MarshalJSON() (text []byte, err error)

MarshalText implements the json.Marshaler interface

func (*LenString) MarshalText

func (s *LenString) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface

func (*LenString) MaxLen

func (s *LenString) MaxLen() int

func (*LenString) MinLen

func (s *LenString) MinLen() int

func (*LenString) ScanString

func (s *LenString) ScanString(source string, validate bool) error

ScanString tries to parse and assign the passed source string as value of the implementing type.

If validate is true, the source string is checked for validity before it is assigned to the type.

If validate is false and the source string can still be assigned in some non-normalized way it will be assigned without returning an error.

func (*LenString) SetString

func (s *LenString) SetString(str string) error

func (*LenString) String

func (s *LenString) String() string

String returns the string or "<nil>". String implements the fmt.Stringer interface.

func (*LenString) UnmarshalJSON

func (s *LenString) UnmarshalJSON(text []byte) error

UnmarshalJSON implements the json.Unmarshaler interface

func (*LenString) UnmarshalText

func (s *LenString) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface

func (*LenString) Validate

func (s *LenString) Validate() error

Validate implements the ValidatErr interface

type Set

type Set[T cmp.Ordered] map[T]struct{}

func NewSet

func NewSet[T cmp.Ordered](vals ...T) Set[T]

func (Set[T]) Add

func (set Set[T]) Add(val T)

func (Set[T]) AddSet

func (set Set[T]) AddSet(other Set[T])

func (Set[T]) AddSlice

func (set Set[T]) AddSlice(vals []T)

func (Set[T]) Clear

func (set Set[T]) Clear()

func (Set[T]) Clone

func (set Set[T]) Clone() Set[T]

func (Set[T]) Contains

func (set Set[T]) Contains(val T) bool

func (Set[T]) ContainsAll

func (set Set[T]) ContainsAll(vals ...T) bool

func (Set[T]) ContainsAny

func (set Set[T]) ContainsAny(vals ...T) bool

func (Set[T]) ContainsSet

func (set Set[T]) ContainsSet(other Set[T]) bool

func (Set[T]) Delete

func (set Set[T]) Delete(val T)

func (Set[T]) DeleteSet

func (set Set[T]) DeleteSet(other Set[T])

func (Set[T]) DeleteSlice

func (set Set[T]) DeleteSlice(vals []T)

func (Set[T]) Difference

func (set Set[T]) Difference(other Set[T]) Set[T]

func (Set[T]) Equal

func (set Set[T]) Equal(other Set[T]) bool

func (Set[T]) GetOne

func (set Set[T]) GetOne() T

GetOne returns one element of the set or the default value for T if the set is empty.

func (Set[T]) Intersection

func (set Set[T]) Intersection(other Set[T]) Set[T]

func (Set[T]) IsEmpty

func (set Set[T]) IsEmpty() bool

IsEmpty returns true if the set is empty or nil.

func (Set[T]) IsNull

func (set Set[T]) IsNull() bool

IsNull implements the nullable.Nullable interface by returning true if the set is nil.

func (Set[T]) Len

func (set Set[T]) Len() int

Len returns the length of the set. Returns zero for a nil set.

func (Set[T]) Map

func (set Set[T]) Map(mapFunc func(T) (T, bool)) Set[T]

func (Set[T]) MarshalJSON

func (set Set[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements encoding/json.Marshaler by returning the JSON null value for an empty (null) string.

func (Set[T]) Sorted

func (set Set[T]) Sorted() []T

func (Set[T]) String

func (set Set[T]) String() string

String implements the fmt.Stringer interface.

func (Set[T]) Union

func (set Set[T]) Union(other Set[T]) Set[T]

func (*Set[T]) UnmarshalJSON

func (set *Set[T]) UnmarshalJSON(j []byte) error

UnmarshalJSON implements encoding/json.Unmarshaler

type StaticValidatErr

type StaticValidatErr struct {
	Err error
}

StaticValidatErr implements the ValidatErr interface for a validation error value.

func (StaticValidatErr) Validate

func (v StaticValidatErr) Validate() error

Validate returns an error if the data of the implementation is not valid.

type StaticValidator

type StaticValidator bool

StaticValidator implements the Validator interface a bool validity value.

func (StaticValidator) Valid

func (valid StaticValidator) Valid() bool

Valid returns if the data of the implementation is valid.

type ValidatErr

type ValidatErr interface {
	// Validate returns an error if the data of the implementation is not valid.
	Validate() error
}

ValidatErr can be implemented by types that can validate their data.

func CombinedValidatErr

func CombinedValidatErr(validatErrs ...ValidatErr) ValidatErr

CombinedValidatErr creates a ValidatErr whose Validate method returns the first error from the passed validatErrs Validate methods or nil if none returned an error.

type ValidatErrFunc

type ValidatErrFunc func() error

ValidatErrFunc implements the ValidatErr interface with a function.

func (ValidatErrFunc) Validate

func (f ValidatErrFunc) Validate() error

Validate returns an error if the data of the implementation is not valid.

type ValidatErrs

type ValidatErrs []ValidatErr

func (ValidatErrs) Validate

func (v ValidatErrs) Validate() error

Validate returns an error if the data of the implementation is not valid.

type Validator

type Validator interface {
	// Valid returns if the data of the implementation is valid.
	Valid() bool
}

Validator can be implemented by types that can validate their data.

func CombinedValidator

func CombinedValidator(validators ...Validator) Validator

CombinedValidator creates a Validator whose Valid method returns false if any of the passed validators Valid methods returned false, else it returns true.

type ValidatorAsValidatErr

type ValidatorAsValidatErr struct {
	Validator
}

ValidatorAsValidatErr wraps a Validator as a ValidatErr, returning ErrInvalidValue when Validator.Valid() returns false.

func (ValidatorAsValidatErr) Validate

func (v ValidatorAsValidatErr) Validate() error

type ValidatorFunc

type ValidatorFunc func() bool

ValidatorFunc implements the Validator interface with a function.

func (ValidatorFunc) Valid

func (f ValidatorFunc) Valid() bool

Valid returns if the data of the implementation is valid.

type Validators

type Validators []Validator

func (Validators) Valid

func (v Validators) Valid() bool

Valid returns if the data of the implementation is valid.

Directories

Path Synopsis
pq
pq/oid
Package oid contains OID constants as defined by the Postgres server.
Package oid contains OID constants as defined by the Postgres server.
js module
GENERATED
GENERATED

Jump to

Keyboard shortcuts

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