Documentation
¶
Overview ¶
Package fieldmappers provides helpful composable functions that implement morph.FieldMapper for mapping the fields between two structs using morph.
A subpackage, morph/fieldmappers/fieldops, provides additional field mappers that set the Comparer, Copier, and Orderer expressions on struct fields.
Index ¶
- func All(input morph.Field, emit func(output morph.Field))
- func Compose(mappers ...morph.FieldMapper) morph.FieldMapper
- func Conditionally(filter func(morph.Field) bool, mapper morph.FieldMapper) morph.FieldMapper
- func DeleteNamed(names ...string) morph.FieldMapper
- func Filter(filter func(input morph.Field) bool) morph.FieldMapper
- func FilterInv(filter func(input morph.Field) bool) func(input morph.Field) bool
- func FilterNamed(names ...string) func(morph.Field) bool
- func FilterSlices(input morph.Field) bool
- func FilterTypes(types ...string) func(morph.Field) bool
- func None(input morph.Field, emit func(output morph.Field))
- func Reverse(input morph.Field, emit func(output morph.Field))
- func StripComments(input morph.Field, emit func(output morph.Field))
- func StripTags(input morph.Field, emit func(output morph.Field))
- func TimeToInt64(input morph.Field, emit func(output morph.Field))
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All is a morph.FieldMapper that emits every input unchanged.
func Compose ¶
func Compose(mappers ...morph.FieldMapper) morph.FieldMapper
Compose returns a new morph.FieldMapper that applies each of the given non-nil mappers, from left to right. Nil mappers are skipped.
func Conditionally ¶
func Conditionally(filter func(morph.Field) bool, mapper morph.FieldMapper) morph.FieldMapper
Conditionally returns a new morph.FieldMapper that applies mapper to any field where the filter func returns true, or emits the field unchanged if the filter func returns false.
func DeleteNamed ¶
func DeleteNamed(names ...string) morph.FieldMapper
DeleteNamed returns a new morph.FieldMapper that removes the named fields from a struct.
func Filter ¶
func Filter(filter func(input morph.Field) bool) morph.FieldMapper
Filter returns a new morph.FieldMapper that only emits fields where the provided filter function returns true.
func FilterInv ¶
FilterInv returns a filter that implements the inverse of the provided filter. Wherever the input filter would return true, the output filter instead returns false, and vice versa.
func FilterNamed ¶
FilterNamed returns a filter that returns true for any field with a name matching any provided name argument.
func FilterSlices ¶
FilterSlices is a filter that returns true for any field with a type that is a slice.
func FilterTypes ¶
FilterTypes returns a filter that returns true for any field with a type name matching any provided type name argument.
func None ¶
None is a morph.FieldMapper that deletes every input.
func Reverse ¶
Reverse is a morph.FieldMapper that maps a mapped struct back to its original, to the extent that this is possible, by applying the reverse FieldMapper on each field.
func StripComments ¶
StripComments is a morph.FieldMapper that strips all comments from each input field.
func StripTags ¶
StripTags is a morph.FieldMapper that strips all struct tags from each input field.
func TimeToInt64 ¶
TimeToInt64 is a morph.FieldMapper that converts any `time.Time` field to an `int64` field containing the time in seconds since the Unix epoch.
As it is difficult to distinguish between an int64 that's just an integer, and an int64 that used to be a time, this sets a Reverse method on output field. This allows Reverse to automatically perform the reverse mapping.
The function sets appropriate Comparer, Copier, and Orderer expressions on the output field and on the reverse output field.
Example ¶
package main import ( "fmt" "github.com/tawesoft/morph" "github.com/tawesoft/morph/fieldmappers" "github.com/tawesoft/morph/structmappers" ) func must[X any](value X, err error) X { if err != nil { panic(err) } return value } func main() { source := ` package example type Apple struct { Picked time.Time LastEaten time.Time Weight weight.Weight Price price.Price } ` apple := must(morph.ParseStruct("test.go", source, "Apple")) orange := apple.Map( structmappers.Rename("Orange"), ).MapFields( fieldmappers.TimeToInt64, ) fmt.Println(orange) }
Output: type Orange struct { Picked int64 // time in seconds since Unix epoch LastEaten int64 // time in seconds since Unix epoch Weight weight.Weight Price price.Price }
Types ¶
This section is empty.