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

polish some line, change setup of golangci-lint and fix tests #12

Merged
merged 1 commit into from
Apr 8, 2020

Conversation

dasrick
Copy link
Owner

@dasrick dasrick commented Apr 8, 2020

  • polish some line (hints by golangci-lint)
  • change setup of golangci-lint (cyclo)
  • fix tests

@dasrick dasrick merged commit 823a70e into master Apr 8, 2020
// bail if a completely nil section provided
if s == nil {
return fmt.Errorf("AddSection: nil MessageCardSection received")
return fmt.Errorf("func AddSection: nil MessageCardSection received")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dasrick Do you feel that all function references in error messages like this one should have the func prefix? I hadn't really thought about it until now.

Comment on lines -169 to +167
case s.StartGroup != false:
case s.Markdown != false:
case s.StartGroup:
case s.Markdown:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dasrick I completely get why this was changed. I started adding checks at the top of the switch statement and just went down from there only changing up the expected values as I went. Good catch.

Comment on lines -251 to 242
mcs.Images = append(mcs.Images, &img)

mcs.Images = append(mcs.Images, &img) // nolint:scopelint
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did I make a mistake here? I wasn't sure what the scopelint check did initially, but I looked it up (https://github.com/kyoh86/scopelint) and I see that it guards against unintentional variable reassignment/use. I'll take another look at this to make sure I've not accidentally done that here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dug further into this and I think the linting error is probably valid. What do you think of this replacement code?

index 84c8c46..be90bc5 100644
--- a/messagecard.go
+++ b/messagecard.go
@@ -239,7 +239,7 @@ func (mcs *MessageCardSection) AddFactFromKeyValue(key string, values ...string)
 // provide a photo gallery inside a MessageCard section.
 func (mcs *MessageCardSection) AddImage(sectionImage ...MessageCardSectionImage) error {

-       for _, img := range sectionImage {
+       for index, img := range sectionImage {
                if img.Image == "" {
                        return fmt.Errorf("cannot add empty image URL")
                }
@@ -248,7 +248,7 @@ func (mcs *MessageCardSection) AddImage(sectionImage ...MessageCardSectionImage)
                        return fmt.Errorf("cannot add empty image title")
                }

-               mcs.Images = append(mcs.Images, &img)
+               mcs.Images = append(mcs.Images, &sectionImage[index])

        }

Instead of referencing the loop variable we're just using the index of the range loop to pull from the values passed into the method, which I presume are more stable based on the linter's complaint.

@@ -111,5 +107,4 @@ func IsValidMessageCard(webhookMessage MessageCard) (bool, error) {
}

return true, nil

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I try to prune all extra whitespace from PRs that I submit? I tend to use it liberally, but I understand that some tooling and projects wish to eliminate it in order to condense the code.

atc0005 referenced this pull request in atc0005/go-teams-notify Apr 8, 2020
Error from `scopelint` linter:

"Using a reference for the variable on range scope `img`"

Fix:

Use index from range loop to *reach into* the slice argument
to retrieve the address of those values instead of using the
range loop variable, which likely would have been a subtle
source of bugs later on.

refs #6, #11, #12
atc0005 referenced this pull request in atc0005/go-teams-notify Apr 8, 2020
Error from `scopelint` linter:

"Using a reference for the variable on range scope `img`"

Fix:

Use index from range loop to *reach into* the slice argument
to retrieve the address of those values instead of using the
range loop variable, which likely would have been a subtle
source of bugs later on.

refs #6, #11, #12
atc0005 referenced this pull request in atc0005/go-teams-notify Apr 9, 2020
Error from `scopelint` linter:

"Using a reference for the variable on range scope `img`"

Fix:

Use index from range loop to *reach into* the slice argument
to retrieve the address of those values instead of using the
range loop variable, which likely would have been a subtle
source of bugs later on.

refs #6, #11, #12
atc0005 referenced this pull request in atc0005/go-teams-notify Apr 9, 2020
Error from `scopelint` linter:

"Using a reference for the variable on range scope `img`"

Fix:

Use index from range loop to *reach into* the slice argument
to retrieve the address of those values instead of using the
range loop variable, which likely would have been a subtle
source of bugs later on.

refs #6, #11, #12
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.

2 participants