Rename ValidatorRegexp to CaptureRegexp

This commit is contained in:
nemunaire 2023-05-16 09:59:59 +02:00
parent e472b482d6
commit 78189aab37
5 changed files with 58 additions and 58 deletions

View File

@ -730,7 +730,7 @@ type uploadedFlag struct {
IgnoreCase bool IgnoreCase bool
Multiline bool Multiline bool
NoTrim bool NoTrim bool
ValidatorRe *string `json:"validator_regexp"` CaptureRe *string `json:"validator_regexp"`
SortReGroups bool `json:"sort_re_grps"` SortReGroups bool `json:"sort_re_grps"`
Flag string Flag string
Value []byte Value []byte
@ -752,8 +752,8 @@ func createExerciceFlag(c *gin.Context) {
} }
var vre *string = nil var vre *string = nil
if uk.ValidatorRe != nil && len(*uk.ValidatorRe) > 0 { if uk.CaptureRe != nil && len(*uk.CaptureRe) > 0 {
vre = uk.ValidatorRe vre = uk.CaptureRe
} }
exercice := c.MustGet("exercice").(*fic.Exercice) exercice := c.MustGet("exercice").(*fic.Exercice)
@ -842,10 +842,10 @@ func updateExerciceFlag(c *gin.Context) {
flag.ChoicesCost = uk.ChoicesCost flag.ChoicesCost = uk.ChoicesCost
flag.BonusGain = uk.BonusGain flag.BonusGain = uk.BonusGain
if uk.ValidatorRe != nil && len(*uk.ValidatorRe) > 0 { if uk.CaptureRe != nil && len(*uk.CaptureRe) > 0 {
flag.ValidatorRegexp = uk.ValidatorRe flag.CaptureRegexp = uk.CaptureRe
} else { } else {
flag.ValidatorRegexp = nil flag.CaptureRegexp = nil
} }
if _, err := flag.Update(); err != nil { if _, err := flag.Update(); err != nil {

View File

@ -46,8 +46,8 @@ type ExerciceFlag struct {
Ordered bool `toml:",omitempty"` Ordered bool `toml:",omitempty"`
CaseSensitive bool `toml:",omitempty"` CaseSensitive bool `toml:",omitempty"`
NoTrim bool `toml:",omitempty"` NoTrim bool `toml:",omitempty"`
ValidatorRe string `toml:"validator_regexp,omitempty"` CaptureRe string `toml:"capture_regexp,omitempty"`
SortReGroups bool `toml:"sort_validator_regexp_groups,omitempty"` SortReGroups bool `toml:"sort_capture_regexp_groups,omitempty"`
Placeholder string `toml:",omitempty"` Placeholder string `toml:",omitempty"`
Help string `toml:",omitempty"` Help string `toml:",omitempty"`
BonusGain int32 `toml:"bonus_gain,omitempty"` BonusGain int32 `toml:"bonus_gain,omitempty"`
@ -143,7 +143,7 @@ func getExerciceParams(i Importer, exercice *fic.Exercice) (params ExerciceParam
Label: flag.Label, Label: flag.Label,
Type: "ucq", Type: "ucq",
Raw: flag.Raw, Raw: flag.Raw,
ValidatorRe: flag.ValidatorRe, CaptureRe: flag.CaptureRe,
Placeholder: flag.Placeholder, Placeholder: flag.Placeholder,
ChoicesCost: flag.ChoicesCost, ChoicesCost: flag.ChoicesCost,
Choice: flag.Choice, Choice: flag.Choice,

View File

@ -160,7 +160,7 @@ func buildKeyFlag(exercice *fic.Exercice, flag ExerciceFlag, flagline int, defau
flag.Label = flag.Label[1:] flag.Label = flag.Label[1:]
} }
raw, prep, terrs := getRawKey(flag.Raw, flag.ValidatorRe, flag.Ordered, flag.ShowLines, flag.Separator) raw, prep, terrs := getRawKey(flag.Raw, flag.CaptureRe, flag.Ordered, flag.ShowLines, flag.Separator)
if len(terrs) > 0 { if len(terrs) > 0 {
for _, terr := range terrs { for _, terr := range terrs {
@ -175,26 +175,26 @@ func buildKeyFlag(exercice *fic.Exercice, flag ExerciceFlag, flagline int, defau
errs = append(errs, NewFlagError(exercice, &flag, flagline, fmt.Errorf("WARNING non-printable characters in flag, is this really expected?"))) errs = append(errs, NewFlagError(exercice, &flag, flagline, fmt.Errorf("WARNING non-printable characters in flag, is this really expected?")))
} }
hashedFlag, err := fic.ComputeHashedFlag([]byte(raw), !flag.CaseSensitive, flag.NoTrim, validatorRegexp(flag.ValidatorRe), flag.SortReGroups) hashedFlag, err := fic.ComputeHashedFlag([]byte(raw), !flag.CaseSensitive, flag.NoTrim, validatorRegexp(flag.CaptureRe), flag.SortReGroups)
if err != nil { if err != nil {
errs = append(errs, NewFlagError(exercice, &flag, flagline, err)) errs = append(errs, NewFlagError(exercice, &flag, flagline, err))
return return
} }
fk := &fic.FlagKey{ fk := &fic.FlagKey{
Type: flag.Type, Type: flag.Type,
IdExercice: exercice.Id, IdExercice: exercice.Id,
Order: int8(flagline), Order: int8(flagline),
Label: flag.Label, Label: flag.Label,
Placeholder: flag.Placeholder, Placeholder: flag.Placeholder,
Help: flag.Help, Help: flag.Help,
Unit: flag.Unit, Unit: flag.Unit,
IgnoreCase: !flag.CaseSensitive, IgnoreCase: !flag.CaseSensitive,
Multiline: flag.Type == "text", Multiline: flag.Type == "text",
ValidatorRegexp: validatorRegexp(flag.ValidatorRe), CaptureRegexp: validatorRegexp(flag.CaptureRe),
SortReGroups: flag.SortReGroups, SortReGroups: flag.SortReGroups,
Checksum: hashedFlag[:], Checksum: hashedFlag[:],
ChoicesCost: flag.ChoicesCost, ChoicesCost: flag.ChoicesCost,
BonusGain: flag.BonusGain, BonusGain: flag.BonusGain,
} }
// Call checks hooks // Call checks hooks

View File

@ -36,8 +36,8 @@ type FlagKey struct {
NoTrim bool `json:"notrim,omitempty"` NoTrim bool `json:"notrim,omitempty"`
// Multiline indicates if the flag is stored on multiple lines // Multiline indicates if the flag is stored on multiple lines
Multiline bool `json:"multiline"` Multiline bool `json:"multiline"`
// ValidatorRegexp extracts a subset of the player's answer, that will be checked. // CaptureRegexp extracts a subset of the player's answer, that will be checked.
ValidatorRegexp *string `json:"validator_regexp"` CaptureRegexp *string `json:"capture_regexp"`
// SortReGroups indicates if groups resulting of the validator regexp should be sorted. // SortReGroups indicates if groups resulting of the validator regexp should be sorted.
SortReGroups bool `json:"sort_re_grps"` SortReGroups bool `json:"sort_re_grps"`
// Checksum is the expected hashed flag // Checksum is the expected hashed flag
@ -110,7 +110,7 @@ func (e *Exercice) GetFlagKeys() ([]*FlagKey, error) {
k := &FlagKey{} k := &FlagKey{}
k.IdExercice = e.Id k.IdExercice = e.Id
if err := rows.Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Type, &k.Placeholder, &k.Help, &k.Unit, &k.IgnoreCase, &k.NoTrim, &k.Multiline, &k.ValidatorRegexp, &k.SortReGroups, &k.Checksum, &k.ChoicesCost, &k.BonusGain); err != nil { if err := rows.Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Type, &k.Placeholder, &k.Help, &k.Unit, &k.IgnoreCase, &k.NoTrim, &k.Multiline, &k.CaptureRegexp, &k.SortReGroups, &k.Checksum, &k.ChoicesCost, &k.BonusGain); err != nil {
return nil, err return nil, err
} }
@ -127,26 +127,26 @@ func (e *Exercice) GetFlagKeys() ([]*FlagKey, error) {
// GetFlagKey returns a list of flags comming with the challenge. // GetFlagKey returns a list of flags comming with the challenge.
func GetFlagKey(id int) (k *FlagKey, err error) { func GetFlagKey(id int) (k *FlagKey, err error) {
k = &FlagKey{} k = &FlagKey{}
err = DBQueryRow("SELECT id_flag, id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain FROM exercice_flags WHERE id_flag = ?", id).Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Type, &k.Placeholder, &k.Help, &k.Unit, &k.IgnoreCase, &k.Multiline, &k.NoTrim, &k.ValidatorRegexp, &k.SortReGroups, &k.Checksum, &k.ChoicesCost, &k.BonusGain) err = DBQueryRow("SELECT id_flag, id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain FROM exercice_flags WHERE id_flag = ?", id).Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Type, &k.Placeholder, &k.Help, &k.Unit, &k.IgnoreCase, &k.Multiline, &k.NoTrim, &k.CaptureRegexp, &k.SortReGroups, &k.Checksum, &k.ChoicesCost, &k.BonusGain)
return return
} }
// GetFlagKey returns a flag. // GetFlagKey returns a flag.
func (e *Exercice) GetFlagKey(id int) (k *FlagKey, err error) { func (e *Exercice) GetFlagKey(id int) (k *FlagKey, err error) {
k = &FlagKey{} k = &FlagKey{}
err = DBQueryRow("SELECT id_flag, id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain FROM exercice_flags WHERE id_flag = ? AND id_exercice = ?", id, e.Id).Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Type, &k.Placeholder, &k.Help, &k.Unit, &k.IgnoreCase, &k.NoTrim, &k.Multiline, &k.ValidatorRegexp, &k.SortReGroups, &k.Checksum, &k.ChoicesCost, &k.BonusGain) err = DBQueryRow("SELECT id_flag, id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain FROM exercice_flags WHERE id_flag = ? AND id_exercice = ?", id, e.Id).Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Type, &k.Placeholder, &k.Help, &k.Unit, &k.IgnoreCase, &k.NoTrim, &k.Multiline, &k.CaptureRegexp, &k.SortReGroups, &k.Checksum, &k.ChoicesCost, &k.BonusGain)
return return
} }
// GetFlagKeyByLabel returns a flag matching the given label. // GetFlagKeyByLabel returns a flag matching the given label.
func (e *Exercice) GetFlagKeyByLabel(label string) (k *FlagKey, err error) { func (e *Exercice) GetFlagKeyByLabel(label string) (k *FlagKey, err error) {
k = &FlagKey{} k = &FlagKey{}
err = DBQueryRow("SELECT id_flag, id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain FROM exercice_flags WHERE label LIKE ? AND id_exercice = ?", label, e.Id).Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Type, &k.Placeholder, &k.Help, &k.Unit, &k.IgnoreCase, &k.NoTrim, &k.Multiline, &k.ValidatorRegexp, &k.SortReGroups, &k.Checksum, &k.ChoicesCost, &k.BonusGain) err = DBQueryRow("SELECT id_flag, id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain FROM exercice_flags WHERE label LIKE ? AND id_exercice = ?", label, e.Id).Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Type, &k.Placeholder, &k.Help, &k.Unit, &k.IgnoreCase, &k.NoTrim, &k.Multiline, &k.CaptureRegexp, &k.SortReGroups, &k.Checksum, &k.ChoicesCost, &k.BonusGain)
return return
} }
// ComputeHashedFlag calculates the expected checksum for the given raw_value. // ComputeHashedFlag calculates the expected checksum for the given raw_value.
func ComputeHashedFlag(raw_value []byte, ignorecase bool, notrim bool, validator_regexp *string, sortregroups bool) (hash [blake2b.Size]byte, err error) { func ComputeHashedFlag(raw_value []byte, ignorecase bool, notrim bool, capture_regexp *string, sortregroups bool) (hash [blake2b.Size]byte, err error) {
if ignorecase { if ignorecase {
raw_value = bytes.ToLower(raw_value) raw_value = bytes.ToLower(raw_value)
} }
@ -156,8 +156,8 @@ func ComputeHashedFlag(raw_value []byte, ignorecase bool, notrim bool, validator
} }
// Check that raw value passes through the regexp // Check that raw value passes through the regexp
if validator_regexp != nil { if capture_regexp != nil {
if raw_value, err = ExecValidatorRegexp(*validator_regexp, raw_value, ignorecase, sortregroups); err != nil { if raw_value, err = ExecCaptureRegexp(*capture_regexp, raw_value, ignorecase, sortregroups); err != nil {
return return
} }
} }
@ -171,14 +171,14 @@ func ComputeHashedFlag(raw_value []byte, ignorecase bool, notrim bool, validator
return return
} }
func ExecValidatorRegexp(vre string, val []byte, ignorecase bool, sortregroups bool) ([]byte, error) { func ExecCaptureRegexp(vre string, val []byte, ignorecase bool, sortregroups bool) ([]byte, error) {
if ignorecase { if ignorecase {
vre = "(?i)" + vre vre = "(?i)" + vre
} }
if re, err := regexp.Compile(vre); err != nil { if re, err := regexp.Compile(vre); err != nil {
return val, err return val, err
} else if res := re.FindSubmatch(val); res == nil { } else if res := re.FindSubmatch(val); res == nil {
return val, errors.New("expected flag doesn't pass through the validator_regexp") return val, errors.New("expected flag doesn't pass through the capture_regexp")
} else if sortregroups && len(res) > 2 { } else if sortregroups && len(res) > 2 {
var tab []string var tab []string
for _, v := range res[1:] { for _, v := range res[1:] {
@ -192,24 +192,24 @@ func ExecValidatorRegexp(vre string, val []byte, ignorecase bool, sortregroups b
} }
// AddRawFlagKey creates and fills a new struct FlagKey, from a non-hashed flag, and registers it into the database. // AddRawFlagKey creates and fills a new struct FlagKey, from a non-hashed flag, and registers it into the database.
func (e *Exercice) AddRawFlagKey(name string, t string, placeholder string, ignorecase bool, multiline bool, notrim bool, validator_regexp *string, sortregroups bool, raw_value []byte, choicescost int32, bonusgain int32) (*FlagKey, error) { func (e *Exercice) AddRawFlagKey(name string, t string, placeholder string, ignorecase bool, multiline bool, notrim bool, capture_regexp *string, sortregroups bool, raw_value []byte, choicescost int32, bonusgain int32) (*FlagKey, error) {
hash, err := ComputeHashedFlag(raw_value, ignorecase, notrim, validator_regexp, sortregroups) hash, err := ComputeHashedFlag(raw_value, ignorecase, notrim, capture_regexp, sortregroups)
if err != nil { if err != nil {
return nil, err return nil, err
} }
f := &FlagKey{ f := &FlagKey{
Type: t, Type: t,
Label: name, Label: name,
Placeholder: placeholder, Placeholder: placeholder,
IgnoreCase: ignorecase, IgnoreCase: ignorecase,
NoTrim: notrim, NoTrim: notrim,
Multiline: multiline, Multiline: multiline,
ValidatorRegexp: validator_regexp, CaptureRegexp: capture_regexp,
SortReGroups: sortregroups, SortReGroups: sortregroups,
Checksum: hash[:], Checksum: hash[:],
ChoicesCost: choicescost, ChoicesCost: choicescost,
BonusGain: bonusgain, BonusGain: bonusgain,
} }
_, err = f.Create(e) _, err = f.Create(e)
@ -233,13 +233,13 @@ func (k *FlagKey) RecoverId() (Flag, error) {
// AddFlagKey creates and fills a new struct Flag, from a hashed flag, and registers it into the database. // AddFlagKey creates and fills a new struct Flag, from a hashed flag, and registers it into the database.
func (k *FlagKey) Create(e *Exercice) (Flag, error) { func (k *FlagKey) Create(e *Exercice) (Flag, error) {
// Check the regexp compile // Check the regexp compile
if k.ValidatorRegexp != nil { if k.CaptureRegexp != nil {
if _, err := regexp.Compile(*k.ValidatorRegexp); err != nil { if _, err := regexp.Compile(*k.CaptureRegexp); err != nil {
return k, err return k, err
} }
} }
if res, err := DBExec("INSERT INTO exercice_flags (id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", e.Id, k.Order, k.Label, k.Type, k.Placeholder, k.Help, k.Unit, k.IgnoreCase, k.NoTrim, k.Multiline, k.ValidatorRegexp, k.SortReGroups, k.Checksum, k.ChoicesCost, k.BonusGain); err != nil { if res, err := DBExec("INSERT INTO exercice_flags (id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", e.Id, k.Order, k.Label, k.Type, k.Placeholder, k.Help, k.Unit, k.IgnoreCase, k.NoTrim, k.Multiline, k.CaptureRegexp, k.SortReGroups, k.Checksum, k.ChoicesCost, k.BonusGain); err != nil {
return k, err return k, err
} else if kid, err := res.LastInsertId(); err != nil { } else if kid, err := res.LastInsertId(); err != nil {
return k, err return k, err
@ -252,19 +252,19 @@ func (k *FlagKey) Create(e *Exercice) (Flag, error) {
// ComputeChecksum calculates the checksum for a given value. // ComputeChecksum calculates the checksum for a given value.
func (k *FlagKey) ComputeChecksum(val []byte) ([]byte, error) { func (k *FlagKey) ComputeChecksum(val []byte) ([]byte, error) {
cksum, err := ComputeHashedFlag(val, k.IgnoreCase, k.NoTrim, k.ValidatorRegexp, k.SortReGroups) cksum, err := ComputeHashedFlag(val, k.IgnoreCase, k.NoTrim, k.CaptureRegexp, k.SortReGroups)
return cksum[:], err return cksum[:], err
} }
// Update applies modifications back to the database. // Update applies modifications back to the database.
func (k *FlagKey) Update() (int64, error) { func (k *FlagKey) Update() (int64, error) {
if k.ValidatorRegexp != nil { if k.CaptureRegexp != nil {
if _, err := regexp.Compile(*k.ValidatorRegexp); err != nil { if _, err := regexp.Compile(*k.CaptureRegexp); err != nil {
return 0, err return 0, err
} }
} }
if res, err := DBExec("UPDATE exercice_flags SET id_exercice = ?, ordre = ?, label = ?, type = ?, placeholder = ?, help = ?, unit = ?, ignorecase = ?, notrim = ?, multiline = ?, validator_regexp = ?, sort_re_grps = ?, cksum = ?, choices_cost = ?, bonus_gain = ? WHERE id_flag = ?", k.IdExercice, k.Order, k.Label, k.Type, k.Placeholder, k.Help, k.Unit, k.IgnoreCase, k.NoTrim, k.Multiline, k.ValidatorRegexp, k.SortReGroups, k.Checksum, k.ChoicesCost, k.BonusGain, k.Id); err != nil { if res, err := DBExec("UPDATE exercice_flags SET id_exercice = ?, ordre = ?, label = ?, type = ?, placeholder = ?, help = ?, unit = ?, ignorecase = ?, notrim = ?, multiline = ?, validator_regexp = ?, sort_re_grps = ?, cksum = ?, choices_cost = ?, bonus_gain = ? WHERE id_flag = ?", k.IdExercice, k.Order, k.Label, k.Type, k.Placeholder, k.Help, k.Unit, k.IgnoreCase, k.NoTrim, k.Multiline, k.CaptureRegexp, k.SortReGroups, k.Checksum, k.ChoicesCost, k.BonusGain, k.Id); err != nil {
return 0, err return 0, err
} else if nb, err := res.RowsAffected(); err != nil { } else if nb, err := res.RowsAffected(); err != nil {
return 0, err return 0, err

View File

@ -45,7 +45,7 @@ type myTeamFlag struct {
IgnoreOrder bool `json:"ignore_order,omitempty"` IgnoreOrder bool `json:"ignore_order,omitempty"`
IgnoreCase bool `json:"ignore_case,omitempty"` IgnoreCase bool `json:"ignore_case,omitempty"`
Multiline bool `json:"multiline,omitempty"` Multiline bool `json:"multiline,omitempty"`
ValidatorRe *string `json:"validator_regexp,omitempty"` CaptureRe *string `json:"validator_regexp,omitempty"`
Solved *time.Time `json:"found,omitempty"` Solved *time.Time `json:"found,omitempty"`
PSolved *time.Time `json:"part_solved,omitempty"` PSolved *time.Time `json:"part_solved,omitempty"`
Soluce string `json:"soluce,omitempty"` Soluce string `json:"soluce,omitempty"`
@ -272,7 +272,7 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
// Retrieve solved state or solution for public iface // Retrieve solved state or solution for public iface
if t == nil { if t == nil {
flag.IgnoreCase = k.IgnoreCase flag.IgnoreCase = k.IgnoreCase
flag.ValidatorRe = k.ValidatorRegexp flag.CaptureRe = k.CaptureRegexp
flag.Soluce = hex.EncodeToString(k.Checksum) flag.Soluce = hex.EncodeToString(k.Checksum)
} else if PartialValidation { } else if PartialValidation {
flag.Solved = t.HasPartiallySolved(k) flag.Solved = t.HasPartiallySolved(k)