jmattheis / goverter

Generate type-safe Go converters by simply defining an interface
https://goverter.jmattheis.de/
MIT License
496 stars 46 forks source link

feat: goverter:matchIgnoreCase to enable case-insensitive match for field names #16

Closed nissim-natanov-outreach closed 2 years ago

nissim-natanov-outreach commented 2 years ago

This PR enables goverter:matchIgnoreCase: case insensitive comparison for field names. Reason: we use goverter to convert database objects from their models to a protobuf (for the database row to be sent out to other systems on write). Both model and protobuf structs are auto-generated (by go-jet and protoc), and there are lots of cases in which the the fields names only differ in casing: for example protoc converts proto name id to golang field Id while go-jet converts equivalent database column "id" as ID. There are many similar cases.

We want to automate codegen for such conversions with minimal (ideally ZERO) human intervention. To reach that perfection, case-insensitive matching is needed. It is highly unlikely that either of the tools can auto-generate two fields on their own struct that only differ in casing (possible - but it is highly unlikely, hope that devs do not do so because it is simply a bad practice). If such case does happen (e.g. username and user_name => Username and UserName), then devs can solve the ambiguity using goverter:map or goverter:ignore, or avoid adding goverter:matchIgnoreCase.

I though of limiting this feature to a specific interface only, rather than making it a 'global flag' because of its nature - it should be used "with care". I added code to handle special cases, including testing:

codecov-commenter commented 2 years ago

Codecov Report

Merging #16 (bb911bd) into main (58ba62d) will increase coverage by 0.17%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main      #16      +/-   ##
==========================================
+ Coverage   93.61%   93.78%   +0.17%     
==========================================
  Files          22       22              
  Lines        1049     1078      +29     
==========================================
+ Hits          982     1011      +29     
  Misses         51       51              
  Partials       16       16              
Impacted Files Coverage Δ
builder/struct.go 100.00% <100.00%> (ø)
comments/parse_docs.go 97.01% <100.00%> (+0.11%) :arrow_up:
generator/generator.go 97.31% <100.00%> (+0.05%) :arrow_up:
xtype/type.go 88.74% <100.00%> (+1.62%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 58ba62d...bb911bd. Read the comment docs.

nissim-natanov-outreach commented 2 years ago

@jmattheis hi Jannis, this PR is ready for your review, looking forward for your feedback. Thanks!

nissim-natanov-outreach commented 2 years ago

Thanks for the contribution, works pretty good! I have only some remarks about the error messages and error handling.

Thanks for the feedback, I will address it soon-ish and publish a new iteration. I will tag you when it is ready. I will send another PR (after this one finishes) that fixes a tiny bug in extend with a package and also adds support for ignoreUnexprted on the converter method (protobufs have inner 'unexported fields are are totally safe to leave as is when creating them)

nissim-natanov-outreach commented 2 years ago

@jmattheis next iteration is ready, pls merge if all good or share feedback, thanks!

nissim-natanov-outreach commented 2 years ago

Thank you @jmattheis, tag please :)

jmattheis commented 2 years ago

@nissim-natanov-outreach done (:.

nissim-natanov-outreach commented 2 years ago

🙇‍♂️