feat: 添加服务生成配置,优化服务解析逻辑,增强错误处理
This commit is contained in:
@@ -103,6 +103,9 @@ func (pb *ProviderBuilder) BuildFromTypeSpec(typeSpec *ast.TypeSpec, decl *ast.G
|
||||
Imports: make(map[string]string),
|
||||
PkgName: context.PackageName,
|
||||
ProviderFile: context.FilePath,
|
||||
Location: SourceLocation{
|
||||
File: context.FilePath,
|
||||
},
|
||||
}
|
||||
|
||||
// Set return type
|
||||
|
||||
@@ -226,14 +226,26 @@ func (p *MainParser) ParseFile(source string) ([]Provider, error) {
|
||||
// 查找对应的 AST 节点
|
||||
provider, err := p.buildProviderFromDiscovery(discoveredProvider, node, builderContext)
|
||||
if err != nil {
|
||||
context.AddError(source, 0, 0, fmt.Sprintf("failed to build provider %s: %v", discoveredProvider.StructName, err), "error")
|
||||
context.AddError(
|
||||
source,
|
||||
0,
|
||||
0,
|
||||
fmt.Sprintf("failed to build provider %s: %v", discoveredProvider.StructName, err),
|
||||
"error",
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
// 如果启用严格模式,验证 Provider 配置
|
||||
if p.config.StrictMode {
|
||||
if err := p.validator.Validate(&provider); err != nil {
|
||||
context.AddError(source, 0, 0, fmt.Sprintf("validation failed for provider %s: %v", provider.StructName, err), "error")
|
||||
context.AddError(
|
||||
source,
|
||||
0,
|
||||
0,
|
||||
fmt.Sprintf("validation failed for provider %s: %v", provider.StructName, err),
|
||||
"error",
|
||||
)
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -286,7 +298,11 @@ func (p *MainParser) shouldProcessFile(source string) bool {
|
||||
}
|
||||
|
||||
// buildProviderFromDiscovery builds a complete Provider from a discovered provider annotation
|
||||
func (p *MainParser) buildProviderFromDiscovery(discoveredProvider Provider, node *ast.File, context *BuilderContext) (Provider, error) {
|
||||
func (p *MainParser) buildProviderFromDiscovery(
|
||||
discoveredProvider Provider,
|
||||
node *ast.File,
|
||||
context *BuilderContext,
|
||||
) (Provider, error) {
|
||||
// Find the corresponding type specification in the AST
|
||||
var typeSpec *ast.TypeSpec
|
||||
var genDecl *ast.GenDecl
|
||||
|
||||
@@ -434,7 +434,12 @@ func (p *GoParser) parseFileContent(filePath string, node *ast.File) ([]Provider
|
||||
}
|
||||
|
||||
// parseProviderDecl parses a provider from an AST declaration
|
||||
func (p *GoParser) parseProviderDecl(filePath string, fileNode *ast.File, decl ast.Decl, imports map[string]string) (*Provider, error) {
|
||||
func (p *GoParser) parseProviderDecl(
|
||||
filePath string,
|
||||
fileNode *ast.File,
|
||||
decl ast.Decl,
|
||||
imports map[string]string,
|
||||
) (*Provider, error) {
|
||||
genDecl, ok := decl.(*ast.GenDecl)
|
||||
if !ok {
|
||||
return nil, nil
|
||||
@@ -478,6 +483,9 @@ func (p *GoParser) parseProviderDecl(filePath string, fileNode *ast.File, decl a
|
||||
Imports: make(map[string]string),
|
||||
PkgName: fileNode.Name.Name,
|
||||
ProviderFile: filepath.Join(filepath.Dir(filePath), "provider.gen.go"),
|
||||
Location: SourceLocation{
|
||||
File: filePath,
|
||||
},
|
||||
}
|
||||
|
||||
// Set default return type if not specified
|
||||
@@ -518,7 +526,12 @@ func (p *GoParser) parseProviderDecl(filePath string, fileNode *ast.File, decl a
|
||||
}
|
||||
|
||||
// parseStructFields parses struct fields for injection parameters
|
||||
func (p *GoParser) parseStructFields(structType *ast.StructType, imports map[string]string, provider *Provider, onlyMode bool) error {
|
||||
func (p *GoParser) parseStructFields(
|
||||
structType *ast.StructType,
|
||||
imports map[string]string,
|
||||
provider *Provider,
|
||||
onlyMode bool,
|
||||
) error {
|
||||
for _, field := range structType.Fields.List {
|
||||
if field.Names == nil {
|
||||
continue
|
||||
@@ -573,7 +586,10 @@ func (p *GoParser) parseStructFields(structType *ast.StructType, imports map[str
|
||||
}
|
||||
|
||||
// parseFieldType parses a field type and returns its components
|
||||
func (p *GoParser) parseFieldType(expr ast.Expr, imports map[string]string) (star, pkg, pkgAlias, typ string, err error) {
|
||||
func (p *GoParser) parseFieldType(
|
||||
expr ast.Expr,
|
||||
imports map[string]string,
|
||||
) (star, pkg, pkgAlias, typ string, err error) {
|
||||
switch t := expr.(type) {
|
||||
case *ast.Ident:
|
||||
typ = t.Name
|
||||
|
||||
Reference in New Issue
Block a user