Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2567,6 +2567,7 @@ linters:
- noctx
- nolintlint
- nonamedreturns
- noniljson
- nosnakecase
- nosprintfhostport
- paralleltest
Expand Down Expand Up @@ -2690,6 +2691,7 @@ linters:
- noctx
- nolintlint
- nonamedreturns
- noniljson
- nosnakecase
- nosprintfhostport
- paralleltest
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ require (
github.com/nunnatsa/ginkgolinter v0.15.2
github.com/polyfloyd/go-errorlint v1.4.8
github.com/quasilyte/go-ruleguard/dsl v0.3.22
github.com/rezkam/noniljson v1.0.2
github.com/ryancurrah/gomodguard v1.3.0
github.com/ryanrolds/sqlclosecheck v0.5.1
github.com/sanposhiho/wastedassign/v2 v2.0.7
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions pkg/golinters/noniljson.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package golinters

import (
"github.com/rezkam/noniljson"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewNoNilJSON() *goanalysis.Linter {
a := noniljson.Analyzer

return goanalysis.NewLinter(
a.Name,
"checks that nullable fields in structs used for JSON marshaling use 'omitempty'",
[]*analysis.Analyzer{a},
nil,
)
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithURL("https://github.com/firefart/nonamedreturns"),

linter.NewConfig(golinters.NewNoNilJSON()).
WithSince("v1.57.0").
WithLoadForGoAnalysis().
WithPresets(linter.PresetBugs).
WithURL("https://github.com/rezkam/noniljson"),

linter.NewConfig(golinters.NewNoSnakeCase()).
WithSince("v1.47.0").
WithPresets(linter.PresetStyle).
Expand Down
22 changes: 22 additions & 0 deletions test/testdata/noniljson.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//golangcitest:args -Enoniljson
package testdata

type userData struct {
Name *string `json:"name"` // want `nullable field 'Name' in struct 'userData' must include 'omitempty' in its json tag to avoid marshaling as null`
Email string `json:"email,omitempty"`
Address *string `json:"address,omitempty"`
Friends []string `json:"friends"` // want `nullable field 'Friends' in struct 'userData' must include 'omitempty' in its json tag to avoid marshaling as null`
Metadata map[string]string `json:"metadata"` // want `nullable field 'Metadata' in struct 'userData' must include 'omitempty' in its json tag to avoid marshaling as null`
}

type dynamicData struct {
Data interface{} `json:"data"` // want `nullable field 'Data' in struct 'dynamicData' must include 'omitempty' in its json tag to avoid marshaling as null`
}

type productData struct {
ID int `json:"id"`
Description *string `json:"description"` // want `nullable field 'Description' in struct 'productData' must include 'omitempty' in its json tag to avoid marshaling as null`
Price float64 `json:"price,omitempty"`
Availability []int `json:"availability"` // want `nullable field 'Availability' in struct 'productData' must include 'omitempty' in its json tag to avoid marshaling as null`
Attributes map[string]interface{} `json:"attributes"` // want `nullable field 'Attributes' in struct 'productData' must include 'omitempty' in its json tag to avoid marshaling as null`
}