feat(parser/plugin): new parser that can load from a plugin (#81)
This commit is contained in:
		
							parent
							
								
									45490f3d1e
								
							
						
					
					
						commit
						1e75ab1c6e
					
				|  | @ -10,6 +10,7 @@ import ( | ||||||
| 	_ "github.com/joint-online-judge/JOJ3/internal/parser/healthcheck" | 	_ "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/keyword" | ||||||
| 	_ "github.com/joint-online-judge/JOJ3/internal/parser/log" | 	_ "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/resultdetail" | ||||||
| 	_ "github.com/joint-online-judge/JOJ3/internal/parser/resultstatus" | 	_ "github.com/joint-online-judge/JOJ3/internal/parser/resultstatus" | ||||||
| 	_ "github.com/joint-online-judge/JOJ3/internal/parser/sample" | 	_ "github.com/joint-online-judge/JOJ3/internal/parser/sample" | ||||||
|  |  | ||||||
							
								
								
									
										21
									
								
								internal/parser/plugin/meta.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								internal/parser/plugin/meta.go
									
									
									
									
									
										Normal 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{}) | ||||||
|  | } | ||||||
							
								
								
									
										31
									
								
								internal/parser/plugin/parser.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								internal/parser/plugin/parser.go
									
									
									
									
									
										Normal 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) | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user