JOJ3/internal/parser/sample/parser.go
张泊明518370910136 5bbbcb14aa
Some checks failed
build / build (push) Successful in 2m3s
build / trigger-build-image (push) Successful in 8s
submodules sync / sync (push) Failing after 30s
refactor(internal): rename executor & parser dir
2024-10-09 05:38:30 -04:00

58 lines
1.3 KiB
Go

package sample
import (
"encoding/json"
"fmt"
"github.com/criyle/go-judge/envexec"
"github.com/joint-online-judge/JOJ3/internal/stage"
"github.com/joint-online-judge/JOJ3/pkg/sample"
)
type Conf struct {
Score int
Comment string
}
type Sample struct{}
func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
stdout := executorResult.Files["stdout"]
stderr := executorResult.Files["stderr"]
if executorResult.Status != stage.Status(envexec.StatusAccepted) {
return stage.ParserResult{
Score: 0,
Comment: fmt.Sprintf(
"Unexpected executor status: %s.\nStderr: %s",
executorResult.Status, stderr,
),
}
}
var sampleResult sample.Result
err := json.Unmarshal([]byte(stdout), &sampleResult)
if err != nil {
return stage.ParserResult{
Score: 0,
Comment: fmt.Sprintf("Failed to parse result: %s", err),
}
}
return stage.ParserResult{
Score: sampleResult.Score + conf.Score,
Comment: sampleResult.Comment + conf.Comment,
}
}
func (*Sample) Run(results []stage.ExecutorResult, confAny any) (
[]stage.ParserResult, bool, error,
) {
conf, err := stage.DecodeConf[Conf](confAny)
if err != nil {
return nil, true, err
}
var res []stage.ParserResult
for _, result := range results {
res = append(res, Parse(result, *conf))
}
return res, false, nil
}