feat(GODT-3121): refactored retrieval kb article index lookup.

This commit is contained in:
Xavier Michelon
2023-12-07 08:10:46 +01:00
parent b93c10ad47
commit 83935f3a03
3 changed files with 42 additions and 14 deletions

View File

@ -20,6 +20,7 @@ package kb
import (
_ "embed"
"encoding/json"
"errors"
"regexp"
"strings"
@ -27,6 +28,8 @@ import (
"golang.org/x/exp/slices"
)
var ErrArticleNotFound = errors.New("KB article not found")
//go:embed kbArticleList.json
var articleListString []byte
@ -75,6 +78,20 @@ func GetSuggestions(userInput string) (ArticleList, error) {
return articles, nil
}
// GetArticleIndex retrieve the index of an article from its url. if the article is not found, ErrArticleNotFound is returned.
func GetArticleIndex(url string) (uint64, error) {
articles, err := GetArticleList()
if err != nil {
return 0, err
}
index := xslices.IndexFunc(articles, func(article *Article) bool { return strings.EqualFold(article.URL, url) })
if index == -1 {
return 0, ErrArticleNotFound
}
return uint64(index), nil
}
func simplifyUserInput(input string) string {
// replace any sequence not matching of the following with a single space:
// - letters in any language (accentuated or not)