stick

package
v0.34.4 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 20 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

IsDNSName will check if a string is a valid DNS name.

IsEmail will check if a string is a valid email.

IsHost will check if a string is a valid host.

View Source
var IsIPAddress = IsFormat(govalidator.IsIP)

IsIPAddress will check if a string is a valid IP address.

IsNumeric will check if a string is numeric.

View Source
var IsValidUTF8 = IsFormat(utf8.ValidString)

IsValidUTF8 will check if a string is valid utf8.

View Source
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 Backoff

func Backoff(min, max time.Duration, factor float64, attempt int) time.Duration

Backoff will calculate the exponential delay.

func Contains

func Contains[T comparable](list []T, str T) bool

Contains return whether the list contains the item.

func Get

func Get(v interface{}, name string) (interface{}, bool)

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

func GetRaw(v interface{}, name string) (reflect.Value, bool)

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

func InternalBSONValue(typ bsontype.Type, bytes []byte) []byte

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

func IsEmpty(sub Subject) error

IsEmpty will check if the provided value is empty. Emptiness can only be checked for slices and maps.

func IsEqual added in v0.32.2

func IsEqual(v interface{}) func(sub Subject) error

IsEqual will check equality against the provided value.

func IsNotEmpty added in v0.30.0

func IsNotEmpty(sub Subject) error

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

func IsNotZero(sub Subject) error

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

func IsValid(sub Subject) error

IsValid will check if the value is valid by calling Validate(), IsValid() or Valid().

func IsZero added in v0.30.0

func IsZero(sub Subject) error

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

func MustGetRaw(v interface{}, name string) reflect.Value

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

func Set(v interface{}, name string, value interface{}) bool

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
	})
}

func Validate added in v0.30.0

func Validate(v interface{}, fn func(v *Validator)) error

Validate will validate the provided accessible using the specified validator function.

func Z added in v0.33.0

func Z[T any]() T

Z is a shorthand to get a zero value of the specified type.

Types

type Accessible

type Accessible interface {
	GetAccessor(interface{}) *Accessor
}

Accessible is a type that provides a custom accessor for dynamic access.

type Accessor

type Accessor struct {
	Name   string
	Fields map[string]*Field
}

Accessor provides dynamic access to a structs fields.

func Access added in v0.32.2

func Access(v interface{}, ignore ...string) *Accessor

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

func BuildAccessor(v interface{}, ignore ...string) *Accessor

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.

const (
	JSON Coding = "json"
	BSON Coding = "bson"
)

The available coding schemes.

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

func (c Coding) Marshal(in interface{}) ([]byte, error)

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) MimeType added in v0.32.0

func (c Coding) MimeType() string

MimeType returns the coding mim type.

func (Coding) SafeUnmarshal added in v0.29.1

func (c Coding) SafeUnmarshal(in []byte, out interface{}) error

SafeUnmarshal will decode the specified value from the provided byte sequence. It will preserve JSON numbers when decoded into an interface{} value.

func (Coding) Transfer

func (c Coding) Transfer(in, out interface{}) error

Transfer will transfer data from one value to another using.

func (Coding) Unmarshal

func (c Coding) Unmarshal(in []byte, out interface{}) error

Unmarshal will decode the specified value from the provided byte sequence.

type Field

type Field struct {
	Index int
	Type  reflect.Type
}

Field is a dynamically accessible field.

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 MustMap

func MustMap(from interface{}, coding Coding) Map

MustMap will marshal the specified object to a map and panic on any error.

func (*Map) Flat added in v0.29.4

func (m *Map) Flat(separator string) Map

Flat will return a flattened map by separator.

func (*Map) Marshal

func (m *Map) Marshal(from interface{}, coding Coding) error

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

func (m *Map) MustMarshal(from interface{}, coding Coding)

MustMarshal will marshal and panic on error.

func (*Map) MustUnmarshal

func (m *Map) MustUnmarshal(to interface{}, coding Coding)

MustUnmarshal will unmarshal and panic on error.

