wasi-module-sdk-go/state_proxy.go

77 lines
1.6 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
rpcv1 "code.icb4dc0.de/buildr/wasi-module-sdk-go/protocol/generated/rpc/v1"
2023-05-24 20:11:05 +00:00
"code.icb4dc0.de/buildr/wasi-module-sdk-go/mem"
2023-05-08 13:21:31 +00:00
)
2023-08-15 20:10:30 +00:00
type StateProxy struct{}
2023-05-08 13:21:31 +00:00
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)
2023-05-24 20:11:05 +00:00
if resultSize == 0 {
return nil
}
2023-05-08 13:21:31 +00:00
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)
2023-05-24 20:11:05 +00:00
if resultSize == 0 {
return nil, StateMetadata{}, nil
}
2023-05-08 13:21:31 +00:00
getStateResult := new(rpcv1.GetStateResponse)
if err := getStateResult.UnmarshalVT(mem.PtrToData(resultPtr, resultSize)); err != nil {
return nil, StateMetadata{}, err
}
return getStateResult.Data, StateMetadata{}, nil
}