Documentation
¶
Index ¶
- Constants
- func ErrorNil(name Name) error
- func ErrorParse(name Name, value any, err error) error
- func ErrorType(name Name, value any) error
- func ErrorValue(name Name, value any, expected string) error
- func MarshalJSON(wrapper WrapperProvider) ([]byte, error)
- func New[T WrapperImplementation[V, R], V any, R UnwrapResult]() T
- func NewWithValue[T WrapperImplementation[V, R], V any, R UnwrapResult](value V) (T, error)
- func NewWithValueDiscard[T WrapperImplementation[V, R], V any, R UnwrapResult](value V) T
- func UnmarshalJSON(data []byte, wrapper WrapperProvider) error
- type Discarder
- type Name
- type UnwrapResult
- type ValidationError
- type Wrapper
- type WrapperBase
- type WrapperBool
- func (wrapper *WrapperBool) Get() bool
- func (wrapper *WrapperBool) GetAny() any
- func (wrapper *WrapperBool) MarshalJSON() ([]byte, error)
- func (wrapper *WrapperBool) UnmarshalJSON(data []byte) error
- func (wrapper *WrapperBool) Unwrap() bool
- func (wrapper *WrapperBool) UnwrapAny() any
- func (wrapper *WrapperBool) Wrap(value any, discard bool) error
- type WrapperCountry
- func (wrapper *WrapperCountry) Get() countries.CountryCode
- func (wrapper *WrapperCountry) GetAny() any
- func (wrapper *WrapperCountry) MarshalJSON() ([]byte, error)
- func (wrapper *WrapperCountry) UnmarshalJSON(data []byte) error
- func (wrapper *WrapperCountry) Unwrap() string
- func (wrapper *WrapperCountry) UnwrapAny() any
- func (wrapper *WrapperCountry) Wrap(value any, discard bool) error
- type WrapperFloat
- func (wrapper *WrapperFloat) Get() float64
- func (wrapper *WrapperFloat) GetAny() any
- func (wrapper *WrapperFloat) MarshalJSON() ([]byte, error)
- func (wrapper *WrapperFloat) UnmarshalJSON(data []byte) error
- func (wrapper *WrapperFloat) Unwrap() float64
- func (wrapper *WrapperFloat) UnwrapAny() any
- func (wrapper *WrapperFloat) Wrap(value any, discard bool) error
- type WrapperImplementation
- type WrapperInt
- func (wrapper *WrapperInt) Get() int64
- func (wrapper *WrapperInt) GetAny() any
- func (wrapper *WrapperInt) MarshalJSON() ([]byte, error)
- func (wrapper *WrapperInt) UnmarshalJSON(data []byte) error
- func (wrapper *WrapperInt) Unwrap() int64
- func (wrapper *WrapperInt) UnwrapAny() any
- func (wrapper *WrapperInt) Wrap(value any, discard bool) error
- type WrapperProvider
- type WrapperString
- func (wrapper *WrapperString) Get() string
- func (wrapper *WrapperString) GetAny() any
- func (wrapper *WrapperString) MarshalJSON() ([]byte, error)
- func (wrapper *WrapperString) UnmarshalJSON(data []byte) error
- func (wrapper *WrapperString) Unwrap() string
- func (wrapper *WrapperString) UnwrapAny() any
- func (wrapper *WrapperString) Wrap(value any, discard bool) error
- type WrapperTime
- func (wrapper *WrapperTime) Get() time.Time
- func (wrapper *WrapperTime) GetAny() any
- func (wrapper *WrapperTime) MarshalJSON() ([]byte, error)
- func (wrapper *WrapperTime) UnmarshalJSON(data []byte) error
- func (wrapper *WrapperTime) Unwrap() string
- func (wrapper *WrapperTime) UnwrapAny() any
- func (wrapper *WrapperTime) Wrap(value any, discard bool) error
- type WrapperTimeDuration
- func (wrapper *WrapperTimeDuration) Get() time.Duration
- func (wrapper *WrapperTimeDuration) GetAny() any
- func (wrapper *WrapperTimeDuration) MarshalJSON() ([]byte, error)
- func (wrapper *WrapperTimeDuration) UnmarshalJSON(data []byte) error
- func (wrapper *WrapperTimeDuration) Unwrap() string
- func (wrapper *WrapperTimeDuration) UnwrapAny() any
- func (wrapper *WrapperTimeDuration) Wrap(value any, discard bool) error
- type WrapperTimeISO8601
- func (wrapper *WrapperTimeISO8601) Get() time.Time
- func (wrapper *WrapperTimeISO8601) GetAny() any
- func (wrapper *WrapperTimeISO8601) MarshalJSON() ([]byte, error)
- func (wrapper *WrapperTimeISO8601) UnmarshalJSON(data []byte) error
- func (wrapper *WrapperTimeISO8601) Unwrap() string
- func (wrapper *WrapperTimeISO8601) UnwrapAny() any
- func (wrapper *WrapperTimeISO8601) Wrap(value any, discard bool) error
Constants ¶
const ( WrapperBoolName Name = "WrapperBool" WrapperBoolExample string = "true, false, yes, no, 1, 0" )
const ( WrappersTagHeader = "wrappers" WrappersTagDiscard = "discard" )
Variables ¶
This section is empty.
Functions ¶
func MarshalJSON ¶
func MarshalJSON(wrapper WrapperProvider) ([]byte, error)
MarshalJSON is a generic implementation of the MarshalJSON method for wrappers. It is used to marshal a wrapper into a JSON value. All wrappers should call this method in their MarshalJSON implementation.
func New ¶
func New[T WrapperImplementation[V, R], V any, R UnwrapResult]() T
func NewWithValue ¶
func NewWithValue[T WrapperImplementation[V, R], V any, R UnwrapResult](value V) (T, error)
func NewWithValueDiscard ¶ added in v1.1.0
func NewWithValueDiscard[T WrapperImplementation[V, R], V any, R UnwrapResult](value V) T
func UnmarshalJSON ¶
func UnmarshalJSON(data []byte, wrapper WrapperProvider) error
UnmarshalJSON is a generic implementation of the UnmarshalJSON method for wrappers. It is used to unmarshal a JSON value into a wrapper. All wrappers should call this method in their UnmarshalJSON implementation.
Types ¶
type Discarder ¶
type Discarder[W WrapperProvider] struct { Proxy W }
Discarder is a generic type that proxies any WrapperProvider. It handles unmarshalling by suppressing errors of discarded invalid values.
func NewDiscarder ¶
func NewDiscarder[W WrapperProvider](wrapper W) *Discarder[W]
NewDiscarder initializes a new Discarder wrapper with the provided underlying wrapper.
func (*Discarder[W]) MarshalJSON ¶
MarshalJSON marshals the underlying wrapper to JSON.
func (*Discarder[W]) UnmarshalJSON ¶
UnmarshalJSON unmarshals JSON data into the underlying wrapper. If unmarshalling fails, it discards the value without returning an error.
type Name ¶
type Name string
Name is a type that holds the name of the wrapper. It is used to identify the wrapper in error messages.
const (
WrapperCountryName Name = "WrapperCountry"
)
const (
WrapperFloatName Name = "WrapperFloat"
)
const (
WrapperIntName Name = "WrapperInt"
)
const (
WrapperStringName Name = "WrapperString"
)
const (
WrapperTimeDurationName Name = "WrapperTimeDuration"
)
const (
WrapperTimeISO8601Name Name = "WrapperTimeISO8601"
)
const (
WrapperTimeName Name = "WrapperTime"
)
type UnwrapResult ¶
type ValidationError ¶
ValidationError represents an error during validation.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
type Wrapper ¶
type Wrapper[V any, R any] struct { WrapperBase Value V // The value that is wrapped. }
The core wrapper struct used for all implementations. Importantly, it is a generic implementation but embeds the WrapperBase struct.
type WrapperBase ¶
type WrapperBase struct {
// contains filtered or unexported fields
}
WrapperBase is a struct that holds the basic fields of a wrapper. It is embedded in all wrapper implementations.
func (*WrapperBase) Discard ¶
func (wrapper *WrapperBase) Discard()
func (*WrapperBase) Initialize ¶
func (wrapper *WrapperBase) Initialize()
func (*WrapperBase) IsDiscarded ¶
func (wrapper *WrapperBase) IsDiscarded() bool
func (*WrapperBase) IsInitialized ¶
func (wrapper *WrapperBase) IsInitialized() bool
type WrapperBool ¶
func (*WrapperBool) Get ¶
func (wrapper *WrapperBool) Get() bool
func (*WrapperBool) GetAny ¶
func (wrapper *WrapperBool) GetAny() any
func (*WrapperBool) MarshalJSON ¶
func (wrapper *WrapperBool) MarshalJSON() ([]byte, error)
func (*WrapperBool) UnmarshalJSON ¶
func (wrapper *WrapperBool) UnmarshalJSON(data []byte) error
func (*WrapperBool) Unwrap ¶
func (wrapper *WrapperBool) Unwrap() bool
func (*WrapperBool) UnwrapAny ¶
func (wrapper *WrapperBool) UnwrapAny() any
type WrapperCountry ¶
type WrapperCountry Wrapper[countries.CountryCode, string]
func (*WrapperCountry) Get ¶
func (wrapper *WrapperCountry) Get() countries.CountryCode
func (*WrapperCountry) GetAny ¶
func (wrapper *WrapperCountry) GetAny() any
func (*WrapperCountry) MarshalJSON ¶
func (wrapper *WrapperCountry) MarshalJSON() ([]byte, error)
func (*WrapperCountry) UnmarshalJSON ¶
func (wrapper *WrapperCountry) UnmarshalJSON(data []byte) error
func (*WrapperCountry) Unwrap ¶
func (wrapper *WrapperCountry) Unwrap() string
func (*WrapperCountry) UnwrapAny ¶
func (wrapper *WrapperCountry) UnwrapAny() any
type WrapperFloat ¶
func (*WrapperFloat) Get ¶
func (wrapper *WrapperFloat) Get() float64
func (*WrapperFloat) GetAny ¶
func (wrapper *WrapperFloat) GetAny() any
func (*WrapperFloat) MarshalJSON ¶
func (wrapper *WrapperFloat) MarshalJSON() ([]byte, error)
func (*WrapperFloat) UnmarshalJSON ¶
func (wrapper *WrapperFloat) UnmarshalJSON(data []byte) error
func (*WrapperFloat) Unwrap ¶
func (wrapper *WrapperFloat) Unwrap() float64
func (*WrapperFloat) UnwrapAny ¶
func (wrapper *WrapperFloat) UnwrapAny() any
type WrapperImplementation ¶
type WrapperImplementation[V any, R UnwrapResult] interface { WrapperProvider Unwrap() R // Gets the stored value as one of the types specified in the any interface. Get() V // Gets the stored "wrapped" value. Useful when you want to work with the value directly. This is the case when nesting wrappers. }
The main implementation of a Wrapper. This is the core implementation that all other wrappers should implement.
type WrapperInt ¶
func (*WrapperInt) Get ¶
func (wrapper *WrapperInt) Get() int64
func (*WrapperInt) GetAny ¶
func (wrapper *WrapperInt) GetAny() any
func (*WrapperInt) MarshalJSON ¶
func (wrapper *WrapperInt) MarshalJSON() ([]byte, error)
func (*WrapperInt) UnmarshalJSON ¶
func (wrapper *WrapperInt) UnmarshalJSON(data []byte) error
func (*WrapperInt) Unwrap ¶
func (wrapper *WrapperInt) Unwrap() int64
func (*WrapperInt) UnwrapAny ¶
func (wrapper *WrapperInt) UnwrapAny() any
type WrapperProvider ¶
type WrapperProvider interface {
// The initization methods are important in cases where parameters or other custom logic is needed before the wrapper can be used.
// An example of this would be derivitives of the WrapperRegex wrapper where we need to set the regex pattern before we can use the wrapper.
Initialize() // Initializes the wrapper.
IsInitialized() bool // Returns true if the wrapper has been initialized.
Discard() // Discards the value. Sets the Discard flag to true.
IsDiscarded() bool // Returns true if the value was discarded. This method is important as it is called during marshalling to JSON. If the value was nullified, we should return nil.
Wrap(any, bool) error // Wraps a value. The value is validated and stored in the wrapper with the wrappers type. The discard parameter indicates if the value should be discarded if it is invalid without returning an error.
// We need to implement the MarshalJSON and UnmarshalJSON methods in order to be able to use the wrappers in JSON marshalling and unmarshalling.
MarshalJSON() ([]byte, error)
UnmarshalJSON([]byte) error
UnwrapAny() any // Similar to Unwrap, but returns the value as an interface{}.
GetAny() any // Similar to Get, but returns the value as an interface{}.
}
WrapperProvider is an interface that defines the methods that a wrapper must implement.
type WrapperString ¶
func (*WrapperString) Get ¶
func (wrapper *WrapperString) Get() string
func (*WrapperString) GetAny ¶
func (wrapper *WrapperString) GetAny() any
func (*WrapperString) MarshalJSON ¶
func (wrapper *WrapperString) MarshalJSON() ([]byte, error)
func (*WrapperString) UnmarshalJSON ¶
func (wrapper *WrapperString) UnmarshalJSON(data []byte) error
func (*WrapperString) Unwrap ¶
func (wrapper *WrapperString) Unwrap() string
func (*WrapperString) UnwrapAny ¶
func (wrapper *WrapperString) UnwrapAny() any
type WrapperTime ¶
func (*WrapperTime) Get ¶
func (wrapper *WrapperTime) Get() time.Time
func (*WrapperTime) GetAny ¶
func (wrapper *WrapperTime) GetAny() any
func (*WrapperTime) MarshalJSON ¶
func (wrapper *WrapperTime) MarshalJSON() ([]byte, error)
func (*WrapperTime) UnmarshalJSON ¶
func (wrapper *WrapperTime) UnmarshalJSON(data []byte) error
func (*WrapperTime) Unwrap ¶
func (wrapper *WrapperTime) Unwrap() string
func (*WrapperTime) UnwrapAny ¶
func (wrapper *WrapperTime) UnwrapAny() any
type WrapperTimeDuration ¶
func (*WrapperTimeDuration) Get ¶
func (wrapper *WrapperTimeDuration) Get() time.Duration
func (*WrapperTimeDuration) GetAny ¶
func (wrapper *WrapperTimeDuration) GetAny() any
func (*WrapperTimeDuration) MarshalJSON ¶
func (wrapper *WrapperTimeDuration) MarshalJSON() ([]byte, error)
func (*WrapperTimeDuration) UnmarshalJSON ¶
func (wrapper *WrapperTimeDuration) UnmarshalJSON(data []byte) error
func (*WrapperTimeDuration) Unwrap ¶
func (wrapper *WrapperTimeDuration) Unwrap() string
func (*WrapperTimeDuration) UnwrapAny ¶
func (wrapper *WrapperTimeDuration) UnwrapAny() any
type WrapperTimeISO8601 ¶
func (*WrapperTimeISO8601) Get ¶
func (wrapper *WrapperTimeISO8601) Get() time.Time
func (*WrapperTimeISO8601) GetAny ¶
func (wrapper *WrapperTimeISO8601) GetAny() any
func (*WrapperTimeISO8601) MarshalJSON ¶
func (wrapper *WrapperTimeISO8601) MarshalJSON() ([]byte, error)
func (*WrapperTimeISO8601) UnmarshalJSON ¶
func (wrapper *WrapperTimeISO8601) UnmarshalJSON(data []byte) error
func (*WrapperTimeISO8601) Unwrap ¶
func (wrapper *WrapperTimeISO8601) Unwrap() string
func (*WrapperTimeISO8601) UnwrapAny ¶
func (wrapper *WrapperTimeISO8601) UnwrapAny() any