-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Add json:
(or custom) struct tag support for field matching
#132
Comments
For this goverter has support for defining the mapping on the conversion methods, see
Could you explain why you would prefer defining the conversion names on one of the structs used in the conversion? For me it seems that the conversion methods are the better place. E.g. if you have the following conversion A -> B, A -> C. If the mapping is defined directly on the A struct, then you can only define the mapping for one of them.
Would these "true field mismatches" be fixable with
I think the change would actually not that big, there is one method responsible for matching the field by names. In there we could add the additional checks for the struct tags. Line 87 in 48f86d9
That said, I do think tag matching can improve the developer experience of goverter. Goverter could support something like this: // goverter:converter
// goverter:matchIgnoreCase
// goverter:matchTag json
type Converter interface {
Convert(source []Input) []Output
}
type Input struct {
Name string `json:"fullName"`
}
type Output struct {
FullName string
} the type Input struct {
Name string `json:"fullName"`
FullName string
}
type Output struct {
FullName string
} Then goverter should fail with an error, but this functionality already exist because of Would this solve your use-case? |
Yes, this would definitely work. But what prompted my thinking on tags is that we have several hundred entities where we would have to apply
I think because my use case started out with "correct" json tag matching, the
Yes, totally, one-offs I think are fine. Though slightly awkward because I think you'd need
Gotcha, I think I wasn't fully grokking that to notice it wasn't just a single name but returned StructField(s).
Yep, this makes sense. Would
As you laid out, I agree this would /streamline/ my use case, but not strictly necessary. :) I'll take a look at this over the next couple of weeks unless someone beats me to it. ;-) Thanks for the quick response! |
Yeah, in this case I agree. Having tag support in goverter would remove a lot of duplicated mappings there.
Yes.
Yes. The tags should support this common format Do you think it's okay to have tag matching as addition to the normal field name matching? Or should it be explicitly definable, e.g. |
If I understand you correctly, my first thought was indeed your "in addition" take. I feel like Having Enabling tag processing could also create situations where devs were relying on name matching, but had |
I mean it more like a general matching configuration.
this would mean that only the json tag is matchend against the fieldName
this means that both the json tag and fieldname will be matched against the target field. But yeah, I think this is overkill, so i'll say we do it "in addition". |
Ah, so normal field name matching would happen, except |
I'd like to +1 this request. My issue is that I have a lot of generation in my stack, and due to some unique naming restrictions (due to the domain, even if it doesn't make the most programmatic sense), I have some weird friction points. One of them is that due to this unique naming restriction, my generated protobuf code has extraneous underscores in the name (due to casing of the source material). This makes it so every field I want to map requires two mapping directives (one for each direction). My current model that goverter is supporting is over 400 lines (in it's most compact form, this is actual properties/objects that get converted). So now i'm stuck auto-generating my goverter definition file and having it auto-determine correlation based on some known naming conventions. This auto-generated goverter template is over 1500 lines long, mostly full of mapping directives. This pre-generation workflow could go away if I could just match on the json tags. In a perfect world i'd make things align, and maybe I will, but that requires forking the protoc-gen-grpc plugin... and i'm not ready to do that. I'd be willing to throw a $50 bounty towards this. |
See PR #164 @morganhein |
This is great! Lmk when this is merged if you want the bounty or not, and the best way to get it to you! Thanks! |
Is your feature request related to a problem? Please describe.
Most of the time, struct field names can be made to match. Sometimes the fields differ in capitalization (e.g., Go's entityID vs JSON's entityId) which can be resolved with case insensitivity.
However, there are cases where field names differ beyond the coder's control, like from one protobuf to multiple slightly different "internal" structs, etc.
Describe the solution you'd like
Support for this could be added but using
json:
struct tags to inform matching. Ideally, struct fields could be annotated with an additionalgoverter:
tag to inform field mapping, so that existingjson:
tags could be ignored.Honoring
json:
would obviously need to be an opt-in flag, but to me it's debatable whethergoverter:
tag support could be on by default, or also opt-in.I'd originally tried to use copygen which has tag support, but in general that project just doesn't work with protobuf or complex structs in the mix. YMMV.
Briefly looking through the goverter code, it does look like adding tag support could be a bit involved. I'd be happy to get advice on the project's preferences on how this would be implemented.
Describe alternatives you've considered
There's no real alternative to true field mismatches, other than creating a ghost field to receive the goverted value, and then add a wrapper that manually copies over these ghost values.
Thanks!
The text was updated successfully, but these errors were encountered: