-
Notifications
You must be signed in to change notification settings - Fork 8
/
call.go
94 lines (79 loc) · 2.6 KB
/
call.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package txsvc
import (
"context"
"encoding/json"
"fmt"
"github.com/kwilteam/kwil-db/common"
txpb "github.com/kwilteam/kwil-db/core/rpc/protobuf/tx/v1"
"github.com/kwilteam/kwil-db/core/types/transactions"
"github.com/kwilteam/kwil-db/internal/ident"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *Service) Call(ctx context.Context, req *txpb.CallRequest) (*txpb.CallResponse, error) {
body, msg, err := convertActionCall(req)
if err != nil {
// NOTE: http api needs to be able to get the error message
return nil, status.Errorf(codes.InvalidArgument, "failed to convert action call: %s", err.Error())
}
args := make([]any, len(body.Arguments))
for i, arg := range body.Arguments {
args[i] = arg
}
for i, isNil := range body.NilArg { // length validation in convertActionCall
if isNil {
args[i] = nil
}
}
tx, err := s.db.BeginReadTx(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback(ctx)
signer := msg.Sender
caller := "" // string representation of sender, if signed. Otherwise, empty string
if signer != nil && msg.AuthType != "" {
caller, err = ident.Identifier(msg.AuthType, signer)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "failed to get caller: %s", err.Error())
}
}
ctxExec, cancel := context.WithTimeout(ctx, s.readTxTimeout)
defer cancel()
executeResult, err := s.engine.Procedure(ctxExec, tx, &common.ExecutionData{
Dataset: body.DBID,
Procedure: body.Action,
Args: args,
Signer: signer,
Caller: caller,
})
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "failed to execute view action: %s", err.Error())
}
// marshalling the map is less efficient, but necessary for backwards compatibility
btsResult, err := json.Marshal(resultMap(executeResult))
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to marshal call result")
}
return &txpb.CallResponse{
Result: btsResult,
}, nil
}
func convertActionCall(req *txpb.CallRequest) (*transactions.ActionCall, *transactions.CallMessage, error) {
var actionPayload transactions.ActionCall
err := actionPayload.UnmarshalBinary(req.Body.Payload)
if err != nil {
return nil, nil, err
}
if nArg := len(actionPayload.NilArg); nArg > 0 && nArg != len(actionPayload.Arguments) {
return nil, nil, fmt.Errorf("input arguments of length %d but nil args of length %d",
len(actionPayload.Arguments), len(actionPayload.NilArg))
}
return &actionPayload, &transactions.CallMessage{
Body: &transactions.CallMessageBody{
Payload: req.Body.Payload,
},
AuthType: req.AuthType,
Sender: req.Sender,
}, nil
}