Documentation
¶
Overview ¶
Package structmappers implements some useful transformations from one struct to another, in a "functional options" style for the morph.Struct.Map method.
Each transformer receives a new copy that it is free to mutate.
Index ¶
- func AppendFields(fields []morph.Field) morph.StructMapper
- func Compose(mappers ...morph.StructMapper) morph.StructMapper
- func PrependFields(fields []morph.Field) func(in morph.Struct) morph.Struct
- func Rename(name string) morph.StructMapper
- func Reverse(input morph.Struct) morph.Struct
- func SetComment(comment string) morph.StructMapper
- func StripComment(s morph.Struct) morph.Struct
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendFields ¶
func AppendFields(fields []morph.Field) morph.StructMapper
AppendFields returns a new morph.StructMapper that adds the given fields to the end of a struct's list of fields.
AppendFields is reversible with Reverse.
func Compose ¶
func Compose(mappers ...morph.StructMapper) morph.StructMapper
Compose returns a new morph.StructMapper that applies each of the given non-nil mappers, from left to right. Nil mappers are skipped.
func PrependFields ¶
PrependFields returns a new morph.StructMapper that inserts the given fields at the start of a struct's list of fields.
PrependFields is reversible with Reverse.
func Rename ¶
func Rename(name string) morph.StructMapper
Rename returns a new morph.StructMapper that sets the struct's name to the provided string.
In the provided name, the token "$" is rewritten to the existing name. For example, Rename("$Xml") on a struct named "Foo" maps to a struct named "FooXml".
Rename is reversible with Reverse.
Example ¶
package main import ( "fmt" "github.com/tawesoft/morph" "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")) fmt.Println(orange) appleAgain := orange.Map(structmappers.Reverse) fmt.Println(appleAgain) }
Output: type Orange struct { Picked time.Time LastEaten time.Time Weight weight.Weight Price price.Price } type Apple struct { Picked time.Time LastEaten time.Time Weight weight.Weight Price price.Price }
func Reverse ¶
Reverse is a morph.StructMapper that maps a mapped struct back to its original, to the extent that this is possible, by applying the reverse StructMapper.
func SetComment ¶
func SetComment(comment string) morph.StructMapper
SetComment returns a new morph.StructMapper that sets the struct's comment to the provided string.
func StripComment ¶
StripComment is a morph.StructMapper that sets the struct's comment to the empty string.
Types ¶
This section is empty.