gomapper

package module
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: Apache-2.0 Imports: 4 Imported by: 1

README

codecov build Goreport GoDoc

GoMapper

GoMapper is a library for struct to struct mapping. There are two use cases: Manual and Auto.

  • Manual mode allows you to specify a function to convert one structure to another.
  • Auto mode uses matching field names for automatic conversion; it is important that not only the field names match, but also their types. This mode also supports structures in structure fields and automatically works by matching field names. It's based on fmap switch case and reflect based library.

Both modes are route based. In which the reflect.Type of the source structure and the type of the destination structure are specified. If such a route was not found, gomapper will return an error.

Also gomapper support slices, you don't need to specify types of slices for mapping.

Installation

go get github.com/insei/gomapper@latest

Examples

You can found a lot of examples in tests.
Manual route.

package main

import (
	"fmt"

	"github.com/insei/gomapper"
)

type Source struct {
	Name string
	Age  uint8
}

type Dest struct {
	NameCustom string
	Age        uint8
}

func main() {
	err := gomapper.AddRoute[Source, Dest](func(source Source, dest *Dest) error {
		dest.NameCustom = source.Name
		dest.Age = source.Age
		return nil
	})
	if err != nil {
		panic(err)
	}
	s := Source{
		Name: "DefaultName",
		Age:  16,
	}
	dest, err := gomapper.MapTo[Dest](s) 
	// or gomapper.MapTo[Dest](&s)
	// or d := Dest{}
	// gomapper.Map(&s, &d)
	if err != nil {
		panic(err)
	}
	fmt.Print(dest)
}

Auto Route.

package main

import (
	"fmt"

	"github.com/insei/gomapper"
)

type Source struct {
	Name string
	Age  uint8
}

type Dest struct {
	NameCustom string
	Age        uint8
}

func main() {
	err := gomapper.AutoRoute[Source, Dest]()
	if err != nil {
		panic(err)
	}
	s := Source{
		Name: "DefaultName",
		Age:  16,
	}
	dest, err := gomapper.MapTo[Dest](s) 
	// or gomapper.MapTo[Dest](&s)
	// or dest := Dest{}
	// gomapper.Map(&s, &dest)
	if err != nil {
		panic(err)
	}
	fmt.Print(dest)
}

Map structs into slices.

package main

import (
	"fmt"

	"github.com/insei/gomapper"
)

type Source struct {
	Name string
	Age  uint8
}

type Dest struct {
	NameCustom string
	Age        uint8
}

func main() {
	err := gomapper.AutoRoute[Source, Dest]() // or manual
	if err != nil {
		panic(err)
	}
	s := Source{
		Name: "DefaultName",
		Age:  16,
	}
	sSlice := []Source{ s }
	sDest, err := gomapper.MapTo[[]Dest](sSlice) 
	// or sDest := []Dest{}
	// sDest, err := gomapper.MapTo(sSlice, &sDest) 
	if err != nil {
		panic(err)
	}
	fmt.Print(sDest)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddRoute

func AddRoute[TSource, TDest any | []any](mapFunc func(source TSource, dest *TDest) error) error

func AutoRoute added in v1.0.1

func AutoRoute[TSource, TDest any | []any](opts ...Option) error

func Map

func Map(source interface{}, dest interface{}) error

Map source to dest

func MapTo

func MapTo[TDest interface{}](source interface{}) (TDest, error)

MapTo Map source to the new dest object

Types

type Option added in v1.1.4

type Option interface {
	// contains filtered or unexported methods
}

func WithFieldSkip added in v1.1.6

func WithFieldSkip[TSource any](fn func(*TSource) any) Option

func WithFunc added in v1.1.4

func WithFunc[TSource, TDest any](fn func(TSource, *TDest)) Option

Jump to

Keyboard shortcuts

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