Skip to content

Setting: converter

converter accepts no arguments and can be defined as converter comment.

converter instructs goverter to generate an implementation for the given interface. You can have multiple converters in one package.

See output to control the output location/package of the generated converter.

go
package simple

// goverter:converter
type Converter interface {
	Convert(source []Input) []Output
}

type Input struct {
	Name string
	Age  int
}

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

package generated

import simple "github.com/jmattheis/goverter/example/simple"

type ConverterImpl struct{}

func (c *ConverterImpl) Convert(source []simple.Input) []simple.Output {
	var simpleOutputList []simple.Output
	if source != nil {
		simpleOutputList = make([]simple.Output, len(source))
		for i := 0; i < len(source); i++ {
			simpleOutputList[i] = c.simpleInputToSimpleOutput(source[i])
		}
	}
	return simpleOutputList
}
func (c *ConverterImpl) simpleInputToSimpleOutput(source simple.Input) simple.Output {
	var simpleOutput simple.Output
	simpleOutput.Name = source.Name
	simpleOutput.Age = source.Age
	return simpleOutput
}