navidrome/plugins/cmd/ndpgen/testdata/echo_expected.go.txt

89 lines
2.2 KiB
Plaintext

// Code generated by ndpgen. DO NOT EDIT.
package testpkg
import (
"context"
"encoding/json"
extism "github.com/extism/go-sdk"
)
// EchoEchoRequest is the request type for Echo.Echo.
type EchoEchoRequest struct {
Message string `json:"message"`
}
// 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 JSON request from plugin memory
reqBytes, err := p.ReadBytes(stack[0])
if err != nil {
echoWriteError(p, stack, err)
return
}
var req EchoEchoRequest
if err := json.Unmarshal(reqBytes, &req); err != nil {
echoWriteError(p, stack, err)
return
}
// Call the service method
reply, svcErr := service.Echo(ctx, req.Message)
if svcErr != nil {
echoWriteError(p, stack, svcErr)
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
}