mirror of
https://github.com/navidrome/navidrome.git
synced 2026-03-04 06:35:52 +00:00
- 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.
91 lines
2.3 KiB
Go
91 lines
2.3 KiB
Go
// Code generated by hostgen. DO NOT EDIT.
|
|
|
|
package testpkg
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
extism "github.com/extism/go-sdk"
|
|
)
|
|
|
|
// SearchFindRequest is the request type for Search.Find.
|
|
type SearchFindRequest struct {
|
|
Query string `json:"query"`
|
|
}
|
|
|
|
// SearchFindResponse is the response type for Search.Find.
|
|
type SearchFindResponse struct {
|
|
Results []Result `json:"results,omitempty"`
|
|
Total int32 `json:"total,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
// RegisterSearchHostFunctions registers Search service host functions.
|
|
// The returned host functions should be added to the plugin's configuration.
|
|
func RegisterSearchHostFunctions(service SearchService) []extism.HostFunction {
|
|
return []extism.HostFunction{
|
|
newSearchFindHostFunction(service),
|
|
}
|
|
}
|
|
|
|
func newSearchFindHostFunction(service SearchService) extism.HostFunction {
|
|
return extism.NewHostFunctionWithStack(
|
|
"search_find",
|
|
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
|
|
// Read JSON request from plugin memory
|
|
reqBytes, err := p.ReadBytes(stack[0])
|
|
if err != nil {
|
|
searchWriteError(p, stack, err)
|
|
return
|
|
}
|
|
var req SearchFindRequest
|
|
if err := json.Unmarshal(reqBytes, &req); err != nil {
|
|
searchWriteError(p, stack, err)
|
|
return
|
|
}
|
|
|
|
// Call the service method
|
|
results, total, svcErr := service.Find(ctx, req.Query)
|
|
if svcErr != nil {
|
|
searchWriteError(p, stack, svcErr)
|
|
return
|
|
}
|
|
|
|
// Write JSON response to plugin memory
|
|
resp := SearchFindResponse{
|
|
Results: results,
|
|
Total: total,
|
|
}
|
|
searchWriteResponse(p, stack, resp)
|
|
},
|
|
[]extism.ValueType{extism.ValueTypePTR},
|
|
[]extism.ValueType{extism.ValueTypePTR},
|
|
)
|
|
}
|
|
|
|
// searchWriteResponse writes a JSON response to plugin memory.
|
|
func searchWriteResponse(p *extism.CurrentPlugin, stack []uint64, resp any) {
|
|
respBytes, err := json.Marshal(resp)
|
|
if err != nil {
|
|
searchWriteError(p, stack, err)
|
|
return
|
|
}
|
|
respPtr, err := p.WriteBytes(respBytes)
|
|
if err != nil {
|
|
stack[0] = 0
|
|
return
|
|
}
|
|
stack[0] = respPtr
|
|
}
|
|
|
|
// searchWriteError writes an error response to plugin memory.
|
|
func searchWriteError(p *extism.CurrentPlugin, stack []uint64, err error) {
|
|
errResp := struct {
|
|
Error string `json:"error"`
|
|
}{Error: err.Error()}
|
|
respBytes, _ := json.Marshal(errResp)
|
|
respPtr, _ := p.WriteBytes(respBytes)
|
|
stack[0] = respPtr
|
|
}
|