# Struct Mapper The structmapper package converts Go structures into other Go structures. It is possible to use automatic conversion or to define your own strategies. ## AutoMap AutoMap automatically attempts to convert Struct1 to Struct2. JSON serialisation is used for this purpose. ```Go package main import "git.apihub24.de/admin/structmapper" type Struct1 struct { Id int Name string } type Struct2 struct { Id int Name string Active bool } func main() { result, err := structmapper.AutoMap[Struct1, Struct2](Struct1{ Id: 1, Name: "test" }) // prints 1 println(result.Id) // prints test println(result.Name) // prints false println(result.Active) } ``` ## Strategies Custom mapping logics can be created and used with RegisterStrategy or removed again with UnregisterStrategy. These are then automatically searched for and used by the Map function. If no suitable strategy is found, an AutoMap is attempted. ```Go type Right1 struct { Id int Name string } type Right2 struct { Id int Name string } structmapper.RegisterStrategy(func(from *Right1) (*Right2, error) { return &testRight2{ Id: from.Id, Name: from.Name, }, nil }) structmapper.RegisterStrategy(func(from *Right2) (*Right1, error) { return &Right1{ Id: from.Id, Name: from.Name, }, nil }) right := Right1 { Id: 1, Name: "test" } // now you can use the Map Method result, err := structmapper.Map[*Right1, *Right2](right) ``` Strategies can be removed with UnregisterStrategy. ```Go structmapper.UnregisterStrategy[Right1, Right2]() structmapper.UnregisterStrategy[Right2, Right1]() ``` ## SliceMap SliceMap is a helper function that makes it easy to map slices in strategies. For complexity reasons, only an empty slice is returned here in the event of an error, not an error! ```Go type Right1 struct { Id int Name string } type Right2 struct { Id int Name string } structmapper.RegisterStrategy(func(from *Right1) (*Right2, error) { return &testRight2{ Id: from.Id, Name: from.Name, }, nil }) structmapper.RegisterStrategy(func(from *Right2) (*Right1, error) { return &Right1{ Id: from.Id, Name: from.Name, }, nil }) right1 := Right1{ Id: 1, Name: "READ" } right2 := Right1{ Id: 2, Name: "WRITE" } result := structmapper.SliceMap[Right1, Right2]([]Right1{ right1, right2, }) ```