Use pointer receiver more offen

This commit is contained in:
nemunaire 2021-11-22 15:35:07 +01:00
parent 6999b4e728
commit c7569b5e54
59 changed files with 688 additions and 672 deletions

View file

@ -66,24 +66,24 @@ func init() {
// Synchronize
router.POST("/api/sync/themes/:thid/exercices/:eid", apiHandler(themedExerciceHandler(
func(theme fic.Theme, exercice fic.Exercice, _ []byte) (interface{}, error) {
func(theme *fic.Theme, exercice *fic.Exercice, _ []byte) (interface{}, error) {
_, _, errs := sync.SyncExercice(sync.GlobalImporter, theme, exercice.Path, nil)
return errs, nil
})))
router.POST("/api/sync/exercices/:eid/hints", apiHandler(exerciceHandler(
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
func(exercice *fic.Exercice, _ []byte) (interface{}, error) {
_, errs := sync.SyncExerciceHints(sync.GlobalImporter, exercice, sync.ExerciceFlagsMap(sync.GlobalImporter, exercice))
return errs, nil
})))
router.POST("/api/sync/exercices/:eid/flags", apiHandler(exerciceHandler(
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
func(exercice *fic.Exercice, _ []byte) (interface{}, error) {
_, errs := sync.SyncExerciceFlags(sync.GlobalImporter, exercice)
_, herrs := sync.SyncExerciceHints(sync.GlobalImporter, exercice, sync.ExerciceFlagsMap(sync.GlobalImporter, exercice))
return append(errs, herrs...), nil
})))
router.POST("/api/sync/exercices/:eid/fixurlid", apiHandler(exerciceHandler(
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
func(exercice *fic.Exercice, _ []byte) (interface{}, error) {
if exercice.FixURLId() {
return exercice.Update()
}
@ -125,13 +125,13 @@ func loadFlags(n func() ([]fic.Flag, error)) (interface{}, error) {
var ret []fic.Flag
for _, flag := range flags {
if f, ok := flag.(fic.FlagKey); ok {
if f, ok := flag.(*fic.FlagKey); ok {
if k, err := fic.GetFlagKey(f.Id); err != nil {
return nil, err
} else {
ret = append(ret, k)
}
} else if f, ok := flag.(fic.MCQ); ok {
} else if f, ok := flag.(*fic.MCQ); ok {
if m, err := fic.GetMCQ(f.Id); err != nil {
return nil, err
} else {
@ -146,27 +146,27 @@ func loadFlags(n func() ([]fic.Flag, error)) (interface{}, error) {
}
}
func listExerciceHints(exercice fic.Exercice, body []byte) (interface{}, error) {
func listExerciceHints(exercice *fic.Exercice, body []byte) (interface{}, error) {
return exercice.GetHints()
}
func listExerciceFlags(exercice fic.Exercice, body []byte) (interface{}, error) {
func listExerciceFlags(exercice *fic.Exercice, body []byte) (interface{}, error) {
return exercice.GetFlagKeys()
}
func listFlagChoices(flag fic.FlagKey, _ fic.Exercice, body []byte) (interface{}, error) {
func listFlagChoices(flag *fic.FlagKey, _ *fic.Exercice, body []byte) (interface{}, error) {
return flag.GetChoices()
}
func listExerciceQuiz(exercice fic.Exercice, body []byte) (interface{}, error) {
func listExerciceQuiz(exercice *fic.Exercice, body []byte) (interface{}, error) {
return exercice.GetMCQ()
}
func showExercice(exercice fic.Exercice, body []byte) (interface{}, error) {
func showExercice(exercice *fic.Exercice, body []byte) (interface{}, error) {
return exercice, nil
}
func getExerciceHistory(exercice fic.Exercice, body []byte) (interface{}, error) {
func getExerciceHistory(exercice *fic.Exercice, body []byte) (interface{}, error) {
return exercice.GetHistory()
}
@ -179,7 +179,7 @@ type exerciceStats struct {
MCQSolved []int64 `json:"mcq_solved"`
}
func getExerciceStats(e fic.Exercice, body []byte) (interface{}, error) {
func getExerciceStats(e *fic.Exercice, body []byte) (interface{}, error) {
return exerciceStats{
TeamTries: e.TriedTeamCount(),
TotalTries: e.TriedCount(),
@ -216,7 +216,7 @@ type uploadedExerciceHistory struct {
Coeff float32
}
func updateExerciceHistory(exercice fic.Exercice, body []byte) (interface{}, error) {
func updateExerciceHistory(exercice *fic.Exercice, body []byte) (interface{}, error) {
var uh uploadedExerciceHistory
if err := json.Unmarshal(body, &uh); err != nil {
return nil, err
@ -225,7 +225,7 @@ func updateExerciceHistory(exercice fic.Exercice, body []byte) (interface{}, err
return exercice.UpdateHistoryItem(uh.Coeff, uh.IdTeam, uh.Kind, uh.Time, uh.Secondary)
}
func delExerciceHistory(exercice fic.Exercice, body []byte) (interface{}, error) {
func delExerciceHistory(exercice *fic.Exercice, body []byte) (interface{}, error) {
var uh uploadedExerciceHistory
if err := json.Unmarshal(body, &uh); err != nil {
return nil, err
@ -234,11 +234,11 @@ func delExerciceHistory(exercice fic.Exercice, body []byte) (interface{}, error)
return exercice.DelHistoryItem(uh.IdTeam, uh.Kind, uh.Time, uh.Secondary)
}
func deleteExercice(exercice fic.Exercice, _ []byte) (interface{}, error) {
func deleteExercice(exercice *fic.Exercice, _ []byte) (interface{}, error) {
return exercice.DeleteCascade()
}
func updateExercice(exercice fic.Exercice, body []byte) (interface{}, error) {
func updateExercice(exercice *fic.Exercice, body []byte) (interface{}, error) {
var ue fic.Exercice
if err := json.Unmarshal(body, &ue); err != nil {
return nil, err
@ -257,7 +257,7 @@ func updateExercice(exercice fic.Exercice, body []byte) (interface{}, error) {
return ue, nil
}
func partUpdateExercice(exercice fic.Exercice, body []byte) (interface{}, error) {
func partUpdateExercice(exercice *fic.Exercice, body []byte) (interface{}, error) {
var ue fic.Exercice
if err := json.Unmarshal(body, &ue); err != nil {
return nil, err
@ -318,7 +318,7 @@ func partUpdateExercice(exercice fic.Exercice, body []byte) (interface{}, error)
return exercice, nil
}
func createExercice(theme fic.Theme, body []byte) (interface{}, error) {
func createExercice(theme *fic.Theme, body []byte) (interface{}, error) {
// Create a new exercice
var ue fic.Exercice
if err := json.Unmarshal(body, &ue); err != nil {
@ -334,7 +334,7 @@ func createExercice(theme fic.Theme, body []byte) (interface{}, error) {
if d, err := fic.GetExercice(*ue.Depend); err != nil {
return nil, err
} else {
depend = &d
depend = d
}
}
@ -349,7 +349,7 @@ type uploadedHint struct {
URI string
}
func createExerciceHint(exercice fic.Exercice, body []byte) (interface{}, error) {
func createExerciceHint(exercice *fic.Exercice, body []byte) (interface{}, error) {
var uh uploadedHint
if err := json.Unmarshal(body, &uh); err != nil {
return nil, err
@ -367,15 +367,15 @@ func createExerciceHint(exercice fic.Exercice, body []byte) (interface{}, error)
}
}
func showExerciceHint(hint fic.EHint, body []byte) (interface{}, error) {
func showExerciceHint(hint *fic.EHint, body []byte) (interface{}, error) {
return hint, nil
}
func showExerciceHintDeps(hint fic.EHint, body []byte) (interface{}, error) {
func showExerciceHintDeps(hint *fic.EHint, body []byte) (interface{}, error) {
return loadFlags(hint.GetDepends)
}
func updateExerciceHint(hint fic.EHint, body []byte) (interface{}, error) {
func updateExerciceHint(hint *fic.EHint, body []byte) (interface{}, error) {
var uh fic.EHint
if err := json.Unmarshal(body, &uh); err != nil {
return nil, err
@ -394,7 +394,7 @@ func updateExerciceHint(hint fic.EHint, body []byte) (interface{}, error) {
return uh, nil
}
func deleteExerciceHint(hint fic.EHint, _ []byte) (interface{}, error) {
func deleteExerciceHint(hint *fic.EHint, _ []byte) (interface{}, error) {
return hint.Delete()
}
@ -410,7 +410,7 @@ type uploadedFlag struct {
ChoicesCost int64 `json:"choices_cost"`
}
func createExerciceFlag(exercice fic.Exercice, body []byte) (interface{}, error) {
func createExerciceFlag(exercice *fic.Exercice, body []byte) (interface{}, error) {
var uk uploadedFlag
if err := json.Unmarshal(body, &uk); err != nil {
return nil, err
@ -428,15 +428,15 @@ func createExerciceFlag(exercice fic.Exercice, body []byte) (interface{}, error)
return exercice.AddRawFlagKey(uk.Label, uk.Type, uk.Placeholder, uk.IgnoreCase, uk.Multiline, vre, []byte(uk.Flag), uk.ChoicesCost)
}
func showExerciceFlag(flag fic.FlagKey, _ fic.Exercice, body []byte) (interface{}, error) {
func showExerciceFlag(flag *fic.FlagKey, _ *fic.Exercice, body []byte) (interface{}, error) {
return flag, nil
}
func showExerciceFlagDeps(flag fic.FlagKey, _ fic.Exercice, body []byte) (interface{}, error) {
func showExerciceFlagDeps(flag *fic.FlagKey, _ *fic.Exercice, body []byte) (interface{}, error) {
return loadFlags(flag.GetDepends)
}
func tryExerciceFlag(flag fic.FlagKey, _ fic.Exercice, body []byte) (interface{}, error) {
func tryExerciceFlag(flag *fic.FlagKey, _ *fic.Exercice, body []byte) (interface{}, error) {
var uk uploadedFlag
if err := json.Unmarshal(body, &uk); err != nil {
return nil, err
@ -453,7 +453,7 @@ func tryExerciceFlag(flag fic.FlagKey, _ fic.Exercice, body []byte) (interface{}
}
}
func updateExerciceFlag(flag fic.FlagKey, exercice fic.Exercice, body []byte) (interface{}, error) {
func updateExerciceFlag(flag *fic.FlagKey, exercice *fic.Exercice, body []byte) (interface{}, error) {
var uk uploadedFlag
if err := json.Unmarshal(body, &uk); err != nil {
return nil, err
@ -492,11 +492,11 @@ func updateExerciceFlag(flag fic.FlagKey, exercice fic.Exercice, body []byte) (i
return flag, nil
}
func deleteExerciceFlag(flag fic.FlagKey, _ fic.Exercice, _ []byte) (interface{}, error) {
func deleteExerciceFlag(flag *fic.FlagKey, _ *fic.Exercice, _ []byte) (interface{}, error) {
return flag.Delete()
}
func createFlagChoice(flag fic.FlagKey, exercice fic.Exercice, body []byte) (interface{}, error) {
func createFlagChoice(flag *fic.FlagKey, exercice *fic.Exercice, body []byte) (interface{}, error) {
var uc fic.FlagChoice
if err := json.Unmarshal(body, &uc); err != nil {
return nil, err
@ -506,14 +506,14 @@ func createFlagChoice(flag fic.FlagKey, exercice fic.Exercice, body []byte) (int
uc.Label = uc.Value
}
return flag.AddChoice(uc)
return flag.AddChoice(&uc)
}
func showFlagChoice(choice fic.FlagChoice, _ fic.Exercice, body []byte) (interface{}, error) {
func showFlagChoice(choice *fic.FlagChoice, _ *fic.Exercice, body []byte) (interface{}, error) {
return choice, nil
}
func updateFlagChoice(choice fic.FlagChoice, _ fic.Exercice, body []byte) (interface{}, error) {
func updateFlagChoice(choice *fic.FlagChoice, _ *fic.Exercice, body []byte) (interface{}, error) {
var uc fic.FlagChoice
if err := json.Unmarshal(body, &uc); err != nil {
return nil, err
@ -534,19 +534,19 @@ func updateFlagChoice(choice fic.FlagChoice, _ fic.Exercice, body []byte) (inter
return choice, nil
}
func deleteFlagChoice(choice fic.FlagChoice, _ fic.Exercice, _ []byte) (interface{}, error) {
func deleteFlagChoice(choice *fic.FlagChoice, _ *fic.Exercice, _ []byte) (interface{}, error) {
return choice.Delete()
}
func showExerciceQuiz(quiz fic.MCQ, _ fic.Exercice, body []byte) (interface{}, error) {
func showExerciceQuiz(quiz *fic.MCQ, _ *fic.Exercice, body []byte) (interface{}, error) {
return quiz, nil
}
func showExerciceQuizDeps(quiz fic.MCQ, _ fic.Exercice, body []byte) (interface{}, error) {
func showExerciceQuizDeps(quiz *fic.MCQ, _ *fic.Exercice, body []byte) (interface{}, error) {
return loadFlags(quiz.GetDepends)
}
func updateExerciceQuiz(quiz fic.MCQ, exercice fic.Exercice, body []byte) (interface{}, error) {
func updateExerciceQuiz(quiz *fic.MCQ, exercice *fic.Exercice, body []byte) (interface{}, error) {
var uq fic.MCQ
if err := json.Unmarshal(body, &uq); err != nil {
return nil, err
@ -604,7 +604,7 @@ func updateExerciceQuiz(quiz fic.MCQ, exercice fic.Exercice, body []byte) (inter
return quiz, nil
}
func deleteExerciceQuiz(quiz fic.MCQ, _ fic.Exercice, _ []byte) (interface{}, error) {
func deleteExerciceQuiz(quiz *fic.MCQ, _ *fic.Exercice, _ []byte) (interface{}, error) {
for _, choice := range quiz.Entries {
if _, err := choice.Delete(); err != nil {
return nil, err
@ -614,11 +614,11 @@ func deleteExerciceQuiz(quiz fic.MCQ, _ fic.Exercice, _ []byte) (interface{}, er
return quiz.Delete()
}
func listExerciceTags(exercice fic.Exercice, _ []byte) (interface{}, error) {
func listExerciceTags(exercice *fic.Exercice, _ []byte) (interface{}, error) {
return exercice.GetTags()
}
func addExerciceTag(exercice fic.Exercice, body []byte) (interface{}, error) {
func addExerciceTag(exercice *fic.Exercice, body []byte) (interface{}, error) {
var ut []string
if err := json.Unmarshal(body, &ut); err != nil {
return nil, err
@ -634,7 +634,7 @@ func addExerciceTag(exercice fic.Exercice, body []byte) (interface{}, error) {
return ut, nil
}
func updateExerciceTags(exercice fic.Exercice, body []byte) (interface{}, error) {
func updateExerciceTags(exercice *fic.Exercice, body []byte) (interface{}, error) {
exercice.WipeTags()
return addExerciceTag(exercice, body)
}