-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.go
194 lines (167 loc) · 5.55 KB
/
ast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package docify
import (
"fmt"
"github.com/getevo/docify/serializer"
"go/ast"
"go/format"
"go/parser"
"go/token"
"os"
"path/filepath"
"reflect"
"strings"
)
func GetStructDefinition(t reflect.Type) (*serializer.StructDefinition, error) {
if t.Kind() != reflect.Struct {
return nil, fmt.Errorf("provided type is not a struct")
}
// Get the directory of the package where the type is defined
dir := "../" + t.PkgPath()
// Get the name of the struct
structName := t.Name()
var structDef serializer.StructDefinition
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasSuffix(info.Name(), ".go") {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, path, nil, parser.AllErrors|parser.ParseComments)
if err != nil {
return err
}
var commentMap = map[int]string{}
// ✅ Step 1: Build a map of comments by line number
for _, commentGroup := range node.Comments {
for _, comment := range commentGroup.List {
pos := fset.Position(comment.Pos()).Line
commentMap[pos+1] = strings.TrimPrefix(comment.Text, "// ")
}
}
// ✅ Step 2: Inspect AST to find the struct definition
ast.Inspect(node, func(n ast.Node) bool {
ts, ok := n.(*ast.TypeSpec)
if ok && ts.Name.Name == structName {
st, ok := ts.Type.(*ast.StructType)
if ok {
var sb strings.Builder
var description strings.Builder
// ✅ Step 3: Extract attached comments for the struct
if ts.Doc != nil {
for _, comment := range ts.Doc.List {
text := strings.TrimPrefix(comment.Text, "// ")
description.WriteString(text + "\n")
sb.WriteString(comment.Text + "\n")
}
}
sb.WriteString(fmt.Sprintf("type %s struct {\n", structName))
for _, field := range st.Fields.List {
names := []string{}
for _, name := range field.Names {
names = append(names, name.Name)
}
// ✅ Step 4: Use getTypeString to resolve all types
typeStr := getTypeString(field.Type)
// ✅ Step 5: Extract struct tag
var tag string
if field.Tag != nil {
tag = strings.Trim(field.Tag.Value, "`")
}
// ✅ Step 6: Extract field comment from the map
pos := fset.Position(field.Pos()).Line
fieldComment := commentMap[pos]
// ✅ Step 7: Store field information
structDef.Fields = append(structDef.Fields, serializer.FieldDefinition{
Name: strings.Join(names, ", "),
Type: typeStr,
Tag: tag,
Description: fieldComment,
})
// ✅ Step 8: Write fields to body
if fieldComment != "" {
sb.WriteString(fmt.Sprintf(" \n// %s\n", fieldComment))
}
sb.WriteString(fmt.Sprintf(" %s %s", strings.Join(names, ", "), typeStr))
if tag != "" {
sb.WriteString(fmt.Sprintf(" `%s`", tag))
}
sb.WriteString("\n")
/*if tag != "" {
if fieldComment != "" {
sb.WriteString(fmt.Sprintf(" %s %s `%s` // %s\n", strings.Join(names, ", "), typeStr, tag, fieldComment))
} else {
sb.WriteString(fmt.Sprintf(" %s %s `%s`\n", strings.Join(names, ", "), typeStr, tag))
}
} else {
if fieldComment != "" {
sb.WriteString(fmt.Sprintf(" %s %s // %s\n", strings.Join(names, ", "), typeStr, fieldComment))
} else {
sb.WriteString(fmt.Sprintf(" %s %s\n", strings.Join(names, ", "), typeStr))
}
}*/
}
sb.WriteString("}\n")
// ✅ Step 9: Format the generated code using go/format
formattedCode, err := format.Source([]byte(sb.String()))
if err != nil {
return true // Ignore formatting errors and keep the original code
}
// ✅ Step 10: Store final values
structDef = serializer.StructDefinition{
Description: description.String(),
Body: string(formattedCode),
Fields: structDef.Fields,
File: path,
}
return false // Stop searching once the struct is found
}
}
return true
})
}
return nil
})
if err != nil {
return nil, err
}
if structDef.Body == "" {
return nil, fmt.Errorf("struct definition not found")
}
return &structDef, nil
}
// ✅ Recursive function to resolve field types (including generics)
func getTypeString(expr ast.Expr) string {
switch t := expr.(type) {
case *ast.Ident:
return t.Name
case *ast.StarExpr: // Pointers
return "*" + getTypeString(t.X)
case *ast.ArrayType: // Arrays and Slices
if t.Len != nil {
return fmt.Sprintf("[%s]%s", getTypeString(t.Len), getTypeString(t.Elt))
}
return "[]" + getTypeString(t.Elt)
case *ast.MapType: // Maps
return fmt.Sprintf("map[%s]%s", getTypeString(t.Key), getTypeString(t.Value))
case *ast.SelectorExpr: // Qualified types (e.g., time.Time)
if x, ok := t.X.(*ast.Ident); ok {
return x.Name + "." + t.Sel.Name
}
case *ast.InterfaceType: // Interfaces
return "interface{}"
case *ast.StructType: // Inline structs
return "struct{...}"
case *ast.IndexExpr: // Generics like types.JSONType[map[string]interface{}]
base := getTypeString(t.X)
index := getTypeString(t.Index)
return fmt.Sprintf("%s[%s]", base, index)
case *ast.IndexListExpr: // Multiple type parameters (e.g., T[K, V])
base := getTypeString(t.X)
var params []string
for _, index := range t.Indices {
params = append(params, getTypeString(index))
}
return fmt.Sprintf("%s[%s]", base, strings.Join(params, ", "))
}
return "unknown"
}