From 0a701e3b605160e75e3f10c4506b6a4c6ea04987 Mon Sep 17 00:00:00 2001 From: Boming Zhang Date: Sun, 3 Mar 2024 03:14:53 -0500 Subject: [PATCH] feat: run with parsed toml --- README.md | 4 +-- cmd/joj3/main.go | 26 ++++++++++++++++++- go.mod | 6 ++++- go.sum | 20 +++++++++++++++ internal/parsers/dummy/parser.go | 17 ++++++++++--- internal/stage/model.go | 37 +++++++++++++++++++++++++++ internal/stage/parser.go | 7 +----- internal/stage/run.go | 43 ++++++++++++-------------------- 8 files changed, 120 insertions(+), 40 deletions(-) create mode 100644 internal/stage/model.go diff --git a/README.md b/README.md index edbaba6..3c17784 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,6 @@ $ make clean && make && ./build/joj3 rm -rf ./build/* rm -rf *.out go build -o ./build/joj3 ./cmd/joj3 -dummy stage 0: score: 0, comment: I'm a dummy -dummy stage 1: score: 0, comment: I'm a dummy +stage 0: score: 100, comment: dummy comment for stage 0 +stage 1: score: 101, comment: dummy comment for stage 1 ``` diff --git a/cmd/joj3/main.go b/cmd/joj3/main.go index a48f167..704d203 100644 --- a/cmd/joj3/main.go +++ b/cmd/joj3/main.go @@ -9,7 +9,31 @@ import ( ) func main() { - stages := stage.ParseStages() + tomlConfig := ` + [[stages]] + name = "stage 0" + [stages.executor] + name = "dummy" + [stages.executor.with] + args = [ "/usr/bin/cat", "/dev/null" ] + [stages.parser] + name = "dummy" + [stages.parser.with] + score = 100 + comment = "dummy comment for stage 0" + [[stages]] + name = "stage 1" + [stages.executor] + name = "dummy" + [stages.executor.with] + args = [ "/usr/bin/cat", "/dev/null" ] + [stages.parser] + name = "dummy" + [stages.parser.with] + score = 101 + comment = "dummy comment for stage 1" + ` + stages := stage.ParseStages(tomlConfig) results := stage.Run(stages) for _, result := range results { fmt.Printf( diff --git a/go.mod b/go.mod index 18c2a21..453a292 100644 --- a/go.mod +++ b/go.mod @@ -2,11 +2,15 @@ module focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3 go 1.22.0 -require github.com/criyle/go-judge v1.8.1 +require ( + github.com/criyle/go-judge v1.8.1 + github.com/mitchellh/mapstructure v1.5.0 +) require ( github.com/creack/pty v1.1.21 // indirect github.com/criyle/go-sandbox v0.10.1 // indirect + github.com/pelletier/go-toml/v2 v2.1.1 golang.org/x/sync v0.6.0 // indirect golang.org/x/sys v0.16.0 // indirect ) diff --git a/go.sum b/go.sum index ad35e91..c864d72 100644 --- a/go.sum +++ b/go.sum @@ -4,7 +4,27 @@ github.com/criyle/go-judge v1.8.1 h1:VI9OGz1MnLp9cv//gMVL8uruNwxSod5UmNwew8ZnfCA github.com/criyle/go-judge v1.8.1/go.mod h1:yZepeuMTmQXEJnBgHZQIGVuytRYWlLhQBQHLaL84N5w= github.com/criyle/go-sandbox v0.10.1 h1:z9Il/UXQwKEvIwdr1wVheWWWAqGWtdTItBmEsWqFqT4= github.com/criyle/go-sandbox v0.10.1/go.mod h1:ivPw/HEh5unxVRlXJxCgkgTCuy+cxTkQDX7D2XQf/kg= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= +github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/parsers/dummy/parser.go b/internal/parsers/dummy/parser.go index 89e6bb1..d25f843 100644 --- a/internal/parsers/dummy/parser.go +++ b/internal/parsers/dummy/parser.go @@ -3,13 +3,24 @@ package dummy import ( "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" "github.com/criyle/go-judge/cmd/go-judge/model" + "github.com/mitchellh/mapstructure" ) +type Config struct { + Score int + Comment string +} + type Dummy struct{} -func (e *Dummy) Run(result model.Result, config string) stage.ParserResult { +func (e *Dummy) Run(result model.Result, configAny any) stage.ParserResult { + var config Config + err := mapstructure.Decode(configAny, &config) + if err != nil { + panic(err) + } return stage.ParserResult{ - Score: 0, - Comment: "I'm a dummy", + Score: config.Score, + Comment: config.Comment, } } diff --git a/internal/stage/model.go b/internal/stage/model.go new file mode 100644 index 0000000..199f65e --- /dev/null +++ b/internal/stage/model.go @@ -0,0 +1,37 @@ +package stage + +import ( + "github.com/criyle/go-judge/cmd/go-judge/model" +) + +type Stage struct { + Name string + Executor Executor + ExecutorCmd model.Cmd + Parser Parser + ParserConfig any +} + +type ParserResult struct { + Score int + Comment string +} + +type StageResult struct { + Name string + ParserResult +} + +type StagesConfig struct { + Stages []struct { + Name string + Executor struct { + Name string + With interface{} + } + Parser struct { + Name string + With interface{} + } + } +} diff --git a/internal/stage/parser.go b/internal/stage/parser.go index 43f3994..0b572c1 100644 --- a/internal/stage/parser.go +++ b/internal/stage/parser.go @@ -5,12 +5,7 @@ import "github.com/criyle/go-judge/cmd/go-judge/model" var parserMap = map[string]Parser{} type Parser interface { - Run(model.Result, string) ParserResult -} - -type ParserResult struct { - Score int - Comment string + Run(model.Result, any) ParserResult } func RegisterParser(name string, parser Parser) { diff --git a/internal/stage/run.go b/internal/stage/run.go index fcb92a2..5ccfc52 100644 --- a/internal/stage/run.go +++ b/internal/stage/run.go @@ -2,34 +2,23 @@ package stage import ( "github.com/criyle/go-judge/cmd/go-judge/model" + "github.com/pelletier/go-toml/v2" ) -type Stage struct { - name string - executor Executor - executorCmd model.Cmd - parser Parser - parserConfig string -} - -type StageResult struct { - Name string - ParserResult -} - -func ParseStages() []Stage { - stages := []Stage{} - config := [][]string{ - {"dummy stage 0", "dummy", "dummy"}, - {"dummy stage 1", "dummy", "dummy"}, +func ParseStages(tomlConfig string) []Stage { + var stagesConfig StagesConfig + err := toml.Unmarshal([]byte(tomlConfig), &stagesConfig) + if err != nil { + panic(err) } - for _, v := range config { + stages := []Stage{} + for _, stage := range stagesConfig.Stages { stages = append(stages, Stage{ - name: v[0], - executor: executorMap[v[1]], - executorCmd: model.Cmd{}, - parser: parserMap[v[2]], - parserConfig: "", + Name: stage.Name, + Executor: executorMap[stage.Executor.Name], + ExecutorCmd: model.Cmd{}, + Parser: parserMap[stage.Parser.Name], + ParserConfig: stage.Parser.With, }) } return stages @@ -38,10 +27,10 @@ func ParseStages() []Stage { func Run(stages []Stage) []StageResult { var parserResults []StageResult for _, stage := range stages { - executorResult := stage.executor.Run(stage.executorCmd) - parserResult := stage.parser.Run(executorResult, stage.parserConfig) + executorResult := stage.Executor.Run(stage.ExecutorCmd) + parserResult := stage.Parser.Run(executorResult, stage.ParserConfig) parserResults = append(parserResults, StageResult{ - Name: stage.name, + Name: stage.Name, ParserResult: parserResult, }) }