feat(parser/plugin): new parser that can load from a plugin (#81)
All checks were successful
submodules sync / sync (push) Successful in 46s
build / build (push) Successful in 1m48s
build / trigger-build-image (push) Successful in 8s

This commit is contained in:
张泊明518370910136 2025-02-17 09:10:00 -05:00
parent 45490f3d1e
commit 1e75ab1c6e
GPG Key ID: D47306D7062CDA9D
3 changed files with 53 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import (
_ "github.com/joint-online-judge/JOJ3/internal/parser/healthcheck"
_ "github.com/joint-online-judge/JOJ3/internal/parser/keyword"
_ "github.com/joint-online-judge/JOJ3/internal/parser/log"
_ "github.com/joint-online-judge/JOJ3/internal/parser/plugin"
_ "github.com/joint-online-judge/JOJ3/internal/parser/resultdetail"
_ "github.com/joint-online-judge/JOJ3/internal/parser/resultstatus"
_ "github.com/joint-online-judge/JOJ3/internal/parser/sample"

View File

@ -0,0 +1,21 @@
// Package plugin provides functionality to load and run parser plugins
// dynamically. It is used for custom parsers.
// The plugin needs to be located at `ModPath` and export a symbol with name
// `SymName` that implements the stage.Parser interface.
package plugin
import "github.com/joint-online-judge/JOJ3/internal/stage"
var name = "plugin"
type Conf struct {
ModPath string
SymName string `default:"Parser"`
}
type Plugin struct{}
func init() {
stage.RegisterParser(name, &Plugin{})
}

View File

@ -0,0 +1,31 @@
package plugin
import (
"fmt"
"plugin"
"github.com/joint-online-judge/JOJ3/internal/stage"
)
func (*Plugin) Run(results []stage.ExecutorResult, confAny any) (
[]stage.ParserResult, bool, error,
) {
conf, err := stage.DecodeConf[Conf](confAny)
if err != nil {
return nil, true, err
}
plug, err := plugin.Open(conf.ModPath)
if err != nil {
return nil, true, err
}
symParser, err := plug.Lookup(conf.SymName)
if err != nil {
return nil, true, err
}
var parser stage.Parser
parser, ok := symParser.(stage.Parser)
if !ok {
return nil, true, fmt.Errorf("unexpected type from module symbol")
}
return parser.Run(results, confAny)
}