wasi-module-sdk-go/state_proxy.go

70 lines
1.4 KiB
Go

package sdk
import (
"errors"
remotev1 "code.icb4dc0.de/buildr/api/generated/remote/v1"
"code.icb4dc0.de/buildr/wasi-module-sdk-go/mem"
)
type StateProxy struct{}
func (s StateProxy) Set(key, state []byte) error {
setCmd := &remotev1.SetState{
Key: key,
Data: state,
}
if resp, err := SetState(setCmd); err != nil {
return err
} else if resp.Error != "" {
return errors.New(resp.Error)
} else {
return nil
}
}
func (s StateProxy) Get(key []byte) (state []byte, meta StateMetadata, err error) {
getCmd := &remotev1.GetStateRequest{
Key: key,
}
if resp, err := GetState(getCmd); err != nil {
return nil, StateMetadata{}, err
} else {
return resp.Data, StateMetadata{}, nil
}
}
func GetState(req *remotev1.GetStateRequest) (*remotev1.GetStateResponse, error) {
data, err := req.MarshalVT()
if err != nil {
return nil, err
}
resultPtr := _get_state(mem.DataToManagedPtr(data))
data = mem.PtrToData(mem.UnifiedPtrToSizePtr(resultPtr))
resp := new(remotev1.GetStateResponse)
if err := resp.UnmarshalVT(data); err != nil {
return nil, err
}
return resp, nil
}
func SetState(req *remotev1.SetState) (*remotev1.Result, error) {
data, err := req.MarshalVT()
if err != nil {
return nil, err
}
resultPtr := _set_state(mem.DataToManagedPtr(data))
data = mem.PtrToData(mem.UnifiedPtrToSizePtr(resultPtr))
resp := new(remotev1.Result)
if err := resp.UnmarshalVT(data); err != nil {
return nil, err
}
return resp, nil
}