Skip to content

Commit 13b7bbd

Browse files
committed
fix: ignore fields of json:"-"
1 parent b94e5ed commit 13b7bbd

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

pkg/analysis/commentstart/testdata/src/a/a.go

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ type CommentStartTestStruct struct {
55
EmptyJSONTag string `json:""`
66
InlineJSONTag string `json:",inline"`
77
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
8+
Ignored string `json:"-"`
9+
Hyphen string `json:"-,"` // want "field Hyphen is missing godoc comment"
810

911
// IncorrectStartComment is a field with an incorrect start to the comment. // want "godoc for field IncorrectStartComment should start with 'incorrectStartComment ...'"
1012
IncorrectStartComment string `json:"incorrectStartComment"`

pkg/analysis/commentstart/testdata/src/a/a.go.golden

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ type CommentStartTestStruct struct {
55
EmptyJSONTag string `json:""`
66
InlineJSONTag string `json:",inline"`
77
NoComment string `json:"noComment"` // want "field NoComment is missing godoc comment"
8+
Ignored string `json:"-"`
9+
Hyphen string `json:"-,"` // want "field Hyphen is missing godoc comment"
810

911
// incorrectStartComment is a field with an incorrect start to the comment. // want "godoc for field IncorrectStartComment should start with 'incorrectStartComment ...'"
1012
IncorrectStartComment string `json:"incorrectStartComment"`

pkg/analysis/helpers/extractjsontags/analyzer.go

+13
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
8181
return results, nil
8282
}
8383

84+
//nolint:cyclop
8485
func extractTagInfo(tag *ast.BasicLit) FieldTagInfo {
8586
if tag == nil || tag.Value == "" {
8687
return FieldTagInfo{Missing: true}
@@ -106,6 +107,15 @@ func extractTagInfo(tag *ast.BasicLit) FieldTagInfo {
106107

107108
tagValues := strings.Split(tagValue, ",")
108109

110+
if len(tagValues) == 1 && tagValues[0] == "-" {
111+
return FieldTagInfo{
112+
Ignored: true,
113+
RawValue: tagValue,
114+
Pos: pos,
115+
End: end,
116+
}
117+
}
118+
109119
if len(tagValues) == 2 && tagValues[0] == "" && tagValues[1] == "inline" {
110120
return FieldTagInfo{
111121
Inline: true,
@@ -132,6 +142,9 @@ type FieldTagInfo struct {
132142
// Name is the name of the field extracted from the json tag.
133143
Name string
134144

145+
// Ignored is true if the field is ignored by json package.
146+
Ignored bool
147+
135148
// OmitEmpty is true if the field has the omitempty option in the json tag.
136149
OmitEmpty bool
137150

0 commit comments

Comments
 (0)