navidrome/plugins/cmd/hostgen/testdata/echo_expected.go
Deluan de90e191bb feat(hostgen): add hostgen tool for generating Extism host function wrappers
- Implemented hostgen tool to generate wrappers from annotated Go interfaces.
- Added command-line flags for input/output directories and package name.
- Introduced parsing and code generation logic for host services.
- Created test data for various service interfaces and expected generated code.
- Added documentation for host services and annotations for code generation.
- Implemented SubsonicAPI service with corresponding generated code.
2025-12-31 17:06:28 -05:00

77 lines
1.9 KiB
Go

// Code generated by hostgen. DO NOT EDIT.
package testpkg
import (
"context"
"encoding/json"
extism "github.com/extism/go-sdk"
)
// EchoEchoResponse is the response type for Echo.Echo.
type EchoEchoResponse struct {
Reply string `json:"reply,omitempty"`
Error string `json:"error,omitempty"`
}
// RegisterEchoHostFunctions registers Echo service host functions.
// The returned host functions should be added to the plugin's configuration.
func RegisterEchoHostFunctions(service EchoService) []extism.HostFunction {
return []extism.HostFunction{
newEchoEchoHostFunction(service),
}
}
func newEchoEchoHostFunction(service EchoService) extism.HostFunction {
return extism.NewHostFunctionWithStack(
"echo_echo",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
// Read parameters from stack
message, err := p.ReadString(stack[0])
if err != nil {
return
}
// Call the service method
reply, err := service.Echo(ctx, message)
if err != nil {
echoWriteError(p, stack, err)
return
}
// Write JSON response to plugin memory
resp := EchoEchoResponse{
Reply: reply,
}
echoWriteResponse(p, stack, resp)
},
[]extism.ValueType{extism.ValueTypePTR},
[]extism.ValueType{extism.ValueTypePTR},
)
}
// echoWriteResponse writes a JSON response to plugin memory.
func echoWriteResponse(p *extism.CurrentPlugin, stack []uint64, resp any) {
respBytes, err := json.Marshal(resp)
if err != nil {
echoWriteError(p, stack, err)
return
}
respPtr, err := p.WriteBytes(respBytes)
if err != nil {
stack[0] = 0
return
}
stack[0] = respPtr
}
// echoWriteError writes an error response to plugin memory.
func echoWriteError(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
}