Skip to content

Add option to error on unmapped source fields (strict mapping) #216

Description

@kiteggrad

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:

  1. goverter would analyze all fields on the Source struct.
  2. 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.
  3. 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:

  1. Map the field
    (Add Phone to Target and goverter will automap it)
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions