mp

package module
v0.0.0-...-4876f1d Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2023 License: MIT Imports: 11 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConvertedTyper

type ConvertedTyper interface {
	ConvertedType() reflect.Type
}

type Errors

type Errors map[string]error

Errors is a map of field name to error. It implements the error interface.

func (Errors) Error

func (e Errors) Error() string

func (Errors) MarshalJSON

func (e Errors) MarshalJSON() ([]byte, error)

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

func (r *Record) Attrs() map[string]any

Attrs returns the converted attributes of the record.

func (*Record) Errors

func (r *Record) Errors() error

Errors returns the errors for the record. If the record is valid then nil is returned.

func (*Record) Get

func (r *Record) Get(s string) any

Get returns the value of the field named s. If s is not a field of the type then Get panics.

func (*Record) Pick

func (r *Record) Pick(keys ...string) map[string]any

Pick returns a map with the keys and values of the fields named in keys. If any of the keys are not fields of the type then Pick panics.

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 NewType

func NewType(fields ...Field) *Type

func (*Type) ConvertValue

func (t *Type) ConvertValue(v any) (any, error)

ConvertValue converts a map[string]any to a Record. If v is nil then nil is returned.

func (*Type) Fields

func (t *Type) Fields() []Field

Fields returns the fields of the type. The returned slice must not be modified.

func (*Type) Parse

func (t *Type) Parse(attrs map[string]any) *Record

Parse creates a Record from attrs.

type ValueConverter

type ValueConverter interface {
	ConvertValue(any) (any, error)
}

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

type ValueConverterFunc func(any) (any, error)

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.

Jump to

Keyboard shortcuts

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