MCPcopy Create free account
hub / github.com/apache/fory / discoverTypesFromFile

Function discoverTypesFromFile

go/fory/codegen/parser.go:32–67  ·  view source on GitHub ↗

discoverTypesFromFile scans the source file for //fory:generate comments

(pkg *packages.Package, sourceFile string)

Source from the content-addressed store, hash-verified

30
31// discoverTypesFromFile scans the source file for //fory:generate comments
32func discoverTypesFromFile(pkg *packages.Package, sourceFile string) ([]string, error) {
33 var discoveredTypes []string
34
35 // Find the syntax tree for the specific file
36 for _, file := range pkg.Syntax {
37 // Get the file's position and convert to absolute path for comparison
38 filename := pkg.Fset.Position(file.Pos()).Filename
39 absFilename, err := filepath.Abs(filename)
40 if err != nil {
41 continue
42 }
43
44 if absFilename != sourceFile {
45 continue
46 }
47
48 // Scan for type declarations with //fory:generate comments
49 for _, decl := range file.Decls {
50 if genDecl, ok := decl.(*ast.GenDecl); ok && genDecl.Tok == token.TYPE {
51 for _, spec := range genDecl.Specs {
52 if typeSpec, ok := spec.(*ast.TypeSpec); ok {
53 // Check if it's a struct type
54 if _, ok := typeSpec.Type.(*ast.StructType); ok {
55 // Look for //fory:generate comment
56 if hasGenerateComment(genDecl.Doc) || hasGenerateComment(typeSpec.Doc) {
57 discoveredTypes = append(discoveredTypes, typeSpec.Name.Name)
58 }
59 }
60 }
61 }
62 }
63 }
64 }
65
66 return discoveredTypes, nil
67}
68
69// hasGenerateComment checks if comment group contains //fory:generate
70func hasGenerateComment(commentGroup *ast.CommentGroup) bool {

Callers 1

processPackageFileFunction · 0.85

Calls 2

hasGenerateCommentFunction · 0.85
AbsMethod · 0.80

Tested by

no test coverage detected