feat: If with generics

This commit is contained in:
Paul Pan 2022-10-30 22:40:59 +08:00
parent 97c05a836c
commit 3140a60e67
8 changed files with 10 additions and 10 deletions

View File

@ -63,7 +63,7 @@ func (h *handler) Judge(_ context.Context, t *asynq.Task) error {
// 5. run and judge
result, point, status := h.runnerService.RunAndJudge(p.ProblemVersionID, user, p.Submission.Language, &config)
return utils.If(status != e.Success, e.InternalError, e.Success).(e.Status), point, result
return utils.If(status != e.Success, e.InternalError, e.Success), point, result
}()
h.taskService.SubmitUpdate(&model.SubmitUpdatePayload{

View File

@ -28,7 +28,7 @@ func RunRunner(g *global.Global) error {
DB: g.Conf.Redis.QueueDb,
},
asynq.Config{
Concurrency: utils.If(runtime.NumCPU() > 1, runtime.NumCPU()-1, 1).(int),
Concurrency: utils.If(runtime.NumCPU() > 1, runtime.NumCPU()-1, 1),
Logger: zapasynq.New(g.Log),
Queues: map[string]int{model.QueueRunner: 1},
},

View File

@ -65,7 +65,7 @@ func RunServer(g *global.Global) error {
DB: g.Conf.Redis.QueueDb,
},
asynq.Config{
Concurrency: utils.If(runtime.NumCPU() > 1, runtime.NumCPU()-1, 1).(int),
Concurrency: utils.If(runtime.NumCPU() > 1, runtime.NumCPU()-1, 1),
Logger: zapasynq.New(g.Log),
Queues: map[string]int{model.QueueServer: 1},
},

View File

@ -11,7 +11,7 @@ import (
func (g *Global) SetupZap() {
cfg := zap.Config{
Level: zap.NewAtomicLevelAt(
utils.If(g.Conf.Development, zapcore.DebugLevel, zapcore.InfoLevel).(zapcore.Level),
utils.If(g.Conf.Development, zapcore.DebugLevel, zapcore.InfoLevel),
),
Development: g.Conf.Development,
Encoding: "console", // or json

View File

@ -17,7 +17,7 @@ import (
)
func InitRouters(g *global.Global) *gin.Engine {
gin.SetMode(utils.If(g.Conf.Development, gin.DebugMode, gin.ReleaseMode).(string))
gin.SetMode(utils.If(g.Conf.Development, gin.DebugMode, gin.ReleaseMode))
r := gin.New()
r.MaxMultipartMemory = 8 << 20

View File

@ -16,14 +16,14 @@ func (s *service) Compile(version uint, user string, lang string) (JudgeStatus,
log := filepath.Join(UserDir, user, fmt.Sprintf("%s.compile.log", user))
msg, err := utils.FileRead(log)
msg = utils.If(err == nil, msg, nil).([]byte)
msg = utils.If(err == nil, msg, nil)
msgText := string(msg)
if !utils.FileExist(target) || utils.FileEmpty(target) {
return JudgeStatus{
Message: "compile failed",
Tasks: []TaskStatus{{Verdict: VerdictCompileError, Message: msgText}}},
utils.If(status == e.Success, e.RunnerUserCompileFailed, status).(e.Status)
utils.If(status == e.Success, e.RunnerUserCompileFailed, status)
}
return JudgeStatus{}, e.Success

View File

@ -20,7 +20,7 @@ func FileWrite(filePath string, content []byte) error {
func FileExist(filePath string) bool {
_, err := os.Stat(filePath)
return If(err == nil || os.IsExist(err), true, false).(bool)
return If(err == nil || os.IsExist(err), true, false)
}
func FileEmpty(filePath string) bool {
@ -35,5 +35,5 @@ func FileTouch(filePath string) bool {
base := filepath.Dir(filePath)
_ = os.MkdirAll(base, 0755)
_, err := os.OpenFile(filePath, os.O_RDONLY|os.O_CREATE, 0644)
return If(err == nil, true, false).(bool)
return If(err == nil, true, false)
}

View File

@ -1,6 +1,6 @@
package utils
func If(condition bool, trueVal, falseVal interface{}) interface{} {
func If[T any](condition bool, trueVal, falseVal T) T {
if condition {
return trueVal
} else {