Add add config validators
This commit is contained in:
62
src/configloader/ListParsers.go
Normal file
62
src/configloader/ListParsers.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package configloader
|
||||
|
||||
import _ "embed"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"slices"
|
||||
)
|
||||
|
||||
type ParserListItem struct {
|
||||
Title string `json:"title"`
|
||||
Comment string `json:"comment"`
|
||||
Authors []string `json:"authors"`
|
||||
Links []string `json:"links"`
|
||||
}
|
||||
|
||||
// Load parser list from JSON retrieved from musdb's API
|
||||
// See https://<subdomain>.museum-digital.org/musdb/api/meta/list_import_parsers
|
||||
// The embed module requires the variable to not be bound to a function.
|
||||
|
||||
//go:embed parser_list.json
|
||||
var rawParserList []byte
|
||||
|
||||
// Returns a list of the available parsers.
|
||||
func ListParsers() []ParserListItem {
|
||||
|
||||
parserListFromApi := []ParserListItem{}
|
||||
// parserListFromApi := new(ApiParserListFormat)
|
||||
json.Unmarshal(rawParserList, &parserListFromApi)
|
||||
print(parserListFromApi)
|
||||
|
||||
return parserListFromApi
|
||||
|
||||
}
|
||||
|
||||
// Checks if inputParser is in the list of available parsers.
|
||||
// Returns the parser name or an error.
|
||||
func ValidateParser(inputParser string) (string, error) {
|
||||
|
||||
parserList := ListParsers()
|
||||
parserTitles := []string{}
|
||||
|
||||
for _, p := range(parserList) {
|
||||
parserTitles = append(parserTitles, p.Title)
|
||||
}
|
||||
|
||||
if slices.Contains(parserTitles, inputParser) {
|
||||
return inputParser, nil
|
||||
}
|
||||
|
||||
// Accepted invalid modes to clean up:
|
||||
// ParserLido > Lido
|
||||
// lido > Lido
|
||||
cleaned := inputParser[6:]
|
||||
if slices.Contains(parserTitles, cleaned) {
|
||||
return cleaned, nil
|
||||
}
|
||||
|
||||
return "", errors.New("Invalid parser selected")
|
||||
|
||||
}
|
Reference in New Issue
Block a user