From a7271f5d0a2dda43cc6fa6d34154ab1bc6754ed9 Mon Sep 17 00:00:00 2001 From: yujular <759099345@qq.com> Date: Tue, 2 Apr 2024 22:00:39 +0800 Subject: [PATCH] feat(question): Change Answer to Index of Options --- apis/cache.go | 18 +++++++++--------- apis/question.go | 4 ++-- apis/schemas.go | 7 +++++-- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/apis/cache.go b/apis/cache.go index b4b4655..df87249 100644 --- a/apis/cache.go +++ b/apis/cache.go @@ -10,7 +10,6 @@ import ( "github.com/goccy/go-json" "github.com/opentreehole/go-common" "github.com/rs/zerolog/log" - "golang.org/x/exp/slices" "gopkg.in/yaml.v3" "auth_next/config" @@ -159,11 +158,9 @@ LOAD_FILES: valid = false continue } - if !slices.Contains(currentQuestion.Options, currentQuestion.Answer[0]) { - currentQuestion.Options = append(currentQuestion.Options, currentQuestion.Answer[0]) - } + currentQuestion.AnswerOptions = append(currentQuestion.AnswerOptions, currentQuestion.Options[currentQuestion.Answer[0]]) case TrueOrFalse: - if len(currentQuestion.Answer) != 1 || (currentQuestion.Answer[0] != "true" && currentQuestion.Answer[0] != "false") { + if len(currentQuestion.Answer) != 1 || (currentQuestion.Answer[0] != 0 && currentQuestion.Answer[0] != 1) { log.Warn(). Str("filename", file.Name()). Str("question", currentQuestion.Question). @@ -172,6 +169,8 @@ LOAD_FILES: valid = false continue } + currentQuestion.AnswerOptions = append(currentQuestion.AnswerOptions, + currentQuestion.Options[currentQuestion.Answer[0]]) case MultiSelection: if len(currentQuestion.Answer) < 1 { log.Warn(). @@ -182,12 +181,13 @@ LOAD_FILES: valid = false continue } - for _, answer := range currentQuestion.Answer { - if !slices.Contains(currentQuestion.Options, answer) { - currentQuestion.Options = append(currentQuestion.Options, answer) + currentQuestion.AnswerOptions = make([]string, 0, len(currentQuestion.Answer)) + for _, index := range currentQuestion.Answer { + if index < len(currentQuestion.Options) { + currentQuestion.AnswerOptions = append(currentQuestion.AnswerOptions, currentQuestion.Options[index]) } } - sort.Strings(currentQuestion.Answer) + sort.Strings(currentQuestion.AnswerOptions) } } if !valid { diff --git a/apis/question.go b/apis/question.go index 2b10b70..55d3897 100644 --- a/apis/question.go +++ b/apis/question.go @@ -223,7 +223,7 @@ func AnswerQuestions(c *fiber.Ctx) (err error) { case SingleSelection: fallthrough case TrueOrFalse: - if len(answer.Answer) != 1 || answer.Answer[0] != question.Answer[0] { + if len(answer.Answer) != 1 || answer.Answer[0] != question.AnswerOptions[0] { wrongQuestions = append(wrongQuestions, answer.ID) continue } @@ -231,7 +231,7 @@ func AnswerQuestions(c *fiber.Ctx) (err error) { sortedAnswer := make([]string, len(answer.Answer)) copy(sortedAnswer, answer.Answer) sort.Strings(sortedAnswer) - if slices.Equal(sortedAnswer, question.Answer) { + if !slices.Equal(sortedAnswer, question.AnswerOptions) { wrongQuestions = append(wrongQuestions, answer.ID) continue } diff --git a/apis/schemas.go b/apis/schemas.go index 2859af6..74dc480 100644 --- a/apis/schemas.go +++ b/apis/schemas.go @@ -109,11 +109,14 @@ type Question struct { // 问题描述 Question string `json:"question" yaml:"question" validate:"required"` - // 答案描述,单选、判断、多选 + // 答案索引,单选、判断、多选,从 0 开始 + Answer []int `json:"answer,omitempty" yaml:"answer,omitempty" validate:"min=1"` + + // 根据Answer的索引解析出的答案, 单选、判断、多选 // 如果是单选题,则只有一个答案 // 如果是多选题,则有一个或多个答案 // 如果是判断题,则只有一个答案,且只能是 true 或者 false - Answer []string `json:"answer,omitempty" yaml:"answer,omitempty" validate:"min=1"` + AnswerOptions []string `json:"-" yaml:"-"` // 选项描述,单选、判断、多选 // 有一个或多个选项,如果是判断题,则选项只能是 true 或者 false