Use pointer receiver more offen
This commit is contained in:
parent
6999b4e728
commit
c7569b5e54
59 changed files with 688 additions and 672 deletions
|
@ -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))
|
||||
|
|
|
@ -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],
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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))
|
||||
|
|
Reference in a new issue