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

Add linting with golangci-lint #722

Merged
merged 16 commits into from Nov 23, 2022

Conversation

jamietanna
Copy link
Collaborator

@jamietanna jamietanna commented Aug 16, 2022

No description provided.

@jamietanna
Copy link
Collaborator Author

@deepmap-marcinr mind having a look? I need to clean up the commit history (hence this still being a draft) but would be good to get this through to start giving us a bit more control + confidence in our code style, as well as closing #58.

@@ -122,12 +122,12 @@ func ValidateRequestFromContext(c *gin.Context, router routers.Router, options *

// Pass the gin context into the request validator, so that any callbacks
// which it invokes make it available.
requestContext := context.WithValue(context.Background(), GinContextKey, c)
requestContext := context.WithValue(context.Background(), GinContextKey, c) //nolint:staticcheck
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

SA1029: should not use built-in type string as key for value; define your own type to avoid collisions (staticcheck)

@@ -99,7 +99,7 @@ func MarshalForm(ptr interface{}, encodings map[string]RequestBodyEncoding) (url
tag = strings.Split(tag, ",")[0] // extract the name of the tag
if encoding, ok := encodings[tag]; ok && encoding.ContentType != "" {
if strings.HasPrefix(encoding.ContentType, jsonContentType) {
if data, err := json.Marshal(field); err != nil {
if data, err := json.Marshal(field); err != nil { //nolint:staticcheck
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

SA9005: struct type 'reflect.Value' doesn't have any exported fields, nor custom marshaling

@@ -242,7 +242,7 @@ func assignPathValues(dst interface{}, pathValues fieldOrValue) error {
tm, err = time.Parse(time.RFC3339Nano, pathValues.value)
if err != nil {
// Fall back to parsing it as a date.
tm, err = time.Parse(types.DateFormat, pathValues.value)
tm, err = time.Parse(types.DateFormat, pathValues.value) //nolint:ineffassign,staticcheck
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ineffectual assignment to tm (ineffassign)

@deepmap-marcinr
Copy link
Contributor

I rebased your branch, and fixed up a bunch of merge conflicts, however, you're making breaking API changes due to initialisms - people depending on the testutils or runtime packages will see code break. Our hands are kinda tied there until a breaking release.

@jamietanna jamietanna force-pushed the chore/golangci-lint branch 2 times, most recently from 1c3f13d to 677718e Compare October 30, 2022 08:31
@jamietanna
Copy link
Collaborator Author

I rebased your branch, and fixed up a bunch of merge conflicts, however, you're making breaking API changes due to initialisms - people depending on the testutils or runtime packages will see code break. Our hands are kinda tied there until a breaking release.

Ah that's fair, I'll ignore the initialisms check, for now then 👍

@stevenh
Copy link
Contributor

stevenh commented Nov 11, 2022

Hi @jamietanna I added golangci-lint separately in #845 see if it does what you need it to?

Jamie Tanna added 9 commits November 12, 2022 21:04
Right now, we can ignore the return values, as we're hardcoding the
response of an `err` to always return, which isn't ideal.
As we've added them on purpose, to show that they don't get
(un)marshalled.
As it's probably a bit late to change this, in case it changes expected
behaviour, via SA1029.
Jamie Tanna added 5 commits November 12, 2022 21:06
Or, least show that we've considered whether to ignore them, which we do
to follow existing style of the file to ignore everything.
As we'll come back to it later.

pkg/runtime/deepobject.go:245:5: ineffectual assignment to tm (ineffassign)
                                tm, err = time.Parse(types.DateFormat, pathValues.value)
Jamie Tanna added 2 commits November 12, 2022 21:06
pkg/runtime/bindform.go:102:34: SA9005: struct type 'reflect.Value' doesn't have any exported fields, nor custom marshaling (staticcheck)
                                if data, err := json.Marshal(field); err != nil {
To give a bit more consistency across the project with the Go
ecosystem, as well as picking up on some common errors that new
contributions can bring, enable golangci-lint, with default
configuration.
Copy link
Contributor

@stevenh stevenh left a comment

Choose a reason for hiding this comment

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

Few nits but the the main issue is just using _ = function() to quell error not checked, we should never do that.

@@ -26,7 +26,8 @@ func TestClient_WhenPathHasColon_RequestHasCorrectPath(t *testing.T) {
assert.Equal(t, "http://host/pets:validate", req.URL.String())
})

client.ValidatePetsWithResponse(context.Background(), ValidatePetsJSONRequestBody{
// TODO: this err should be checked, but it defaults to returning the "something went wrong", so we should refactor accordingly
Copy link
Contributor

Choose a reason for hiding this comment

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

We should just check the errors, its easy enough, also check the errors for NewClient above

@@ -28,14 +28,14 @@ func walkSwagger(swagger *openapi3.T, doFn func(RefWrapper) (bool, error)) error

for _, p := range swagger.Paths {
for _, param := range p.Parameters {
walkParameterRef(param, doFn)
_ = walkParameterRef(param, doFn)
Copy link
Contributor

@stevenh stevenh Nov 12, 2022

Choose a reason for hiding this comment

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

You should never assign to _ just to silence a lint error, either check the error if its genuine or add a //nolint: errcheck comment to the line when its not such as a .Close() on a io.ReadCloser(), lots more below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Although yes, I 100% agree, the file so far has a lot of these, and I'd prefer us to come back and sort them

Copy link
Contributor

Choose a reason for hiding this comment

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

I see a two options:

  1. Disable errcheck linter in .golangci-lint.yaml for now.
  2. Use ci: enable golangci-lint and fix errors #845 which already fixes all of these.

@jamietanna jamietanna merged commit 40890b2 into deepmap:master Nov 23, 2022
@jamietanna jamietanna deleted the chore/golangci-lint branch November 23, 2022 09:09
@stevenh
Copy link
Contributor

stevenh commented Nov 23, 2022

Great to see this merged!

I've rebased my golangci-lint changes on top of it, hopefully we can get that in too.

adrianpk pushed a commit to foorester/oapi-codegen that referenced this pull request Jan 16, 2024
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.

None yet

3 participants