Skip to content

Commit 39e5faa

Browse files
committed
fix: refactor codebase for better readability
1 parent e416723 commit 39e5faa

File tree

1 file changed

+45
-46
lines changed

1 file changed

+45
-46
lines changed

parse.go

+45-46
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313

1414
type Func struct {
1515
FullDescriptions []string
16-
functionDescriptions []FunctionDescription
17-
testFunctionDescriptions []FunctionDescription
16+
FunctionDescriptions []FunctionDescription
17+
TestFunctionDescriptions []FunctionDescription
1818
}
1919

2020
type FunctionDescription struct {
@@ -33,41 +33,38 @@ type Param struct {
3333
func (f *Func) ParseFunctions(p Param) {
3434
code, err := readFile(p.FilePath)
3535
if err != nil {
36-
log.Println("Error reading file:", err)
36+
log.Printf("Error reading file %s: %v", p.FilePath, err)
3737
return
3838
}
3939

4040
file, err := parseCode(p.FileName, code)
4141
if err != nil {
42-
log.Println("Error parsing file:", err)
42+
log.Printf("Error parsing file %s: %v", p.FileName, err)
4343
return
4444
}
4545

4646
description, funcDescriptions, testFuncDescriptions := buildFileDescription(p, file, code)
4747
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...)
5450
}
5551

5652
func (f *Func) Print() {
5753
for _, desc := range f.FullDescriptions {
5854
fmt.Println(desc)
5955
}
6056
}
57+
6158
func readFile(filePath string) (string, error) {
6259
codeFile, err := os.Open(filePath)
6360
if err != nil {
64-
return "", err
61+
return "", fmt.Errorf("failed to open file: %w", err)
6562
}
6663
defer codeFile.Close()
6764

6865
srcbuf, err := io.ReadAll(codeFile)
6966
if err != nil {
70-
return "", err
67+
return "", fmt.Errorf("failed to read file: %w", err)
7168
}
7269
return string(srcbuf), nil
7370
}
@@ -79,49 +76,53 @@ func parseCode(fileName, code string) (*ast.File, error) {
7976

8077
func buildFileDescription(p Param, file *ast.File, code string) (string, []FunctionDescription, []FunctionDescription) {
8178
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+
9784
ast.Inspect(file, func(n ast.Node) bool {
9885
if fn, ok := n.(*ast.FuncDecl); ok {
9986
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)
10995
} 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)
11797
}
11898
}
11999
return true
120100
})
121-
sb.WriteString(endFuncWord)
101+
102+
writeFileFooter(&sb, p, isTestFile)
122103
return sb.String(), funcDescriptions, testFuncDescriptions
123104
}
124105

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+
125126
func describeFunctionDeclaration(funcSb *strings.Builder, fn *ast.FuncDecl, code string, includeBody bool) string {
126127
var sb strings.Builder
127128
writeComments(&sb, fn.Doc)
@@ -181,8 +182,6 @@ func writeFunctionBody(sb *strings.Builder, fn *ast.FuncDecl, code string) {
181182
sb.WriteString("```go\n")
182183
sb.WriteString(code[fn.Pos()-1 : fn.End()-1])
183184
sb.WriteString("```\n")
184-
sb.WriteString(code[fn.Pos()-1 : fn.End()-1])
185-
186185
}
187186

188187
func expr(e ast.Expr) string {

0 commit comments

Comments
 (0)