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

Function processPackageFile

go/fory/codegen/generator.go:173–232  ·  view source on GitHub ↗
(pkg *packages.Package, sourceFile string, typeList string)

Source from the content-addressed store, hash-verified

171}
172
173func processPackageFile(pkg *packages.Package, sourceFile string, typeList string) error {
174 // Convert to absolute path for comparison
175 absSourceFile, err := filepath.Abs(sourceFile)
176 if err != nil {
177 return fmt.Errorf("getting absolute path for %s: %w", sourceFile, err)
178 }
179
180 // Find target types from the specific file
181 var targetTypes []string
182
183 // If type list is provided, use it
184 if typeList != "" {
185 targetTypes = strings.Split(typeList, ",")
186 } else {
187 // Auto-discover types with //fory:generate comments
188 discoveredTypes, err := discoverTypesFromFile(pkg, absSourceFile)
189 if err != nil {
190 return fmt.Errorf("discovering types from file: %w", err)
191 }
192 targetTypes = discoveredTypes
193 }
194
195 if len(targetTypes) == 0 {
196 fmt.Printf("No types found to generate in %s\n", sourceFile)
197 return nil
198 }
199
200 // Also check if there are any compilation errors
201 if len(pkg.Errors) > 0 {
202 for _, err := range pkg.Errors {
203 logger.Printf("package error: %s", err)
204 }
205 }
206
207 // Parse structs from package
208 structs, err := parseStructsFromPackage(pkg, targetTypes)
209 if err != nil {
210 return fmt.Errorf("parsing structs from package: %w", err)
211 }
212
213 if len(structs) == 0 {
214 if len(targetTypes) > 0 {
215 scope := pkg.Types.Scope()
216 allNames := scope.Names()
217 logger.Printf("Warning: No matching structs found for target types: %v", targetTypes)
218 logger.Printf("Available types in package: %v", allNames)
219 return fmt.Errorf("no matching structs found for target types: %v", targetTypes)
220 }
221 logger.Printf("No structs to generate (no target types specified)")
222 return nil
223 }
224
225 // Generate code with file-based naming
226 logger.Printf("Generating code for %d struct(s) from %s: %v", len(structs), sourceFile, getStructNames(structs))
227 if err := generateCodeForFile(pkg, structs, sourceFile); err != nil {
228 return err
229 }
230 logger.Printf("Successfully generated code for %s", sourceFile)

Callers 1

runFileModeFunction · 0.85

Calls 5

discoverTypesFromFileFunction · 0.85
parseStructsFromPackageFunction · 0.85
getStructNamesFunction · 0.85
generateCodeForFileFunction · 0.85
AbsMethod · 0.80

Tested by

no test coverage detected