jmattheis / goverter

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

Is it possible to set to automatically ignore non-existent fields #58

Closed shuqingzai closed 1 year ago

shuqingzai commented 1 year ago

Have you read the project readme?

Describe your question A clear and concise description of what the question is. Include errors and go source files.

type In struct {
    Name      string
    Age       int
    OtherMiss string
}

type Out struct {
    Id       int
    Name     string
    Age      int
    Password string
}

// goverter:converter
type Converter interface {
    // InToOut 转换 *In 到 *Out
    // goverter:ignoreMissSource
    // goverter:ignore OtherMiss
    InToOut(source *In) (*Out, error)
    // InToOut2 转换 *Out 到 *In
    // goverter:ignoreMissTarget
    // goverter:ignore Id Password
    InToOut2(source *In) (*Out, error)
    // InToOut3 转换 *In 到 *Out
    // goverter:ignoreMissAll
    InToOut3(source *In) (*Out, error)
    // InToOut4 转换 *In 到 *Out
    // goverter:only Name
    InToOut4(source *In) (*Out, error)
}

In the above conversion, I only want to convert Age Name

It is recommended to add 4 annotations

  1. ignoreMissSource Indicates ignoring fields missing from the source relative to the target, including: Out.Id Out.Password So InToOut() needs to add goverter:ignore OtherMiss
  2. ignoreMissTarget Indicates to ignore fields missing from the target relative to the source, including: In.OtherMiss So InToOut2() needs to add goverter:ignore Id goverter:ignore Password
  3. ignoreMissAll Indicates ignoring fields that do not match on both sides, including: In.OtherMiss Out.Id Out.Password So InToOut3() does not need to add the goverter:ignore annotation
  4. goverter:only Indicates that only the selected fields will be converted, other fields will be ignored So InToOut4 only: Name will be converted, other fields will be ignored

This will greatly reduce the workload of writing goverter:ignore annotations, which is very helpful to improve efficiency

jmattheis commented 1 year ago

ignoreMissTarget is currently the default, goverter doesn't error when the target struct misses fields that are present on the source struct. ignoreMissSource seems like a duplicate of https://github.com/jmattheis/goverter/issues/23

Could you give a real use-case for goverter:only?

shuqingzai commented 1 year ago

ignoreMissTarget is currently the default, goverter doesn't error when the target struct misses fields that are present on the source struct. ignoreMissSource seems like a duplicate of #23

Could you give a real use-case for goverter:only?

goverter:only doesn't seem to be necessary

23 seems to provide the default, I don't think this is a good choice, it should be manually confirmed by the user to let them know what happened.

But their functions are similar

ignoreMissAll is a long awaited feature

jmattheis commented 1 year ago

I've added a comment to https://github.com/jmattheis/goverter/issues/23#issuecomment-1441902003, for your feature request. I'll close this one as duplicate.