From 27688e40e6e71b4a064a50974fc298fb8802f98e Mon Sep 17 00:00:00 2001 From: "Kirill Che." Date: Tue, 6 Feb 2024 19:36:07 +0400 Subject: [PATCH] fix: don't panic on empty field doc Fix panic when parsing an empty doc string in anonymous structure field. Ref: #2 --- inspector.go | 7 ++++--- inspector_test.go | 14 ++++++++++++++ testdata/nodocs.go | 7 +++++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 testdata/nodocs.go diff --git a/inspector.go b/inspector.go index 394c458..d60631e 100644 --- a/inspector.go +++ b/inspector.go @@ -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{ diff --git a/inspector_test.go b/inspector_test.go index 157bfbf..38c936a 100644 --- a/inspector_test.go +++ b/inspector_test.go @@ -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 { diff --git a/testdata/nodocs.go b/testdata/nodocs.go new file mode 100644 index 0000000..0694d4e --- /dev/null +++ b/testdata/nodocs.go @@ -0,0 +1,7 @@ +package main + +type Config struct { + Repo struct { + Conn string `env:"CONN,notEmpty"` + } `envPrefix:"REPO_"` +}