Skip to content

update:ignoreZeroValueField generates uncompilable != zero check for non-comparable struct fields #227

Description

@ilia-rassadin-rn

Describe the bug
update:ignoreZeroValueField can generate Go code that does not compile when the source field is a struct containing a non-comparable field, such as a slice, map, or function.

The generated code compares the source field against its zero value using !=, but Go only allows struct comparison when all struct fields are comparable.

To Reproduce
Given a nullable-style wrapper type and a converter using update mode with zero-value skipping:

type NullUUIDs struct {
    UUIDs []uuid.UUID
    Valid bool
}

// goverter:converter
type Converter interface {
    // goverter:update target
    // goverter:update:ignoreZeroValueField yes
    Convert(source *Source, target *Target)
}

type Source struct {
    UUIDs NullUUIDs
}

type Target struct {
    UUIDs []uuid.UUID
}

This lead to goverter generates code that cannot be compiled:

// invalid operation: source.UUIDs != (NullUUIDs{}) (struct containing []uuid.UUID cannot be compared)
if source.UUIDs != (NullUUIDs{}) {
    target.UUIDs = convertNullUUIDs(source.UUIDs)
}

Expected behavior

Goverter should avoid generating != comparisons for non-comparable struct types.
Possible approaches:

  1. Detect whether a struct type is comparable before emitting != zero and Fall back to reflect.Value.IsZero or an equivalent helper for non-comparable structs.
  2. Provide a directive/hook to customize zero-value detection for a specific source field/type.
  3. Provide some generic post-processing hook, so we can ignore these fields with // goverter:ignore and then convert them with an extra-hook.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions