jmattheis/goverter

Ignore target field that exists on source struct

Closed this issue · 4 comments

Expected Ignore to work both ways. Need

type A {
  fieldA int
  fieldB int
}

type B {
  fieldA int
}

A->B throws error because fieldB doesn't exist on B

Example

package main

// go mod init localhost
// go run github.com/jmattheis/goverter/cmd/goverter@latest gen ./

import "fmt"

// goverter:converter
type Converter interface {
	// goverter:ignore Cat
	ObjectAToObjectB(source ObjectA) ObjectB
}

type ObjectA struct {
	ID  int
	Cat bool
}

type ObjectB struct {
	ID int
}

func main() {
	fmt.Print("Hello World")
}

Output

Error while creating converter method:
    func (localhost.Converter).ObjectAToObjectB(source localhost.ObjectA) localhost.ObjectB

| localhost.ObjectA
|
source.
target.Cat
|      |
|      | ???
|
| localhost.ObjectB

Field "Cat" does not exist.
Remove or adjust field settings referencing this field.
exit status 1

Please add your converter interface and the error message you are receiving.

Thanks! To answer your question: Goverter only errors when a field on the target type isn't mapped. You are defining the conversion for the type with Cat field to type without Cat field. Goverter can map all fields on the target type, so no need for an ignore. ignoreing an not existing field will cause an error in goverter because you are referencing a field that doesn't exist, so goverter sees this as misconfiguration.

If it's the other way around, then you could ignore the field, because then it would be unmapped.

// goverter:converter
type Converter interface {
	ObjectAToObjectB(source ObjectA) ObjectB

	// goverter:ignore Cat
	ObjectBToObjectA(source ObjectB) ObjectA
}

Awesome! Thanks. I didn't realize the projection was implicit.