Skip to content

Setting: update

update ARG

update ARG is a can be defined as method comment.

update instructs goverter to update an existing instance of a struct passed via an argument named ARG.

Constraints:

Example (click me)
go
package update

// goverter:converter
type Converter interface {
	// goverter:update target
	Convert(source Input, target *Output)
}

type Input struct {
	Name    *string
	Aliases []string
	Age     int
}

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

package generated

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

type ConverterImpl struct{}

func (c *ConverterImpl) Convert(source update.Input, target *update.Output) {
	if source.Name != nil {
		xstring := *source.Name
		target.Name = &xstring
	}
	if source.Aliases != nil {
		target.Aliases = make([]string, len(source.Aliases))
		for i := 0; i < len(source.Aliases); i++ {
			target.Aliases[i] = source.Aliases[i]
		}
	}
	target.Age = source.Age
}

update:ignoreZeroValueField [yes|no]

update:ignoreZeroValueField [yes|no] can be defined as CLI argument, conversion comment or method comment. This setting is inheritable. Default no.

update:ignoreZeroValueField instructs goverter to skip mapping fields when the source field is the zero value of the type.

There are three subcategories, ignoreZeroValueField is the same as enabling / disabling all these categories together:

  • update:ignoreZeroValueField:basic [yes|no]. Ignore zero values for basic types
  • update:ignoreZeroValueField:struct [yes|no]. Ignore zero values for structs
  • update:ignoreZeroValueField:nillable [yes|no]. Ignore zero values for nillable types: channels, maps, functions, interfaces, slices
Example (click me)
go
package update

// goverter:converter
// goverter:update:ignoreZeroValueField
// ^- this setting can be specified on the conversion method
type WithIgnore interface {
	// goverter:update target
	Convert(source Input, target *Output)
}

// goverter:converter
type WithoutIgnore interface {
	// goverter:update target
	Convert(source Input, target *Output)
}

type Input struct {
	Name    string
	Age     int
	Cool    bool
	Address *string
}

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

package generated

import updateignorezero "github.com/jmattheis/goverter/example/update-ignore-zero"

type WithIgnoreImpl struct{}

func (c *WithIgnoreImpl) Convert(source updateignorezero.Input, target *updateignorezero.Output) {
	if source.Name != "" {
		target.Name = source.Name
	}
	if source.Age != 0 {
		target.Age = source.Age
	}
	if source.Cool != false {
		target.Cool = source.Cool
	}
	if source.Address != nil {
		xstring := *source.Address
		target.Address = &xstring
	}
}

type WithoutIgnoreImpl struct{}

func (c *WithoutIgnoreImpl) Convert(source updateignorezero.Input, target *updateignorezero.Output) {
	target.Name = source.Name
	target.Age = source.Age
	target.Cool = source.Cool
	if source.Address != nil {
		xstring := *source.Address
		target.Address = &xstring
	}
}

update:ignoreZeroValueField:nillable does include pointers, but due to the internal design of goverter (checking for nil before dereferencing), it's ignored by default and cannot be changed currently. This setting still does affect pointers when skipCopySameType is enabled.