68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
package sdk
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"code.icb4dc0.de/buildr/wasi-module-sdk/mem"
|
|
rpcv1 "code.icb4dc0.de/buildr/wasi-module-sdk/protocol/generated/rpc/v1"
|
|
)
|
|
|
|
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
|
|
}
|