-
-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mapping different subsets of properties on the same types #146
Comments
Good catch! Goverter uses the method signature to identify methods. While parsing the methods from the interface, the second method will override the first one, causing a missing method and not satisfying the interface. This is mostly because of method re-usage. E.g. given this // goverter:converter
type Converter interface {
Convert(Input) Output
Convert2(Input2) Output
ConvertNested(NestedInput) NestedOutput
}
type Input struct {
Name string
Nested NestedInput
}
type Input2 struct {
Name string
Nested NestedInput
}
type Output struct {
Name string
Nested NestedOutput
}
type NestedInput struct{ Value string }
type NestedOutput struct{ Value string } goverter will generate this code func (c *ConverterImpl) Convert(source nestedstruct.Input) nestedstruct.Output {
var exampleOutput nestedstruct.Output
exampleOutput.Name = source.Name
exampleOutput.Nested = c.ConvertNested(source.Nested)
return exampleOutput
}
func (c *ConverterImpl) Convert2(source nestedstruct.Input2) nestedstruct.Output {
var exampleOutput nestedstruct.Output
exampleOutput.Name = source.Name
exampleOutput.Nested = c.ConvertNested(source.Nested)
return exampleOutput
}
func (c *ConverterImpl) ConvertNested(source nestedstruct.NestedInput) nestedstruct.NestedOutput {
var exampleNestedOutput nestedstruct.NestedOutput
exampleNestedOutput.Value = source.Value
return exampleNestedOutput
} The generated method Supporting multiple root methods with the same signature can probably be done without too much effort and then goverter could error when the described ambiguity occurs to prevent unexpected behavior. Would this cover your use-case? Along with #80 goverter need a kind of method flavoring, to have different versions of methods with the same signature and have a way to choose which methods should be used when doing nested conversions. These flavored methods could be used to chose between multiple methods when there is an ambiguity. Your
Goverter would apply all non ignored properties from source to target. |
Thanks for the clarification! Happy to know I guessed on the right direction. This behavior is pretty cool for a generator :)
I'd say that a "copy to" would actually be better than extending support for multiple root (flavored) methods. To me it looks cleaner to have that interface. Although that brings me to another question: I see that in the generated code for nested structs goverter only checks if the high-level ref is not nil, but will map the fields independently of them being |
I've created #147 for the copy to functionality.
With the current release that's not possible. This limitation was talked about in #96 (comment) and #97 tracks the issue. I've a wip branch from a couple of month ago for this called
I think there is one bug left or I'm not really sure what to do there. If you convert a map[K]*T to map[K]*T then the new impl won't copy the value to the target map, if the *T on the source map entry is nil. See Convert2 in https://github.com/jmattheis/goverter/blob/assign-not-nil/scenario/gomap_primitive_pointer.yml I'd expect it to similar to: func (c *ConverterImpl) Convert2(source map[string]*int) map[string]*int {
var mapStringPInt map[string]*int
if source != nil {
mapStringPInt = make(map[string]*int, len(source))
for key, value := range source {
if value != nil {
xint := *value
mapStringPInt[key] = &xint
} else {
mapStringPInt[key] = nil
}
}
}
return mapStringPInt
} |
Thanks! Already upvoted.
It works fine!
Maybe you could only override if |
Great. I'll release this soon, likely on the weekend.
Yeah, sounds valid, but I'll wait until someone has a concrete problem for this before adding a setting. For now I'll let goverter override existing default values with |
As that fixed a nil assignment override[1][2] which prevented us from emulating copy constructors[3]. The update also allows us to use generics. [1]: jmattheis/goverter#146 (comment) [2]: jmattheis/goverter#97 [3]: jmattheis/goverter#147 Signed-off-by: Isabella Basso do Amaral <[email protected]>
As that fixed a nil assignment override[1][2] which prevented us from emulating copy constructors[3]. The update also allows us to use generics. [1]: jmattheis/goverter#146 (comment) [2]: jmattheis/goverter#97 [3]: jmattheis/goverter#147 Signed-off-by: Isabella Basso do Amaral <[email protected]> Signed-off-by: Isabella do Amaral <[email protected]>
As that fixed a nil assignment override[1][2] which prevented us from emulating copy constructors[3]. The update also allows us to use generics. [1]: jmattheis/goverter#146 (comment) [2]: jmattheis/goverter#97 [3]: jmattheis/goverter#147 Signed-off-by: Isabella Basso do Amaral <[email protected]> Signed-off-by: Isabella do Amaral <[email protected]>
* update goverter to 1.4.1 As that fixed a nil assignment override[1][2] which prevented us from emulating copy constructors[3]. The update also allows us to use generics. [1]: jmattheis/goverter#146 (comment) [2]: jmattheis/goverter#97 [3]: jmattheis/goverter#147 Signed-off-by: Isabella Basso do Amaral <[email protected]> Signed-off-by: Isabella do Amaral <[email protected]> * simplify converter utils using generics Signed-off-by: Isabella do Amaral <[email protected]> * server: update existing objects on PATCH Signed-off-by: Isabella do Amaral <[email protected]> --------- Signed-off-by: Isabella Basso do Amaral <[email protected]> Signed-off-by: Isabella do Amaral <[email protected]>
* update goverter to 1.4.1 As that fixed a nil assignment override[1][2] which prevented us from emulating copy constructors[3]. The update also allows us to use generics. [1]: jmattheis/goverter#146 (comment) [2]: jmattheis/goverter#97 [3]: jmattheis/goverter#147 Signed-off-by: Isabella Basso do Amaral <[email protected]> Signed-off-by: Isabella do Amaral <[email protected]> * simplify converter utils using generics Signed-off-by: Isabella do Amaral <[email protected]> * server: update existing objects on PATCH Signed-off-by: Isabella do Amaral <[email protected]> --------- Signed-off-by: Isabella Basso do Amaral <[email protected]> Signed-off-by: Isabella do Amaral <[email protected]> Signed-off-by: muzhouliu <[email protected]>
Have you read the project documentation?
Describe your question
I'm trying to create mapping functions for merging instances, so, e.g. I have a
and now, on the same converter instance, I'd like to have two separate methods for merging Update into Existing, and the other way around.
Now I notice that as soon as I have another method for wrapping a specific
Model
(sayMyModel
) one of them goes missing, so e.g. when specifying something likeI get an error that the goverter struct doesn't properly implement the interface.
Source code
Provided above.
Errors
The text was updated successfully, but these errors were encountered: