Use pointer receiver more offen

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

View file

@ -67,7 +67,7 @@ func init() {
}))
router.GET("/api/teams/:tid/certificates", apiHandler(teamHandler(
func(team fic.Team, _ []byte) (interface{}, error) {
func(team *fic.Team, _ []byte) (interface{}, error) {
if serials, err := pki.GetTeamSerials(TeamsDir, team.Id); err != nil {
return nil, err
} else {
@ -84,18 +84,18 @@ func init() {
})))
router.GET("/api/teams/:tid/associations", apiHandler(teamHandler(
func(team fic.Team, _ []byte) (interface{}, error) {
func(team *fic.Team, _ []byte) (interface{}, error) {
return pki.GetTeamAssociations(TeamsDir, team.Id)
})))
router.POST("/api/teams/:tid/associations/:assoc", apiHandler(teamAssocHandler(
func(team fic.Team, assoc string, _ []byte) (interface{}, error) {
func(team *fic.Team, assoc string, _ []byte) (interface{}, error) {
if err := os.Symlink(fmt.Sprintf("%d", team.Id), path.Join(TeamsDir, assoc)); err != nil {
return nil, err
}
return "\"" + assoc + "\"", nil
})))
router.DELETE("/api/teams/:tid/associations/:assoc", apiHandler(teamAssocHandler(
func(team fic.Team, assoc string, _ []byte) (interface{}, error) {
func(team *fic.Team, assoc string, _ []byte) (interface{}, error) {
if err := os.Remove(path.Join(TeamsDir, assoc)); err != nil {
return nil, err
}
@ -110,11 +110,11 @@ func init() {
router.GET("/api/certs/:certid", apiHandler(certificateHandler(getTeamP12File)))
router.PUT("/api/certs/:certid", apiHandler(certificateHandler(updateCertificateAssociation)))
router.DELETE("/api/certs/:certid", apiHandler(certificateHandler(
func(cert fic.Certificate, _ []byte) (interface{}, error) { return cert.Revoke() })))
func(cert *fic.Certificate, _ []byte) (interface{}, error) { return cert.Revoke() })))
}
func genHtpasswd(ssha bool) (ret string, err error) {
var teams []fic.Team
var teams []*fic.Team
teams, err = fic.GetTeams()
if err != nil {
return
@ -133,7 +133,7 @@ func genHtpasswd(ssha bool) (ret string, err error) {
}
for _, serial := range serials {
var cert fic.Certificate
var cert *fic.Certificate
cert, err = fic.GetCertificate(serial)
if err != nil {
// Ignore invalid/incorrect/non-existant certificates
@ -219,7 +219,7 @@ func getCAPEM(_ httprouter.Params, _ []byte) (interface{}, error) {
}
}
func getTeamP12File(cert fic.Certificate, _ []byte) (interface{}, error) {
func getTeamP12File(cert *fic.Certificate, _ []byte) (interface{}, error) {
// Create p12 if necessary
if _, err := os.Stat(pki.ClientP12Path(cert.Id)); os.IsNotExist(err) {
if err := pki.WriteP12(cert.Id, cert.Password); err != nil {
@ -308,7 +308,7 @@ type CertUploaded struct {
Team *int64 `json:"id_team"`
}
func updateCertificateAssociation(cert fic.Certificate, body []byte) (interface{}, error) {
func updateCertificateAssociation(cert *fic.Certificate, body []byte) (interface{}, error) {
var uc CertUploaded
if err := json.Unmarshal(body, &uc); err != nil {
return nil, err

View file

@ -15,7 +15,7 @@ import (
func init() {
router.GET("/api/teams/:tid/issue.json", apiHandler(teamHandler(
func(team fic.Team, _ []byte) (interface{}, error) {
func(team *fic.Team, _ []byte) (interface{}, error) {
return team.MyIssueFile()
})))
@ -48,35 +48,35 @@ func getClaims(_ httprouter.Params, _ []byte) (interface{}, error) {
return fic.GetClaims()
}
func getTeamClaims(team fic.Team, _ []byte) (interface{}, error) {
func getTeamClaims(team *fic.Team, _ []byte) (interface{}, error) {
return team.GetClaims()
}
func getExerciceClaims(exercice fic.Exercice, _ []byte) (interface{}, error) {
func getExerciceClaims(exercice *fic.Exercice, _ []byte) (interface{}, error) {
return exercice.GetClaims()
}
func getClaimLastUpdate(claim fic.Claim, _ []byte) (interface{}, error) {
func getClaimLastUpdate(claim *fic.Claim, _ []byte) (interface{}, error) {
return claim.GetLastUpdate()
}
type ClaimExported struct {
Id int64 `json:"id"`
Subject string `json:"subject"`
IdTeam *int64 `json:"id_team"`
Team *fic.Team `json:"team"`
IdExercice *int64 `json:"id_exercice"`
Exercice *fic.Exercice `json:"exercice"`
IdAssignee *int64 `json:"id_assignee"`
Assignee *fic.ClaimAssignee `json:"assignee"`
Creation time.Time `json:"creation"`
LastUpdate time.Time `json:"last_update"`
State string `json:"state"`
Priority string `json:"priority"`
Descriptions []fic.ClaimDescription `json:"descriptions"`
Id int64 `json:"id"`
Subject string `json:"subject"`
IdTeam *int64 `json:"id_team"`
Team *fic.Team `json:"team"`
IdExercice *int64 `json:"id_exercice"`
Exercice *fic.Exercice `json:"exercice"`
IdAssignee *int64 `json:"id_assignee"`
Assignee *fic.ClaimAssignee `json:"assignee"`
Creation time.Time `json:"creation"`
LastUpdate time.Time `json:"last_update"`
State string `json:"state"`
Priority string `json:"priority"`
Descriptions []*fic.ClaimDescription `json:"descriptions"`
}
func showClaim(claim fic.Claim, _ []byte) (interface{}, error) {
func showClaim(claim *fic.Claim, _ []byte) (interface{}, error) {
var e ClaimExported
var err error
if e.Team, err = claim.GetTeam(); err != nil {
@ -130,7 +130,7 @@ func newClaim(_ httprouter.Params, body []byte) (interface{}, error) {
if team, err := fic.GetTeam(*uc.IdTeam); err != nil {
return nil, fmt.Errorf("Unable to get associated team: %w", err)
} else {
t = &team
t = team
}
} else {
t = nil
@ -141,7 +141,7 @@ func newClaim(_ httprouter.Params, body []byte) (interface{}, error) {
if exercice, err := fic.GetExercice(*uc.IdExercice); err != nil {
return nil, fmt.Errorf("Unable to get associated exercice: %w", err)
} else {
e = &exercice
e = exercice
}
} else {
e = nil
@ -152,7 +152,7 @@ func newClaim(_ httprouter.Params, body []byte) (interface{}, error) {
if assignee, err := fic.GetAssignee(*uc.IdAssignee); err != nil {
return nil, fmt.Errorf("Unable to get associated assignee: %w", err)
} else {
a = &assignee
a = assignee
}
} else {
a = nil
@ -180,7 +180,7 @@ func generateTeamIssuesFile(team fic.Team) error {
return nil
}
func addClaimDescription(claim fic.Claim, body []byte) (interface{}, error) {
func addClaimDescription(claim *fic.Claim, body []byte) (interface{}, error) {
var ud fic.ClaimDescription
if err := json.Unmarshal(body, &ud); err != nil {
return nil, fmt.Errorf("Unable to decode JSON: %w", err)
@ -199,7 +199,7 @@ func addClaimDescription(claim fic.Claim, body []byte) (interface{}, error) {
}
}
func updateClaimDescription(claim fic.Claim, body []byte) (interface{}, error) {
func updateClaimDescription(claim *fic.Claim, body []byte) (interface{}, error) {
var ud fic.ClaimDescription
if err := json.Unmarshal(body, &ud); err != nil {
return nil, fmt.Errorf("Unable to decode JSON: %w", err)
@ -216,7 +216,7 @@ func updateClaimDescription(claim fic.Claim, body []byte) (interface{}, error) {
}
}
func updateClaim(claim fic.Claim, body []byte) (interface{}, error) {
func updateClaim(claim *fic.Claim, body []byte) (interface{}, error) {
var uc ClaimUploaded
if err := json.Unmarshal(body, &uc); err != nil {
return nil, fmt.Errorf("Unable to decode JSON: %w", err)
@ -261,7 +261,7 @@ func updateClaim(claim fic.Claim, body []byte) (interface{}, error) {
}
}
func deleteClaim(claim fic.Claim, _ []byte) (interface{}, error) {
func deleteClaim(claim *fic.Claim, _ []byte) (interface{}, error) {
return claim.Delete()
}
@ -269,7 +269,7 @@ func getAssignees(_ httprouter.Params, _ []byte) (interface{}, error) {
return fic.GetAssignees()
}
func showClaimAssignee(assignee fic.ClaimAssignee, _ []byte) (interface{}, error) {
func showClaimAssignee(assignee *fic.ClaimAssignee, _ []byte) (interface{}, error) {
return assignee, nil
}
func newAssignee(_ httprouter.Params, body []byte) (interface{}, error) {
@ -281,7 +281,7 @@ func newAssignee(_ httprouter.Params, body []byte) (interface{}, error) {
return fic.NewClaimAssignee(ua.Name)
}
func updateClaimAssignee(assignee fic.ClaimAssignee, body []byte) (interface{}, error) {
func updateClaimAssignee(assignee *fic.ClaimAssignee, body []byte) (interface{}, error) {
var ua fic.ClaimAssignee
if err := json.Unmarshal(body, &ua); err != nil {
return nil, fmt.Errorf("Unable to decode JSON: %w", err)
@ -296,6 +296,6 @@ func updateClaimAssignee(assignee fic.ClaimAssignee, body []byte) (interface{},
}
}
func deleteClaimAssignee(assignee fic.ClaimAssignee, _ []byte) (interface{}, error) {
func deleteClaimAssignee(assignee *fic.ClaimAssignee, _ []byte) (interface{}, error) {
return assignee.Delete()
}

View file

@ -49,7 +49,7 @@ func getLastEvents(_ httprouter.Params, _ []byte) (interface{}, error) {
}
}
func showEvent(event fic.Event, _ []byte) (interface{}, error) {
func showEvent(event *fic.Event, _ []byte) (interface{}, error) {
return event, nil
}
@ -71,7 +71,7 @@ func clearEvents(_ httprouter.Params, _ []byte) (interface{}, error) {
return fic.ClearEvents()
}
func updateEvent(event fic.Event, body []byte) (interface{}, error) {
func updateEvent(event *fic.Event, body []byte) (interface{}, error) {
var ue fic.Event
if err := json.Unmarshal(body, &ue); err != nil {
return nil, err
@ -87,7 +87,7 @@ func updateEvent(event fic.Event, body []byte) (interface{}, error) {
}
}
func deleteEvent(event fic.Event, _ []byte) (interface{}, error) {
func deleteEvent(event *fic.Event, _ []byte) (interface{}, error) {
if i, err := event.Delete(); err != nil {
return i, err
} else {

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)
}

View file

@ -36,17 +36,17 @@ func init() {
// Synchronize
router.POST("/api/sync/exercices/:eid/files", apiHandler(exerciceHandler(
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
func(exercice *fic.Exercice, _ []byte) (interface{}, error) {
return sync.SyncExerciceFiles(sync.GlobalImporter, exercice), nil
})))
}
type APIFile struct {
fic.EFile
*fic.EFile
Depends []fic.Flag `json:"depends,omitempty"`
}
func genFileList(in []fic.EFile, e error) (out []APIFile, err error) {
func genFileList(in []*fic.EFile, e error) (out []APIFile, err error) {
if e != nil {
return nil, e
}
@ -61,14 +61,14 @@ func genFileList(in []fic.EFile, e error) (out []APIFile, err error) {
}
for _, d := range deps {
if k, ok := d.(fic.FlagKey); ok {
if k, ok := d.(*fic.FlagKey); ok {
k, err = fic.GetFlagKey(k.Id)
if err != nil {
return
}
g.Depends = append(g.Depends, k)
} else if m, ok := d.(fic.MCQ); ok {
} else if m, ok := d.(*fic.MCQ); ok {
m, err = fic.GetMCQ(m.Id)
if err != nil {
return
@ -91,7 +91,7 @@ func listFiles(_ httprouter.Params, body []byte) (interface{}, error) {
return genFileList(fic.GetFiles())
}
func listExerciceFiles(exercice fic.Exercice, body []byte) (interface{}, error) {
func listExerciceFiles(exercice *fic.Exercice, body []byte) (interface{}, error) {
return genFileList(exercice.GetFiles())
}
@ -99,7 +99,7 @@ func clearFiles(_ httprouter.Params, _ []byte) (interface{}, error) {
return fic.ClearFiles()
}
func showFile(file fic.EFile, _ []byte) (interface{}, error) {
func showFile(file *fic.EFile, _ []byte) (interface{}, error) {
return file, nil
}
@ -108,7 +108,7 @@ type uploadedFile struct {
Digest string
}
func createExerciceFile(exercice fic.Exercice, body []byte) (interface{}, error) {
func createExerciceFile(exercice *fic.Exercice, body []byte) (interface{}, error) {
var uf uploadedFile
if err := json.Unmarshal(body, &uf); err != nil {
return nil, err
@ -124,7 +124,7 @@ func createExerciceFile(exercice fic.Exercice, body []byte) (interface{}, error)
})
}
func updateFile(file fic.EFile, body []byte) (interface{}, error) {
func updateFile(file *fic.EFile, body []byte) (interface{}, error) {
var uf fic.EFile
if err := json.Unmarshal(body, &uf); err != nil {
return nil, err
@ -139,14 +139,14 @@ func updateFile(file fic.EFile, body []byte) (interface{}, error) {
}
}
func deleteFile(file fic.EFile, _ []byte) (interface{}, error) {
func deleteFile(file *fic.EFile, _ []byte) (interface{}, error) {
return file.Delete()
}
func deleteFileDep(file fic.EFile, depid int, _ []byte) (interface{}, error) {
return true, file.DeleteDepend(fic.FlagKey{Id: depid})
func deleteFileDep(file *fic.EFile, depid int, _ []byte) (interface{}, error) {
return true, file.DeleteDepend(&fic.FlagKey{Id: depid})
}
func checkFile(file fic.EFile, _ []byte) (interface{}, error) {
func checkFile(file *fic.EFile, _ []byte) (interface{}, error) {
return true, file.CheckFileOnDisk()
}

View file

@ -94,12 +94,12 @@ func teamPublicHandler(f func(*fic.Team, []byte) (interface{}, error)) func(http
} else if team, err := fic.GetTeam(tid); err != nil {
return nil, err
} else {
return f(&team, body)
return f(team, body)
}
}
}
func teamHandler(f func(fic.Team, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func teamHandler(f func(*fic.Team, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
if tid, err := strconv.ParseInt(string(ps.ByName("tid")), 10, 64); err != nil {
return nil, err
@ -111,11 +111,11 @@ func teamHandler(f func(fic.Team, []byte) (interface{}, error)) func(httprouter.
}
}
func teamAssocHandler(f func(fic.Team, string, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func teamAssocHandler(f func(*fic.Team, string, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
var team fic.Team
var team *fic.Team
teamHandler(func(tm fic.Team, _ []byte) (interface{}, error) {
teamHandler(func(tm *fic.Team, _ []byte) (interface{}, error) {
team = tm
return nil, nil
})(ps, body)
@ -124,7 +124,7 @@ func teamAssocHandler(f func(fic.Team, string, []byte) (interface{}, error)) fun
}
}
func themeHandler(f func(fic.Theme, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func themeHandler(f func(*fic.Theme, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
if thid, err := strconv.ParseInt(string(ps.ByName("thid")), 10, 64); err != nil {
return nil, err
@ -136,7 +136,7 @@ func themeHandler(f func(fic.Theme, []byte) (interface{}, error)) func(httproute
}
}
func exerciceHandler(f func(fic.Exercice, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func exerciceHandler(f func(*fic.Exercice, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
if eid, err := strconv.ParseInt(string(ps.ByName("eid")), 10, 64); err != nil {
return nil, err
@ -148,17 +148,17 @@ func exerciceHandler(f func(fic.Exercice, []byte) (interface{}, error)) func(htt
}
}
func themedExerciceHandler(f func(fic.Theme, fic.Exercice, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func themedExerciceHandler(f func(*fic.Theme, *fic.Exercice, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
var theme fic.Theme
var exercice fic.Exercice
var theme *fic.Theme
var exercice *fic.Exercice
themeHandler(func(th fic.Theme, _ []byte) (interface{}, error) {
themeHandler(func(th *fic.Theme, _ []byte) (interface{}, error) {
theme = th
return nil, nil
})(ps, body)
exerciceHandler(func(ex fic.Exercice, _ []byte) (interface{}, error) {
exerciceHandler(func(ex *fic.Exercice, _ []byte) (interface{}, error) {
exercice = ex
return nil, nil
})(ps, body)
@ -167,7 +167,7 @@ func themedExerciceHandler(f func(fic.Theme, fic.Exercice, []byte) (interface{},
}
}
func hintHandler(f func(fic.EHint, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func hintHandler(f func(*fic.EHint, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
if hid, err := strconv.ParseInt(string(ps.ByName("hid")), 10, 64); err != nil {
return nil, err
@ -179,10 +179,10 @@ func hintHandler(f func(fic.EHint, []byte) (interface{}, error)) func(httprouter
}
}
func flagKeyHandler(f func(fic.FlagKey, fic.Exercice, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func flagKeyHandler(f func(*fic.FlagKey, *fic.Exercice, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
var exercice fic.Exercice
exerciceHandler(func(ex fic.Exercice, _ []byte) (interface{}, error) {
var exercice *fic.Exercice
exerciceHandler(func(ex *fic.Exercice, _ []byte) (interface{}, error) {
exercice = ex
return nil, nil
})(ps, body)
@ -202,11 +202,11 @@ func flagKeyHandler(f func(fic.FlagKey, fic.Exercice, []byte) (interface{}, erro
}
}
func choiceHandler(f func(fic.FlagChoice, fic.Exercice, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func choiceHandler(f func(*fic.FlagChoice, *fic.Exercice, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
var exercice fic.Exercice
var flag fic.FlagKey
flagKeyHandler(func(fl fic.FlagKey, ex fic.Exercice, _ []byte) (interface{}, error) {
var exercice *fic.Exercice
var flag *fic.FlagKey
flagKeyHandler(func(fl *fic.FlagKey, ex *fic.Exercice, _ []byte) (interface{}, error) {
exercice = ex
flag = fl
return nil, nil
@ -222,10 +222,10 @@ func choiceHandler(f func(fic.FlagChoice, fic.Exercice, []byte) (interface{}, er
}
}
func quizHandler(f func(fic.MCQ, fic.Exercice, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func quizHandler(f func(*fic.MCQ, *fic.Exercice, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
var exercice fic.Exercice
exerciceHandler(func(ex fic.Exercice, _ []byte) (interface{}, error) {
var exercice *fic.Exercice
exerciceHandler(func(ex *fic.Exercice, _ []byte) (interface{}, error) {
exercice = ex
return nil, nil
})(ps, body)
@ -245,10 +245,10 @@ func quizHandler(f func(fic.MCQ, fic.Exercice, []byte) (interface{}, error)) fun
}
}
func exerciceFileHandler(f func(fic.EFile, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func exerciceFileHandler(f func(*fic.EFile, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
var exercice fic.Exercice
exerciceHandler(func(ex fic.Exercice, _ []byte) (interface{}, error) {
var exercice *fic.Exercice
exerciceHandler(func(ex *fic.Exercice, _ []byte) (interface{}, error) {
exercice = ex
return nil, nil
})(ps, body)
@ -268,7 +268,7 @@ func exerciceFileHandler(f func(fic.EFile, []byte) (interface{}, error)) func(ht
}
}
func eventHandler(f func(fic.Event, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func eventHandler(f func(*fic.Event, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
if evid, err := strconv.ParseInt(string(ps.ByName("evid")), 10, 64); err != nil {
return nil, err
@ -280,7 +280,7 @@ func eventHandler(f func(fic.Event, []byte) (interface{}, error)) func(httproute
}
}
func claimHandler(f func(fic.Claim, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func claimHandler(f func(*fic.Claim, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
if cid, err := strconv.ParseInt(string(ps.ByName("cid")), 10, 64); err != nil {
return nil, fmt.Errorf("Invalid claim id: %w", err)
@ -292,7 +292,7 @@ func claimHandler(f func(fic.Claim, []byte) (interface{}, error)) func(httproute
}
}
func claimAssigneeHandler(f func(fic.ClaimAssignee, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func claimAssigneeHandler(f func(*fic.ClaimAssignee, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
if aid, err := strconv.ParseInt(string(ps.ByName("aid")), 10, 64); err != nil {
return nil, err
@ -304,7 +304,7 @@ func claimAssigneeHandler(f func(fic.ClaimAssignee, []byte) (interface{}, error)
}
}
func fileHandler(f func(fic.EFile, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func fileHandler(f func(*fic.EFile, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
if fileid, err := strconv.ParseInt(string(ps.ByName("fileid")), 10, 64); err != nil {
return nil, err
@ -316,19 +316,19 @@ func fileHandler(f func(fic.EFile, []byte) (interface{}, error)) func(httprouter
}
}
func fileDependancyHandler(f func(fic.EFile, int, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func fileDependancyHandler(f func(*fic.EFile, int, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
if depid, err := strconv.ParseInt(string(ps.ByName("depid")), 10, 64); err != nil {
return nil, err
} else {
return fileHandler(func(file fic.EFile, b []byte) (interface{}, error) {
return fileHandler(func(file *fic.EFile, b []byte) (interface{}, error) {
return f(file, int(depid), b)
})(ps, body)
}
}
}
func certificateHandler(f func(fic.Certificate, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func certificateHandler(f func(*fic.Certificate, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
var cid uint64
var err error

View file

@ -25,11 +25,11 @@ func init() {
}
}))
router.GET("/api/teams/:tid/password", apiHandler(teamHandler(
func(team fic.Team, _ []byte) (interface{}, error) {
func(team *fic.Team, _ []byte) (interface{}, error) {
return team.Password, nil
})))
router.POST("/api/teams/:tid/password", apiHandler(teamHandler(
func(team fic.Team, _ []byte) (interface{}, error) {
func(team *fic.Team, _ []byte) (interface{}, error) {
if passwd, err := fic.GeneratePassword(); err != nil {
return nil, err
} else {
@ -145,7 +145,7 @@ type dexConfigClient struct {
type dexConfig struct {
Clients []dexConfigClient
Teams []fic.Team
Teams []*fic.Team
}
func genDexConfig() ([]byte, error) {

View file

@ -15,7 +15,7 @@ func init() {
router.POST("/api/qa/:qid/comments", apiHandler(qaHandler(importQAComment)))
}
func qaHandler(f func(fic.QAQuery, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
func qaHandler(f func(*fic.QAQuery, []byte) (interface{}, error)) func(httprouter.Params, []byte) (interface{}, error) {
return func(ps httprouter.Params, body []byte) (interface{}, error) {
if qid, err := strconv.ParseInt(string(ps.ByName("qid")), 10, 64); err != nil {
return nil, err
@ -34,7 +34,7 @@ func importExerciceQA(_ httprouter.Params, body []byte) (interface{}, error) {
return nil, err
}
var exercice fic.Exercice
var exercice *fic.Exercice
var err error
if uq.IdExercice == 0 {
return nil, errors.New("id_exercice not filled")
@ -62,7 +62,7 @@ func importExerciceQA(_ httprouter.Params, body []byte) (interface{}, error) {
}
}
func importQAComment(query fic.QAQuery, body []byte) (interface{}, error) {
func importQAComment(query *fic.QAQuery, body []byte) (interface{}, error) {
// Create a new query
var uc fic.QAComment
if err := json.Unmarshal(body, &uc); err != nil {

View file

@ -48,17 +48,17 @@ func init() {
router.POST("/api/teams/", apiHandler(createTeam))
router.GET("/api/teams/:tid/", apiHandler(teamHandler(
func(team fic.Team, _ []byte) (interface{}, error) {
func(team *fic.Team, _ []byte) (interface{}, error) {
return team, nil
})))
router.PUT("/api/teams/:tid/", apiHandler(teamHandler(updateTeam)))
router.POST("/api/teams/:tid/", apiHandler(teamHandler(addTeamMember)))
router.DELETE("/api/teams/:tid/", apiHandler(teamHandler(
func(team fic.Team, _ []byte) (interface{}, error) {
func(team *fic.Team, _ []byte) (interface{}, error) {
return team.Delete()
})))
router.GET("/api/teams/:tid/score-grid.json", apiHandler(teamHandler(
func(team fic.Team, _ []byte) (interface{}, error) {
func(team *fic.Team, _ []byte) (interface{}, error) {
return team.ScoreGrid()
})))
router.GET("/api/teams/:tid/my.json", apiHandler(teamPublicHandler(
@ -91,7 +91,7 @@ func init() {
return fic.GetTries(team, nil)
})))
router.GET("/api/teams/:tid/members", apiHandler(teamHandler(
func(team fic.Team, _ []byte) (interface{}, error) {
func(team *fic.Team, _ []byte) (interface{}, error) {
return team.GetMembers()
})))
router.POST("/api/teams/:tid/members", apiHandler(teamHandler(addTeamMember)))
@ -163,7 +163,7 @@ func createTeam(_ httprouter.Params, body []byte) (interface{}, error) {
return fic.CreateTeam(strings.TrimSpace(ut.Name), ut.Color, ut.ExternalId)
}
func updateTeam(team fic.Team, body []byte) (interface{}, error) {
func updateTeam(team *fic.Team, body []byte) (interface{}, error) {
var ut fic.Team
if err := json.Unmarshal(body, &ut); err != nil {
return nil, err
@ -229,7 +229,7 @@ func enableAllTeams(_ httprouter.Params, _ []byte) (interface{}, error) {
}
}
func addTeamMember(team fic.Team, body []byte) (interface{}, error) {
func addTeamMember(team *fic.Team, body []byte) (interface{}, error) {
var members []fic.Member
if err := json.Unmarshal(body, &members); err != nil {
return nil, err
@ -242,7 +242,7 @@ func addTeamMember(team fic.Team, body []byte) (interface{}, error) {
return team.GetMembers()
}
func setTeamMember(team fic.Team, body []byte) (interface{}, error) {
func setTeamMember(team *fic.Team, body []byte) (interface{}, error) {
var members []fic.Member
if err := json.Unmarshal(body, &members); err != nil {
return nil, err

View file

@ -72,7 +72,7 @@ func init() {
return sync.SyncDeep(sync.GlobalImporter), nil
}))
router.POST("/api/sync/deep/:thid", apiHandler(themeHandler(
func(theme fic.Theme, _ []byte) (interface{}, error) {
func(theme *fic.Theme, _ []byte) (interface{}, error) {
st := sync.SyncThemeDeep(sync.GlobalImporter, theme, 0, 250)
sync.EditDeepReport(map[string][]string{theme.Name: st}, false)
sync.DeepSyncProgress = 255
@ -126,27 +126,27 @@ func init() {
return sync.SyncThemes(sync.GlobalImporter), nil
}))
router.POST("/api/sync/themes/:thid/exercices", apiHandler(themeHandler(
func(theme fic.Theme, _ []byte) (interface{}, error) {
func(theme *fic.Theme, _ []byte) (interface{}, error) {
return sync.SyncExercices(sync.GlobalImporter, theme), nil
})))
router.POST("/api/sync/themes/:thid/exercices/:eid/files", apiHandler(exerciceHandler(
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
func(exercice *fic.Exercice, _ []byte) (interface{}, error) {
return sync.SyncExerciceFiles(sync.GlobalImporter, exercice), nil
})))
router.POST("/api/sync/themes/:thid/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/themes/:thid/exercices/:eid/keys", 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/themes/:thid/fixurlid", apiHandler(themeHandler(
func(theme fic.Theme, _ []byte) (interface{}, error) {
func(theme *fic.Theme, _ []byte) (interface{}, error) {
if theme.FixURLId() {
return theme.Update()
}
@ -190,13 +190,13 @@ func bindingFiles(_ httprouter.Params, body []byte) (interface{}, error) {
}
}
func getExercice(args []string) (fic.Exercice, error) {
func getExercice(args []string) (*fic.Exercice, error) {
if tid, err := strconv.ParseInt(string(args[0]), 10, 64); err != nil {
return fic.Exercice{}, err
return nil, err
} else if theme, err := fic.GetTheme(tid); err != nil {
return fic.Exercice{}, err
return nil, err
} else if eid, err := strconv.Atoi(string(args[1])); err != nil {
return fic.Exercice{}, err
return nil, err
} else {
return theme.GetExercice(eid)
}
@ -210,15 +210,15 @@ func exportThemes(_ httprouter.Params, _ []byte) (interface{}, error) {
return fic.ExportThemes()
}
func showTheme(theme fic.Theme, _ []byte) (interface{}, error) {
func showTheme(theme *fic.Theme, _ []byte) (interface{}, error) {
return theme, nil
}
func listThemedExercices(theme fic.Theme, _ []byte) (interface{}, error) {
func listThemedExercices(theme *fic.Theme, _ []byte) (interface{}, error) {
return theme.GetExercices()
}
func showThemedExercice(theme fic.Theme, exercice fic.Exercice, body []byte) (interface{}, error) {
func showThemedExercice(theme *fic.Theme, exercice fic.Exercice, body []byte) (interface{}, error) {
return exercice, nil
}
@ -232,10 +232,10 @@ func createTheme(_ httprouter.Params, body []byte) (interface{}, error) {
return nil, errors.New("Theme's name not filled")
}
return fic.CreateTheme(ut)
return fic.CreateTheme(&ut)
}
func updateTheme(theme fic.Theme, body []byte) (interface{}, error) {
func updateTheme(theme *fic.Theme, body []byte) (interface{}, error) {
var ut fic.Theme
if err := json.Unmarshal(body, &ut); err != nil {
return nil, err
@ -254,11 +254,11 @@ func updateTheme(theme fic.Theme, body []byte) (interface{}, error) {
}
}
func deleteTheme(theme fic.Theme, _ []byte) (interface{}, error) {
func deleteTheme(theme *fic.Theme, _ []byte) (interface{}, error) {
return theme.Delete()
}
func getThemedExercicesStats(theme fic.Theme, body []byte) (interface{}, error) {
func getThemedExercicesStats(theme *fic.Theme, body []byte) (interface{}, error) {
if exercices, err := theme.GetExercices(); err != nil {
return nil, err
} else {

View file

@ -83,7 +83,7 @@ func parseExerciceParams(i Importer, exPath string) (p ExerciceParams, md toml.M
}
// getExerciceParams returns normalized
func getExerciceParams(i Importer, exercice fic.Exercice) (params ExerciceParams, errs []string) {
func getExerciceParams(i Importer, exercice *fic.Exercice) (params ExerciceParams, errs []string) {
var err error
if params, _, err = parseExerciceParams(i, exercice.Path); err != nil {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: %s", path.Base(exercice.Path), err))

View file

@ -13,7 +13,7 @@ import (
"srs.epita.fr/fic-server/libfic"
)
func BuildFilesListInto(i Importer, exercice fic.Exercice, into string) (files []string, digests map[string][]byte, errs []string) {
func BuildFilesListInto(i Importer, exercice *fic.Exercice, into string) (files []string, digests map[string][]byte, errs []string) {
// If no files directory, don't display error
if !i.exists(path.Join(exercice.Path, into)) {
return
@ -72,7 +72,7 @@ func BuildFilesListInto(i Importer, exercice fic.Exercice, into string) (files [
}
// CheckExerciceFilesPresence limits remote checks to presence, don't get it to check digest.
func CheckExerciceFilesPresence(i Importer, exercice fic.Exercice) (files []string, errs []string) {
func CheckExerciceFilesPresence(i Importer, exercice *fic.Exercice) (files []string, errs []string) {
flist, digests, berrs := BuildFilesListInto(i, exercice, "files")
errs = append(errs, berrs...)
@ -96,7 +96,7 @@ func CheckExerciceFilesPresence(i Importer, exercice fic.Exercice) (files []stri
}
// CheckExerciceFiles checks that remote files have the right digest.
func CheckExerciceFiles(i Importer, exercice fic.Exercice) (files []string, errs []string) {
func CheckExerciceFiles(i Importer, exercice *fic.Exercice) (files []string, errs []string) {
flist, digests, berrs := BuildFilesListInto(i, exercice, "files")
errs = append(errs, berrs...)
@ -117,7 +117,7 @@ func CheckExerciceFiles(i Importer, exercice fic.Exercice) (files []string, errs
// SyncExerciceFiles reads the content of files/ directory and import it as EFile for the given challenge.
// It takes care of DIGESTS.txt and ensure imported files match.
func SyncExerciceFiles(i Importer, exercice fic.Exercice) (errs []string) {
func SyncExerciceFiles(i Importer, exercice *fic.Exercice) (errs []string) {
if _, err := exercice.WipeFiles(); err != nil {
errs = append(errs, err.Error())
}
@ -138,7 +138,7 @@ func SyncExerciceFiles(i Importer, exercice fic.Exercice) (errs []string) {
}); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to import file %q: %s", path.Base(exercice.Path), fname, err))
continue
} else if f.(fic.EFile).Size == 0 {
} else if f.(*fic.EFile).Size == 0 {
errs = append(errs, fmt.Sprintf("%q: WARNING imported file %q is empty!", path.Base(exercice.Path), fname))
}
}
@ -149,15 +149,15 @@ func SyncExerciceFiles(i Importer, exercice fic.Exercice) (errs []string) {
func ApiGetRemoteExerciceFiles(ps httprouter.Params, _ []byte) (interface{}, error) {
theme, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
if theme != nil {
exercice, _, _, _, errs := BuildExercice(GlobalImporter, *theme, path.Join(theme.Path, ps.ByName("exid")), nil)
exercice, _, _, _, errs := BuildExercice(GlobalImporter, theme, path.Join(theme.Path, ps.ByName("exid")), nil)
if exercice != nil {
files, digests, errs := BuildFilesListInto(GlobalImporter, *exercice, "files")
files, digests, errs := BuildFilesListInto(GlobalImporter, exercice, "files")
if files != nil {
var ret []fic.EFile
var ret []*fic.EFile
for _, fname := range files {
fPath := path.Join(exercice.Path, "files", fname)
fSize, _ := getFileSize(GlobalImporter, fPath)
ret = append(ret, fic.EFile{
ret = append(ret, &fic.EFile{
Path: fPath,
Name: fname,
Checksum: digests[fname],

View file

@ -22,7 +22,7 @@ type importHint struct {
FlagsDeps []int64
}
func buildExerciceHints(i Importer, exercice fic.Exercice) (hints []importHint, errs []string) {
func buildExerciceHints(i Importer, exercice *fic.Exercice) (hints []importHint, errs []string) {
params, _, err := parseExerciceParams(i, exercice.Path)
if err != nil {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: %s", path.Base(exercice.Path), err))
@ -102,19 +102,19 @@ func buildExerciceHints(i Importer, exercice fic.Exercice) (hints []importHint,
}
// CheckExerciceHints checks if all hints are corrects..
func CheckExerciceHints(i Importer, exercice fic.Exercice) ([]importHint, []string) {
func CheckExerciceHints(i Importer, exercice *fic.Exercice) ([]importHint, []string) {
return buildExerciceHints(i, exercice)
}
// SyncExerciceHints reads the content of hints/ directories and import it as EHint for the given challenge.
func SyncExerciceHints(i Importer, exercice fic.Exercice, flagsBindings map[int64]fic.Flag) (hintsBindings map[int]fic.EHint, errs []string) {
func SyncExerciceHints(i Importer, exercice *fic.Exercice, flagsBindings map[int64]fic.Flag) (hintsBindings map[int]*fic.EHint, errs []string) {
if _, err := exercice.WipeHints(); err != nil {
errs = append(errs, err.Error())
} else {
hints, berrs := buildExerciceHints(i, exercice)
errs = append(errs, berrs...)
hintsBindings = map[int]fic.EHint{}
hintsBindings = map[int]*fic.EHint{}
for _, hint := range hints {
// Import hint
@ -143,9 +143,9 @@ func SyncExerciceHints(i Importer, exercice fic.Exercice, flagsBindings map[int6
func ApiGetRemoteExerciceHints(ps httprouter.Params, _ []byte) (interface{}, error) {
theme, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
if theme != nil {
exercice, _, _, _, errs := BuildExercice(GlobalImporter, *theme, path.Join(theme.Path, ps.ByName("exid")), nil)
exercice, _, _, _, errs := BuildExercice(GlobalImporter, theme, path.Join(theme.Path, ps.ByName("exid")), nil)
if exercice != nil {
hints, errs := CheckExerciceHints(GlobalImporter, *exercice)
hints, errs := CheckExerciceHints(GlobalImporter, exercice)
if hints != nil {
return hints, nil
} else {

View file

@ -95,7 +95,7 @@ func getRawKey(input interface{}, validatorRe string, ordered bool, showLines bo
return
}
func buildKeyFlag(exercice fic.Exercice, flag ExerciceFlag, flagline int, defaultLabel string) (f *fic.Flag, choices []fic.FlagChoice, errs []string) {
func buildKeyFlag(exercice *fic.Exercice, flag ExerciceFlag, flagline int, defaultLabel string) (f *fic.Flag, choices []*fic.FlagChoice, errs []string) {
if len(flag.Label) == 0 {
flag.Label = defaultLabel
}
@ -129,7 +129,7 @@ func buildKeyFlag(exercice fic.Exercice, flag ExerciceFlag, flagline int, defaul
errs = append(errs, fmt.Sprintf("%q: flag #%d: %s", path.Base(exercice.Path), flagline, err.Error()))
return
}
fl := fic.Flag(fic.FlagKey{
fl := fic.Flag(&fic.FlagKey{
Type: flag.Type,
IdExercice: exercice.Id,
Order: int8(flagline),
@ -173,7 +173,7 @@ func buildKeyFlag(exercice fic.Exercice, flag ExerciceFlag, flagline int, defaul
val = strings.ToLower(val)
}
choices = append(choices, fic.FlagChoice{
choices = append(choices, &fic.FlagChoice{
Label: choice.Label,
Value: val,
})
@ -196,7 +196,7 @@ func buildKeyFlag(exercice fic.Exercice, flag ExerciceFlag, flagline int, defaul
type importFlag struct {
Line int
Flag fic.Flag
Choices []fic.FlagChoice
Choices []*fic.FlagChoice
FilesDeps []string
FlagsDeps []int64
}
@ -216,7 +216,7 @@ func iface2Number(input interface{}, output *string) error {
}
// buildExerciceFlags read challenge.txt and extract all flags.
func buildExerciceFlag(i Importer, exercice fic.Exercice, flag ExerciceFlag, nline int) (ret []importFlag, errs []string) {
func buildExerciceFlag(i Importer, exercice *fic.Exercice, flag ExerciceFlag, nline int) (ret []importFlag, errs []string) {
switch strings.ToLower(flag.Type) {
case "":
flag.Type = "key"
@ -281,7 +281,7 @@ func buildExerciceFlag(i Importer, exercice fic.Exercice, flag ExerciceFlag, nli
IdExercice: exercice.Id,
Order: int8(nline + 1),
Title: flag.Label,
Entries: []fic.MCQ_entry{},
Entries: []*fic.MCQ_entry{},
}
hasOne := false
@ -317,7 +317,7 @@ func buildExerciceFlag(i Importer, exercice fic.Exercice, flag ExerciceFlag, nli
continue
}
addedFlag.Entries = append(addedFlag.Entries, fic.MCQ_entry{
addedFlag.Entries = append(addedFlag.Entries, &fic.MCQ_entry{
Label: choice.Label,
Response: val,
})
@ -339,14 +339,14 @@ func buildExerciceFlag(i Importer, exercice fic.Exercice, flag ExerciceFlag, nli
ret = append(ret, importFlag{
Line: nline + 1,
Flag: addedFlag,
Flag: &addedFlag,
})
}
return
}
// buildExerciceFlags read challenge.txt and extract all flags.
func buildExerciceFlags(i Importer, exercice fic.Exercice) (flags map[int64]importFlag, flagids []int64, errs []string) {
func buildExerciceFlags(i Importer, exercice *fic.Exercice) (flags map[int64]importFlag, flagids []int64, errs []string) {
params, gerrs := getExerciceParams(i, exercice)
if len(gerrs) > 0 {
return flags, flagids, gerrs
@ -397,7 +397,7 @@ func buildExerciceFlags(i Importer, exercice fic.Exercice) (flags map[int64]impo
}
// CheckExerciceFlags checks if all flags for the given challenge are correct.
func CheckExerciceFlags(i Importer, exercice fic.Exercice, files []string) (rf []fic.Flag, errs []string) {
func CheckExerciceFlags(i Importer, exercice *fic.Exercice, files []string) (rf []fic.Flag, errs []string) {
flags, flagsids, berrs := buildExerciceFlags(i, exercice)
errs = append(errs, berrs...)
@ -432,7 +432,7 @@ func CheckExerciceFlags(i Importer, exercice fic.Exercice, files []string) (rf [
}
// ExerciceFlagsMap builds the flags bindings between challenge.txt and DB.
func ExerciceFlagsMap(i Importer, exercice fic.Exercice) (kmap map[int64]fic.Flag) {
func ExerciceFlagsMap(i Importer, exercice *fic.Exercice) (kmap map[int64]fic.Flag) {
flags, flagids, _ := buildExerciceFlags(i, exercice)
kmap = map[int64]fic.Flag{}
@ -449,7 +449,7 @@ func ExerciceFlagsMap(i Importer, exercice fic.Exercice) (kmap map[int64]fic.Fla
}
// SyncExerciceFlags imports all kind of flags for the given challenge.
func SyncExerciceFlags(i Importer, exercice fic.Exercice) (kmap map[int64]fic.Flag, errs []string) {
func SyncExerciceFlags(i Importer, exercice *fic.Exercice) (kmap map[int64]fic.Flag, errs []string) {
if _, err := exercice.WipeFlags(); err != nil {
errs = append(errs, err.Error())
} else if _, err := exercice.WipeMCQs(); err != nil {
@ -466,7 +466,7 @@ func SyncExerciceFlags(i Importer, exercice fic.Exercice) (kmap map[int64]fic.Fl
if addedFlag, err := exercice.AddFlag(flag.Flag); err != nil {
errs = append(errs, fmt.Sprintf("%q: error flag #%d: %s", path.Base(exercice.Path), flag.Line, err))
} else {
if f, ok := addedFlag.(fic.FlagKey); ok {
if f, ok := addedFlag.(*fic.FlagKey); ok {
for _, choice := range flag.Choices {
if _, err := f.AddChoice(choice); err != nil {
errs = append(errs, fmt.Sprintf("%q: error in flag #%d choice #FIXME: %s", path.Base(exercice.Path), flag.Line, err))
@ -505,9 +505,9 @@ func SyncExerciceFlags(i Importer, exercice fic.Exercice) (kmap map[int64]fic.Fl
func ApiGetRemoteExerciceFlags(ps httprouter.Params, _ []byte) (interface{}, error) {
theme, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
if theme != nil {
exercice, _, _, _, errs := BuildExercice(GlobalImporter, *theme, path.Join(theme.Path, ps.ByName("exid")), nil)
exercice, _, _, _, errs := BuildExercice(GlobalImporter, theme, path.Join(theme.Path, ps.ByName("exid")), nil)
if exercice != nil {
flags, errs := CheckExerciceFlags(GlobalImporter, *exercice, []string{})
flags, errs := CheckExerciceFlags(GlobalImporter, exercice, []string{})
if flags != nil {
return flags, nil
} else {

View file

@ -22,7 +22,7 @@ func fixnbsp(s string) string {
}
// GetExercices returns all exercice directories existing in a given theme, considering the given Importer.
func GetExercices(i Importer, theme fic.Theme) ([]string, error) {
func GetExercices(i Importer, theme *fic.Theme) ([]string, error) {
var exercices []string
if len(theme.Path) == 0 {
@ -42,12 +42,12 @@ func GetExercices(i Importer, theme fic.Theme) ([]string, error) {
return exercices, nil
}
func buildDependancyMap(i Importer, theme fic.Theme) (dmap map[int64]fic.Exercice, err error) {
func buildDependancyMap(i Importer, theme *fic.Theme) (dmap map[int64]*fic.Exercice, err error) {
var exercices []string
if exercices, err = GetExercices(i, theme); err != nil {
return
} else {
dmap = map[int64]fic.Exercice{}
dmap = map[int64]*fic.Exercice{}
for _, edir := range exercices {
var eid int
@ -58,7 +58,7 @@ func buildDependancyMap(i Importer, theme fic.Theme) (dmap map[int64]fic.Exercic
continue
}
var e fic.Exercice
var e *fic.Exercice
e, err = theme.GetExerciceByTitle(ename)
if err != nil {
return
@ -90,7 +90,7 @@ func parseExerciceDirname(edir string) (eid int, ename string, err error) {
}
// BuildExercice creates an Exercice from a given importer.
func BuildExercice(i Importer, theme fic.Theme, epath string, dmap *map[int64]fic.Exercice) (e *fic.Exercice, p ExerciceParams, eid int, edir string, errs []string) {
func BuildExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*fic.Exercice) (e *fic.Exercice, p ExerciceParams, eid int, edir string, errs []string) {
e = &fic.Exercice{}
e.Path = epath
@ -211,7 +211,7 @@ func BuildExercice(i Importer, theme fic.Theme, epath string, dmap *map[int64]fi
}
// SyncExercice imports new or updates existing given exercice.
func SyncExercice(i Importer, theme fic.Theme, epath string, dmap *map[int64]fic.Exercice) (e *fic.Exercice, eid int, errs []string) {
func SyncExercice(i Importer, theme *fic.Theme, epath string, dmap *map[int64]*fic.Exercice) (e *fic.Exercice, eid int, errs []string) {
var err error
var edir string
var p ExerciceParams
@ -242,7 +242,7 @@ func SyncExercice(i Importer, theme fic.Theme, epath string, dmap *map[int64]fic
}
// SyncExercices imports new or updates existing exercices, in a given theme.
func SyncExercices(i Importer, theme fic.Theme) (errs []string) {
func SyncExercices(i Importer, theme *fic.Theme) (errs []string) {
if !avoidImporterSync() {
if err := i.Sync(); err != nil {
errs = append(errs, err.Error())
@ -260,7 +260,7 @@ func SyncExercices(i Importer, theme fic.Theme) (errs []string) {
e, eid, cur_errs := SyncExercice(i, theme, path.Join(theme.Path, edir), &dmap)
if e != nil {
emap[e.Title] = eid
dmap[int64(eid)] = *e
dmap[int64(eid)] = e
errs = append(errs, cur_errs...)
}
}
@ -281,7 +281,7 @@ func SyncExercices(i Importer, theme fic.Theme) (errs []string) {
func ApiListRemoteExercices(ps httprouter.Params, _ []byte) (interface{}, error) {
theme, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
if theme != nil {
return GetExercices(GlobalImporter, *theme)
return GetExercices(GlobalImporter, theme)
} else {
return nil, fmt.Errorf("%q", errs)
}
@ -291,7 +291,7 @@ func ApiListRemoteExercices(ps httprouter.Params, _ []byte) (interface{}, error)
func ApiGetRemoteExercice(ps httprouter.Params, _ []byte) (interface{}, error) {
theme, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
if theme != nil {
exercice, _, _, _, errs := BuildExercice(GlobalImporter, *theme, path.Join(theme.Path, ps.ByName("exid")), nil)
exercice, _, _, _, errs := BuildExercice(GlobalImporter, theme, path.Join(theme.Path, ps.ByName("exid")), nil)
if exercice != nil {
return exercice, nil
} else {

View file

@ -129,6 +129,10 @@ func getFileContent(i Importer, URI string) (string, error) {
buf = append(buf, b)
}
if len(buf) == 0 {
return "", fmt.Errorf("File is empty")
}
return strings.TrimSpace(string(buf)), nil
}
}

View file

@ -182,7 +182,7 @@ func EditDeepReport(errs map[string][]string, erase bool) {
}
// SyncThemeDeep performs a recursive synchronisation: from challenges to challenge items.
func SyncThemeDeep(i Importer, theme fic.Theme, tid int, themeStep uint8) (errs []string) {
func SyncThemeDeep(i Importer, theme *fic.Theme, tid int, themeStep uint8) (errs []string) {
oneThemeDeepSync.Lock()
defer oneThemeDeepSync.Unlock()

View file

@ -213,15 +213,15 @@ func SyncThemes(i Importer) (errs []string) {
}
}
var theme fic.Theme
var theme *fic.Theme
if theme, err = fic.GetThemeByPath(btheme.Path); err != nil {
if _, err := fic.CreateTheme(*btheme); err != nil {
if _, err := fic.CreateTheme(btheme); err != nil {
errs = append(errs, fmt.Sprintf("%q: an error occurs during add: %s", tdir, err))
continue
}
}
if !fic.CmpTheme(theme, *btheme) {
if !fic.CmpTheme(theme, btheme) {
btheme.Id = theme.Id
if _, err := btheme.Update(); err != nil {
errs = append(errs, fmt.Sprintf("%q: an error occurs during update: %s", tdir, err))