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: add PackagePath to prevent loop import of the generated package #22

Closed nissim-natanov-outreach closed 2 years ago

nissim-natanov-outreach commented 2 years ago

This PR prevents loop import of the same generated package inside itself.

I added a new test case to cover this: 3_struct_extend_in_generated_package.yml. Without the PackagePath param, the test would generate the following import, leading to a loop import of self package and compilation error:

import (
    execution "github.com/jmattheis/goverter/execution"
    // this one is buggy because it imports the generated code's package and golang does not allow this
    generated "github.com/jmattheis/goverter/execution/generated"
)

After the fix, the loop import is not generated, see the success in the new test case.

nissim-natanov-outreach commented 2 years ago

@jmattheis hi Jannis, this PR is ready, PTAL, thanks! our devs hit the local loop import issue twice already, time to fix it.

Note: I could also enable use of unexported methods from the generated package (now that it is an explicit param to the generator), but I have not included this fix in the PR. Our team does not have the need for unexported methods because we hide the goverter-tagged interface, its gen code and package-specific convert methods inside an internal/ sub-package, so even though helper convert methods are exported there, the whole sub-package is hidden due to its name.

codecov-commenter commented 2 years ago

Codecov Report

Merging #22 (276afad) into main (128ced6) will increase coverage by 0.05%. The diff coverage is 77.77%.

@@            Coverage Diff             @@
##             main      #22      +/-   ##
==========================================
+ Coverage   93.62%   93.68%   +0.05%     
==========================================
  Files          22       22              
  Lines        1083     1093      +10     
==========================================
+ Hits         1014     1024      +10     
- Misses         52       53       +1     
+ Partials       17       16       -1     
Impacted Files Coverage Δ
cmd/goverter/main.go 0.00% <0.00%> (ø)
generator/generate.go 93.54% <100.00%> (ø)
runner.go 92.30% <100.00%> (+1.39%) :arrow_up:
generator/generator.go 97.38% <0.00%> (+0.07%) :arrow_up:
builder/pointer.go 92.68% <0.00%> (+4.87%) :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 128ced6...276afad. Read the comment docs.