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