wasi-module-sdk-go/state_proxy.go

69 lines
1.5 KiB
Go
Raw Normal View History

2023-05-08 13:21:31 +00:00
package sdk
import (
"errors"
2023-05-18 16:21:44 +00:00
"code.icb4dc0.de/buildr/wasi-module-sdk-go/mem"
rpcv1 "code.icb4dc0.de/buildr/wasi-module-sdk-go/protocol/generated/rpc/v1"
2023-05-08 13:21:31 +00:00
)
type StateProxy struct {
}
func (s StateProxy) Set(key, state []byte) error {
setCmd := &rpcv1.SetState{
Key: key,
Data: state,
}
data, err := setCmd.MarshalVT()
if err != nil {
return err
}
result := _set_state(mem.DataToManagedPtr(data))
if result == 0 {
return errors.New("unknown error occurred")
}
resultPtr := uint32(result >> 32)
resultSize := uint32(result)
resultMsg := new(rpcv1.Result)
if err := resultMsg.UnmarshalVT(mem.PtrToData(resultPtr, resultSize)); err != nil {
return err
}
if !resultMsg.Success {
if resultMsg.Error != "" {
return errors.New(resultMsg.Error)
}
return errors.New("unknown error occurred")
}
return nil
}
func (s StateProxy) Get(key []byte) (state []byte, meta StateMetadata, err error) {
getCmd := &rpcv1.GetStateRequest{
Key: key,
}
data, err := getCmd.MarshalVT()
if err != nil {
return nil, StateMetadata{}, err
}
result := _get_state(mem.DataToManagedPtr(data))
if result == 0 {
return nil, StateMetadata{}, errors.New("error occurred while processing request")
}
resultPtr := uint32(result >> 32)
resultSize := uint32(result)
getStateResult := new(rpcv1.GetStateResponse)
if err := getStateResult.UnmarshalVT(mem.PtrToData(resultPtr, resultSize)); err != nil {
return nil, StateMetadata{}, err
}
return getStateResult.Data, StateMetadata{}, nil
}