Problem Statement
Currently, goverter successfully generates a converter if all target fields are mapped (or ignored). However, it does not provide feedback if fields on the source struct are left unmapped.
type Source struct {
Name string
Email string
}
type Target struct {
Name string
}
goverter will happily map Source.Name to Target.Name and silently ignore Source.Email.
Why this is a problem
In many use cases, especially when mapping between database models and API DTOs (Data Transfer Objects), we want to ensure every field from the source is explicitly handled.
If a developer adds a new field to Source:
type Source struct {
Name string
Email string
Phone string // New field
}
...they might forget to "carry it over" to the Target struct or to the converter logic. This can lead to silent data loss, where new data is available but never exposed in the API or passed to the next service.
Proposed Solution
I propose adding a new setting (perhaps at the converter or method level) that enables a "strict" mode, let's call it errorOnUnmappedSource: true.
When this setting is enabled:
goverter would analyze all fields on the Source struct.
- If it finds a source field that is not used in any mapping (neither via
autoMap nor an explicit map rule) and is not explicitly ignored, it should fail the code generation with an error.
- The error message should be clear, e.g.,
Error: source field "Phone" is not mapped or ignored.
Example Workflow
With // goverter:errorOnUnmappedSource true:
// goverter:converter
// goverter:errorOnUnmappedSource true
type MyConverter interface {
Convert(Source) Target
}
type Source struct {
Name string
Phone string
}
type Target struct {
Name string
}
This should fail code generation because Source.Phone is unmapped.
To fix this, the developer must make an explicit choice:
- Map the field
(Add Phone to Target and goverter will automap it)
- Explicitly ignore the field
We would need a way to ignore a source field. Perhaps goverter already has this, or we could add:
// goverter:converter
// goverter:errorOnUnmappedSource true
type MyConverter interface {
// goverter:ignoreSource Phone
Convert(Source) Target
}
Please 👍 this issue if you want this functionality. If you have a specific use-case in mind, feel free to comment it.
Problem Statement
Currently, goverter successfully generates a converter if all target fields are mapped (or ignored). However, it does not provide feedback if fields on the source struct are left unmapped.
goverter will happily map
Source.NametoTarget.Nameand silently ignoreSource.Email.Why this is a problem
In many use cases, especially when mapping between database models and API DTOs (Data Transfer Objects), we want to ensure every field from the source is explicitly handled.
If a developer adds a new field to
Source:...they might forget to "carry it over" to the Target struct or to the converter logic. This can lead to silent data loss, where new data is available but never exposed in the API or passed to the next service.
Proposed Solution
I propose adding a new setting (perhaps at the converter or method level) that enables a "strict" mode, let's call it
errorOnUnmappedSource: true.When this setting is enabled:
goverterwould analyze all fields on the Source struct.autoMapnor an explicitmaprule) and is not explicitly ignored, it should fail the code generation with an error.Error: source field "Phone" is not mapped or ignored.Example Workflow
With
// goverter:errorOnUnmappedSource true:This should fail code generation because
Source.Phoneis unmapped.To fix this, the developer must make an explicit choice:
(Add Phone to Target and goverter will automap it)
We would need a way to ignore a source field. Perhaps goverter already has this, or we could add:
Please 👍 this issue if you want this functionality. If you have a specific use-case in mind, feel free to comment it.