JSPF: Implemented export as loves and listens

This commit is contained in:
Philipp Wolfer 2025-05-01 15:10:00 +02:00
parent cfc3cd522d
commit a645ec5c78
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
6 changed files with 225 additions and 42 deletions

View file

@ -1,5 +1,5 @@
/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
Copyright © 2023-2025 Philipp Wolfer <phw@uploadedlobster.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@ -22,7 +22,28 @@ THE SOFTWARE.
package jspf
import "time"
import (
"encoding/json"
"fmt"
"time"
)
// Represents a JSPF extension
type Extension any
// A map of JSPF extensions
type ExtensionMap map[string]Extension
// Parses the extension with the given ID and unmarshals it into "v".
// If the extensions is not found or the data cannot be unmarshalled,
// an error is returned.
func (e ExtensionMap) Get(id string, v any) error {
ext, ok := e[id]
if !ok {
return fmt.Errorf("extension %q not found", id)
}
return unmarshalExtension(ext, v)
}
const (
// The identifier for the MusicBrainz / ListenBrainz JSPF playlist extension
@ -83,3 +104,11 @@ type MusicBrainzTrackExtension struct {
// this document.
AdditionalMetadata map[string]any `json:"additional_metadata,omitempty"`
}
func unmarshalExtension(ext Extension, v any) error {
asJson, err := json.Marshal(ext)
if err != nil {
return err
}
return json.Unmarshal(asJson, v)
}