Skip to content

Commit

Permalink
fix: don't panic on empty field doc
Browse files Browse the repository at this point in the history
Fix panic when parsing an empty doc string in anonymous structure field.

Ref: #2
  • Loading branch information
g4s8 committed Feb 6, 2024
1 parent 445e54b commit 27688e4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
7 changes: 4 additions & 3 deletions inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,12 @@ func (i *inspector) parseField(f *ast.Field) (out []envField) {
item.typeRef = fieldType.Name
case *ast.StructType:
nameGen := fastRandString(16)
i.getStruct(&ast.TypeSpec{
typeSpec := &ast.TypeSpec{
Name: &ast.Ident{Name: nameGen},
Type: fieldType,
Doc: &ast.CommentGroup{List: f.Doc.List},
})
Doc: f.Doc,
}
i.getStruct(typeSpec)
item.typeRef = nameGen
posRange := [2]token.Pos{fieldType.Pos(), fieldType.End()}
i.anonymousStructs[posRange] = anonymousStruct{
Expand Down
14 changes: 14 additions & 0 deletions inspector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,20 @@ func TestInspector(t *testing.T) {
},
},
},
{
name: "nodocs.go",
typeName: "Config",
expect: []EnvDocItem{
{
Children: []EnvDocItem{
{
Name: "REPO_CONN",
Opts: EnvVarOptions{Required: true, NonEmpty: true},
},
},
},
},
},
} {
scopes := c.expectScopes
if scopes == nil {
Expand Down
7 changes: 7 additions & 0 deletions testdata/nodocs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

type Config struct {
Repo struct {
Conn string `env:"CONN,notEmpty"`
} `envPrefix:"REPO_"`
}

0 comments on commit 27688e4

Please sign in to comment.