-
Notifications
You must be signed in to change notification settings - Fork 15
linters: add forbiddenmarkers and nonullable linters #111
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
base: main
Are you sure you want to change the base?
linters: add forbiddenmarkers and nonullable linters #111
Conversation
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: everettraven The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
I'll need to add the linter documentation to the new location as well :P /hold |
const name = "forbiddenmarkers" | ||
|
||
type analyzer struct { | ||
forbiddenMarkers []string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if I wanted to forbid a marker only if it had a certain parameter? Or a certain value?
E.g. I want to prevent someone from using a particular kubebuilder:validation:Format
, would this allow me to do that?
Or I wanted to say, you can't use the optionalOldSelf
property on an XValidation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good questions. This would not allow that for now. I think those are reasonable use cases and make sense to consider.
I can add some configuration options to allow for this type of customization.
pkg/analysis/nonullable/analyzer.go
Outdated
analyzer.Name = name | ||
analyzer.Doc = "Check that nullable marker is not present on any types or fields." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe create a constructor on the forbidden markers side that allows you to pass options for Name and Doc, rather than overriding them here? Then this just becomes
return NewAnalyzerWithOpts(forbidden.Opts{
Name: ...
Doc: ...
Markers: []string{...}
})
Or even, you could just call that inside the initializer.
* The `nonullable` linter ensures that types and fields do not have the `nullable` marker. | ||
* | ||
* Fixes are suggested to remove the `nullable` marker. | ||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the asterisks at the beginning of each line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copied format from another linter doc.go - but also I think my editor does this by default. I can strip the asterisks at the beginning of each line if necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't matter to me, but normally with /* */
syntax I didn't think it was needed
// Initializer returns the AnalyzerInitializer for this | ||
// Analyzer so that it can be added to the registry. | ||
func Initializer() initializer { | ||
return initializer{} | ||
} | ||
|
||
// intializer implements the AnalyzerInitializer interface. | ||
type initializer struct{} | ||
|
||
// Name returns the name of the Analyzer. | ||
func (initializer) Name() string { | ||
return name | ||
} | ||
|
||
// Init returns the intialized Analyzer. | ||
func (initializer) Init(_ config.LintersConfig) (*analysis.Analyzer, error) { | ||
return newAnalyzer(), nil | ||
} | ||
|
||
// Default determines whether this Analyzer is on by default, or not. | ||
func (initializer) Default() bool { | ||
return true | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm starting to wonder if we should create a util to generate the initializer from a func 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that idea, but to keep the scope of this PR tightened to these two linters I'd suggest that be a follow up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now implemented, PR will need a rebase
Working on allowing configuration of forbidden attributes and values. /hold |
36bf9d5
to
5d01549
Compare
Signed-off-by: Bryce Palmer <[email protected]>
Signed-off-by: Bryce Palmer <[email protected]>
Signed-off-by: Bryce Palmer <[email protected]>
Signed-off-by: Bryce Palmer <[email protected]>
5d01549
to
4652204
Compare
This is mostly ready to go, just have to fix linting issues and squash commits. |
return field.ErrorList{field.Required(fldPath, "configuration is required for the forbiddenmarkers linter when it is enabled")} | ||
} | ||
|
||
fieldErrors := field.ErrorList{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation of validation missing?
} | ||
|
||
func validateMarkers(fldPath *field.Path, markers ...Marker) field.ErrorList { | ||
childPath := fldPath.Child("markers") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally the child element is called by the parent, rather than here
|
||
for i, marker := range markers { | ||
indexPath := childPath.Index(i) | ||
if knownMarkers.Has(marker.Identifier) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if I wanted to say identifier foo is allow, unless it has attribute A and B, or A and C? Are we sure the identifiers must be unique? Or should they be unique only when the attributes match?
For example, what if I wanted to forbid Blue Apples, or Green Oranges, but allow Red Apples and Orange Oranges? Would that be configurable today?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. I'd have to think through potential scenarios a bit further,but giving this a quick analysis.
In this example we essentially want the markers +custom:marker:fruit=apple,color=blue
and +custom:marker:fruit=orange,color=green
to be invalid. I think boils down to a configuration of:
markers:
- identifier: "custom:marker"
attributes: # entries join as an AND operation
- name: fruit
values: # OR operation
- apple
- orange
- name: color
values: # OR operation
- blue
- green
This basically becomes a boolean conditional chain of (fruit == apple || fruit == orange) && (color == blue || color == green)
which means that it would successfully work for the exact constraints of your example, but would also forbid Green Apples and Blue Oranges.
I could imagine a case where you may want to forbid something like Blue Apples and Green Oranges but still want to allow something like Green Apples as valid.
Maybe doing something like a "rule set" that does an OR (basically a configured set of the existing ANDed attribute rules) would allow for that behavior while keeping all configuration under a unique identifier.
For example, to handle the case of forbidding Blue Apples and Green Oranges, but allowing Green Apples you might be able to do something like:
markers:
- identifier: "custom:marker"
ruleSets: # OR operation
- attributes: # entries join as an AND operation
- name: fruit
values: # OR operation
- apple
- name: color
values: # OR operation
- blue
- attributes: # entries join as an AND operation
- name: fruit
values: # OR operation
- orange
- name: color
values: # OR operation
- green
Which would essentially become a conditional of (fruit == apple && color == blue) || (fruit == orange && color == green)
which would work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think the ruleset idea could work, shall we give that a go?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'll move this in that direction. I probably won't have time to do this until later this week though
Adds a disabled by default
forbiddenmarkers
linter that has configuration options to specify marker identifiers that should never exist on a type/field. Suggests fixes to remove the forbidden markers.Adds an enabled by default
nonullable
linter that wraps theforbiddenmarkers
linter and configures thenullable
marker to be forbidden.Fixes #100