Skip to content
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

Merged
merged 3 commits into from
Sep 23, 2024

Conversation

chavacava
Copy link
Collaborator

@chavacava chavacava commented Sep 22, 2024

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.

Comment on lines 22 to 54
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()))
}
}
}
Copy link
Contributor

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

Suggested change
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


```toml
[rule.receiver-naming]
arguments=["max-length=2"]
Copy link
Collaborator

@denisvmedia denisvmedia Sep 23, 2024

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.

Copy link
Collaborator

@denisvmedia denisvmedia left a 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]>
@chavacava chavacava merged commit 53a111d into mgechev:master Sep 23, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add the receiver name length check to rule.receiver-naming
4 participants