-
-
Notifications
You must be signed in to change notification settings - Fork 51
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
Allow functions calls in goverter:map
#91
Comments
Could you give an example? With goverter comments and your preferred solution. |
Smth like this
|
goverter:map
Yeah, sounds good. I'd say goverter should automatically try to map methods to properties, so your example above should be automatically handled. Still it should be possible to manually define it, when the names on the source and target doesn't match type Input struct {
id uint64
}
func (i Input) Identifier() uint64 {
return i.id
}
type Output struct {
ID uint64
}
// goverter:converter
type Converter interface {
// goverter:map Identifier() ID
Convert(source Input) Output
} |
Exactly, method and field can't have the same names, so if goverter can't find struct field with this name, it should to search a method, and now we have one restriction that is one method parameter, I think an idea to convert multi inputs in one output also good, it's pretty good if you use domain aggreagate of few entities Now goverter can't map fields of anonymous nested struct, dunno why, but it is |
Could you elaborate this with an example? I don't understand what you mean with multiple inputs. Goverter should be able to map fields on anonymous structs, but the definition is a bit cumbersome, could you give an example that doesn't work? |
About multi inputs: type Account struct { type Role struct { type Group struct { // goverter:converter Error while creating converter method: ... expected signature to have only one parameter |
About one input, but nested EntityMixin struct, entities the same as upper, but contract is:
Error while creating converter method: .... | | Cannot match the target field with the source entry: "ID" does not exist. Can't find AccountEntity.EntityMixin.ID source, EntityMixin is anonimus nested struct |
Multiple inputs will probably be supported with #68 I'll keep this in mind when implementing it. Do you mean in the second example If yes, then you can use autoMap for this
See https://goverter.jmattheis.de/#/conversion/mapping?id=auto-map & #55 |
autoMap doesn't work because of anonymous nested EntityMixin, still error: Cannot match the target field with the source entry: "ID" does not exist. type EntityMixin struct { type Account struct { |
Can you provide a smaller example that is self contained? You should be able to access the anonymous or rather embedded struct by the type name in your case EntityMixin in goverter. See #24 |
Yes, if i write the mapping function i can get access to the ID field through the EntityMixin, but goverter doesn't see this ability |
Gets error too |
Oh, if i don't use pointers inside of DatabaseToEntitiesInput it can map field from embedded struct |
ID is on the A struct so it isn't seen by just automapping B, you need properties from both B and B.A: // goverter:converter
type Converter interface {
// goverter:autoMap B.A
// goverter:autoMap B
DatabaseToEntities(*DatabaseToEntitiesInput) *C
} This builds without problems. |
Yeah, got now problems with mapping time.Time type | | | |
type A struct { type B struct { type C struct { type DatabaseToEntitiesInput struct { // goverter:converter goverter tries to watch inside of time.Time structure instead of copy as object |
Goverter doesn't know that see #4 |
Yeah i see, thnq for the discussion, so now we have 2 features: #68 and that is in the title |
Feature added in v1.1.0. Example: Input: package structs
// goverter:converter
type Converter interface {
Convert(source Input) Output
}
type Input struct {
Name string
}
func (Input) Age() int {
return 42
}
type Output struct {
Name string
Age int
} Output: // Code generated by github.com/jmattheis/goverter, DO NOT EDIT.
package generated
import execution "github.com/jmattheis/goverter/execution"
type ConverterImpl struct{}
func (c *ConverterImpl) Convert(source execution.Input) execution.Output {
var structsOutput execution.Output
structsOutput.Name = source.Name
structsOutput.Age = source.Age()
return structsOutput
} Fixes #91 |
I think it would be a great idea to map source structure method result to a target structure field (it can help to map private structure fields)
The text was updated successfully, but these errors were encountered: