5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 06:51:26 +08:00
wails/ipc_response.go
2019-01-08 07:58:46 +11:00

44 lines
1.1 KiB
Go

package wails
import (
"encoding/json"
"strings"
)
// 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)
result := strings.Replace(string(b), "\\", "\\\\", -1)
result = strings.Replace(result, "'", "\\'", -1)
return result, err
}