- models: Package doc comment describing data models with DeJSON/DeList - utils: Package doc comment for utility functions (JSON, signing, schema mismatch, errors) - ynison: Package doc comment for Ynison WebSocket client protocol Co-Authored-By: Claude Code <noreply@anthropic.com>
81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
// Package models provides data models for the Yandex Music API.
|
|
// All models implement the Model interface with DeJSON/DeList deserialization methods.
|
|
package models
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"gitea.mrixs.me/Mrixs/yandex-music-api-go/internal/utils"
|
|
)
|
|
|
|
// Model is the base interface for all models
|
|
type Model interface {
|
|
// DeJSON deserializes from JSON
|
|
DeJSON(data []byte, c ClientInterface) (Model, error)
|
|
// DeList deserializes a list of models
|
|
DeList(data []byte, c ClientInterface) ([]Model, error)
|
|
// ToJSON serializes to JSON
|
|
ToJSON() ([]byte, error)
|
|
// ToMap converts to map
|
|
ToMap() (map[string]any, error)
|
|
}
|
|
|
|
// BaseModel provides common functionality for models
|
|
type BaseModel struct {
|
|
Client ClientInterface `json:"-"`
|
|
}
|
|
|
|
// NewBaseModel creates a new BaseModel
|
|
func NewBaseModel(c ClientInterface) *BaseModel {
|
|
return &BaseModel{Client: c}
|
|
}
|
|
|
|
// DeJSON implements Model.DeJSON for basic models
|
|
func (b *BaseModel) DeJSON(data []byte, c ClientInterface) (Model, error) {
|
|
// This should be overridden by concrete types
|
|
return nil, nil
|
|
}
|
|
|
|
// DeList implements Model.DeList for basic models
|
|
func (b *BaseModel) DeList(data []byte, c ClientInterface) ([]Model, error) {
|
|
var rawList []json.RawMessage
|
|
if err := json.Unmarshal(data, &rawList); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := make([]Model, 0, len(rawList))
|
|
for range rawList {
|
|
// This should be overridden by concrete types
|
|
result = append(result, nil)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// ToJSON serializes the model to JSON
|
|
func (b *BaseModel) ToJSON() ([]byte, error) {
|
|
return utils.Marshal(b)
|
|
}
|
|
|
|
// ToMap converts the model to a map
|
|
func (b *BaseModel) ToMap() (map[string]any, error) {
|
|
data, err := b.ToJSON()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var result map[string]any
|
|
if err := json.Unmarshal(data, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// SetClient sets the client on the model
|
|
func (b *BaseModel) SetClient(c ClientInterface) {
|
|
b.Client = c
|
|
}
|
|
|
|
// GetClient returns the client
|
|
func (b *BaseModel) GetClient() ClientInterface {
|
|
return b.Client
|
|
}
|