-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc.go
107 lines (99 loc) · 3 KB
/
ipc.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
95
96
97
98
99
100
101
102
103
104
105
106
107
//go:build !wasm && !js
package main
import (
"encoding/json"
"github.com/Bedrock-OSS/go-burrito/burrito"
"github.com/MCDevKit/jsonte/jsonte"
"github.com/MCDevKit/jsonte/jsonte/types"
"github.com/MCDevKit/jsonte/jsonte/utils"
"github.com/fatih/color"
"github.com/gammazero/deque"
ipc "github.com/james-barrow/golang-ipc"
)
type EvaluateExpressionRequest struct {
ExtraScope map[string]interface{} `json:"extraScope"`
Expression string `json:"expression"`
Path string `json:"path"`
}
type EvaluateExpressionResponse struct {
Result interface{} `json:"result"`
Action string `json:"action"`
ValueName string `json:"valueName"`
IndexName string `json:"indexName"`
}
type ErrorResponse struct {
Error string `json:"error"`
}
const (
CloseConnection int = 1
EvaluateExpression int = 2
Error int = 3
)
func StartIPC(ipcName string, scope *types.JsonObject) error {
color.NoColor = true
sc, err := ipc.StartServer(ipcName, nil)
if err != nil {
return burrito.WrapError(err, "An error occurred while starting the server")
}
utils.Logger.Infof("Started IPC server %s", ipcName)
for {
message, err := sc.Read()
if err == nil {
if message.MsgType <= 0 {
continue
}
if message.MsgType == CloseConnection {
utils.Logger.Infof("Received close connection message")
sc.Close()
break
} else if message.MsgType == EvaluateExpression {
utils.Logger.Infof("Received evaluate expression message")
var request EvaluateExpressionRequest
err := json.Unmarshal(message.Data, &request)
if err != nil {
sendError(sc, burrito.WrapErrorf(err, "An error occurred while unmarshalling the request"))
continue
}
s := deque.Deque[*types.JsonObject]{}
s.PushBack(scope)
s.PushBack(types.AsObject(request.ExtraScope))
utils.Logger.Infof("Evaluating expression %s", request.Expression)
result, err := jsonte.Eval(request.Expression, s, request.Path)
if err != nil {
sendError(sc, burrito.WrapErrorf(err, "An error occurred while evaluating the expression %s", request.Expression))
continue
}
response := EvaluateExpressionResponse{
Result: result.Value.Unbox(),
Action: result.Action.String(),
ValueName: result.Name,
IndexName: result.IndexName,
}
data, err := json.Marshal(response)
err = sc.Write(EvaluateExpression, data)
if err != nil {
utils.Logger.Errorf("An error occurred while writing to the client: %s", err)
return err
}
}
} else {
return burrito.WrapErrorf(err, "An error occurred while reading from the client")
}
}
return nil
}
func sendError(sc *ipc.Server, err error) {
response := ErrorResponse{
Error: err.Error(),
}
data, err := json.Marshal(response)
if err != nil {
utils.Logger.Errorf("An error occurred while marshalling the response: %s", err)
return
}
err = sc.Write(Error, data)
if err != nil {
utils.Logger.Errorf("An error occurred while writing to the client: %s", err)
return
}
}