structmapper

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2025 License: MIT Imports: 7 Imported by: 0

README

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.

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.

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.

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!

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,
})

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	MappingStrategyNotFound  = exception.NewCustom("MappingStrategyNotFound")
	StrategyRuntimeException = exception.NewCustom("StrategyRuntimeException")
)

Functions

func AutoMap

func AutoMap[TFrom any, TTo any](from TFrom) (TTo, error)

func Map

func Map[TFrom any, TTo any](from TFrom) (TTo, error)

func RegisterStrategy

func RegisterStrategy[TFrom any, TTo any](mapper func(TFrom) (TTo, error))

func SliceMap

func SliceMap[TFrom any, TTo any](from []TFrom) []TTo

func UnregisterStrategy

func UnregisterStrategy[TFrom any, TTo any]()

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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