Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add implement state api invoke function #235

Merged
merged 2 commits into from
Oct 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions pkg/wasm/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/golang/protobuf/proto"
"mosn.io/layotto/pkg/grpc"
"mosn.io/layotto/spec/proto/runtime/v1"
runtimev1pb "mosn.io/layotto/spec/proto/runtime/v1"
"mosn.io/mosn/pkg/wasm/abi/proxywasm010"
"mosn.io/proxy-wasm-go-host/common"
Expand Down Expand Up @@ -64,6 +65,34 @@ func (d *LayottoHandler) CallForeignFunction(funcName string, param string) (str
return resp.Hello, proxywasm.WasmResultOk
}

b, err := proto.Marshal(resp)
if err != nil {
return "", proxywasm.WasmResultSerializationFailure
}
return string(b), proxywasm.WasmResultOk
case "State":
isJson := false
req := &runtimev1pb.GetStateRequest{}
if err := proto.Unmarshal([]byte(param), req); err != nil {
jsonReq := &getStateRequest{}
err = json.Unmarshal([]byte(param), jsonReq)
if err != nil {
return "", proxywasm.WasmResultBadArgument
}
req.Key = jsonReq.Key
req.StoreName = jsonReq.StoreName
req.Metadata = jsonReq.Metadata
req.Consistency = jsonReq.Consistency
isJson = true
}
resp, err := Layotto.GetState(context.Background(), req)
if err != nil {
return "", proxywasm.WasmResultInternalFailure
}
if isJson {
return string(resp.Data), proxywasm.WasmResultOk
}

b, err := proto.Marshal(resp)
if err != nil {
return "", proxywasm.WasmResultSerializationFailure
Expand All @@ -84,3 +113,14 @@ func (d *LayottoHandler) GetFuncCallData() common.IoBuffer {
}
return d.IoBuffer
}

type getStateRequest struct {
// Required. The name of state store.
StoreName string `json:"store_name,omitempty"`
// Required. The key of the desired state
Key string `json:"key,omitempty"`
// (optional) read consistency mode
Consistency runtime.StateOptions_StateConsistency `json:"consistency,omitempty"`
// (optional) The metadata which will be sent to state store components.
Metadata map[string]string `json:"metadata,omitempty"`
}