Documentation ¶
Overview ¶
Package mp (map parser) provides a parser that parses maps into types defined at runtime.
The purpose of this package is to make it easy to parse and validate JSON or form submissions.
A Type is defined by a set of fields. Each field has a name and a set of ValueConverters. A ValueConverter is a function that converts a value to a different type or validates the value.
A Record is an "instance" of a Type. It is created by calling Type.Parse with a map[string]any. The map is converted to a Record by applying the ValueConverters for each field. If any of the ValueConverters fail then the Record is considered invalid. The original map and the errors are stored in the Record.
Index ¶
- type ConvertedTyper
- type Errors
- type Field
- type Record
- type StandardField
- type Type
- type ValueConverter
- func AllowStrings(allowedItems ...string) ValueConverter
- func Bool() ValueConverter
- func Decimal() ValueConverter
- func ExcludeStrings(excludedItems ...string) ValueConverter
- func Float32() ValueConverter
- func Float64() ValueConverter
- func GreaterThan(x any) ValueConverter
- func GreaterThanOrEqual(x any) ValueConverter
- func IfNotNil(converters ...ValueConverter) ValueConverter
- func Int32() ValueConverter
- func Int64() ValueConverter
- func LessThan(x any) ValueConverter
- func LessThanOrEqual(x any) ValueConverter
- func MaxLen(max int) ValueConverter
- func MinLen(min int) ValueConverter
- func MultiLineString() ValueConverter
- func NilifyEmpty() ValueConverter
- func NotNil() ValueConverter
- func Require() ValueConverter
- func SingleLineString() ValueConverter
- func Slice[T any](elementConverter ValueConverter) ValueConverter
- func String() ValueConverter
- func Time(formats ...string) ValueConverter
- func UUID() ValueConverter
- type ValueConverterFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConvertedTyper ¶
type Errors ¶
Errors is a map of field name to error. It implements the error interface.
func (Errors) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
type Field ¶
type Field interface { Name() string ValueConverter }
type Record ¶
type Record struct {
// contains filtered or unexported fields
}
Record is an "instance" of a type. It is created by calling Type.Parse.
func (*Record) Errors ¶
Errors returns the errors for the record. If the record is valid then nil is returned.
type StandardField ¶
type StandardField struct {
// contains filtered or unexported fields
}
StandardField is a field of a Type.
func NewField ¶
func NewField(name string, valueConverters ...ValueConverter) *StandardField
NewField creates a new field with the given name and valueConverters.
func (*StandardField) ConvertValue ¶
func (f *StandardField) ConvertValue(value any) (any, error)
ConvertValue implements the ValueConverter interface.
func (*StandardField) Name ¶
func (f *StandardField) Name() string
Name returns the name of the field.
func (*StandardField) ValueConverters ¶
func (f *StandardField) ValueConverters() []ValueConverter
ValueConverters returns the valueConverters of the field. The returned slice must not be modified.
type Type ¶
type Type struct {
// contains filtered or unexported fields
}
Type is a type that can be used to convert a map[string]any to a Record.
It implements the ValueConverter interface so it can be used to build nested structs.
func (*Type) ConvertValue ¶
ConvertValue converts a map[string]any to a Record. If v is nil then nil is returned.
type ValueConverter ¶
ValueConverter is an interface that converts a value to a different type or validates the value.
func AllowStrings ¶
func AllowStrings(allowedItems ...string) ValueConverter
AllowStrings returns a ValueConverter that returns an error unless value is one of the allowedItems. If value is nil then nil is returned. If value is not a string then an error is returned.
func Bool ¶
func Bool() ValueConverter
Bool returns a ValueConverter that converts value to a bool. If value is nil or a blank string nil is returned.
func Decimal ¶
func Decimal() ValueConverter
Decimal returns a ValueConverter that converts value to a decimal.Decimal. If value is nil or a blank string nil is returned.
func ExcludeStrings ¶
func ExcludeStrings(excludedItems ...string) ValueConverter
ExcludeStrings returns a ValueConverter that returns an error if value is one of the excludedItems. If value is nil then nil is returned. If value is not a string then an error is returned.
func Float32 ¶
func Float32() ValueConverter
Float32 returns a ValueConverter that converts value to an float32. If value is nil or a blank string nil is returned.
func Float64 ¶
func Float64() ValueConverter
Float64 returns a ValueConverter that converts value to an float64. If value is nil or a blank string nil is returned.
func GreaterThan ¶
func GreaterThan(x any) ValueConverter
GreaterThan returns a ValueConverter that fails unless value > x. x must be convertable to a decimal number or GreaterThan panics. value must be convertable to a decimal number. nil is returned unmodified.
func GreaterThanOrEqual ¶
func GreaterThanOrEqual(x any) ValueConverter
GreaterThanOrEqual returns a ValueConverter that fails unless value >= x. x must be convertable to a decimal number or GreaterThanOrEqual panics. value must be convertable to a decimal number. nil is returned unmodified.
func IfNotNil ¶
func IfNotNil(converters ...ValueConverter) ValueConverter
func Int32 ¶
func Int32() ValueConverter
Int32 returns a ValueConverter that converts value to an int32. If value is nil or a blank string nil is returned.
func Int64 ¶
func Int64() ValueConverter
Int64 returns a ValueConverter that converts value to an int64. If value is nil or a blank string nil is returned.
func LessThan ¶
func LessThan(x any) ValueConverter
LessThan returns a ValueConverter that fails unless value < x. x must be convertable to a decimal number or LessThan panics. value must be convertable to a decimal number. nil is returned unmodified.
func LessThanOrEqual ¶
func LessThanOrEqual(x any) ValueConverter
LessThanOrEqual returns a ValueConverter that fails unless value <= x. x must be convertable to a decimal number or LessThanOrEqual panics. value must be convertable to a decimal number. nil is returned unmodified.
func MaxLen ¶
func MaxLen(max int) ValueConverter
MaxLen returns a ValueConverter that fails if len(value) > max. value must be a string, slice, or map. nil is returned unmodified.
func MinLen ¶
func MinLen(min int) ValueConverter
MinLen returns a ValueConverter that fails if len(value) < min. value must be a string, slice, or map. nil is returned unmodified.
func MultiLineString ¶
func MultiLineString() ValueConverter
MultiLineString returns a ValueConverter that converts a string value to a normalized string. If value is nil then nil is returned. If value is not a string then an error is returned.
It performs the following operations:
- Remove any invalid UTF-8
- Replace characters that are not graphic or space with standard space
func NilifyEmpty ¶
func NilifyEmpty() ValueConverter
NilifyEmpty converts strings, slices, and maps where len(value) == 0 to nil. Any other value not modified.
func NotNil ¶
func NotNil() ValueConverter
NotNil returns a ValueConverter that fails if value is nil.
func Require ¶
func Require() ValueConverter
Require returns a ValueConverter that returns an error if value is nil or "".
func SingleLineString ¶
func SingleLineString() ValueConverter
SingleLineString returns a ValueConverter that converts a string value to a normalized string. If value is nil then nil is returned. If value is not a string then an error is returned.
It performs the following operations:
- Remove any invalid UTF-8
- Replace non-printable characters with standard space
- Remove spaces from left and right
func Slice ¶
func Slice[T any](elementConverter ValueConverter) ValueConverter
Slice returns a ValueConverter that converts value to a []T. value must be a []T or []any. If value is nil then nil is returned.
func String ¶
func String() ValueConverter
String returns a ValueConverter that converts value to a string. If value is nil then nil is returned. It does not perform any normalization. In almost all cases, SingleLineString or MultiLineString should be used instead.
func Time ¶
func Time(formats ...string) ValueConverter
Time returns a ValueConverter that converts value to a time.Time using formats. If value is nil or a blank string nil is returned.
func UUID ¶
func UUID() ValueConverter
UUID returns a ValueConverter that converts value to a uuid.UUID. If value is nil or a blank string nil is returned.
type ValueConverterFunc ¶
ValueConverterFunc is a function that implements the ValueConverter interface.
func (ValueConverterFunc) ConvertValue ¶
func (vcf ValueConverterFunc) ConvertValue(v any) (any, error)
ConvertValue implements the ValueConverter interface.