Skip to content
Merged
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
13 changes: 10 additions & 3 deletions pkg/analysis/helpers/extractjsontags/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,16 @@ func run(pass *analysis.Pass) (any, error) {
return
}

results.insertFieldTagInfo(field, extractTagInfo(field.Tag))
results.insertFieldTagInfo(field, extractTagInfo(field, field.Tag))
})

return results, nil
}

const emptyJSONTagPrefix = `json:"`

//nolint:cyclop
func extractTagInfo(tag *ast.BasicLit) FieldTagInfo {
func extractTagInfo(field *ast.Field, tag *ast.BasicLit) FieldTagInfo {
if tag == nil || tag.Value == "" {
return FieldTagInfo{Missing: true}
}
Expand All @@ -115,6 +117,11 @@ func extractTagInfo(tag *ast.BasicLit) FieldTagInfo {
}

if tagValue == "" {
if field.Names == nil { // Embedded field with `json:""`
pos := tag.Pos() + token.Pos(strings.Index(tag.Value, emptyJSONTagPrefix)+len(emptyJSONTagPrefix))
return FieldTagInfo{Inline: true, RawValue: "", Pos: pos, End: pos + token.Pos(1)}
}

return FieldTagInfo{}
}

Expand Down Expand Up @@ -169,7 +176,7 @@ type FieldTagInfo struct {
// OmitZero is true if the field has the omitzero option in the json tag.
OmitZero bool

// Inline is true if the field has the inline option in the json tag.
// Inline is true if the json tag is ",inline", or if the field is embedded and the json tag is "".
Inline bool

// Missing is true when the field had no json tag.
Expand Down
12 changes: 12 additions & 0 deletions pkg/analysis/nonpointerstructs/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type A struct {
WithOptionalField `json:",inline"`
WithRequiredAndOptionalField `json:",inline"`
WithOptionalFieldsAndMinProperties `json:",inline"`
WithRequiredFieldEmbedded `json:""`
WithOptionalFieldEmbedded `json:""`
}

type WithRequiredField struct {
Expand All @@ -69,3 +71,13 @@ type WithOptionalFieldsAndMinProperties struct {
// +k8s:optional
OptionalField string `json:"optionalField"`
}

type WithRequiredFieldEmbedded struct {
// +required
RequiredField string `json:"requiredField"`
}

type WithOptionalFieldEmbedded struct {
// +optional
OptionalField string `json:"optionalField"`
}
12 changes: 12 additions & 0 deletions pkg/analysis/nonpointerstructs/testdata/src/a/a.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type A struct {
WithOptionalField `json:",inline"`
WithRequiredAndOptionalField `json:",inline"`
WithOptionalFieldsAndMinProperties `json:",inline"`
WithRequiredFieldEmbedded `json:""`
WithOptionalFieldEmbedded `json:""`
}

type WithRequiredField struct {
Expand All @@ -73,3 +75,13 @@ type WithOptionalFieldsAndMinProperties struct {
// +k8s:optional
OptionalField string `json:"optionalField"`
}

type WithRequiredFieldEmbedded struct {
// +required
RequiredField string `json:"requiredField"`
}

type WithOptionalFieldEmbedded struct {
// +optional
OptionalField string `json:"optionalField"`
}
4 changes: 4 additions & 0 deletions pkg/analysis/optionalorrequired/testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ type OptionalOrRequiredTestStruct struct {

// +optional
C `json:"c,omitempty"`

Embedded `json:""`
}

type A struct{}
Expand All @@ -102,3 +104,5 @@ type C struct{}
type Interface interface {
InaccessibleFunction() string
}

type Embedded struct{}
4 changes: 4 additions & 0 deletions pkg/analysis/optionalorrequired/testdata/src/a/a.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ type OptionalOrRequiredTestStruct struct {

// +optional
C `json:"c,omitempty"`

Embedded `json:""`
}

type A struct{}
Expand All @@ -97,3 +99,5 @@ type C struct{}
type Interface interface {
InaccessibleFunction() string
}

type Embedded struct{}
Loading