navidrome/plugins/cmd/hostgen/testdata/meta_client_expected.go
Deluan cab656dbe5 refactor: host function wrappers to use structured request and response types
- Updated the host function signatures in `nd_host_artwork.go`, `nd_host_scheduler.go`, `nd_host_subsonicapi.go`, and `nd_host_websocket.go` to accept a single parameter for JSON requests.
- Introduced structured request and response types for various cache operations in `nd_host_cache.go`.
- Modified cache functions to marshal requests to JSON and unmarshal responses, improving error handling and code clarity.
- Removed redundant memory allocation for string parameters in favor of JSON marshaling.
- Enhanced error handling in WebSocket and cache operations to return structured error responses.
2025-12-31 17:06:29 -05:00

104 lines
2.3 KiB
Go

// Code generated by hostgen. DO NOT EDIT.
//
// This file contains client wrappers for the Meta host service.
// It is intended for use in Navidrome plugins built with TinyGo.
//
//go:build wasip1
package main
import (
"encoding/json"
"github.com/extism/go-pdk"
)
// meta_get is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user meta_get
func meta_get(uint64) uint64
// meta_set is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user meta_set
func meta_set(uint64) uint64
// MetaGetRequest is the request type for Meta.Get.
type MetaGetRequest struct {
Key string `json:"key"`
}
// MetaGetResponse is the response type for Meta.Get.
type MetaGetResponse struct {
Value any `json:"value,omitempty"`
Error string `json:"error,omitempty"`
}
// MetaSetRequest is the request type for Meta.Set.
type MetaSetRequest struct {
Data map[string]any `json:"data"`
}
// MetaSetResponse is the response type for Meta.Set.
type MetaSetResponse struct {
Error string `json:"error,omitempty"`
}
// MetaGet calls the meta_get host function.
func MetaGet(key string) (*MetaGetResponse, error) {
// Marshal request to JSON
req := MetaGetRequest{
Key: key,
}
reqBytes, err := json.Marshal(req)
if err != nil {
return nil, err
}
reqMem := pdk.AllocateBytes(reqBytes)
defer reqMem.Free()
// Call the host function
responsePtr := meta_get(reqMem.Offset())
// Read the response from memory
responseMem := pdk.FindMemory(responsePtr)
responseBytes := responseMem.ReadBytes()
// Parse the response
var response MetaGetResponse
if err := json.Unmarshal(responseBytes, &response); err != nil {
return nil, err
}
return &response, nil
}
// MetaSet calls the meta_set host function.
func MetaSet(data map[string]any) (*MetaSetResponse, error) {
// Marshal request to JSON
req := MetaSetRequest{
Data: data,
}
reqBytes, err := json.Marshal(req)
if err != nil {
return nil, err
}
reqMem := pdk.AllocateBytes(reqBytes)
defer reqMem.Free()
// Call the host function
responsePtr := meta_set(reqMem.Offset())
// Read the response from memory
responseMem := pdk.FindMemory(responsePtr)
responseBytes := responseMem.ReadBytes()
// Parse the response
var response MetaSetResponse
if err := json.Unmarshal(responseBytes, &response); err != nil {
return nil, err
}
return &response, nil
}