Skip to content

Commit 9857794

Browse files
welkeyeverAsterDY
authored andcommitted
feat: add method to generate relations of struct and its methods (cloudwego#5)
1 parent cf1ebde commit 9857794

1 file changed

Lines changed: 55 additions & 12 deletions

File tree

src/compress/golang/plugin/go_ast.go

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ type Function struct {
2626
MethodCalls []string
2727
}
2828

29+
type Struct struct {
30+
Name string
31+
Content string
32+
methods map[string]Function
33+
}
34+
2935
func (p *goParser) parseFile(filePath string) ([]Function, error) {
3036
fset := token.NewFileSet()
3137

@@ -152,9 +158,10 @@ func (p *goParser) parseFile(filePath string) ([]Function, error) {
152158
}
153159

154160
type goParser struct {
155-
modName string
156-
homePageDir string
157-
processedPkg map[string][]Function
161+
modName string
162+
homePageDir string
163+
processedPkgFunctions map[string][]Function
164+
processedPkgStruct map[string][]Struct
158165
}
159166

160167
func getGoFilesInDir(dir string) []string {
@@ -180,8 +187,8 @@ func (p *goParser) ParseDir(dir string) ([]Function, bool) {
180187
dir = strings.TrimPrefix(dir, relativePrefix)
181188
}
182189

183-
if p.processedPkg[dir] != nil {
184-
return p.processedPkg[dir], false
190+
if p.processedPkgFunctions[dir] != nil {
191+
return p.processedPkgFunctions[dir], false
185192
}
186193
functionList := make([]Function, 0)
187194
for _, f := range getGoFilesInDir(dir) {
@@ -192,7 +199,7 @@ func (p *goParser) ParseDir(dir string) ([]Function, bool) {
192199
}
193200
functionList = append(functionList, funcs...)
194201
}
195-
p.processedPkg[dir] = functionList
202+
p.processedPkgFunctions[dir] = functionList
196203
return functionList, true
197204
}
198205

@@ -209,7 +216,7 @@ func (p *goParser) ParseTilTheEnd(startDir string) {
209216
functionList, _ := p.ParseDir(startDir)
210217
for _, f := range functionList {
211218
for _, fc := range f.FunctionCalls {
212-
if p.processedPkg[fc.PkgDir] != nil {
219+
if p.processedPkgFunctions[fc.PkgDir] != nil {
213220
continue
214221
}
215222
p.ParseTilTheEnd(fc.PkgDir)
@@ -229,15 +236,50 @@ type SingleFunction struct {
229236
Content string
230237
}
231238

232-
func (p *goParser) generate() *MainStream {
239+
func (p *goParser) generateStruct() {
240+
processedStruct := make(map[string]*Struct)
241+
for pkgName, fs := range p.processedPkgFunctions {
242+
if len(fs) == 0 {
243+
continue
244+
}
245+
for _, f := range fs {
246+
if !f.IsMethod {
247+
continue
248+
}
249+
if processedStruct[f.AssociatedStruct] == nil {
250+
st := &Struct{Name: f.AssociatedStruct, methods: make(map[string]Function)}
251+
st.methods[f.Name] = f
252+
processedStruct[f.AssociatedStruct] = st
253+
continue
254+
}
255+
256+
processedStruct[f.AssociatedStruct].methods[f.Name] = f
257+
continue
258+
}
259+
260+
if len(processedStruct) == 0 {
261+
continue
262+
}
263+
264+
structList := make([]Struct, 0, len(processedStruct))
265+
266+
for _, s := range processedStruct {
267+
structList = append(structList, *s)
268+
}
269+
270+
p.processedPkgStruct[pkgName] = structList
271+
}
272+
}
273+
274+
func (p *goParser) getMain() *MainStream {
233275
m := &MainStream{
234276
RelatedFunctions: make([]SingleFunction, 0),
235277
}
236278

237279
var functionCalledInMain []Function
238280

239281
Out:
240-
for _, v := range p.processedPkg {
282+
for _, v := range p.processedPkgFunctions {
241283
for _, vv := range v {
242284
if vv.Name == "main" {
243285
m.MainFunc = vv.Content
@@ -253,7 +295,7 @@ Out:
253295

254296
func (p *goParser) fillFunctionContent(f []Function, fl *[]SingleFunction) {
255297
for _, ff := range f {
256-
for _, pf := range p.processedPkg[ff.PkgDir] {
298+
for _, pf := range p.processedPkgFunctions[ff.PkgDir] {
257299
if pf.IsMethod {
258300
// Skip method here
259301
continue
@@ -308,10 +350,11 @@ func main() {
308350

309351
homeDir := os.Args[1]
310352

311-
p := &goParser{modName: "", homePageDir: homeDir, processedPkg: make(map[string][]Function)}
353+
p := &goParser{modName: "", homePageDir: homeDir, processedPkgFunctions: make(map[string][]Function), processedPkgStruct: make(map[string][]Struct)}
312354
p.ParseTilTheEnd(p.homePageDir)
313355

314-
m := p.generate()
356+
p.generateStruct()
357+
m := p.getMain()
315358

316359
out := bytes.NewBuffer(nil)
317360
encoder := json.NewEncoder(out)

0 commit comments

Comments
 (0)