func (*Map) Unmarshal

func (m *Map) Unmarshal(to interface{}, coding Coding) error

Unmarshal will unmarshal the map to the specified value. If the value already contains data, only fields present in the map are overwritten.

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

func (r *Registry[T]) Get(predicate T) (T, bool)

Get will attempt to find up a value using the specified predicate.

func (*Registry[T]) Lookup added in v0.33.2

func (r *Registry[T]) Lookup(index int, key string) (T, bool)

Lookup will attempt to look up a valued using the specified index and key.

func (*Registry[T]) MustGet added in v0.33.0

func (r *Registry[T]) MustGet(predicate T) T

MustGet will call Get and panic if not value has been found.

type Rule added in v0.30.0

type Rule func(sub Subject) error

Rule is a single validation rule.

func IsField added in v0.33.0

func IsField(obj any, types ...any) Rule

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

func IsFormat(fns ...func(string) bool) Rule

IsFormat will check of the value corresponds to the format determined by the provided string format checker.

func IsMaxFloat added in v0.30.0

func IsMaxFloat(max float64) Rule

IsMaxFloat checks whether the value satisfies the provided maximum.

func IsMaxInt added in v0.30.0

func IsMaxInt(max int64) Rule

IsMaxInt checks whether the value satisfies the provided maximum.

func IsMaxLen added in v0.30.0

func IsMaxLen(max int) Rule

IsMaxLen checks whether the value does not exceed the specified length.

func IsMaxUint added in v0.30.0

func IsMaxUint(max uint64) Rule

IsMaxUint checks whether the value satisfies the provided maximum.

func IsMinFloat added in v0.30.0

func IsMinFloat(min float64) Rule

IsMinFloat checks whether the value satisfies the provided minimum.

func IsMinInt added in v0.30.0

func IsMinInt(min int64) Rule

IsMinInt checks whether the value satisfies the provided minimum.

func IsMinLen added in v0.30.0

func IsMinLen(min int) Rule

IsMinLen checks whether the value has at least the specified length.

func IsMinUint added in v0.30.0

func IsMinUint(min uint64) Rule

IsMinUint checks whether the value satisfies the provided minimum.

func IsPatternMatch added in v0.30.0

func IsPatternMatch(pattern string) Rule

IsPatternMatch will check if a string matches a regular expression pattern.

func IsRegexMatch added in v0.30.0

func IsRegexMatch(reg *regexp.Regexp) Rule

IsRegexMatch will check if a string matches a regular expression.

func IsURL added in v0.30.0

func IsURL(full bool) Rule

IsURL will check if a string is a valid URL.

func IsValidBy added in v0.33.0

func IsValidBy[T any](fn func(T) error) Rule

IsValidBy will check if the value is valid by calling the specified typed validator.

type Subject added in v0.30.0

type Subject struct {
	IValue interface{}
	RValue reflect.Value
}

Subject carries the to be validated value.

func (*Subject) Reference added in v0.32.2

func (s *Subject) Reference() Subject

Reference will obtain a referenced subject for interface testing.

func (*Subject) Unwrap added in v0.30.0

func (s *Subject) Unwrap() bool

Unwrap will unwrap all pointers and return whether a value is available.

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

type ValidationError map[error][]string

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

func (v *Validator) Error() error

Error will return the validation error or nil of no errors have yet been reported.

func (*Validator) Items added in v0.30.0

func (v *Validator) Items(name string, rules ...Rule)

Items will validate each item of the array/slice at the named field using the provided rules.

func (*Validator) Nest added in v0.30.0

func (v *Validator) Nest(field string, fn func())

Nest will nest validations under the specified field.

func (*Validator) Report added in v0.30.0

func (v *Validator) Report(name string, err error)

Report will report a validation error.

func (*Validator) Value added in v0.30.0

func (v *Validator) Value(name string, optional bool, rules ...Rule)

Value will validate the value at the named field using the provided rules. Pointer may be optional and are skipped if nil or unwrapped if present.

Jump to

Keyboard shortcuts

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