@@ -66,65 +66,69 @@ func (i *inspector) InspectFields(inspectField func(field *ast.Field, stack []as
6666 return false
6767 }
6868
69- if len (stack ) < 3 {
70- return true
69+ field , ok := n .(* ast.Field )
70+ if ! ok || ! i .shouldProcessField (stack ) {
71+ return ok
7172 }
7273
73- // The 0th node in the stack is the *ast.File.
74- // The 1st node in the stack is the *ast.GenDecl.
75- decl , ok := stack [1 ].(* ast.GenDecl )
76- if ! ok {
77- // Make sure that we don't inspect structs within a function.
74+ if i .shouldSkipField (field ) {
7875 return false
7976 }
8077
81- if decl .Tok != token .TYPE {
82- // Returning false here means we won't inspect non-type declarations (e.g. var, const, import).
83- return false
84- }
78+ i .processFieldWithRecovery (field , stack , inspectField )
8579
86- structType , ok := stack [len (stack )- 3 ].(* ast.StructType )
87- if ! ok {
88- // A field within a struct has a FieldList parent and then a StructType parent.
89- // If we don't have a StructType parent, then we're not in a struct.
90- return false
91- }
80+ return true
81+ })
82+ }
9283
93- if isItemsType (structType ) {
94- // The field belongs to an items type, we don't need to report lint errors for this.
95- return false
96- }
84+ // shouldProcessField checks if the field should be processed.
85+ func (i * inspector ) shouldProcessField (stack []ast.Node ) bool {
86+ if len (stack ) < 3 {
87+ return false
88+ }
9789
98- field , ok := n .(* ast.Field )
99- if ! ok {
100- return true
101- }
90+ // The 0th node in the stack is the *ast.File.
91+ // The 1st node in the stack is the *ast.GenDecl.
92+ decl , ok := stack [1 ].(* ast.GenDecl )
93+ if ! ok || decl .Tok != token .TYPE {
94+ // Make sure that we don't inspect structs within a function or non-type declarations.
95+ return false
96+ }
10297
103- tagInfo := i . jsonTags . FieldTags ( field )
104- if tagInfo . Ignored {
105- // Returning false here means we won't inspect the children of an ignored field .
106- return false
107- }
98+ structType , ok := stack [ len ( stack ) - 3 ].( * ast. StructType )
99+ if ! ok || isItemsType ( structType ) {
100+ // Not in a struct or belongs to an items type .
101+ return false
102+ }
108103
109- markerSet := i .markers .FieldMarkers (field )
104+ return true
105+ }
110106
111- if isSchemalessType (markerSet ) {
112- // If the field is marked as schemaless, we don't need to inspect it.
113- return false
114- }
107+ // shouldSkipField checks if a field should be skipped.
108+ func (i * inspector ) shouldSkipField (field * ast.Field ) bool {
109+ tagInfo := i .jsonTags .FieldTags (field )
110+ if tagInfo .Ignored {
111+ return true
112+ }
115113
116- defer func () {
117- if r := recover (); r != nil {
118- // If the inspectField function panics, we recover and log information that will help identify the issue.
119- debug := printDebugInfo (field )
120- panic (fmt .Sprintf ("%s %v" , debug , r )) // Re-panic to propagate the error.
121- }
122- }()
114+ markerSet := i .markers .FieldMarkers (field )
123115
124- inspectField (field , stack , tagInfo , i .markers )
116+ return isSchemalessType (markerSet )
117+ }
125118
126- return true
127- })
119+ // processFieldWithRecovery processes a field with panic recovery.
120+ func (i * inspector ) processFieldWithRecovery (field * ast.Field , stack []ast.Node , inspectField func (field * ast.Field , stack []ast.Node , jsonTagInfo extractjsontags.FieldTagInfo , markersAccess markers.Markers )) {
121+ tagInfo := i .jsonTags .FieldTags (field )
122+
123+ defer func () {
124+ if r := recover (); r != nil {
125+ // If the inspectField function panics, we recover and log information that will help identify the issue.
126+ debug := printDebugInfo (field )
127+ panic (fmt .Sprintf ("%s %v" , debug , r )) // Re-panic to propagate the error.
128+ }
129+ }()
130+
131+ inspectField (field , stack , tagInfo , i .markers )
128132}
129133
130134// InspectTypeSpec inspects the type spec and calls the provided inspectTypeSpec function.
0 commit comments