-
-
Notifications
You must be signed in to change notification settings - Fork 55
Support for getters & setters (for gRPC Opaque API) #179
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
Comments
yeah, goverter should support getter and setters. These questions are more of a list of todos for me, you don't have to answer them, but opinions are welcome (:.
|
I think if the field is available then prefer the field. Mixing sounds like a bad default. Having it configurable would be good though.
Ye that would be good for when you have a mix of exported fields and some fields behind getters and setters. By default it should prefer fields if available though.
Yes.
I think it should work out of the box and try find a field or a setter. Having to opt in to setters everywhere would become tedious. Better with an opt out strategy IMO.
Yes, as that would open this feature up to allow other strategies. Maybe it could be added into the It's quite common to have the getter just be the field name and the setter has the "Set" prefix. So by default it should look for both with and without the "Get" prefix.
As a fallback this might actually make sense. So goverter would pick an Additionally, I came up with 2 new considerations:
Great tool btw! I'm considering contributing as I'd like to have this tool more "plug and play", as today it feels quite limiting without going down the route of adding a bunch of comments. I'll create some issues of my pain point that I got from trying it out. |
Currently, goverter can convert unexported fields given the correct output package is specified where these fields can be accessed. It could be unclear if goverter should error with a field cannot be accessed error, or try to use setters. Especially if manual goverter:map are defined for unexported fields. Tho I'm not sure if that's actually used.
Goverter already uses methods on the struct if the name is the same. So basically the first Foo -> Bar conversion already does work with a goverter:map // goverter:converter
type Converter interface {
// goverter:map GetName Name
ConvertFoo(foo Foo) Bar
}
How do you mean this, could you give an example?
Yeah, I think this should be ignored for now, as it probably is used pretty rarely, and there are other ways to solve this with existing functionality.
I think goverter will try to satisfy every setter on the target struct, if there is something missing it will error, and then it either has to be goverter:ignore'd or mapped correctly.
Awesome! Feel free to put this in one issue, and then we can split it into smaller ones after discussing the use-cases. |
Perfect! Though what I'm advocating for is that we should not need the When working with gRPC opaque types generated code then we'd need to have one
Just wanted to make clear that it'd probably be better to stick with For example if the user for some reason has Although if you're onboard with the idea of goverter finding getters/setters automatically without the need for any Such as: // goverter:converter
// goverter:getter "Gimme{{ .Name }}"
// goverter:setter "Override{{ .Name }}"
type Converter interface {
ConvertFoo(foo Foo) Bar
ConvertBar(bar Bar) Foo
}
type Foo struct {
name string
}
func (f *Foo) GimmeName() string { return f.name }
func (f *Foo) OverrideName(name string) { f.name = name }
type Bar struct {
Name string
} ^ where
Yeah, nice then we're on the same page on that. But what I meant was this: // goverter:converter
type Converter interface {
ConvertSource(source Source) Target
}
type Source struct {
name string
}
func (s *Source) GetName() string { return s.name }
type Target struct {
name string
}
func (t *Target) SetName(name string) string { t.name = name } This should generate something like this: func (c ConverterImpl) ConvertSource(source Source) Target {
var target Target
target.SetName(source.GetName())
return target
} But the question is how would goverter know that Perhaps this could be ignored in the initial iteration with a note in the docs stating that getters/setters are not supported if both the source and target use getters/setters. |
Yeah, agreed. Only wanted to showcase that field methods already are supported internally, so that only the automatic mapping is needed there.
Understood, and agree. It also must be included into :map, because Custom Methods must continue to work with method setters / getters.E.g. // goverter:converter
type Converter interface {
// goverter:map Foo OverrideFoo | AdjustFoo
SomeConvert(foo Foo) Bar
}
func AdjustFoo(value any) any {
// ...
}
Yeah definitly, the other usecase I think could be common is I'd imagine the settings either to be a prefix
Or a pattern and a format, to both get the name from a setter, and create a setter from a name.
It'll probably have a logic like this:
the fields list will then be tried to map,
Without having the possibility to get all possible field names from the setters of a struct, goverter cannot determine if all setters are satisfied, so I see this as required in the initial iteration. |
seems its impossible to map getter().field to another name, i have the situation where the target value could be set from the result struct field returned from getter |
@pot-code please create a new issue for this with a concrete example. |
hmmm...i can add another method to make it work so it's not a big issue, i thought it was not implemented for some reason and does it turn out to be a bug? |
Not a bug, just not implemented yet. |
Uh oh!
There was an error while loading. Please reload this page.
Is your feature request related to a problem? Please describe.
Support for this:
This is the general idea of how Go's new gRPC Opaque API works: https://go.dev/blog/protobuf-opaque
But when trying this I get this:
Describe the solution you'd like
goverter should try look for
Get{{ .Name }}
andSet{{ .Name }}
if no field is found.Describe alternatives you've considered
nothing comes to mind
Please 👍 this issue if you like 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: