Skip to content

Check whether go structs contain fields that prevent promotion of fields of embedded structs

License

Notifications You must be signed in to change notification settings

the4thamigo-uk/prmchk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

prmchk

Check if a struct contains a field that overwrites a promoted field from an embedded struct

In go, fields from embedded structs are promoted to the containing struct, as if they were declared in the containing struct. However, if you declare a field in the containing struct which has the same name, then promotion is suppressed.

Sometimes, we have structs for which we never want to suppress field promotion, but it is easy to accidentally do this, especially if the embedding is deeply nested or there are many fields.

This library provides functions to check whether a struct type contains any fields that suppress promotion of a field of an embedded type.

The idea is to put these checks in your unit tests for structs for which you want to enforce this constraint. The alternative is to create a linter, but it was thought that if you are designing a struct using this rule, then it is likely to be a bug if the rule is broken, so this check ought to operate at level of testing, rather than linting.

See the unit tests for usage.

TODO:

Support embedded struct pointers

About

Check whether go structs contain fields that prevent promotion of fields of embedded structs

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages