-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support ignoring non-exported fields #81
Comments
Then mark them as optional with field tags? |
@xobotyi Sure. What I am asking here is that default would be for lowercase fields to be optional by default. |
It wont be default because
I will think about making the option to opt-out from pivate fields being mandatory |
That. I do not want to change it for others. But having this option in golangci-lint would be awesome. |
Agree with @xobotyi. I'm not convinced private fields are inherently less important for callers witin the same package. What if you have a private field that needs to always be initialized within the package, e.g. a field that must be set inside a constructor? The analyzer offers no Overall the value add seems small, and enabling this behavior is likely to make enforcement more inflexible / more complex to configure. |
Then you create a |
@mitar If you require the The key point is that there's nothing generally special about unexported fields that would suggest they shouldn't be set. Do you have some pattern in mind to motivate this special treatment? |
(I do not want to go in circles here, so feel free to stop if you feel we are doing that.) To me the motivation is really simple. I have a package with exported struct. That struct has exported (uppercase) fields. Main use case for that package and the struct is external use. This is why there are exported fields. And there is no need to initialize anything, so there is no So external users of my package can use this struct and use it only be specifying exported fields. It works for them and the package and the struct is designed that it works only with exported fields set. Private fields are just for caching/optimization/locks and are populated internally by methods of the struct as needed. I mean, Now I run this linter on the code of this package. And linter complains because there struct initialization code which uses the struct in the same way as external user would. And I would like that the linter simulates how the external user sees the struct. If I add a public field that it requires from me that I add it to all test cases for example. And if I add a private field, I in fact want that I do not add it all around the package - because I want to make sure things work without that private field being initialized, because this is the contact I have in my package for external users of the package.
I think the pattern is that if the struct is designed that it is directly usable (without If I change the struct so that there is now a private field which has to be set, and add a But you are right, it probably makes no sense that this is a package-level/linter-level setting. Probably what I am really asking for is that this should be a struct-level setting. So instead of me having to add a struct tag to all private fields, that I could mark the whole struct as having private fields optional. Maybe through some comment or something. |
That's a good point. I don't personally use many "plain-old data types" that have internal caching; those with complex state typically get constructors to enforce valid assignments, since I see the opposite case of initialization & validation requirements more often: in particular, maps and channels need to be assigned to avoid panics on writes. But the Go ecosystem probably differs from what I've written, given that the standard library usually accepts zero valued structs. It does seem like more of a struct or package-level config, or perhaps even a global one if a |
If I have private struct with some exported fields (uppercase) and some not (lowercase) I would like that this linter complains only if I forget about an exported field. While other fields can be omitted (they are designed so).
The text was updated successfully, but these errors were encountered: