go-waku/library/response.go

45 lines
820 B
Go
Raw Normal View History

2022-03-21 23:15:53 +00:00
package main
import "C"
import (
"encoding/json"
)
type jsonResponse struct {
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 {
errStr := err.Error()
errResponse := jsonResponse{
Error: &errStr,
2022-03-21 23:15:53 +00:00
}
response, _ := json.Marshal(&errResponse)
return C.CString(string(response))
}
data, err := json.Marshal(jsonResponse{Result: result})
2022-03-21 23:15:53 +00:00
if err != nil {
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
}
out := jsonResponse{
2022-03-21 23:15:53 +00:00
Error: errString,
}
outBytes, _ := json.Marshal(out)
return C.CString(string(outBytes))
}