-
-
Notifications
You must be signed in to change notification settings - Fork 44
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
Is it possible to provide default values to converters? #93
Comments
You can use a method mapping like so // goverter:converter
type Converter interface {
// goverter:map Age | DefaultAge
Convert(Input) Output
}
type Input struct {
Name string
}
type Output struct {
Name string
Age int
}
func DefaultAge() int {
return 42
}
This is currently not possible but sounds like a useful addition. I'll keep this as a feature request. |
Many thanks for the prompt answer!
Yeah exactly, but in that case I think it will be set to
I would be happy to contribute, or at least to try contributing, if that sounds good for you! |
Correct.
You could do it like this, but there is no way around the function definition // goverter:converter
type Converter interface {
// goverter:map Age | DefaultAge
Convert(Input) Output
}
type Input struct {
Name string
Age *int
}
type Output struct {
Name string
Age int
}
func DefaultAge(source *int) int {
if source == nil {
return 42
}
return *source
}
this isn't currently possible.
Sure. I'm not sure yet about the config name yet, but we can decide this later and then rename it in the PR. |
Hi everyone 👋,
First of all I am a new user of this tool and this is pretty amazing!!
Have you read the project readme?
Describe your question
I apologize if I am asking for something stupid or simple but I am struggling finding how I can provide some defaults to be applied during the mapping, here a possible use case:
Use case:
Let's assume that we have a default for
Name
whenever the source, in this casePerson
object, does not provide it.Q1: Is there a way to accomplish this goal without having to create a custom function converter?
Q2: In my specific case I do have an autogenerated constructor that automatically sets all defaults for a specific struct, is there a way to provide that custom constructor to goverter such that the autogenerated code initialize the struct using that constructor instead of simple
FlatPerson{}
initialization?The text was updated successfully, but these errors were encountered: