merging unrelated objects into one

This commit is contained in:
Hazel Noack
2025-10-09 12:53:25 +02:00
parent e79e364ff9
commit fe244c7e68

View File

@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"regexp"
"strconv" "strconv"
"strings" "strings"
@@ -77,6 +78,8 @@ func (s musicObjectStore) currentMusicObjects() ([]data.MusicObject, error) {
return (s)[len(s)-1], nil return (s)[len(s)-1], nil
} }
var indexSelectionPattern = regexp.MustCompile(`^[\d ,]+$`)
func interpretCommand(command string, store musicObjectStore) (musicObjectStore, error) { func interpretCommand(command string, store musicObjectStore) (musicObjectStore, error) {
// going back in history // going back in history
if command == ".." { if command == ".." {
@@ -88,21 +91,33 @@ func interpretCommand(command string, store musicObjectStore) (musicObjectStore,
} }
// fetch special music object // fetch special music object
if index, err := strconv.Atoi(command); err == nil { if indexSelectionPattern.MatchString(command) {
currentMusicObjects, err := store.currentMusicObjects() currentMusicObjects, err := store.currentMusicObjects()
if err != nil { if err != nil {
return store, err return store, err
} }
if index >= len(currentMusicObjects) || index < 0 { var fetched data.MusicObject
return store, errors.New(command + " is out of bounds [0 <= " + strconv.Itoa(index) + " <= " + strconv.Itoa(len(currentMusicObjects)-1) + "]")
}
current := currentMusicObjects[index] for _, stringIndex := range strings.Split(command, ",") {
index, _ := strconv.Atoi(strings.TrimSpace(stringIndex))
fetched, err := plugin.Fetch(current) if index >= len(currentMusicObjects) || index < 0 {
if err != nil { return store, errors.New(command + " is out of bounds [0 <= " + strconv.Itoa(index) + " <= " + strconv.Itoa(len(currentMusicObjects)-1) + "]")
return store, err }
current := currentMusicObjects[index]
newFetched, err := plugin.Fetch(current)
if err != nil {
return store, err
}
if fetched == nil {
fetched = newFetched
} else {
fetched = fetched.Merge(newFetched)
}
} }
return append(store, fetched.Related()), nil return append(store, fetched.Related()), nil