Setting: useEmptyOnNil
useEmptyOnNil [yes,no], useEmptyOnNil:slice [yes,no] and useEmptyOnNil:map [yes,no] are boolean settings and can be defined as CLI argument, conversion comment or method comment. These settings are inheritable.
By default, goverter converts a nil slice/map to nil.
- Set
useEmptyOnNilto enable both of the settings below. - Set
useEmptyOnNil:mapto convertnilmaps to empty maps (map[K]V{}). - Set
useEmptyOnNil:sliceto convertnilslices to empty slices ([]T{}).
go
package emptyonnil
// goverter:converter
// goverter:useEmptyOnNil
type Converter interface {
ConvertSlice(source []string) []string
ConvertMap(source map[string]string) map[string]string
}
// goverter:converter
type StrictConverter interface {
ConvertSlice(source []string) []string
ConvertMap(source map[string]string) map[string]string
}go
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.
//go:build !goverter
package generated
type ConverterImpl struct{}
func (c *ConverterImpl) ConvertMap(source map[string]string) map[string]string {
var mapStringString map[string]string
mapStringString = make(map[string]string, len(source))
for key, value := range source {
mapStringString[key] = value
}
return mapStringString
}
func (c *ConverterImpl) ConvertSlice(source []string) []string {
var stringList []string
stringList = make([]string, len(source))
for i := 0; i < len(source); i++ {
stringList[i] = source[i]
}
return stringList
}
type StrictConverterImpl struct{}
func (c *StrictConverterImpl) ConvertMap(source map[string]string) map[string]string {
var mapStringString map[string]string
if source != nil {
mapStringString = make(map[string]string, len(source))
for key, value := range source {
mapStringString[key] = value
}
}
return mapStringString
}
func (c *StrictConverterImpl) ConvertSlice(source []string) []string {
var stringList []string
if source != nil {
stringList = make([]string, len(source))
for i := 0; i < len(source); i++ {
stringList[i] = source[i]
}
}
return stringList
}