Closed li-jin-gou closed 11 months ago
Would a flag that instructs Goverter to not copy same types solve your problem as well? It's a little more aggressive than only disable copying base types, but it's probably what people want when they only use Goverter to convert between types.
Something like this:
// goverter:converter
type Converter interface {
Convert(source Input) Output
}
type Input struct {
ItemCounts map[string]int
}
type Output struct {
ItemCounts map[string]int
}
would result in
package generated
import example "goverter/example"
type ConverterImpl struct{}
func (c *ConverterImpl) Convert(source example.Input) example.Output {
var exampleOutput example.Output
exampleOutput.ItemCounts = source.ItemCounts
return exampleOutput
}
The map[string]int
is the same on the source and target struct, so Goverter would just pass the source instance through.
My current solution is to use the extension for map and list.
// goverter:converter
// goverter:extend Strings
// goverter:extend Int64s
// goverter:extend Int32s
// goverter:extend Floats
// goverter:extend MapString
type Converter interface {
ConvertDto(source *pack.Dto) *pack.Dto
}
func Strings(i []string) []string {
return i
}
func Int64s(i []int64) []int64 {
return i
}
func Int32s(i []int32) []int32 {
return i
}
But I use a lot of pointers in my structure.
var pString3 *string
if (*source).Url != nil {
xstring3 := *(*source).Url
pString3 = &xstring3
}
standard_bizAnchorCommonStruct.Url = pString3
For assignments to pointers of basic types, it is possible to optimize.
standard_bizAnchorCommonStruct.Url = *source.Url
This way both performance and lines of code can be optimized.
Would a flag that instructs Goverter to not copy same types solve your problem as well? It's a little more aggressive than only disable copying base types, but it's probably what people want when they only use Goverter to convert between types.
Something like this:
// goverter:converter type Converter interface { Convert(source Input) Output } type Input struct { ItemCounts map[string]int } type Output struct { ItemCounts map[string]int }
would result in
package generated import example "goverter/example" type ConverterImpl struct{} func (c *ConverterImpl) Convert(source example.Input) example.Output { var exampleOutput example.Output exampleOutput.ItemCounts = source.ItemCounts return exampleOutput }
The
map[string]int
is the same on the source and target struct, so Goverter would just pass the source instance through.Would a flag that instructs Goverter to not copy same types solve your problem as well
It'll solve my problem and thank you very much..
Could you try out the dont-copy
branch?
With go install
go install github.com/jmattheis/goverter/cmd/goverter@dont-copy
or go run
go run github.com/jmattheis/goverter/cmd/goverter@dont-copy -output ./generated/generated.go ./converterdir
or go get
go get github.com/jmattheis/goverter@dont-copy
Enable on the converter with goverter:skipCopySameType
:
// goverter:converter
// goverter:skipCopySameType
type Converter interface {
Convert(source Input) Output
}
// goverter:skipCopySameType
It's ok, thanks a lot!
Released with v0.18.0: https://goverter.jmattheis.de/reference/skipCopySameType
Is your feature request related to a problem? Please describe. First of all a big thank you to goverter for solving my problem with a very complex structure conversion. the deep copy of the base type in the actual runtime has a serious performance impact.
Describe the solution you'd like Hopefully it will support shallow copies of base types
Describe alternatives you've considered I noticed the code is here. Any quick fixes? https://github.com/jmattheis/goverter/blob/main/builder/basic.go#L34 Thank you very much.