feat!: support multiple parsers in one stage (#52)

This commit is contained in:
张泊明518370910136 2024-10-09 18:50:06 -04:00
parent 9f17bb317d
commit 6620f9bc28
GPG Key ID: D47306D7062CDA9D
4 changed files with 67 additions and 36 deletions

View File

@ -30,7 +30,7 @@ type Conf struct {
Cases []OptionalCmd
}
}
Parser struct {
Parsers []struct {
Name string
With interface{}
}

View File

@ -16,7 +16,7 @@ import (
func generateStages(conf conf.Conf, group string) ([]stage.Stage, error) {
stages := []stage.Stage{}
existNames := map[string]bool{}
for _, s := range conf.Stages {
for _, s := range conf.Stage.Stages {
if s.Group != "" && group != s.Group {
continue
}
@ -50,12 +50,20 @@ func generateStages(conf conf.Conf, group string) ([]stage.Stage, error) {
if len(s.Executor.With.Cases) == 0 {
cmds = []stage.Cmd{defaultCmd}
}
parsers := []stage.StageParser{}
for _, p := range s.Parsers {
parsers = append(parsers, stage.StageParser{
Name: p.Name,
Conf: p.With,
})
}
stages = append(stages, stage.Stage{
Name: s.Name,
ExecutorName: s.Executor.Name,
ExecutorCmds: cmds,
ParserName: s.Parser.Name,
ParserConf: s.Parser.With,
Executor: stage.StageExecutor{
Name: s.Executor.Name,
Cmds: cmds,
},
Parsers: parsers,
})
}
slog.Debug("stages generated", "stages", stages)

View File

@ -150,12 +150,19 @@ func (r ExecutorResult) String() string {
return fmt.Sprintf("%+v", d)
}
type StageExecutor struct {
Name string
Cmds []Cmd
}
type StageParser struct {
Name string
Conf any
}
type Stage struct {
Name string
ExecutorName string
ExecutorCmds []Cmd
ParserName string
ParserConf any
Executor StageExecutor
Parsers []StageParser
}
type ParserResult struct {

View File

@ -8,51 +8,67 @@ import (
func Run(stages []Stage) (stageResults []StageResult, err error) {
var executorResults []ExecutorResult
var parserResults []ParserResult
var tmpParserResults []ParserResult
var forceQuit bool
slog.Info("stage run start")
for _, stage := range stages {
slog.Info("stage start", "name", stage.Name)
slog.Info("executor run start", "name", stage.ExecutorName)
slog.Debug("executor run start", "name", stage.ExecutorName,
"cmds", stage.ExecutorCmds)
executor, ok := executorMap[stage.ExecutorName]
slog.Info("executor run start", "name", stage.Executor.Name)
slog.Debug("executor run start", "name", stage.Executor.Name,
"cmds", stage.Executor.Cmds)
executor, ok := executorMap[stage.Executor.Name]
if !ok {
slog.Error("executor not found", "name", stage.ExecutorName)
err = fmt.Errorf("executor not found: %s", stage.ExecutorName)
slog.Error("executor not found", "name", stage.Executor.Name)
err = fmt.Errorf("executor not found: %s", stage.Executor.Name)
return
}
executorResults, err = executor.Run(stage.ExecutorCmds)
executorResults, err = executor.Run(stage.Executor.Cmds)
if err != nil {
slog.Error("executor run error", "name", stage.ExecutorName, "error", err)
slog.Error("executor run error", "name", stage.Executor.Name, "error", err)
return
}
slog.Debug("executor run done", "results", executorResults)
for _, executorResult := range executorResults {
slog.Debug("executor run done", "result.Files", executorResult.Files)
}
slog.Info("parser run start", "name", stage.ParserName)
slog.Debug("parser run start", "name", stage.ParserName,
"conf", stage.ParserConf)
parser, ok := parserMap[stage.ParserName]
parserResults = []ParserResult{}
for _, stageParser := range stage.Parsers {
slog.Info("parser run start", "name", stageParser.Name)
slog.Debug("parser run start", "name", stageParser.Name,
"conf", stageParser.Conf)
parser, ok := parserMap[stageParser.Name]
if !ok {
slog.Error("parser not found", "name", stage.ParserName)
err = fmt.Errorf("parser not found: %s", stage.ParserName)
slog.Error("parser not found", "name", stageParser.Name)
err = fmt.Errorf("parser not found: %s", stageParser.Name)
return
}
parserResults, forceQuit, err = parser.Run(executorResults, stage.ParserConf)
tmpParserResults, forceQuit, err = parser.Run(
executorResults, stageParser.Conf)
if err != nil {
slog.Error("parser run error", "name", stage.ParserName, "error", err)
slog.Error("parser run error", "name", stageParser.Name, "error", err)
return
}
slog.Debug("parser run done", "results", parserResults)
slog.Debug("parser run done", "results", tmpParserResults)
if len(parserResults) == 0 {
parserResults = tmpParserResults
} else {
for i := range len(parserResults) {
parserResults[i].Score += tmpParserResults[i].Score
parserResults[i].Comment += tmpParserResults[i].Comment
}
}
if forceQuit {
slog.Error("parser force quit", "name", stageParser.Name)
break
}
}
stageResults = append(stageResults, StageResult{
Name: stage.Name,
Results: parserResults,
ForceQuit: forceQuit,
})
if forceQuit {
slog.Error("parser force quit", "name", stage.ParserName)
return
break
}
}
return