Documentation ¶
Index ¶
- Variables
- func Backoff(min, max time.Duration, factor float64, attempt int) time.Duration
- func Contains[T comparable](list []T, str T) bool
- func Get(v interface{}, name string) (interface{}, bool)
- func GetRaw(v interface{}, name string) (reflect.Value, bool)
- func Includes[T comparable](all, subset []T) bool
- func InternalBSONValue(typ bsontype.Type, bytes []byte) []byte
- func Intersect[T comparable](listA, listB []T) []T
- func IsEmpty(sub Subject) error
- func IsEqual(v interface{}) func(sub Subject) error
- func IsNotEmpty(sub Subject) error
- func IsNotZero(sub Subject) error
- func IsValid(sub Subject) error
- func IsZero(sub Subject) error
- func Merge[T any](base T, with ...T) T
- func MustGet(v interface{}, name string) interface{}
- func MustGetRaw(v interface{}, name string) reflect.Value
- func MustSet(v interface{}, name string, value interface{})
- func N[T any]() *T
- func P[T any](id T) *T
- func Set(v interface{}, name string, value interface{}) bool
- func Subtract[T comparable](listA, listB []T) []T
- func Union[T comparable](lists ...[]T) []T
- func Unique[T comparable](list []T) []T
- func UnmarshalKeyedList[T any, L ~[]T, K comparable](c Coding, data []byte, list *L, mapper func(T) K) error
- func Validate(v interface{}, fn func(v *Validator)) error
- func Z[T any]() T
- type Accessible
- type Accessor
- type Coding
- func (c Coding) GetKey(field reflect.StructField) string
- func (c Coding) Marshal(in interface{}) ([]byte, error)
- func (c Coding) MimeType() string
- func (c Coding) SafeUnmarshal(in []byte, out interface{}) error
- func (c Coding) Transfer(in, out interface{}) error
- func (c Coding) Unmarshal(in []byte, out interface{}) error
- type Field
- type Map
- type NoValidation
- type Registry
- type Rule
- func IsField(obj any, types ...any) Rule
- func IsFormat(fns ...func(string) bool) Rule
- func IsMaxFloat(max float64) Rule
- func IsMaxInt(max int64) Rule
- func IsMaxLen(max int) Rule
- func IsMaxUint(max uint64) Rule
- func IsMinFloat(min float64) Rule
- func IsMinInt(min int64) Rule
- func IsMinLen(min int) Rule
- func IsMinUint(min uint64) Rule
- func IsPatternMatch(pattern string) Rule
- func IsRegexMatch(reg *regexp.Regexp) Rule
- func IsURL(full bool) Rule
- func IsValidBy[T any](fn func(T) error) Rule
- type Subject
- type Validatable
- type ValidationError
- type Validator
Constants ¶
This section is empty.
Variables ¶
var IsDNSName = IsFormat(govalidator.IsDNSName)
IsDNSName will check if a string is a valid DNS name.
var IsEmail = IsFormat(govalidator.IsEmail)
IsEmail will check if a string is a valid email.
var IsHost = IsFormat(govalidator.IsHost)
IsHost will check if a string is a valid host.
var IsIPAddress = IsFormat(govalidator.IsIP)
IsIPAddress will check if a string is a valid IP address.
var IsNumeric = IsFormat(govalidator.IsNumeric)
IsNumeric will check if a string is numeric.
var IsValidUTF8 = IsFormat(utf8.ValidString)
IsValidUTF8 will check if a string is valid utf8.
var IsVisible = IsFormat(utf8.ValidString, func(s string) bool { c := 0 w := 0 for _, r := range s { c++ if unicode.IsSpace(r) { w++ } } return w < c })
IsVisible will check if a string is visible.
Functions ¶
func Contains ¶
func Contains[T comparable](list []T, str T) bool
Contains return whether the list contains the item.
func Get ¶
Get will look up and return the value of the specified field and whether the field was found at all.
func GetRaw ¶ added in v0.32.2
GetRaw will look up and return the value of the specified field and whether$ the field was found at all.
func Includes ¶
func Includes[T comparable](all, subset []T) bool
Includes returns whether a list includes another list.
func InternalBSONValue ¶ added in v0.29.1
InternalBSONValue will return a byte sequence for an internal BSON value.
func Intersect ¶
func Intersect[T comparable](listA, listB []T) []T
Intersect will return a list with items that are part of both lists.
func IsEmpty ¶ added in v0.30.0
IsEmpty will check if the provided value is empty. Emptiness can only be checked for slices and maps.
func IsNotEmpty ¶ added in v0.30.0
IsNotEmpty will check if the provided value is not empty. Emptiness can only be checked for slices and maps.
func IsNotZero ¶ added in v0.30.0
IsNotZero will check if the provided value is not zero. It will determine zeroness using IsZero() or Zero() if implemented. A nil pointer, slice, array or map is also considered as zero.
func IsValid ¶ added in v0.30.0
IsValid will check if the value is valid by calling Validate(), IsValid() or Valid().
func IsZero ¶ added in v0.30.0
IsZero will check if the provided value is zero. It will determine zeroness using IsZero() or Zero() if implemented. A nil pointer, slice, array or map is also considered as zero.
func Merge ¶ added in v0.32.3
func Merge[T any](base T, with ...T) T
Merge will merge the specified base value with the provided values and return the base value.
func MustGet ¶
func MustGet(v interface{}, name string) interface{}
MustGet will call Get and panic if the operation failed.
func MustGetRaw ¶ added in v0.32.2
MustGetRaw will call GetRaw and panic if the operation failed.
func MustSet ¶
func MustSet(v interface{}, name string, value interface{})
MustSet will call Set and panic if the operation failed.
func N ¶ added in v0.33.0
func N[T any]() *T
N is a shorthand to get a typed nil pointer of the specified type.
func P ¶ added in v0.33.0
func P[T any](id T) *T
P is a shorthand function to get a pointer of the value.
func Set ¶
Set will set the specified field with the provided value and return whether the field has been found and the value has been set.
func Subtract ¶ added in v0.29.1
func Subtract[T comparable](listA, listB []T) []T
Subtract will return a list with items that are only part of the first list.
func Union ¶
func Union[T comparable](lists ...[]T) []T
Union will merge all list and remove duplicates.
func Unique ¶
func Unique[T comparable](list []T) []T
Unique will return a new list with unique strings.
func UnmarshalKeyedList ¶ added in v0.34.1
func UnmarshalKeyedList[T any, L ~[]T, K comparable](c Coding, data []byte, list *L, mapper func(T) K) error
UnmarshalKeyedList will unmarshal a list and match objects by comparing a custom key instead of their position in the list.
When using with custom types the type should implement the following methods:
func (l *Links) UnmarshalJSON(bytes []byte) error { return stick.UnmarshalKeyedList(stick.JSON, bytes, l, func(link Link) string { return link.Ref }) } func (l *Links) UnmarshalBSONValue(typ bsontype.Type, bytes []byte) error { return stick.UnmarshalKeyedList(stick.BSON, stick.InternalBSONValue(typ, bytes), l, func(link Link) string { return link.Ref }) }
Types ¶
type Accessible ¶
type Accessible interface {
GetAccessor(interface{}) *Accessor
}
Accessible is a type that provides a custom accessor for dynamic access.
type Accessor ¶
Accessor provides dynamic access to a structs fields.
func Access ¶ added in v0.32.2
Access will create, cache and return an accessor for the provided value.
Note: Ignored fields are only applied the first time Access is called for the provided type.
func BuildAccessor ¶
BuildAccessor will build and return an accessor for the provided type.
func GetAccessor ¶ added in v0.29.5
func GetAccessor(v interface{}) *Accessor
GetAccessor is a shorthand to retrieve a value's accessor.
type Coding ¶
type Coding string
Coding defines an encoding, decoding and transfer scheme.
func (Coding) GetKey ¶ added in v0.29.1
func (c Coding) GetKey(field reflect.StructField) string
GetKey will return the coding key for the specified struct field.
func (Coding) Marshal ¶
Marshal will encode the specified value into a byte sequence.
Note: When marshalling a non document compatible type to BSON the result is a custom byte sequence that can only be unmarshalled by this codec.
func (Coding) SafeUnmarshal ¶ added in v0.29.1
SafeUnmarshal will decode the specified value from the provided byte sequence. It will preserve JSON numbers when decoded into an interface{} value.
type Map ¶
type Map map[string]interface{}
Map represents a simple map. It provides methods to marshal and unmarshal concrete types to and from the map using JSON or BSON coding.
func (*Map) Marshal ¶
Marshal will marshal the specified value to the map. If the map already contains data, only fields present in the value are overwritten.
func (*Map) MustMarshal ¶
MustMarshal will marshal and panic on error.
func (*Map) MustUnmarshal ¶
MustUnmarshal will unmarshal and panic on error.
type NoValidation ¶
type NoValidation struct{}
NoValidation can be embedded in a struct to provide a no-op validation method.
func (*NoValidation) Validate ¶
func (*NoValidation) Validate() error
Validate will perform no validation.
type Registry ¶ added in v0.33.0
type Registry[T any] struct { // contains filtered or unexported fields }
Registry is a multi-key index of typed values.
func NewRegistry ¶ added in v0.33.0
func NewRegistry[T any](values []T, validator func(T) error, indexer ...func(T) string) *Registry[T]
NewRegistry will create and return a new registry using the specified index functions that must return unique keys.
func (*Registry[T]) Add ¶ added in v0.33.0
func (r *Registry[T]) Add(values ...T)
Add will add the specified values to the registry.
func (*Registry[T]) All ¶ added in v0.33.0
func (r *Registry[T]) All() []T
All will return a list of all added values.
func (*Registry[T]) Get ¶ added in v0.33.0
Get will attempt to find up a value using the specified predicate.
type Rule ¶ added in v0.30.0
Rule is a single validation rule.
func IsField ¶ added in v0.33.0
IsField will check if a string is a field on the provided object with one of the specified types.
func IsFormat ¶ added in v0.30.0
IsFormat will check of the value corresponds to the format determined by the provided string format checker.
func IsMaxFloat ¶ added in v0.30.0
IsMaxFloat checks whether the value satisfies the provided maximum.
func IsMaxLen ¶ added in v0.30.0
IsMaxLen checks whether the value does not exceed the specified length.
func IsMaxUint ¶ added in v0.30.0
IsMaxUint checks whether the value satisfies the provided maximum.
func IsMinFloat ¶ added in v0.30.0
IsMinFloat checks whether the value satisfies the provided minimum.
func IsMinLen ¶ added in v0.30.0
IsMinLen checks whether the value has at least the specified length.
func IsMinUint ¶ added in v0.30.0
IsMinUint checks whether the value satisfies the provided minimum.
func IsPatternMatch ¶ added in v0.30.0
IsPatternMatch will check if a string matches a regular expression pattern.
func IsRegexMatch ¶ added in v0.30.0
IsRegexMatch will check if a string matches a regular expression.
type Subject ¶ added in v0.30.0
Subject carries the to be validated value.
type Validatable ¶ added in v0.29.0
type Validatable interface {
Validate() error
}
Validatable represents a type that can be validated.
type ValidationError ¶ added in v0.30.0
ValidationError describes a validation error.
func (ValidationError) Error ¶ added in v0.30.0
func (e ValidationError) Error() string
Error implements the error interface.
type Validator ¶ added in v0.30.0
type Validator struct {
// contains filtered or unexported fields
}
Validator is used to validate an object.
func (*Validator) Error ¶ added in v0.30.0
Error will return the validation error or nil of no errors have yet been reported.
func (*Validator) Items ¶ added in v0.30.0
Items will validate each item of the array/slice at the named field using the provided rules.