Skip to content

Setting: default ‚Äč

default [PACKAGE:]METHOD can be defined as method comment.

By default the target object is initialized with zero values | Go docs. With default you can instruct Goverter to use METHOD as default target value or constructor for the target value.

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

go
package example

// goverter:converter
type Converter interface {
	// goverter:default NewOutput
	Convert(*Input) *Output
}

type Input struct {
	Age  int
	Name *string
}
type Output struct {
	Age  int
	Name *string
}

func NewOutput() *Output {
	name := "jmattheis"
	return &Output{
		Age:  42,
		Name: &name,
	}
}
go
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.
//go:build !goverter

package generated

import default1 "github.com/jmattheis/goverter/example/default"

type ConverterImpl struct{}

func (c *ConverterImpl) Convert(source *default1.Input) *default1.Output {
	pExampleOutput := default1.NewOutput()
	if source != nil {
		var exampleOutput default1.Output
		exampleOutput.Age = (*source).Age
		var pString *string
		if (*source).Name != nil {
			xstring := *(*source).Name
			pString = &xstring
		}
		exampleOutput.Name = pString
		pExampleOutput = &exampleOutput
	}
	return pExampleOutput
}