fix(tracker): unmarshal always into fresh, empty structs

A somewhat gotcha: when unmarshaling into &m.d with json.Unmarshal
or yaml.Unmarshal, maps were "merged" with the existing maps, which
is how we ended up with send units with color parameters, among
other things. This fix always unmarshals into fresh var data
modelData and only then sets m.d = data if the unmarshaling was
succesful.
This commit is contained in:
5684185+vsariola@users.noreply.github.com 2024-11-02 22:01:12 +02:00
parent 19661f90ea
commit 9d59cfb3b6

View File

@ -193,7 +193,10 @@ func NewModelPlayer(broker *Broker, synther sointu.Synther, midiContext MIDICont
m.resetSong()
if recoveryFilePath != "" {
if bytes2, err := os.ReadFile(m.d.RecoveryFilePath); err == nil {
json.Unmarshal(bytes2, &m.d)
var data modelData
if json.Unmarshal(bytes2, &data) == nil {
m.d = data
}
}
}
m.signalAnalyzer = NewScopeModel(broker, m.d.Song.BPM)
@ -326,13 +329,18 @@ func (m *Model) SaveRecovery() error {
}
func (m *Model) UnmarshalRecovery(bytes []byte) {
err := json.Unmarshal(bytes, &m.d)
var data modelData
err := json.Unmarshal(bytes, &data)
if err != nil {
return
}
m.d = data
if m.d.RecoveryFilePath != "" { // check if there's a recovery file on disk and load it instead
if bytes2, err := os.ReadFile(m.d.RecoveryFilePath); err == nil {
json.Unmarshal(bytes2, &m.d)
var data modelData
if json.Unmarshal(bytes2, &data) == nil {
m.d = data
}
}
}
m.d.ChangedSinceRecovery = false