/
main.go
114 lines (93 loc) · 2.32 KB
/
main.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
package main
import (
"fmt"
// "go/constant"
"go/ast"
"go/parser"
"go/token"
// "strings"
"golang.org/x/tools/go/packages"
)
func parseStructsFromPkg(packagePath string, typeName string) ([]string, error) {
cfg := &packages.Config{
Mode: packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedEmbedPatterns,
ParseFile: func(fset *token.FileSet, filename string, src []byte) (*ast.File, error) {
return parser.ParseFile(fset, filename, src, parser.ParseComments)
},
}
pkgs, err := packages.Load(cfg, packagePath)
if err != nil {
return nil, err
}
if len(pkgs) == 0 {
return nil, fmt.Errorf("no packages were loaded")
}
var enums []string
for _, pkg := range pkgs {
//scope := pkg.Types.Scope()
//
//obj := scope.Lookup(typeName)
//if obj == nil {
// continue
//}
//
//objStruct, ok := obj.Type().Underlying().(*types.Struct)
//if !ok {
// continue
//}
//
//fmt.Println(objStruct)
//commentStart := 0
//commentEnd := 0
for i := range pkg.Syntax {
ast.Inspect(pkg.Syntax[i], func(n ast.Node) bool {
comment, ok := n.(*ast.Comment)
if !ok {
return true
}
pos := cfg.Fset.Position(comment.Pos())
fmt.Println(comment)
fmt.Println(pos)
// Check if the node is a struct type
typeSpec, ok := n.(*ast.TypeSpec)
if !ok {
return true
}
// Check if the struct name is "Sample"
if typeSpec.Name.Name == typeName {
structType, ok := typeSpec.Type.(*ast.StructType)
if !ok {
return true
}
fmt.Println(structType)
// Print the comments above the struct
//if structType != nil {
// fmt.Println("Comments for struct Sample:")
// for _, comment := range structType.Doc.List {
// fmt.Println(comment.Text)
// }
//}
return false
}
return true
})
}
// for _, name := range scope.Names() {
// varObj := scope.Lookup(name)
// fmt.Println(varObj)
// // if varObj == nil || varObj.Type() == nil {
// // continue
// // }
// //
// if varObj.Type().String() == obj.Type().String() {
// if val, ok := varObj.(*types.Struct); ok {
// vstr := val.Val().ExactString()
// }
// }
// }
}
return enums, nil
}
func main() {
parseStructsFromPkg("github.com/kloudlite/operator/apis/mongodb.msvc/v1", "StandaloneServiceSpec")
}