-
-
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
Generate functions directly without Converter interface #77
Comments
Hi jmattheis, thanks for your great project. |
Hey @zhenzou, there is no progress on this feature. |
I think we can keep the original Converter interface. The downside is that the interface won't have an implementation, which might seem a bit odd. |
@KScaesar Only doing this would prevent the feature of using generated methods in custom methods. E.g. https://goverter.jmattheis.de/#/config/extend?id=method-with-converter // goverter:converter
// goverter:extend ConvertAnimals
type Converter interface {
Convert(source Input) Output
// used only in extend method
ConvertDogs([]Dog) []Animal
ConvertCats([]Cat) []Animal
}
func ConvertAnimals(c Converter, input InputAnimals) []Animal {
dogs := c.ConvertDogs(input.Dogs)
cats := c.ConvertCats(input.Cats)
return append(dogs, cats...)
} Both the function and interface definition should support the same featureset. |
I think we can keep the original Converter interface.
|
At the time of generation |
I'm looking for testers of the new style for generation. The version should work well, but they likely will some changes before this will be released. Install
UsageWhen using the version above, you can declare You can define Converter settings directly on the outer var statement. Methods settings can be defined on the separate variables. Here an complex example in both styles. Interface/struct style// goverter:converter
// goverter:extend SQLStringToPString
type Converter interface {
ConvertHouse(source DBHouse) APIHouse
// goverter:map Name FirstName
// goverter:ignore Age
ConvertPerson(source DBPerson) APIPerson
// goverter:map Owner.Name OwnerName
ConvertApartment(source DBApartment) APIApartment
} Variables style// goverter:variables
// goverter:extend SQLStringToPString
var (
ConvertHouse func(source DBHouse) APIHouse
// goverter:map Name FirstName
// goverter:ignore Age
ConvertPerson func(source DBPerson) APIPerson
// goverter:map Owner.Name OwnerName
ConvertApartment func(source DBApartment) APIApartment
) If you've tried out the new style, it would be great if you could answer the following questions:
|
Hi, thanks for this amazing tool. I'm currently trying out the new I've been using goverter for a while now (#100), and the original To work around this, I defined the following function: func newConverter() converter {
return &converterImpl{}
} When generating: func newConverter() converter {
return nil
} This function helped minimize changes when deleting and regenerating. The new |
Looks great! |
Thanks for you feedback. The solution in #77 (comment) was implemented in v1.5.0. See Guide: Input/output formats. Furthermore, it's possible to generate functions without a struct when defining conversions via an interface as described in #77 (comment). See Guide: Input/output formats. |
Goverter generates an implementation for a user defined interface with conversion methods. The usability of that could be better, because now the user has to initialize the Converter struct and call methods from there. Goverter could generate functions directly. If this ticket will be included into Goverter, then the old interface approach will most likely be removed.
Current example (click me)
Proposed approach
Variables marked with
goverter:func
would allow all configs that can be added to the conversion methods on interfaces. Global goverter flags likegoverter:extend
could be configured on the package declaration.When executing goverter on this input file, it could generate an init() method and set the generated implementation. The generated code for the example above could look like this:
Generated Code Example (click me)
Testable version available: #77 (comment)
Please 👍 this issue if you want this functionality. If you have a specific use-case in mind, feel free to comment it.
The text was updated successfully, but these errors were encountered: