jmattheis / goverter

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

Could goverter ignore the nested fields? #85

Closed h1z3y3 closed 11 months ago

h1z3y3 commented 11 months 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.

My source:

type Input struct {
  MetaData struct {
    Name string 
    Age int
  }
}

My target:

type Output struct {
  state int
  sizeCache int
  MetaData struct {
    state int
    sizeCache int
    Name string 
    Age int
  }
}

But // goverter:ignore state sizeCache MetaData.state MetaData.sizeCache only works for .state and .sizeCache

Error Message:

image

// goverter:ignoreUnexported also didn't work for me ;

jmattheis commented 11 months ago

goverter:ignore doesn't support nesting. You could define goverter:ignoreUnexported on the converter interface or define conversion method for the MetaData subtype and goverter:ignore the fields there too.

// goverter:converter
type Converter interface {
    // goverter:ignore state sizeCache
    Convert(source Input) Output
    // goverter:ignore state sizeCache
    Converter(v struct{Name string; Age int}) MetaData
}

type Input struct {
    MetaData struct {
        Name string
        Age  int
    }
}

type Output struct {
    state     int
    sizeCache int
    MetaData  MetaData
}

type MetaData struct {
    state     int
    sizeCache int
    Name      string
    Age       int
}
h1z3y3 commented 11 months ago

Thanks a lot! I've found the answer here ~