2022-03-21 23:15:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
2022-04-12 12:12:14 +00:00
|
|
|
type jsonResponse struct {
|
2022-03-22 16:30:14 +00:00
|
|
|
Error *string `json:"error,omitempty"`
|
2022-03-21 23:15:53 +00:00
|
|
|
Result interface{} `json:"result"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareJSONResponse(result interface{}, err error) *C.char {
|
|
|
|
|
|
|
|
if err != nil {
|
2022-03-22 16:30:14 +00:00
|
|
|
errStr := err.Error()
|
2022-04-12 12:12:14 +00:00
|
|
|
errResponse := jsonResponse{
|
2022-03-22 16:30:14 +00:00
|
|
|
Error: &errStr,
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
response, _ := json.Marshal(&errResponse)
|
|
|
|
return C.CString(string(response))
|
|
|
|
}
|
|
|
|
|
2022-04-12 12:12:14 +00:00
|
|
|
data, err := json.Marshal(jsonResponse{Result: result})
|
2022-03-21 23:15:53 +00:00
|
|
|
if err != nil {
|
2022-03-22 16:30:14 +00:00
|
|
|
return prepareJSONResponse(nil, err)
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
return C.CString(string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeJSONResponse(err error) *C.char {
|
|
|
|
var errString *string = nil
|
|
|
|
if err != nil {
|
|
|
|
errStr := err.Error()
|
|
|
|
errString = &errStr
|
|
|
|
}
|
|
|
|
|
2022-04-12 12:12:14 +00:00
|
|
|
out := jsonResponse{
|
2022-03-21 23:15:53 +00:00
|
|
|
Error: errString,
|
|
|
|
}
|
|
|
|
outBytes, _ := json.Marshal(out)
|
|
|
|
|
|
|
|
return C.CString(string(outBytes))
|
|
|
|
}
|