Documentation
¶
Index ¶
- func Map(input, output any, opts ...Option) error
- func MapTo[T any](input any, opts ...Option) (*T, error)
- func MapToWith[T any](mapper *Mapper, input any) (*T, error)
- type Callback
- type CallbackError
- type CallbackFunc
- type Config
- type Error
- type FieldError
- type FieldValue
- type Mapper
- type Option
- func WithAutoNumberToStringConversion() Option
- func WithAutoStringToNumberConversion() Option
- func WithCallbacks(callbacks ...*Callback) Option
- func WithIgnoreMissingCallbacks() Option
- func WithIgnoreMissingValidators() Option
- func WithOverrideDefaultValidators() Option
- func WithValidators(validators ...*Validator) Option
- type ValidationError
- type Validator
- type ValidatorFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Callback ¶
type Callback struct {
Name string
Func CallbackFunc
}
func NewCallback ¶
func NewCallback(name string, fn CallbackFunc) *Callback
type CallbackError ¶
type CallbackError struct {
// contains filtered or unexported fields
}
func (*CallbackError) Error ¶
func (e *CallbackError) Error() string
type Config ¶
type Config struct {
// it allows you to override the default validators (e.g. eq, required, etc.) and use your
// custom validator. if it's false (by default), then it ignores your validator and executes
// the default validator.
OverrideDefaultValidators bool
// if you specify a validator in a field's tag that does not exist, you will get an error (by default),
// but this allows you to ignore those missing validators.
IgnoreMissingValidators bool
// if you specify a callback in a field's tag that does not exist, you will get an error (by default),
// but this allows you to ignore those missing callbacks.
IgnoreMissingCallbacks bool
// if you try to map a string value to a numeric value (int, uint or float), you will get an error (by default),
// but this allows you to automatically convert strings to numbers.
AutoStringToNumberConversion bool
// if you try to map a numeric value (int, uint or float) to a string, you will get an error (by default),
// but this allows you to automatically convert numbers to strings.
AutoNumberToStringConversion bool
}
type FieldError ¶
type FieldError struct {
// contains filtered or unexported fields
}
func (*FieldError) Error ¶
func (e *FieldError) Error() string
type FieldValue ¶
func NewFieldValue ¶
func (FieldValue) From ¶
func (f FieldValue) From(v reflect.Value) FieldValue
type Mapper ¶
type Mapper struct {
Config
// contains filtered or unexported fields
}
func (*Mapper) Map ¶
Map takes a struct and converts it into another struct, output type must be a pointer to a struct.
Example ¶
package main
import (
"fmt"
"github.com/alir32a/smapper"
)
func main() {
type Person struct {
ID uint
Username string
}
type User struct {
ID int64
Name string `smapper:"username"`
}
mapper := smapper.New()
person := Person{
ID: 42,
Username: "alir32a",
}
user := User{}
err := mapper.Map(person, &user)
if err != nil {
panic(err)
}
fmt.Println(user.ID) // 42
fmt.Println(user.Name) // alir32a
}
Example (Callbacks) ¶
package main
import (
"errors"
"fmt"
"github.com/alir32a/smapper"
"reflect"
"strings"
)
func main() {
type Person struct {
ID uint
Username string
}
type User struct {
ID int64
Name string `smapper:"Username,callback:uppercase"`
}
person := Person{
ID: 42,
Username: "admin",
}
user := User{}
mapper := smapper.New(smapper.WithCallbacks(
smapper.NewCallback("uppercase", func(src reflect.Type, dst reflect.Type, v any) (any, error) {
// you have access to source and destination types here
if src.Kind() != reflect.String || dst.Kind() != reflect.String {
return nil, errors.New("wrong type")
}
return strings.ToUpper(v.(string)), nil
},
)))
err := mapper.Map(person, &user)
if err != nil {
panic(err)
}
fmt.Println(user.Name) // ADMIN
}
Example (Validators) ¶
package main
import (
"fmt"
"github.com/alir32a/smapper"
)
func main() {
type Person struct {
ID uint
Username string
PhoneNumber string
}
type User struct {
ID int64 `smapper:",required"`
Name string `smapper:"username,eq=admin"`
PhoneNumber string `smapper:",len=8"`
}
p := Person{
ID: 42, // should not be 0
Username: "admin", // must be equal to "admin"
PhoneNumber: "12345678", // must have exactly 8 characters
}
user := User{}
err := smapper.Map(p, &user) // must be ok
if err != nil {
panic(err)
}
p.ID = 0
p.Username = "common"
p.PhoneNumber = "1234567"
err = smapper.Map(p, &user) // returns validation error
if err != nil {
fmt.Println(err.Error())
}
}
type Option ¶
type Option func(*Mapper)
func WithAutoNumberToStringConversion ¶
func WithAutoNumberToStringConversion() Option
WithAutoNumberToStringConversion if you set this option, you can automatically convert numbers (int, uint and floats) to strings.
func WithAutoStringToNumberConversion ¶
func WithAutoStringToNumberConversion() Option
WithAutoStringToNumberConversion if you set this option, you can automatically convert strings to numbers (int, uint and floats)
func WithCallbacks ¶
func WithIgnoreMissingCallbacks ¶
func WithIgnoreMissingCallbacks() Option
WithIgnoreMissingCallbacks if you set this option, you can ignore errors that occur when you're trying to use a missing callback in a field's tag.
func WithIgnoreMissingValidators ¶
func WithIgnoreMissingValidators() Option
WithIgnoreMissingValidators if you set this option, you can ignore errors that occur when you're trying to use a missing validator in a field's tag.
func WithOverrideDefaultValidators ¶
func WithOverrideDefaultValidators() Option
WithOverrideDefaultValidators if you set this option, you can override default validators (e.g. required, len, etc.) and your validator func will being use instead.
func WithValidators ¶
type ValidationError ¶
type ValidationError struct {
// contains filtered or unexported fields
}
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
type Validator ¶
type Validator struct {
Name string
Func ValidatorFunc
}
func NewValidator ¶
func NewValidator(name string, fn ValidatorFunc) *Validator