Implement number flags
This commit is contained in:
parent
30d0afe43f
commit
c3742ade4e
6 changed files with 104 additions and 5 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"math/rand"
|
||||
"path"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
|
@ -83,6 +84,8 @@ func getRawKey(input interface{}, validatorRe string, ordered bool, showLines bo
|
|||
prep = fmt.Sprintf("`%s%s%d", separator, ignord, nbLines)
|
||||
} else if f, ok := input.(int64); ok {
|
||||
raw = fmt.Sprintf("%d", f)
|
||||
} else if f, ok := input.(float64); ok {
|
||||
raw = strconv.FormatFloat(f, 'f', -1, 64)
|
||||
} else if f, ok := input.(string); !ok {
|
||||
errs = append(errs, fmt.Sprintf("has an invalid type: can only be []string or string, not %T", input))
|
||||
return
|
||||
|
@ -127,6 +130,7 @@ func buildKeyFlag(exercice fic.Exercice, flag ExerciceFlag, flagline int, defaul
|
|||
return
|
||||
}
|
||||
fl := fic.Flag(fic.FlagKey{
|
||||
Type: flag.Type,
|
||||
IdExercice: exercice.Id,
|
||||
Order: int8(flagline),
|
||||
Label: flag.Label,
|
||||
|
@ -192,6 +196,20 @@ type importFlag struct {
|
|||
FlagsDeps []int64
|
||||
}
|
||||
|
||||
func iface2Number(input interface{}, output *string) error {
|
||||
if input != nil {
|
||||
if v, ok := input.(int64); ok {
|
||||
*output = fmt.Sprintf("%d", v)
|
||||
} else if v, ok := input.(float64); ok {
|
||||
*output = strconv.FormatFloat(v, 'f', -1, 64)
|
||||
fmt.Printf("%s\n", *output)
|
||||
} else {
|
||||
return fmt.Errorf("has an invalid type: expected int or float, got %T", input)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildExerciceFlags read challenge.txt and extract all flags.
|
||||
func buildExerciceFlag(i Importer, exercice fic.Exercice, flag ExerciceFlag, nline int) (ret []importFlag, errs []string) {
|
||||
switch strings.ToLower(flag.Type) {
|
||||
|
@ -199,6 +217,23 @@ func buildExerciceFlag(i Importer, exercice fic.Exercice, flag ExerciceFlag, nli
|
|||
flag.Type = "key"
|
||||
case "key":
|
||||
flag.Type = "key"
|
||||
case "number":
|
||||
var smin, smax, sstep string
|
||||
err := iface2Number(flag.NumberMin, &smin)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: flag #%d: min %s.", path.Base(exercice.Path), nline+1, err.Error()))
|
||||
}
|
||||
|
||||
err = iface2Number(flag.NumberMax, &smax)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: flag #%d: max %s.", path.Base(exercice.Path), nline+1, err.Error()))
|
||||
}
|
||||
|
||||
err = iface2Number(flag.NumberStep, &sstep)
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: flag #%d: step %s.", path.Base(exercice.Path), nline+1, err.Error()))
|
||||
}
|
||||
flag.Type = fmt.Sprintf("number,%s,%s,%s", smin, smax, sstep)
|
||||
case "text":
|
||||
flag.Type = "text"
|
||||
case "vector":
|
||||
|
@ -208,11 +243,21 @@ func buildExerciceFlag(i Importer, exercice fic.Exercice, flag ExerciceFlag, nli
|
|||
case "mcq":
|
||||
flag.Type = "mcq"
|
||||
default:
|
||||
errs = append(errs, fmt.Sprintf("%q: flag #%d: invalid type of flag: should be 'key', 'text', 'mcq', 'ucq' or 'vector'.", path.Base(exercice.Path), nline+1))
|
||||
errs = append(errs, fmt.Sprintf("%q: flag #%d: invalid type of flag: should be 'key', 'number', 'text', 'mcq', 'ucq' or 'vector'.", path.Base(exercice.Path), nline+1))
|
||||
return
|
||||
}
|
||||
|
||||
if flag.Type == "key" || flag.Type == "text" || flag.Type == "ucq" || flag.Type == "vector" {
|
||||
if !strings.HasPrefix(flag.Type, "number") {
|
||||
if flag.NumberMin != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: flag #%d: property min undefined for this kind of flag: should the type be 'number'.", path.Base(exercice.Path), nline+1))
|
||||
} else if flag.NumberMax != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: flag #%d: property max undefined for this kind of flag: should the type be 'number'.", path.Base(exercice.Path), nline+1))
|
||||
} else if flag.NumberStep != nil {
|
||||
errs = append(errs, fmt.Sprintf("%q: flag #%d: property step undefined for this kind of flag: should the type be 'number'.", path.Base(exercice.Path), nline+1))
|
||||
}
|
||||
}
|
||||
|
||||
if flag.Type == "key" || strings.HasPrefix(flag.Type, "number") || flag.Type == "text" || flag.Type == "ucq" || flag.Type == "vector" {
|
||||
addedFlag, choices, berrs := buildKeyFlag(exercice, flag, nline+1, "Flag")
|
||||
if len(berrs) > 0 {
|
||||
errs = append(errs, berrs...)
|
||||
|
|
Reference in a new issue