Documentation
¶
Index ¶
- Variables
- func CanMarshalJSON(t reflect.Type) bool
- func DeepValidate(v any) []error
- func FromPtr[T any](ptr *T) T
- func FromPtrOr[T any](ptr *T, defaultValue T) T
- func Ptr[T any](value T) *T
- func ReduceSet[S ~map[T]struct{}, T cmp.Ordered, R any](set S, reduceFunc func(last R, val T) R) (result R)
- func ReduceSlice[S ~[]T, T cmp.Ordered, R any](slice S, reduceFunc func(last R, val T) R) (result R)
- func ReflectCompare(a, b reflect.Value) int
- func Seq2NilError[K any](seq iter.Seq[K]) iter.Seq2[K, error]
- func SetToRandomizedSlice[S ~map[T]struct{}, T cmp.Ordered](set S) []T
- func SetToSortedSlice[S ~map[T]struct{}, T cmp.Ordered](set S) []T
- func SliceContainsAll[T comparable](outer []T, inner ...T) bool
- func SliceContainsAny[T comparable](outer []T, inner ...T) bool
- func TryValidate(v any) (err error, isValidatable bool)
- func Validate(v any) error
- func Yield[V any](value V) iter.Seq[V]
- func Yield2[K, V any](key K, value V) iter.Seq2[K, V]
- func YieldErr[K any](err error) iter.Seq2[K, error]
- type Finder
- type KeyMutex
- type LenString
- func (s *LenString) MarshalJSON() (text []byte, err error)
- func (s *LenString) MarshalText() (text []byte, err error)
- func (s *LenString) MaxLen() int
- func (s *LenString) MinLen() int
- func (s *LenString) ScanString(source string, validate bool) error
- func (s *LenString) SetString(str string) error
- func (s *LenString) String() string
- func (s *LenString) UnmarshalJSON(text []byte) error
- func (s *LenString) UnmarshalText(text []byte) error
- func (s *LenString) Validate() error
- type Normalizable
- type NormalizableValidator
- type Set
- func (set Set[T]) Add(val T)
- func (set Set[T]) AddSet(other Set[T])
- func (set Set[T]) AddSlice(vals []T)
- func (set Set[T]) Clear()
- func (set Set[T]) Clone() Set[T]
- func (set Set[T]) Contains(val T) bool
- func (set Set[T]) ContainsAll(vals ...T) bool
- func (set Set[T]) ContainsAny(vals ...T) bool
- func (set Set[T]) ContainsSet(other Set[T]) bool
- func (set Set[T]) Delete(val T)
- func (set Set[T]) DeleteSet(other Set[T])
- func (set Set[T]) DeleteSlice(vals []T)
- func (set Set[T]) Difference(other Set[T]) Set[T]
- func (set Set[T]) Equal(other Set[T]) bool
- func (set Set[T]) GetOne() T
- func (set Set[T]) Intersection(other Set[T]) Set[T]
- func (set Set[T]) IsEmpty() bool
- func (set Set[T]) IsNull() bool
- func (set Set[T]) Len() int
- func (set Set[T]) Map(mapFunc func(T) (T, bool)) Set[T]
- func (set Set[T]) MarshalJSON() ([]byte, error)
- func (set Set[T]) Sorted() []T
- func (set Set[T]) String() string
- func (set Set[T]) Union(other Set[T]) Set[T]
- func (set *Set[T]) UnmarshalJSON(j []byte) error
- type StaticValidatErr
- type StaticValidator
- type ValidatErr
- type ValidatErrFunc
- type ValidatErrs
- type Validator
- type ValidatorAsValidatErr
- type ValidatorFunc
- type Validators
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidValue = errors.New("invalid value")
ErrInvalidValue means that a value is not valid, returned by Validate() and ValidatorAsValidatErr.Validate().
Functions ¶
func CanMarshalJSON ¶
CanMarshalJSON returns true if a type can be marshalled as JSON. It checks if the type implements json.Marshaler or encoding.TextMarshaler, or if it's a struct, map, or slice that can be marshalled by the standard JSON package.
func DeepValidate ¶
DeepValidate recursively 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. It returns all validation errors as a slice. Use errors.Join(DeepValidate(v)...) to join the errors into a single error.
func FromPtr ¶
func FromPtr[T any](ptr *T) T
FromPtr returns the dereferenced value of the pointer if it is not nil, otherwise it returns the zero value of the type T. This safely dereferences a pointer without panicking on nil.
func FromPtrOr ¶
func FromPtrOr[T any](ptr *T, defaultValue T) T
FromPtrOr returns the dereferenced value of the pointer if it is not nil, otherwise it returns the passed defaultValue. This provides a safe way to dereference a pointer with a fallback value.
func Ptr ¶
func Ptr[T any](value T) *T
Ptr returns a new pointer to a copy of the passed value. This is useful for creating pointers to values that need to be passed by reference.
func ReduceSet ¶
func ReduceSet[S ~map[T]struct{}, T cmp.Ordered, R any](set S, reduceFunc func(last R, val T) R) (result R)
ReduceSet applies a reduction function to all values in the set and returns the result. The reduceFunc is called for each value in the set, with the accumulated result and the current value.
func ReduceSlice ¶
func ReduceSlice[S ~[]T, T cmp.Ordered, R any](slice S, reduceFunc func(last R, val T) R) (result R)
ReduceSlice applies a reduction function to all values in the slice and returns the result. The reduceFunc is called for each value in the slice, with the accumulated result and the current value.
func ReflectCompare ¶
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. This is used for sorting map keys in DeepValidate.
func Seq2NilError ¶
Seq2NilError converts a sequence of values to a sequence of key-value pairs with the passed sequence values as keys and nil errors as values. This is useful for converting a value sequence to a key-error sequence where all errors are nil (indicating success).
func SetToRandomizedSlice ¶
SetToRandomizedSlice converts a set to a slice with values in random order.
func SetToSortedSlice ¶
SetToSortedSlice converts a set to a slice with values in sorted order.
func SliceContainsAll ¶
func SliceContainsAll[T comparable](outer []T, inner ...T) bool
SliceContainsAll returns true if outer contains all elements of inner. It returns false if outer has fewer elements than inner or if any element in inner is not found in outer.
func SliceContainsAny ¶
func SliceContainsAny[T comparable](outer []T, inner ...T) bool
SliceContainsAny returns true if outer contains any element of inner. It returns true as soon as the first element from inner is found in outer, false if none of the elements in inner are found in outer.
func TryValidate ¶
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. The boolean return value indicates whether the value was validatable.
func Validate ¶
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.
func Yield ¶
Yield returns an iterator that yields a single value. This is useful for converting a single value into an iterator sequence.
Types ¶
type Finder ¶
type Finder interface {
// FindAllIndex finds all occurrences of the pattern in str and returns
// a slice of index pairs [start, end] for each match.
// The parameter n limits the number of matches returned:
// - n < 0: return all matches
// - n == 0: return no matches
// - n > 0: return at most n matches
FindAllIndex(str []byte, n int) [][]int
}
Finder defines an interface for finding patterns in byte slices. It provides a method to find all occurrences of a pattern within a string, returning the start and end indices of each match.
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.
type LenString ¶
type LenString struct {
// contains filtered or unexported fields
}
LenString holds a string together with a minimum and maximum length constraints. It implements validation to ensure the string length falls within the specified bounds. LenString implements the encoding.UnmarshalText, json.Unmarshaler, and strfmt.StringAssignable interfaces that will perform length validation.
func MustLenString ¶
MustLenString returns a LenString or panics on errors from Validate. Use this when you're certain the string meets the length constraints.
func NewLenString ¶
NewLenString returns a new LenString without validating it. Use this when you want to create a LenString and validate it separately.
func (*LenString) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*LenString) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (*LenString) ScanString ¶
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 ¶
SetString sets the string value after validating it against the length constraints. Returns an error if the string doesn't meet the length requirements.
func (*LenString) String ¶
String returns the string or "<nil>". String implements the fmt.Stringer interface.
func (*LenString) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface. It validates the string length after unmarshaling from JSON.
func (*LenString) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. It validates the text length before setting the string value.
type Normalizable ¶
type Normalizable[T any] interface { // Normalized returns the normalized value of type T or an error if normalization fails. Normalized() (T, error) }
Normalizable is a generic interface for types that can be normalized to a standard form. Normalization typically involves converting input data to a consistent, validated format. This interface is commonly implemented by types that handle user input or external data that may come in various formats but need to be standardized.
The Normalized method should: - Return the normalized value in its standard form - Return an error if the value cannot be normalized - Be idempotent (calling Normalized on an already normalized value should return the same result)
type NormalizableValidator ¶
type NormalizableValidator[T any] interface { Validator Normalizable[T] // ValidAndNormalized returns true if the value is both valid and already in normalized form. // This is a performance optimization that avoids the need to call both Valid() and Normalized() // separately when checking if a value is ready for use. ValidAndNormalized() bool }
NormalizableValidator is a generic interface that combines validation and normalization capabilities. It extends both the Validator interface and Normalizable interface, providing a comprehensive solution for types that need both validation and normalization functionality.
This interface is particularly useful for types that: - Accept input in various formats - Need to validate the input before normalization - Should provide a quick check for both validity and normalization status
type Set ¶
Set represents a collection of unique ordered values. It is implemented as a map[T]struct{} for efficient lookups and memory usage. The type parameter T must be comparable and ordered (implements cmp.Ordered).
func NewSet ¶
NewSet creates a new Set containing the provided values. Duplicate values are automatically deduplicated.
func (Set[T]) AddSlice ¶
func (set Set[T]) AddSlice(vals []T)
AddSlice adds all values from the slice to the set.
func (Set[T]) ContainsAll ¶
ContainsAll returns true if the set contains all of the specified values.
func (Set[T]) ContainsAny ¶
ContainsAny returns true if the set contains any of the specified values.
func (Set[T]) ContainsSet ¶
ContainsSet returns true if this set contains all values from the other set.
func (Set[T]) DeleteSlice ¶
func (set Set[T]) DeleteSlice(vals []T)
DeleteSlice removes all values from the slice from the set.
func (Set[T]) Difference ¶
Difference returns a new set containing values that exist in either set but not in both.
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. The element returned is not guaranteed to be any specific element from the set.
func (Set[T]) Intersection ¶
Intersection returns a new set containing only values that exist in both sets.
func (Set[T]) IsNull ¶
IsNull implements the nullable.Nullable interface by returning true if the set is nil.
func (Set[T]) Map ¶
Map applies a transformation function to each value in the set and returns a new set containing the results. The mapFunc should return the transformed value and a boolean indicating whether to include the result in the new set.
func (Set[T]) MarshalJSON ¶
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
Sorted returns a slice containing all values in the set, sorted in ascending order.
func (Set[T]) String ¶
String implements the fmt.Stringer interface. Returns a string representation of the set with values in sorted order.
func (Set[T]) Union ¶
Union returns a new set containing all values from both this set and the other set.
func (*Set[T]) UnmarshalJSON ¶
UnmarshalJSON implements encoding/json.Unmarshaler. It unmarshals a JSON array into the set, removing any duplicates.
type StaticValidatErr ¶
type StaticValidatErr struct {
Err error
}
StaticValidatErr implements the ValidatErr interface for a validation error value. This is useful when you have a pre-computed validation error.
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 with a bool validity value. This is useful when you have a pre-computed validation result.
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 and return detailed error information. This is more informative than the Validator interface as it provides specific error details.
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. This is equivalent to creating a ValidatErrs slice.
type ValidatErrFunc ¶
type ValidatErrFunc func() error
ValidatErrFunc implements the ValidatErr interface with a function. This allows you to use a function as a validator that returns detailed errors.
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
ValidatErrs is a slice of ValidatErr implementations. It implements the ValidatErr interface itself, returning the first error encountered.
func (ValidatErrs) Validate ¶
func (v ValidatErrs) Validate() error
Validate returns an error if the data of the implementation is not valid. Returns the first error from any validator in the slice, or nil if all are 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. The Valid method should return true if the data is valid, false otherwise.
func CombinedValidator ¶
CombinedValidator creates a Validator whose Valid method returns false if any of the passed validators Valid methods returned false, else it returns true. This is equivalent to creating a Validators slice.
type ValidatorAsValidatErr ¶
type ValidatorAsValidatErr struct {
Validator
}
ValidatorAsValidatErr wraps a Validator as a ValidatErr, returning ErrInvalidValue when Validator.Valid() returns false. This allows you to use a simple boolean validator in contexts that require detailed error information.
func (ValidatorAsValidatErr) Validate ¶
func (v ValidatorAsValidatErr) Validate() error
Validate returns an error if the wrapped validator is not valid.
type ValidatorFunc ¶
type ValidatorFunc func() bool
ValidatorFunc implements the Validator interface with a function. This allows you to use a function as a validator.
func (ValidatorFunc) Valid ¶
func (f ValidatorFunc) Valid() bool
Valid returns if the data of the implementation is valid.
type Validators ¶
type Validators []Validator
Validators is a slice of Validator implementations. It implements the Validator interface itself, returning true only if all validators are valid.
func (Validators) Valid ¶
func (v Validators) Valid() bool
Valid returns if the data of the implementation is valid. Returns false if any validator in the slice returns false.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package account provides account number handling with validation, parsing, and database integration for Go applications.
|
Package account provides account number handling with validation, parsing, and database integration for Go applications. |
|
Package bank provides comprehensive banking data types and utilities for Go applications.
|
Package bank provides comprehensive banking data types and utilities for Go applications. |
|
findencoding
Package findencoding provides utilities for detecting and testing character encodings in text files, particularly useful for debugging encoding issues and finding the correct encoding for unknown text data.
|
Package findencoding provides utilities for detecting and testing character encodings in text files, particularly useful for debugging encoding issues and finding the correct encoding for unknown text data. |
|
Package country provides comprehensive country code handling and validation based on ISO 3166-1 alpha-2 standards for Go applications.
|
Package country provides comprehensive country code handling and validation based on ISO 3166-1 alpha-2 standards for Go applications. |
|
Package date provides comprehensive date handling and validation utilities for Go applications with support for multiple date formats and internationalization.
|
Package date provides comprehensive date handling and validation utilities for Go applications with support for multiple date formats and internationalization. |
|
Package deref provides safe pointer dereferencing utilities for Go applications.
|
Package deref provides safe pointer dereferencing utilities for Go applications. |
|
Package email provides comprehensive email address handling, parsing, validation, and message processing utilities for Go applications.
|
Package email provides comprehensive email address handling, parsing, validation, and message processing utilities for Go applications. |
|
Package float provides utilities for working with floating-point numbers in Go.
|
Package float provides utilities for working with floating-point numbers in Go. |
|
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
|
|
|
Package language provides comprehensive language code handling and validation based on ISO 639-1 standards for Go applications.
|
Package language provides comprehensive language code handling and validation based on ISO 639-1 standards for Go applications. |
|
gen-lang-maps
command
|
|
|
Package nullable provides interfaces and utilities for handling nullable values in Go.
|
Package nullable provides interfaces and utilities for handling nullable values in Go. |
|
stringfrom
Package stringfrom provides utilities for converting various Go types to strings with safe handling of nil pointers and customizable default values.
|
Package stringfrom provides utilities for converting various Go types to strings with safe handling of nil pointers and customizable default values. |