navidrome/plugins/cmd/hostgen/testdata/meta_expected.go
Deluan a0a5168f5f fix(generator): error-only methods in response handling
Signed-off-by: Deluan <deluan@navidrome.org>
2025-12-31 17:06:28 -05:00

118 lines
2.9 KiB
Go

// Code generated by hostgen. DO NOT EDIT.
package testpkg
import (
"context"
"encoding/json"
extism "github.com/extism/go-sdk"
)
// 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"`
}
// RegisterMetaHostFunctions registers Meta service host functions.
// The returned host functions should be added to the plugin's configuration.
func RegisterMetaHostFunctions(service MetaService) []extism.HostFunction {
return []extism.HostFunction{
newMetaGetHostFunction(service),
newMetaSetHostFunction(service),
}
}
func newMetaGetHostFunction(service MetaService) extism.HostFunction {
return extism.NewHostFunctionWithStack(
"meta_get",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
// Read parameters from stack
key, err := p.ReadString(stack[0])
if err != nil {
return
}
// Call the service method
value, err := service.Get(ctx, key)
if err != nil {
metaWriteError(p, stack, err)
return
}
// Write JSON response to plugin memory
resp := MetaGetResponse{
Value: value,
}
metaWriteResponse(p, stack, resp)
},
[]extism.ValueType{extism.ValueTypePTR},
[]extism.ValueType{extism.ValueTypePTR},
)
}
func newMetaSetHostFunction(service MetaService) extism.HostFunction {
return extism.NewHostFunctionWithStack(
"meta_set",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
// Read JSON request from plugin memory
reqBytes, err := p.ReadBytes(stack[0])
if err != nil {
metaWriteError(p, stack, err)
return
}
var req MetaSetRequest
if err := json.Unmarshal(reqBytes, &req); err != nil {
metaWriteError(p, stack, err)
return
}
// Call the service method
err = service.Set(ctx, req.Data)
if err != nil {
// Write error string to plugin memory
if ptr, err := p.WriteString(err.Error()); err == nil {
stack[0] = ptr
}
return
}
// Write empty string to indicate success
if ptr, err := p.WriteString(""); err == nil {
stack[0] = ptr
}
},
[]extism.ValueType{extism.ValueTypePTR},
[]extism.ValueType{extism.ValueTypePTR},
)
}
// metaWriteResponse writes a JSON response to plugin memory.
func metaWriteResponse(p *extism.CurrentPlugin, stack []uint64, resp any) {
respBytes, err := json.Marshal(resp)
if err != nil {
metaWriteError(p, stack, err)
return
}
respPtr, err := p.WriteBytes(respBytes)
if err != nil {
stack[0] = 0
return
}
stack[0] = respPtr
}
// metaWriteError writes an error response to plugin memory.
func metaWriteError(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
}