From abb1822509990b7b1583db40a8e4647de2232af5 Mon Sep 17 00:00:00 2001 From: "Kirill Che." Date: Wed, 10 Apr 2024 23:50:48 +0400 Subject: [PATCH] fix: proccess unexported type nodes Add flag to process unexported types with AST visitor. Fix: #18 --- _examples/unexported.go | 8 ++++++++ _examples/unexported.md | 6 ++++++ ast.go | 8 +++++++- inspector_test.go | 11 +++++++++++ testdata/unexported.go | 6 ++++++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 _examples/unexported.go create mode 100644 _examples/unexported.md create mode 100644 testdata/unexported.go diff --git a/_examples/unexported.go b/_examples/unexported.go new file mode 100644 index 0000000..4b68c6b --- /dev/null +++ b/_examples/unexported.go @@ -0,0 +1,8 @@ +package main + +//go:generate go run ../ -output unexported.md +type appconfig struct { + // Port the application will listen on inside the container + Port int `env:"PORT" envDefault:"8080"` + // some more stuff I omitted here +} diff --git a/_examples/unexported.md b/_examples/unexported.md new file mode 100644 index 0000000..7799201 --- /dev/null +++ b/_examples/unexported.md @@ -0,0 +1,6 @@ +# Environment Variables + +## appconfig + + - `PORT` (default: `8080`) - Port the application will listen on inside the container + diff --git a/ast.go b/ast.go index 1234055..8c00e4c 100644 --- a/ast.go +++ b/ast.go @@ -79,6 +79,12 @@ func (v *astVisitor) Walk(n ast.Node) { } func (v *astVisitor) Visit(n ast.Node) ast.Visitor { + if n == nil { + return nil + } + + v.logger.Printf("ast(%d): visit node (%T)", v.depth, n) + if v.currentNode == nil { v.currentNode = &visitorNode{kind: nodeRoot} } @@ -211,7 +217,7 @@ func fieldNamesToStr(f *ast.Field) []string { } func newASTTypeDocResolver(fileSet *token.FileSet, astFile *ast.File) (func(t *ast.TypeSpec) string, error) { - docs, err := doc.NewFromFiles(fileSet, []*ast.File{astFile}, "./", doc.PreserveAST) + docs, err := doc.NewFromFiles(fileSet, []*ast.File{astFile}, "./", doc.PreserveAST|doc.AllDecls) if err != nil { return nil, fmt.Errorf("extract package docs: %w", err) } diff --git a/inspector_test.go b/inspector_test.go index b68a92b..f43af96 100644 --- a/inspector_test.go +++ b/inspector_test.go @@ -357,6 +357,17 @@ func TestInspector(t *testing.T) { }, }, }, + { + name: "unexported.go", + typeName: "appconfig", + expect: []*EnvDocItem{ + { + Name: "PORT", + Doc: "Port the application will listen on inside the container", + Opts: EnvVarOptions{Default: "8080"}, + }, + }, + }, } { scopes := c.expectScopes if scopes == nil { diff --git a/testdata/unexported.go b/testdata/unexported.go new file mode 100644 index 0000000..e3967f8 --- /dev/null +++ b/testdata/unexported.go @@ -0,0 +1,6 @@ +package testdata + +type appconfig struct { + // Port the application will listen on inside the container + Port int `env:"PORT" envDefault:"8080"` +}