jmattheis / goverter

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

feat: provide custom constructor setting #96

Closed lampajr closed 9 months ago

lampajr commented 9 months ago

Fixes https://github.com/jmattheis/goverter/issues/93

New feature: allow users to provide a custom constructor in order to initialize the target obj.

Usage:

        package execution

        // goverter:converter
        type Converter interface {
            // goverter:init NewOutput
            // goverter:ignore ID
            Convert(source Input) Output
        }

        type Input struct {
            ID int
            FirstName string
            LastName string
        }
        type Output struct {
            ID int
            FirstName string
            LastName string
        }
        func NewOutput() Output {
            return Output{
                ID: 0,
            }
        }

Generated converter:

        func (c *ConverterImpl) Convert(source execution.Input) execution.Output {
            var executionOutput execution.Output = execution.NewOutput()
            executionOutput.FirstName = source.FirstName
            executionOutput.LastName = source.LastName
            return executionOutput
        }

Tests:

Added some test scenarios under /scenario, all those matching constructor*.yml

lampajr commented 9 months ago

Hey @jmattheis,

I gave this a shot, I hope it can help :slightly_smiling_face:

Open points:

codecov[bot] commented 9 months ago

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (13d3233) 97.47% compared to head (6aa5d54) 97.87%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #96 +/- ## ========================================== + Coverage 97.47% 97.87% +0.39% ========================================== Files 38 39 +1 Lines 1663 1693 +30 ========================================== + Hits 1621 1657 +36 + Misses 29 25 -4 + Partials 13 11 -2 ``` | [Files](https://app.codecov.io/gh/jmattheis/goverter/pull/96?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jannis+Mattheis) | Coverage Δ | | |---|---|---| | [builder/default.go](https://app.codecov.io/gh/jmattheis/goverter/pull/96?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jannis+Mattheis#diff-YnVpbGRlci9kZWZhdWx0Lmdv) | `100.00% <100.00%> (ø)` | | | [builder/pointer.go](https://app.codecov.io/gh/jmattheis/goverter/pull/96?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jannis+Mattheis#diff-YnVpbGRlci9wb2ludGVyLmdv) | `100.00% <100.00%> (ø)` | | | [builder/struct.go](https://app.codecov.io/gh/jmattheis/goverter/pull/96?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jannis+Mattheis#diff-YnVpbGRlci9zdHJ1Y3QuZ28=) | `100.00% <100.00%> (ø)` | | | [config/method.go](https://app.codecov.io/gh/jmattheis/goverter/pull/96?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jannis+Mattheis#diff-Y29uZmlnL21ldGhvZC5nbw==) | `100.00% <100.00%> (ø)` | | | [generator/generator.go](https://app.codecov.io/gh/jmattheis/goverter/pull/96?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jannis+Mattheis#diff-Z2VuZXJhdG9yL2dlbmVyYXRvci5nbw==) | `100.00% <100.00%> (+3.37%)` | :arrow_up: | | [config/extend.go](https://app.codecov.io/gh/jmattheis/goverter/pull/96?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Jannis+Mattheis#diff-Y29uZmlnL2V4dGVuZC5nbw==) | `98.14% <66.66%> (ø)` | |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

jmattheis commented 9 months ago

@lampajr could you review the adjusted version?

lampajr commented 9 months ago

@lampajr could you review the adjusted version?

I just fixed a leftover in the doc (init --> default), it looks really great!!

I tried it also locally in my use case, and it worked well :muscle:

jmattheis commented 9 months ago

Good catch, thanks!