package sdk import ( "errors" rpcv1 "code.icb4dc0.de/buildr/wasi-module-sdk-go/protocol/generated/rpc/v1" "code.icb4dc0.de/buildr/wasi-module-sdk-go/mem" ) 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) if resultSize == 0 { return nil } 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) if resultSize == 0 { return nil, StateMetadata{}, nil } getStateResult := new(rpcv1.GetStateResponse) if err := getStateResult.UnmarshalVT(mem.PtrToData(resultPtr, resultSize)); err != nil { return nil, StateMetadata{}, err } return getStateResult.Data, StateMetadata{}, nil }