Skip to content

Setting: useUnderlyingTypeMethods

useUnderlyingTypeMethods [yes,no] is a boolean setting and can be defined as CLI argument, converter comment or method comment. This setting is inheritable.

For each type conversion, goverter will check if there is an existing method that can be used. For named types only the type itself will be checked and not the underlying type. For this type:

go
type InputID  int

InputID would be the named type and int the underlying type.

With useUnderlyingTypeMethods, goverter will check all named/underlying combinations.

  • named -> underlying
  • underlying -> named
  • underlying -> underlying
go
package example

// goverter:converter
// goverter:extend ConvertUnderlying
type Converter interface {
	// goverter:useUnderlyingTypeMethods
	Convert(source Input) Output
}

func ConvertUnderlying(s int) string {
	return ""
}

// these would be used too
// func ConvertUnderlying(s int) OutputID
// func ConvertUnderlying(s InputID) string

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

package generated

import useunderlyingtypemethods "github.com/jmattheis/goverter/example/use-underlying-type-methods"

type ConverterImpl struct{}

func (c *ConverterImpl) Convert(source useunderlyingtypemethods.Input) useunderlyingtypemethods.Output {
	var exampleOutput useunderlyingtypemethods.Output
	exampleOutput.ID = useunderlyingtypemethods.OutputID(useunderlyingtypemethods.ConvertUnderlying(int(source.ID)))
	return exampleOutput
}

Without the setting only the custom method with this signature could be used for the conversion. func ConvertUnderlying(s InputID) OutputID