-
Notifications
You must be signed in to change notification settings - Fork 289
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
adds check for receiver names length #1048
adds check for receiver names length #1048
Conversation
rule/receiver-naming.go
Outdated
func (r *ReceiverNamingRule) configure(arguments lint.Arguments) { | ||
r.Lock() | ||
defer r.Unlock() | ||
if r.receiverNameMaxLength == 0 { | ||
if len(arguments) < 1 { | ||
r.receiverNameMaxLength = defaultReceiverNameMaxLength | ||
return | ||
} | ||
arg := arguments[0] | ||
argStr, ok := arg.(string) | ||
if !ok { | ||
panic(fmt.Sprintf("Invalid argument for %s rule. Expecting an string, got %T", r.Name(), arg)) | ||
} | ||
|
||
parts := strings.Split(argStr, "=") | ||
if len(parts) != 2 { | ||
panic(fmt.Sprintf("Invalid argument for %s rule. Expecting an string of the form 'key=value', got %s", r.Name(), argStr)) | ||
} | ||
|
||
key := strings.TrimSpace(parts[0]) | ||
value := strings.TrimSpace(parts[1]) | ||
switch key { | ||
case "max-length": | ||
var err error | ||
r.receiverNameMaxLength, err = strconv.Atoi(value) | ||
if err != nil { | ||
panic(fmt.Sprintf("Invalid value %s for the configuration key max-length, expected integer value: %v", value, err)) | ||
} | ||
default: | ||
panic(fmt.Sprintf("Unknown configuration key %s for %s rule.", key, r.Name())) | ||
} | ||
} | ||
} |
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.
That's a function wide if
statement
I would suggest this instead
func (r *ReceiverNamingRule) configure(arguments lint.Arguments) { | |
r.Lock() | |
defer r.Unlock() | |
if r.receiverNameMaxLength == 0 { | |
if len(arguments) < 1 { | |
r.receiverNameMaxLength = defaultReceiverNameMaxLength | |
return | |
} | |
arg := arguments[0] | |
argStr, ok := arg.(string) | |
if !ok { | |
panic(fmt.Sprintf("Invalid argument for %s rule. Expecting an string, got %T", r.Name(), arg)) | |
} | |
parts := strings.Split(argStr, "=") | |
if len(parts) != 2 { | |
panic(fmt.Sprintf("Invalid argument for %s rule. Expecting an string of the form 'key=value', got %s", r.Name(), argStr)) | |
} | |
key := strings.TrimSpace(parts[0]) | |
value := strings.TrimSpace(parts[1]) | |
switch key { | |
case "max-length": | |
var err error | |
r.receiverNameMaxLength, err = strconv.Atoi(value) | |
if err != nil { | |
panic(fmt.Sprintf("Invalid value %s for the configuration key max-length, expected integer value: %v", value, err)) | |
} | |
default: | |
panic(fmt.Sprintf("Unknown configuration key %s for %s rule.", key, r.Name())) | |
} | |
} | |
} | |
func (r *ReceiverNamingRule) configure(arguments lint.Arguments) { | |
r.Lock() | |
defer r.Unlock() | |
if r.receiverNameMaxLength > 0 { | |
return | |
} | |
if len(arguments) < 1 { | |
r.receiverNameMaxLength = defaultReceiverNameMaxLength | |
return | |
} | |
arg := arguments[0] | |
argStr, ok := arg.(string) | |
if !ok { | |
panic(fmt.Sprintf("Invalid argument for %s rule. Expecting a string, got %T", r.Name(), arg)) | |
} | |
parts := strings.Split(argStr, "=") | |
if len(parts) != 2 { | |
panic(fmt.Sprintf("Invalid argument for %s rule. Expecting a string of the form 'key=value', got %s", r.Name(), argStr)) | |
} | |
key := strings.TrimSpace(parts[0]) | |
value := strings.TrimSpace(parts[1]) | |
switch key { | |
case "max-length": | |
var err error | |
r.receiverNameMaxLength, err = strconv.Atoi(value) | |
if err != nil { | |
panic(fmt.Sprintf("Invalid value %s for the configuration key max-length, expected integer value: %v", value, err)) | |
} | |
default: | |
panic(fmt.Sprintf("Unknown configuration key %s for %s rule.", key, r.Name())) | |
} | |
} |
Also I fixed the typo an string
twice
RULES_DESCRIPTIONS.md
Outdated
|
||
```toml | ||
[rule.receiver-naming] | ||
arguments=["max-length=2"] |
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 did you decide to have this way of configuration? This seems to be different comparing to what we have in the other rules. We either have ints directly or we have named args as in unchecked-type-assertion.
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.
LGTM
Co-authored-by: yuta nishiyama <[email protected]>
Closes #1040
The rule receiver-naming now accepts an optional argument indicating the maximum length of receiver names. If no argument is provided the rule does not check for names length.