Skip to content

Setting: map

map [SOURCE-PATH] TARGET [| METHOD] can be defined as method comment.

map SOURCE-FIELD TARGET

If the source and target struct have differently named fields, then you can use map to define the mapping.

go
package example

// goverter:converter
type Converter interface {
	// goverter:map LastName Surname
	Convert(Input) Output
}

type Input struct {
	Age      int
	LastName string
}
type Output struct {
	Age     int
	Surname string
}
go
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.
//go:build !goverter

package generated

import mapfield "github.com/jmattheis/goverter/example/map-field"

type ConverterImpl struct{}

func (c *ConverterImpl) Convert(source mapfield.Input) mapfield.Output {
	var exampleOutput mapfield.Output
	exampleOutput.Age = source.Age
	exampleOutput.Surname = source.LastName
	return exampleOutput
}

map SOURCE-PATH TARGET

You can access nested properties by separating the field names with .:

go
// goverter:converter
type Converter interface {
    // goverter:map Nested.LastName Surname
    Convert(Input) Output
}
Example (click to expand)
go
package example

// goverter:converter
type Converter interface {
	// goverter:map Nested.LastName Surname
	Convert(Input) Output
}

type Input struct {
	Age    int
	Nested NestedInput
}
type NestedInput struct {
	LastName string
}
type Output struct {
	Age     int
	Surname string
}
go
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.
//go:build !goverter

package generated

import mappath "github.com/jmattheis/goverter/example/map-path"

type ConverterImpl struct{}

func (c *ConverterImpl) Convert(source mappath.Input) mappath.Output {
	var exampleOutput mappath.Output
	exampleOutput.Age = source.Age
	exampleOutput.Surname = source.Nested.LastName
	return exampleOutput
}

map DOT TARGET

map . TARGET

When using . as source field inside map, then you the instruct Goverter to use the source struct as source for the conversion to the target property. This is useful, when you've a struct that is the flattened version of another struct. See autoMap for the invert operation of this.

go
package example

// goverter:converter
type Converter interface {
	// goverter:map . Address
	Convert(FlatPerson) Person
}

type FlatPerson struct {
	Name    string
	Age     int
	Street  string
	ZipCode string
}
type Person struct {
	Name    string
	Age     int
	Address Address
}
type Address struct {
	Street  string
	ZipCode string
}
go
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.
//go:build !goverter

package generated

import mapidentity "github.com/jmattheis/goverter/example/map-identity"

type ConverterImpl struct{}

func (c *ConverterImpl) Convert(source mapidentity.FlatPerson) mapidentity.Person {
	var examplePerson mapidentity.Person
	examplePerson.Name = source.Name
	examplePerson.Age = source.Age
	examplePerson.Address = c.exampleFlatPersonToExampleAddress(source)
	return examplePerson
}
func (c *ConverterImpl) exampleFlatPersonToExampleAddress(source mapidentity.FlatPerson) mapidentity.Address {
	var exampleAddress mapidentity.Address
	exampleAddress.Street = source.Street
	exampleAddress.ZipCode = source.ZipCode
	return exampleAddress
}

map [SOURCE-PATH] TARGET | METHOD

For [SOURCE-PATH] TARGET you can use everything that's described above in this document.

The METHOD may be everything supported in extend Signatures with the addition that the source parameter is optional.

Similarely to the extend setting you can reference external packages in METHOD by separating the package path and method name by :.

go
package example

// goverter:converter
type Converter interface {
	// goverter:map URL | PrependHTTPS
	// goverter:map . FullName | GetFullName
	// goverter:map Age | DefaultAge
	// goverter:map Value | strconv:Itoa
	Convert(Input) (Output, error)
}

type Input struct {
	URL string

	FirstName string
	LastName  string

	Value int
}
type Output struct {
	URL      string
	FullName string
	Age      int

	Value string
}

func GetFullName(input Input) string {
	return input.FirstName + " " + input.LastName
}
func PrependHTTPS(url string) string { return "https://" + url }
func DefaultAge() int                { return 42 }
go
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.
//go:build !goverter

package generated

import (
	mapcustom "github.com/jmattheis/goverter/example/map-custom"
	"strconv"
)

type ConverterImpl struct{}

func (c *ConverterImpl) Convert(source mapcustom.Input) (mapcustom.Output, error) {
	var exampleOutput mapcustom.Output
	exampleOutput.URL = mapcustom.PrependHTTPS(source.URL)
	exampleOutput.FullName = mapcustom.GetFullName(source)
	exampleOutput.Age = mapcustom.DefaultAge()
	exampleOutput.Value = strconv.Itoa(source.Value)
	return exampleOutput, nil
}