70 lines
1.5 KiB
Text
70 lines
1.5 KiB
Text
|
|
// Code generated by ndpgen. DO NOT EDIT.
|
||
|
|
//
|
||
|
|
// This file contains client wrappers for the Store host service.
|
||
|
|
// It is intended for use in Navidrome plugins built with TinyGo.
|
||
|
|
//
|
||
|
|
//go:build wasip1
|
||
|
|
|
||
|
|
package ndhost
|
||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"errors"
|
||
|
|
|
||
|
|
"github.com/navidrome/navidrome/plugins/pdk/go/pdk"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Item represents the Item data structure.
|
||
|
|
type Item struct {
|
||
|
|
ID string `json:"id"`
|
||
|
|
Name string `json:"name"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// store_save is the host function provided by Navidrome.
|
||
|
|
//
|
||
|
|
//go:wasmimport extism:host/user store_save
|
||
|
|
func store_save(uint64) uint64
|
||
|
|
|
||
|
|
type storeSaveRequest struct {
|
||
|
|
Item Item `json:"item"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type storeSaveResponse struct {
|
||
|
|
Id string `json:"id,omitempty"`
|
||
|
|
Error string `json:"error,omitempty"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// StoreSave calls the store_save host function.
|
||
|
|
func StoreSave(item Item) (string, error) {
|
||
|
|
// Marshal request to JSON
|
||
|
|
req := storeSaveRequest{
|
||
|
|
Item: item,
|
||
|
|
}
|
||
|
|
reqBytes, err := json.Marshal(req)
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
reqMem := pdk.AllocateBytes(reqBytes)
|
||
|
|
defer reqMem.Free()
|
||
|
|
|
||
|
|
// Call the host function
|
||
|
|
responsePtr := store_save(reqMem.Offset())
|
||
|
|
|
||
|
|
// Read the response from memory
|
||
|
|
responseMem := pdk.FindMemory(responsePtr)
|
||
|
|
responseBytes := responseMem.ReadBytes()
|
||
|
|
|
||
|
|
// Parse the response
|
||
|
|
var response storeSaveResponse
|
||
|
|
if err := json.Unmarshal(responseBytes, &response); err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
|
||
|
|
// Convert Error field to Go error
|
||
|
|
if response.Error != "" {
|
||
|
|
return "", errors.New(response.Error)
|
||
|
|
}
|
||
|
|
|
||
|
|
return response.Id, nil
|
||
|
|
}
|