How embedded fields and promoted names work
masterSince Go 1.27, composite literals can name promoted fields directly. exhaustruct follows this behavior:
- Promoted names: If a promoted name reaches a field, that field is reported individually.
- Unreachable promoted names: If a promoted name does not reach a field, the entire embedded field is reported as a single missing unit.
- Directive Priority: A directive like
//exhaustruct:enforceon a field inside an embedded struct outranks the directive on the parent struct. - Optional Embedded Fields: If an embedded field is marked
//exhaustruct:optional,exhaustructdoes not descend into it, meaning its promoted fields are also treated as optional.
type Inner struct {
ID string
//exhaustruct:enforce
Must string
}
//exhaustruct:optional
type Holder struct {
Inner
Port int
}
//exhaustruct:optional
type Own struct {
//exhaustruct:optional
Inner
Port int
}
func example() {
_ = Holder{Port: 8080} // missing field Must
_ = Own{Port: 8080} // OK: Inner is optional in its own right
}