jmattheis / goverter

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

interface level option inheritance #28

Closed kliko9 closed 1 year ago

kliko9 commented 2 years ago

Let's consider example based on code generated by grpc that adds 3 additional fields state, sizeCache and unknownFields to every generated structure. In case when I've defined interface that will translate multiple structures I have to repeat myself on every structure to ignore those fields and additionally matchIgnoreCase. It would be cool to be able to define ignore and matchIgnoreCase at the level of interface so that every method has them in common defined.

Instead of:

type Transformer interface {
 //goverter:matchIgnoreCase
 //goverter:ignore state
 //goverter:ignore sizeCache
 //goverter:ignore unknownFields
 Value(in.Struct1) out.Struct1
 //goverter:matchIgnoreCase
 //goverter:ignore state
 //goverter:ignore sizeCache
 //goverter:ignore unknownFields
 Value(in.Struct2) out.Struct2 
 //goverter:matchIgnoreCase
 //goverter:ignore state
 //goverter:ignore sizeCache
 //goverter:ignore unknownFields
 Value(in.Struct3) out.Struct3
}

do this:

//goverter:matchIgnoreCase
//goverter:ignore state
//goverter:ignore sizeCache
//goverter:ignore unknownFields
type Transformer interface {
 Value(in.Struct1) out.Struct1
 Value(in.Struct2) out.Struct2
 Value(in.Struct3) out.Struct3
}
jmattheis commented 2 years ago

I agree, it should be possible to set these options globally.

jmattheis commented 1 year ago

When you run goverter, you can pass -ignoreUnexportedFields, to automatically ignore all unexported fields. I feel like globally ignoring exported fields should be a rare use-case, and can possibly lead to unwanted behavior, thus, I'll close this issue.