package host
import (
"context"
"encoding/json"
extism "github.com/extism/go-sdk"
)
type StorageGetStoragePathResponse struct {
Result string `json:"result,omitempty"`
}
func RegisterStorageHostFunctions(service StorageService) []extism.HostFunction {
return []extism.HostFunction{
newStorageGetStoragePathHostFunction(service),
}
}
func newStorageGetStoragePathHostFunction(service StorageService) extism.HostFunction {
return extism.NewHostFunctionWithStack(
"storage_getstoragepath",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
result := service.GetStoragePath(ctx)
resp := StorageGetStoragePathResponse{
Result: result,
}
storageWriteResponse(p, stack, resp)
},
[]extism.ValueType{extism.ValueTypePTR},
[]extism.ValueType{extism.ValueTypePTR},
)
}
func storageWriteResponse(p *extism.CurrentPlugin, stack []uint64, resp any) {
respBytes, err := json.Marshal(resp)
if err != nil {
storageWriteError(p, stack, err)
return
}
respPtr, err := p.WriteBytes(respBytes)
if err != nil {
stack[0] = 0
return
}
stack[0] = respPtr
}
func storageWriteError(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
}