s2s

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package s2s maps structs to structs using reflection It does this in an opinionated way by default, but is configurable

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = MapperConfig{
	NameMapper:           nil,
	ValueMapper:          nil,
	SkipMissingField:     true,
	SkipFailedConversion: true,
	MapNilToZeroImplicit: true,
}
View Source
var ErrArgumentsInvalid = errors.New("input or output argument not pointer to struct")
View Source
var ErrInvalidConversion = errors.New("ValueMapper return can't be assigned to targetType")
View Source
var ErrMissingField = errors.New("Destination struct is missing field")

Functions

func CaseInsensitiveMapper

func CaseInsensitiveMapper(s string) string

Mapper that ignores capitalization in names

func MapPtrToVal

func MapPtrToVal(v reflect.Value, t reflect.Type) interface{}

Mapper that maps pointer types to direct types *T -> T using Zero(T) for nil

func MapStruct

func MapStruct(input interface{}, output interface{}) error

MapStruct takes a pointer to an input struct and a pointer to an output struct. It will perform a mapping using the default options

func MapStructEx

func MapStructEx(cfg MapperConfig, input interface{}, output interface{}) error

MapStructEx takes an additional argument compared to MapStruct for configuration.

Types

type MapperConfig

type MapperConfig struct {
	// This function if set will be called every time a field name is used
	// allowing you to map handle conversions like CamelCase -> snake_case
	NameMapper NameMapper

	// This function if set will be called when mapping an input value
	// to an output fields type.
	// For convenience you are allowed to return both a value directly or
	// behind a pointer.
	// Also if `MapNilToZeroImplicit` is set (default), returned typed nil
	// pointers will be implicitly mapped to the zero value if the output
	// field is not of pointer type
	ValueMapper ValueMapper

	// If set (default) the mapper will not error on fields that can't be
	// mapped to an output field
	SkipMissingField bool

	// If set (default) the mapper will not error if a conversion from input
	// field type to output field type fails
	SkipFailedConversion bool

	// If set (default) the mapper will implicitly convert typed nil pointers
	// to zero values of the type as needed
	MapNilToZeroImplicit bool
}

type NameMapper

type NameMapper = func(string) string

Maps field names to allow flexibility in mapping

type ValueMapper

type ValueMapper = func(value reflect.Value, targetType reflect.Type) interface{}

Expected to map `value` to a value of either `targetType` or `*targetType` Can be chained using CompositeMapper

func CompositeMapper

func CompositeMapper(mappers ...ValueMapper) ValueMapper

Used to chain together multiple ValueMapper in the given order

Jump to

Keyboard shortcuts

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