forked from wailsapp/wails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc_response.go
45 lines (38 loc) · 1.06 KB
/
ipc_response.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
package wails
import (
"encoding/hex"
"encoding/json"
)
// ipcResponse contains the response data from an RPC call
type ipcResponse struct {
CallbackID string `json:"callbackid"`
ErrorMessage string `json:"error,omitempty"`
Data interface{} `json:"data,omitempty"`
}
// newErrorResponse returns the given error message to the frontend with the callbackid
func newErrorResponse(callbackID string, errorMessage string) *ipcResponse {
// Create response object
result := &ipcResponse{
CallbackID: callbackID,
ErrorMessage: errorMessage,
}
return result
}
// newSuccessResponse returns the given data to the frontend with the callbackid
func newSuccessResponse(callbackID string, data interface{}) *ipcResponse {
// Create response object
result := &ipcResponse{
CallbackID: callbackID,
Data: data,
}
return result
}
// Serialise formats the response to a string
func (i *ipcResponse) Serialise() (string, error) {
b, err := json.Marshal(i)
if err != nil {
return "", err
}
result := hex.EncodeToString(b)
return result, err
}