@@ -13,8 +13,8 @@ import (
13
13
14
14
type Func struct {
15
15
FullDescriptions []string
16
- functionDescriptions []FunctionDescription
17
- testFunctionDescriptions []FunctionDescription
16
+ FunctionDescriptions []FunctionDescription
17
+ TestFunctionDescriptions []FunctionDescription
18
18
}
19
19
20
20
type FunctionDescription struct {
@@ -33,41 +33,38 @@ type Param struct {
33
33
func (f * Func ) ParseFunctions (p Param ) {
34
34
code , err := readFile (p .FilePath )
35
35
if err != nil {
36
- log .Println ("Error reading file:" , err )
36
+ log .Printf ("Error reading file %s: %v" , p . FilePath , err )
37
37
return
38
38
}
39
39
40
40
file , err := parseCode (p .FileName , code )
41
41
if err != nil {
42
- log .Println ("Error parsing file:" , err )
42
+ log .Printf ("Error parsing file %s: %v" , p . FileName , err )
43
43
return
44
44
}
45
45
46
46
description , funcDescriptions , testFuncDescriptions := buildFileDescription (p , file , code )
47
47
f .FullDescriptions = append (f .FullDescriptions , description )
48
- if funcDescriptions != nil {
49
- f .functionDescriptions = append (f .functionDescriptions , funcDescriptions ... )
50
- }
51
- if testFuncDescriptions != nil {
52
- f .testFunctionDescriptions = append (f .testFunctionDescriptions , testFuncDescriptions ... )
53
- }
48
+ f .FunctionDescriptions = append (f .FunctionDescriptions , funcDescriptions ... )
49
+ f .TestFunctionDescriptions = append (f .TestFunctionDescriptions , testFuncDescriptions ... )
54
50
}
55
51
56
52
func (f * Func ) Print () {
57
53
for _ , desc := range f .FullDescriptions {
58
54
fmt .Println (desc )
59
55
}
60
56
}
57
+
61
58
func readFile (filePath string ) (string , error ) {
62
59
codeFile , err := os .Open (filePath )
63
60
if err != nil {
64
- return "" , err
61
+ return "" , fmt . Errorf ( "failed to open file: %w" , err )
65
62
}
66
63
defer codeFile .Close ()
67
64
68
65
srcbuf , err := io .ReadAll (codeFile )
69
66
if err != nil {
70
- return "" , err
67
+ return "" , fmt . Errorf ( "failed to read file: %w" , err )
71
68
}
72
69
return string (srcbuf ), nil
73
70
}
@@ -79,49 +76,53 @@ func parseCode(fileName, code string) (*ast.File, error) {
79
76
80
77
func buildFileDescription (p Param , file * ast.File , code string ) (string , []FunctionDescription , []FunctionDescription ) {
81
78
var sb strings.Builder
82
- var funcDescriptions []FunctionDescription
83
- var testFuncDescriptions []FunctionDescription
84
- startFuncWord := fmt .Sprintf ("##Start of go file %s \n " , p .FilePath )
85
- endFuncWord := fmt .Sprintf ("----- End of go file %s ------- \n " , p .FilePath )
86
- funcWord := "##Functions\n "
87
- if strings .Contains (p .FileName , "_test" ) {
88
- startFuncWord = fmt .Sprintf ("##Start of go test file %s \n " , p .FilePath )
89
- endFuncWord = fmt .Sprintf ("----- End of go test file %s ------- \n " , p .FilePath )
90
- funcWord = "##Test Functions\n "
91
- }
92
- sb .WriteString (startFuncWord )
93
- sb .WriteString (fmt .Sprintf ("###File path: %s #######\n " , p .FilePath ))
94
- sb .WriteString (fmt .Sprintf ("###File name: %s #######\n " , p .FileName ))
95
- sb .WriteString (fmt .Sprintf ("## Package name: %s\n " , file .Name .Name ))
96
- sb .WriteString (funcWord )
79
+ var funcDescriptions , testFuncDescriptions []FunctionDescription
80
+
81
+ isTestFile := strings .Contains (p .FileName , "_test" )
82
+ writeFileHeader (& sb , p , file , isTestFile )
83
+
97
84
ast .Inspect (file , func (n ast.Node ) bool {
98
85
if fn , ok := n .(* ast.FuncDecl ); ok {
99
86
funcStr := describeFunctionDeclaration (& sb , fn , code , p .IncludeBody )
100
- if strings .Contains (p .FileName , "_test" ) {
101
- testFuncObj := FunctionDescription {
102
- Name : fn .Name .Name ,
103
- Doc : funcStr ,
104
- Package : file .Name .Name ,
105
- IsTestFunction : true ,
106
- }
107
- testFuncDescriptions = append (testFuncDescriptions , testFuncObj )
108
-
87
+ funcDesc := FunctionDescription {
88
+ Name : fn .Name .Name ,
89
+ Doc : funcStr ,
90
+ Package : file .Name .Name ,
91
+ IsTestFunction : isTestFile ,
92
+ }
93
+ if isTestFile {
94
+ testFuncDescriptions = append (testFuncDescriptions , funcDesc )
109
95
} else {
110
- funcObj := FunctionDescription {
111
- Name : fn .Name .Name ,
112
- Doc : funcStr ,
113
- Package : file .Name .Name ,
114
- IsTestFunction : false ,
115
- }
116
- funcDescriptions = append (funcDescriptions , funcObj )
96
+ funcDescriptions = append (funcDescriptions , funcDesc )
117
97
}
118
98
}
119
99
return true
120
100
})
121
- sb .WriteString (endFuncWord )
101
+
102
+ writeFileFooter (& sb , p , isTestFile )
122
103
return sb .String (), funcDescriptions , testFuncDescriptions
123
104
}
124
105
106
+ func writeFileHeader (sb * strings.Builder , p Param , file * ast.File , isTestFile bool ) {
107
+ fileType := "go"
108
+ if isTestFile {
109
+ fileType += " test"
110
+ }
111
+ sb .WriteString (fmt .Sprintf ("##Start of %s file %s\n " , fileType , p .FilePath ))
112
+ sb .WriteString (fmt .Sprintf ("###File path: %s\n " , p .FilePath ))
113
+ sb .WriteString (fmt .Sprintf ("###File name: %s\n " , p .FileName ))
114
+ sb .WriteString (fmt .Sprintf ("##Package name: %s\n " , file .Name .Name ))
115
+ sb .WriteString (fmt .Sprintf ("##%s\n " , strings .Title (fileType )+ " Functions" ))
116
+ }
117
+
118
+ func writeFileFooter (sb * strings.Builder , p Param , isTestFile bool ) {
119
+ fileType := "go"
120
+ if isTestFile {
121
+ fileType += " test"
122
+ }
123
+ sb .WriteString (fmt .Sprintf ("----- End of %s file %s -------\n " , fileType , p .FilePath ))
124
+ }
125
+
125
126
func describeFunctionDeclaration (funcSb * strings.Builder , fn * ast.FuncDecl , code string , includeBody bool ) string {
126
127
var sb strings.Builder
127
128
writeComments (& sb , fn .Doc )
@@ -181,8 +182,6 @@ func writeFunctionBody(sb *strings.Builder, fn *ast.FuncDecl, code string) {
181
182
sb .WriteString ("```go\n " )
182
183
sb .WriteString (code [fn .Pos ()- 1 : fn .End ()- 1 ])
183
184
sb .WriteString ("```\n " )
184
- sb .WriteString (code [fn .Pos ()- 1 : fn .End ()- 1 ])
185
-
186
185
}
187
186
188
187
func expr (e ast.Expr ) string {
0 commit comments