sync: Replace []error by go.uber.org/multierr

This commit is contained in:
nemunaire 2023-11-22 12:16:53 +01:00
commit b6966d47ce
25 changed files with 380 additions and 348 deletions

View file

@ -6,51 +6,53 @@ import (
"log"
"unicode"
"go.uber.org/multierr"
"srs.epita.fr/fic-server/admin/sync"
"srs.epita.fr/fic-server/libfic"
lib "srs.epita.fr/fic-server/repochecker/grammalecte/lib"
)
func GrammalecteCheckKeyFlag(flag *fic.FlagKey, raw string, exercice *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
func GrammalecteCheckKeyFlag(flag *fic.FlagKey, raw string, exercice *fic.Exercice, exceptions *sync.CheckExceptions) (errs error) {
if !isRecognizedLanguage(exercice.Language) {
return
}
label, _, _, _ := flag.AnalyzeFlagLabel()
for _, err := range grammalecte("label ", label, -1, exceptions, &CommonOpts) {
for _, err := range multierr.Errors(grammalecte("label ", label, -1, exceptions, &CommonOpts)) {
if e, ok := err.(lib.GrammarError); ok && e.RuleId == "poncfin_règle1" {
continue
}
errs = append(errs, err)
errs = multierr.Append(errs, err)
}
if len(flag.Help) > 0 {
errs = append(errs, grammalecte("help ", flag.Help, -1, exceptions, &CommonOpts)...)
errs = multierr.Append(errs, grammalecte("help ", flag.Help, -1, exceptions, &CommonOpts))
}
return
}
func GrammalecteCheckFlagChoice(choice *fic.FlagChoice, exercice *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
func GrammalecteCheckFlagChoice(choice *fic.FlagChoice, exercice *fic.Exercice, exceptions *sync.CheckExceptions) (errs error) {
if isRecognizedLanguage(exercice.Language) {
errs = append(errs, grammalecte("label ", choice.Label, -1, exceptions, &CommonOpts)...)
errs = multierr.Append(errs, grammalecte("label ", choice.Label, -1, exceptions, &CommonOpts))
}
if len(errs) == 0 && !unicode.IsUpper(bytes.Runes([]byte(choice.Label))[0]) && !exceptions.HasException(":label_majuscule") {
errs = append(errs, fmt.Errorf("%q nécessite une majuscule (:label_majuscule)", choice.Label))
if len(multierr.Errors(errs)) == 0 && !unicode.IsUpper(bytes.Runes([]byte(choice.Label))[0]) && !exceptions.HasException(":label_majuscule") {
errs = multierr.Append(errs, fmt.Errorf("%q nécessite une majuscule (:label_majuscule)", choice.Label))
}
return
}
func GrammalecteCheckHint(hint *fic.EHint, exercice *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
func GrammalecteCheckHint(hint *fic.EHint, exercice *fic.Exercice, exceptions *sync.CheckExceptions) (errs error) {
if len(hint.Title) > 0 {
if isRecognizedLanguage(exercice.Language) {
errs = append(errs, grammalecte("title ", hint.Title, -1, exceptions, &CommonOpts)...)
errs = multierr.Append(errs, grammalecte("title ", hint.Title, -1, exceptions, &CommonOpts))
}
if len(errs) == 0 && !unicode.IsUpper(bytes.Runes([]byte(hint.Title))[0]) && !exceptions.HasException(":title_majuscule") {
errs = append(errs, fmt.Errorf("%q nécessite une majuscule (:title_majuscule)", hint.Title))
if len(multierr.Errors(errs)) == 0 && !unicode.IsUpper(bytes.Runes([]byte(hint.Title))[0]) && !exceptions.HasException(":title_majuscule") {
errs = multierr.Append(errs, fmt.Errorf("%q nécessite une majuscule (:title_majuscule)", hint.Title))
}
}
@ -59,7 +61,7 @@ func GrammalecteCheckHint(hint *fic.EHint, exercice *fic.Exercice, exceptions *s
return
}
func GrammalecteCheckGrammar(data interface{}, exceptions *sync.CheckExceptions) []error {
func GrammalecteCheckGrammar(data interface{}, exceptions *sync.CheckExceptions) error {
if s, ok := data.(struct {
Str string
Language string

View file

@ -9,6 +9,8 @@ import (
"regexp"
"strings"
"go.uber.org/multierr"
"srs.epita.fr/fic-server/admin/sync"
lib "srs.epita.fr/fic-server/repochecker/grammalecte/lib"
)
@ -119,7 +121,7 @@ var (
mdimg = regexp.MustCompile(`!\[([^\]]+)\]\([^)]+\)`)
)
func grammalecte(name string, text string, paragraph int, exceptions *sync.CheckExceptions, options *GrammalecteOptions) (errs []error) {
func grammalecte(name string, text string, paragraph int, exceptions *sync.CheckExceptions, options *GrammalecteOptions) (errs error) {
// Remove Markdown elements
text = mdimg.ReplaceAllString(text, "Image : ${1}")
@ -173,7 +175,7 @@ func grammalecte(name string, text string, paragraph int, exceptions *sync.Check
}
suggestions, _ := suggest(serror.Value)
errs = append(errs, lib.SpellingError{
errs = multierr.Append(errs, lib.SpellingError{
Prefix: name,
Source: data.Text,
NSource: data.Paragraph,
@ -207,7 +209,7 @@ func grammalecte(name string, text string, paragraph int, exceptions *sync.Check
continue
}
errs = append(errs, err)
errs = multierr.Append(errs, err)
}
}

View file

@ -15,9 +15,10 @@ import (
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
"go.uber.org/multierr"
)
func GrammalecteCheckMDText(str string, lang string, exceptions *sync.CheckExceptions, forbiddenStrings ...string) (errs []error) {
func GrammalecteCheckMDText(str string, lang string, exceptions *sync.CheckExceptions, forbiddenStrings ...string) (errs error) {
if !isRecognizedLanguage(lang) {
return
}
@ -34,7 +35,7 @@ func GrammalecteCheckMDText(str string, lang string, exceptions *sync.CheckExcep
for _, s := range forbiddenStrings {
if strings.Contains(str, s) {
if !exceptions.HasException(":not-forbidden-string:" + s) {
errs = append(errs, fmt.Errorf("Forbidden raw string %q included in file content, don't write it (:not-forbidden-string:%s)", s, s))
errs = multierr.Append(errs, fmt.Errorf("Forbidden raw string %q included in file content, don't write it (:not-forbidden-string:%s)", s, s))
}
}
}
@ -62,13 +63,13 @@ func GrammalecteCheckMDText(str string, lang string, exceptions *sync.CheckExcep
var buf bytes.Buffer
if err := markdown.Convert([]byte(str), &buf); err != nil {
errs = append(errs, err)
errs = multierr.Append(errs, err)
}
errs = append(errs, checker.errs...)
errs = append(errs, voidRenderer.Errors()...)
errs = multierr.Append(errs, checker.errs)
errs = multierr.Append(errs, voidRenderer.Errors())
for _, err := range grammalecte("", buf.String(), 0, exceptions, &CommonOpts) {
for _, err := range multierr.Errors(grammalecte("", buf.String(), 0, exceptions, &CommonOpts)) {
if gerror, ok := err.(lib.GrammarError); ok {
if (gerror.RuleId == "redondances_paragraphe" || gerror.RuleId == "redondances_phrase") && gerror.GetPassage() == "SubstitutDeCode" {
continue
@ -79,7 +80,7 @@ func GrammalecteCheckMDText(str string, lang string, exceptions *sync.CheckExcep
}
}
errs = append(errs, err)
errs = multierr.Append(errs, err)
}
return
@ -87,7 +88,7 @@ func GrammalecteCheckMDText(str string, lang string, exceptions *sync.CheckExcep
type grammarChecker struct {
exceptions *sync.CheckExceptions
errs []error
errs error
}
func (t *grammarChecker) Transform(doc *ast.Document, reader text.Reader, pc parser.Context) {
@ -99,11 +100,11 @@ func (t *grammarChecker) Transform(doc *ast.Document, reader text.Reader, pc par
switch child := node.(type) {
case *ast.Image:
if len(child.Title) > 0 {
t.errs = append(t.errs, grammalecte("", string(child.Title), 0, t.exceptions, &CommonOpts)...)
t.errs = multierr.Append(t.errs, grammalecte("", string(child.Title), 0, t.exceptions, &CommonOpts))
}
case *ast.Link:
if len(child.Title) > 0 {
t.errs = append(t.errs, grammalecte("", string(child.Title), 0, t.exceptions, &CommonOpts)...)
t.errs = multierr.Append(t.errs, grammalecte("", string(child.Title), 0, t.exceptions, &CommonOpts))
}
}

View file

@ -7,10 +7,11 @@ import (
"github.com/yuin/goldmark/ast"
goldrender "github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/util"
"go.uber.org/multierr"
)
type VoidRenderer struct {
errs []error
errs error
}
func NewVoidRenderer() *VoidRenderer {
@ -28,7 +29,7 @@ func (r *VoidRenderer) RegisterFuncs(reg goldrender.NodeRendererFuncRegisterer)
reg.Register(ast.KindString, r.renderString)
}
func (r *VoidRenderer) Errors() []error {
func (r *VoidRenderer) Errors() error {
return r.errs
}
@ -77,7 +78,7 @@ func (r *VoidRenderer) renderImage(w util.BufWriter, source []byte, node ast.Nod
// Check there is a correct image alt
alt := nodeToText(n, source)
if len(bytes.Fields(alt)) <= 1 {
r.errs = append(r.errs, fmt.Errorf("No valid image alternative defined for %q", n.Destination))
r.errs = multierr.Append(r.errs, fmt.Errorf("No valid image alternative defined for %q", n.Destination))
return ast.WalkContinue, nil
}